def main(trainset, window, nfeatures, image, savetodir): output = savetodir+"temp.xpl" #building the xpl file ensemble.build_xpl(trainset,window,output) #reading the "just" created xpl file result = xplutil.read_xpl(output) XPL_data = result.data w0 = result.freq0.copy() w1 = result.freq1.copy() #normalizing the table frequency values w0,w1 = cl.normalize_table(w0, w1) #calculating features and indexes indices, feature_list, _ = ft.cmim(XPL_data, w0, w1, nfeatures) #saving window file tw.to_window_file(indices, result.winshape, savetodir+"window.win") #applying the feature selection algorithm w0, w1, updated_decision, cls_error = cl.apply_feature_selection(XPL_data, indices, w0, w1) #calculating the unique array and their indexes indices = np.sort(indices) unique_array, unique_index = cl._apply_projection(XPL_data, indices) #writing a mimterm file to disk xplutil.write_minterm_file(savetodir+"mintermfile.mtm",indices, result.winshape, unique_array,updated_decision[unique_index]) #building the operator based on the mintermfile ensemble.build_operator(savetodir+"window.win", savetodir+"mintermfile.mtm", savetodir+"operator") #building a new XPL according to the learned window output = savetodir+"Learned.xpl" ensemble.build_xpl(trainset,savetodir+"window.win",output) result_img = savetodir+"Image_applied" #applying the operator on the image ensemble.apply_operator(savetodir+"operator", image, result_img)
def main(trainset, window, save_todir): XPL = save_todir+'file.xpl' #building xpl file ensemble.build_xpl(trainset,window,XPL) #reading the xpl file xpl_data = xplutil.read_xpl(XPL) decision = cl.make_decision(xpl_data.freq0,xpl_data.freq1) size = xpl_data.winshape[0]*xpl_data.winshape[1] xplutil.write_minterm_file(save_todir+'filename.mtm', np.array([range(size)]), xpl_data.winshape, xpl_data.data, decision)
def main(level1trainset,level2trainset, testset, window, nfeatures, niterations, savetodir): output = savetodir+"temp.xpl" #building a XPL to start processing ensemble.build_xpl(level1trainset,window,output) XPL_filepath = output #now let's read the XPL XPL_data = xplutil.read_xpl(XPL_filepath) #writing the minimal empirical error to file ensemble.write_min_empirical_error(savetodir+"min_emp_err.txt",ensemble.min_empirical_error(XPL_data)) #training the ensemble ensemble.train(XPL_data, nfeatures, niterations, savetodir) #building first level operators print print "... Building first level operators ...\n" ensemble.build_operators(savetodir , niterations) #creating the operators combinations for i in range(1,niterations): print print "...Building operators combination : 0 to %s ... \n" %str(i) ensemble.build_operator_combination(level2trainset, np.array(range(i+1)), savetodir, savetodir+"twoLevel_0_to_"+str(i)) #combining all the operators #ensemble.build_operator_combination(trainset, np.array(range(niterations)), savetodir, savetodir+"twoLevel") #Writing MAE from training set to file. set = imageset.Imageset() trainimgset = set.read(level1trainset) mae_t = ensemble.mae(trainimgset) file = open(savetodir+"MAE_TrainingSet.txt", "w") file.write(str(mae_t)) file.close() #Writing MAE from test set to file. set = imageset.Imageset() testimgset = set.read(testset) mae_test = ensemble.mae(testimgset) file = open(savetodir+"MAE_TestSet.txt", "w") file.write(str(mae_test)) file.close() img_list = _get_imgList(testimgset) #applying the first level operators on the test set images for i in range(niterations): for j in img_list: ensemble.apply_operator(savetodir+"mtm"+str(i)+"-op", j, savetodir+"mtm"+str(i)+"-op-files/"+j.split("/")[-1][:-3]+"proc.pnm") ensemble.trios_test(savetodir+"mtm"+str(i)+"-op", testset, savetodir+"mtm"+str(i)+"-op-files/MAE.txt") #applying the second level operators combinations on the test set images for i in range(1,niterations): for j in img_list: ensemble.apply_operator(savetodir+"twoLevel_0_to_"+str(i),j, savetodir+"twoLevel_0_to_"+str(i)+"-files/level1/operator0/"+j.split("/")[-1][:-3]+"proc.pnm") ensemble.trios_test(savetodir+"twoLevel_0_to_"+str(i), testset, savetodir+"twoLevel_0_to_"+str(i)+"-files/level1/operator0/MAE.txt")
def read_from_XPL(xpl_file_path): """xplutil.py already reads from XPL file. Parameters ---------- xpl_file_path : '/home/jdoe/path_to_xpl' The path to the XPL file. Returns ------- result : ExampleData(data, freq0, freq1, winshape, windata, filename) Same as xplutil returns. """ result = xplutil.read_xpl(xpl_file_path) return result
def main(trainset, window, save_todir): XPL = window+'.xpl' #building xpl file ensemble.build_xpl(trainset,window,XPL) #reading xpl file xpl_data = xplutil.read_xpl(XPL) indices = np.array([0,1,3,4,5,7,8]) w0 = xpl_data.freq0.copy() w1 = xpl_data.freq1.copy() w0, w1 = cl.normalize_table(w0, w1) hash, unique_array = project(xpl_data.data, indices) sum0 = [] sum1 = [] for row in unique_array: arr = hash.get(tuple(row.reshape(1,-1)[0])) indexes = tuple(arr[0].reshape(1,-1)[0]) sum0.append(w0[[np.array(indexes)]].sum()) sum1.append(w1[[np.array(indexes)]].sum()) decision = cl.make_decision(sum0, sum1) xplutil.write_minterm_file(save_todir+"mtmFile.mtm",indices, xpl_data.winshape, unique_array,decision)
def main(xpl_data, num_features=None, plotwindow=False, winfile=None): height, width = xpl_data.winshape indices, feature_list, _ = feature.cmim(xpl_data.data, xpl_data.freq0, xpl_data.freq1, num_features) print "Selected features: ", indices if plotwindow: trioswindow.plot_pixels(indices, xpl_data.winshape) if winfile: trioswindow.to_window_file(indices, xpl_data.winshape, winfile) if __name__ == "__main__": parser = argparse.ArgumentParser() description="Applies CMIM feature selection to a given XPL file." parser.add_argument("filename", help="XPL filename") parser.add_argument("-w", "--window", help="Trios window file name where to save the obtained window") parser.add_argument("-n", "--numfeatures", type=int, help="Maximum number of features") parser.add_argument("-s", "--showwindow", action="store_true", help="Show window obtained after feature selection.") args = parser.parse_args() if args.filename: xpl_data = xplutil.read_xpl(args.filename) main(xpl_data, args.numfeatures, args.showwindow, args.window) else: print "Must provide input file name."