def get_trimap_data(trimap_path): filename1 = trimap_path.split("/")[-1] filename2 = filename1.split("_")[0] + "_" + filename1.split("_")[1] outpath = "/mnt/CLAWS1/stamilev/volumes/groundtruth/dense_gt_data/" + filename2 + ".h5" print outpath trimap = read_h5(trimap_path) #dgt= dgt[25:725, 0:700, 0:700] unique = np.unique(trimap) unique_elements = [] unique_tuples = [] print "no of unique elements", len(unique) for i in unique: mask = trimap == i noe = np.sum(mask) print i, noe unique_elements.append(noe) unique_tuples.append((i, noe)) assert len(unique)==len(unique_elements) l = len(unique_elements) nodes_data = np.zeros((l,2), dtype=np.uint) for n in range(l): nodes_data[n,0] = unique_tuples[n][0] nodes_data[n,1] = unique_tuples[n][1] #outpath = "/home/stamylew/volumes/groundtruth/dense_gt_data/entire_usable_data.h5" save_h5(nodes_data, outpath, "labels and occurrence", None)
def get_quality_values(predict, gt, dense_gt, minMemb, minSeg, sigMin, sigWeights, sigSmooth,edgeLengths,nodeFeatures,nodeSizes, nodeLabels, nodeNumStop, beta, metric, wardness,out): """ """ adjusted_predict = adjust_predict(predict) # save_h5(adjusted_predict, "/home/stamylew/delme/prob.h5", "data") relevant_predict, relevant_gt = exclude_ignore_label(adjusted_predict, gt) acc, pre, rec = accuracy_precision_recall(relevant_predict, relevant_gt) auc_score = calculate_roc_auc_score(relevant_predict, relevant_gt) no_of_true_pos, no_of_false_pos = true_and_false_pos(relevant_predict, relevant_gt) no_of_true_neg, no_of_false_neg = true_and_false_neg(relevant_predict, relevant_gt) print "#nodes in dense gt", len(np.unique(dense_gt)) pmin= 0.5 segmentation, super_pixels, wsDt_data, agglCl_data = get_segmentation(adjusted_predict,pmin, minMemb, minSeg, sigMin, sigWeights, sigSmooth, True,False, edgeLengths, nodeFeatures, nodeSizes, nodeLabels, nodeNumStop, beta, metric, wardness, out) save_h5(segmentation, "/home/stamylew/delme/segmap.h5", "data", None) ri_data = skl.randIndex(segmentation.flatten().astype(np.uint32), dense_gt.flatten().astype(np.uint32), True) voi_data = skl.variationOfInformation(segmentation.flatten().astype(np.uint32), dense_gt.flatten().astype(np.uint32), True) sp_ri = skl.randIndex(super_pixels.flatten().astype(np.uint32), dense_gt.flatten().astype(np.uint32), True) sp_voi = skl.variationOfInformation(super_pixels.flatten().astype(np.uint32), dense_gt.flatten().astype(np.uint32), True) print print "super pixels values", (sp_ri, sp_voi) print print "segmentation values", (ri_data, voi_data) return acc, pre, rec, auc_score, ri_data, voi_data, no_of_true_pos, no_of_false_pos, no_of_true_neg, no_of_false_neg, \ wsDt_data, agglCl_data
def make_seg_map(prob_map_path, dense_gt_path): prob_map = read_h5(prob_map_path) dense_gt = read_h5(dense_gt_path) nodes_in_dgt = len(np.unique(dense_gt)) print "nodes in dgt", nodes_in_dgt seed_map = wsDtSegmentation(prob_map, pmin=0.5, minMembraneSize=10, minSegmentSize=0, sigmaMinima=6, sigmaWeights=1, cleanCloseSeeds=True, returnSeedsOnly=True) seg_map, sup_map, wsDt_data, agglCl_data = get_segmentation(prob_map, pmin=0.5, minMemb=10, minSeg=10, sigMin=6, sigWeights=1, sigSmooth=0.1, cleanCloseSeeds=True, returnSeedsOnly=False, edgeLengths=None, nodeFeatures=None, nodeSizes=None, nodeLabels=None, nodeNumStop=nodes_in_dgt, beta=0, metric='l1', wardness=0.2, out=None) print "seeds", len(np.unique(seed_map)) print "seg", len(np.unique(seg_map)) #make folder for seeds seed_folder = prob_map_path.split("prob")[0] + "seeds/" if not os.path.exists(seed_folder): os.mkdir(seed_folder) seed_map_outpath = seed_folder + prob_map_path.split("/")[-1] #make folder for super pixels sup_folder = prob_map_path.split("prob")[0] + "sup_maps/" if not os.path.exists(sup_folder): os.mkdir(sup_folder) sup_map_outpath = sup_folder + prob_map_path.split("/")[-1] #make folder for segmentations seg_folder = prob_map_path.split("prob")[0] + "seg_maps/" if not os.path.exists(seg_folder): os.mkdir(seg_folder) seg_map_outpath = seg_folder + prob_map_path.split("/")[-1] #make folder for segmentation data data_folder = prob_map_path.split("prob")[0] + "seg_data/" if not os.path.exists(data_folder): os.mkdir(data_folder) data_outpath = data_folder + prob_map_path.split("/")[-1] #get segmentation data ri, voi = rand_index_variation_of_information(seg_map, dense_gt) print "ri, voi", ri , voi ri_data = np.array((ri)) voi_data = np.array((voi)) ri_voi_data = np.array((ri, voi)) #save seeds, superpixels and segmentations # save_h5(ri_voi_data, data_outpath, key) # save_h5(ri_data, data_outpath, "rand index", None) # save_h5(voi_data, data_outpath, "variation of information", None) # save_h5(seed_map, seed_map_outpath, "data", None) save_h5(sup_map,sup_map_outpath, "data", None) save_h5(seg_map,seg_map_outpath, "data", None)
def save_quality_values(predict_path, gt_path, dense_gt_path, outpath, slices, minMemb=10, minSeg=10, sigMin=6, sigWeights=1, sigSmooth=0.1,edgeLengths=None, nodeFeatures=None, nodeSizes=None, nodeLabels=None, nodeNumStop=None, beta=0, metric='l1', wardness=0.2, out=None): """ """ #read predict and groundtruth data predict_data = read_h5(predict_path, "exported_data") gt_data = read_h5(gt_path) dense_gt_data = read_h5(dense_gt_path) nodes_in_dgt = np.unique(dense_gt_data) nodeNumStop = nodes_in_dgt print "nodeNumStop", nodeNumStop #all_quality_data = get_quality_values(predict_data, gt_data, dense_gt_data) #get quality values acc, prec, rec, auc_score, ri, voi, tp, fp, tn, fn, wsDt_data, agglCl_data = get_quality_values(predict_data, gt_data, dense_gt_data, minMemb, minSeg, sigMin, sigWeights, sigSmooth, edgeLengths, nodeFeatures, nodeSizes, nodeLabels, nodeNumStop, beta, metric, wardness, out) measurements = {"accuracy":acc, "precision":prec, "recall":rec, "auc score":auc_score, "rand index":ri, "variation of information":voi, "true positives":tp, "false positives":fp, "true negatives":tn, "false negatives":fn} # acc, prec, rec, auc_score, tp, fp, tn, fn, wsDt_data, agglCl_data = get_quality_values(predict_data, gt_data, dense_gt_data) # measurements = {"accuracy":acc, "precision":prec, "recall":rec, "auc score":auc_score, # "true positives":tp, "false positives":fp, "true negatives":tn, # "false negatives":fn} if not os.path.exists(outpath): print "Output h5 file did not exist." for measurement in measurements.iterkeys(): key = "quality/" + measurement data = np.zeros((1,)) data[0] = measurements[str(measurement)] save_h5(data, outpath, key, None) else: for measurement in measurements.iterkeys(): key = "quality/" + measurement data = read_h5(outpath, key) new_data = np.vstack((data, measurements[measurement])) save_h5(new_data, outpath, key, None) for slice in slices: im_outpath = outpath.split(".")[-2] predict_im_outpath = im_outpath + "_slice_" + str(slice) + ".png" gt_im_outpath = im_outpath + "_slice_" + str(slice) + "_gt.png" plt.imsave(predict_im_outpath, adjust_predict(predict_data)[slice]) plt.imsave(gt_im_outpath, gt_data[slice]) save_h5(wsDt_data, outpath, "segmentation/wsDt data", None) save_h5([agglCl_data], outpath, "segmentation/agglCl data", None) fpr, tpr, threshold = draw_roc_curve(predict_data, gt_data) plt.figure() plt.plot(fpr, tpr) plt.savefig(im_outpath + "roc_curve_test.png")
def test_wsDt_agglCl_configs(predict_path, dense_gt_path, pmin=0.5, minMemb=10, minSeg=10, sigMin=2, sigWeights=2, sigSmooth=0.1): """ :param predict_path: :param dense_gt_path: :param pmin: :param minMemb: :param minSeg: :param sigMin: :param sigWeights: :param sigSmooth: :return: """ #get rand index and variation of information predict_data = read_h5(predict_path) adjusted_predict_data = adjust_predict(predict_data) dense_gt_data = read_h5(dense_gt_path) seg, sup = get_segmentation(adjusted_predict_data,pmin,minMemb,minSeg,sigMin,sigWeights,sigSmooth) ri, voi = rand_index_variation_of_information(seg, dense_gt_data) print "ri, voi", ri, voi config = predict_path.split("/")[-2] filename = config + "_" + predict_path.split("/")[-1] outpath_folder = "/home/stamylew/test_folder/q_data/wsDt_agglCl_tests/" outpath = outpath_folder + filename key = "sigMin_" + str(sigMin) + "_sigWeights_" + str(sigWeights) + "_sigSmooth_" + str(sigSmooth) if not os.path.exists(outpath): print "Output h5 file did not exist." data = np.zeros((1,2)) data[0,0] = ri data[0,1] = voi save_h5(data, outpath, key, None) else: old_data = read_h5(outpath, key) data = np.zeros((1,2)) data[0,0] = ri data[0,1] = voi new_data = np.vstack((old_data, data)) save_h5(new_data, outpath, key, None)
def redo_quality2(block_path, dense_gt_path, minMemb, minSeg, sigMin, sigWeights, sigSmooth, edgeLengths=None, nodeFeatures=None, nodeSizes=None, nodeLabels=None, nodeNumStop=None, beta=0, metric='l1', wardness=0.2, out=None): folder_list = [f for f in os.listdir(block_path) if not os.path.isfile(os.path.join(block_path,f)) and ("figure" not in f) and "redone" not in f] print "folder_list", folder_list outpath = block_path + "/redone_quality" if not os.path.exists(outpath): os.mkdir(outpath) dense_gt_data = read_h5(dense_gt_path) nodes_in_dgt = len(np.unique(dense_gt_data)) for f in folder_list: folder_path = os.path.join(block_path, f) prob_file = [h for h in os.listdir(folder_path) if "probs" in h] prob_file_path = os.path.join(folder_path,prob_file[0]) print print print prob_file_path predict_data = read_h5(prob_file_path) adjusted_predict_data = adjust_predict(predict_data) print "#nodes in dense gt", nodes_in_dgt pmin = 0.5 seg, sup, wsDt_data, agglCl_data = get_segmentation(adjusted_predict_data, pmin, minMemb, minSeg, sigMin, sigWeights, sigSmooth,True,False, edgeLengths,nodeFeatures, nodeSizes, nodeLabels, nodeNumStop,beta, metric, wardness, out) nodes_in_seg = len(np.unique(seg)) nodes_in_sup = len(np.unique(sup)) ri, voi = rand_index_variation_of_information(seg, dense_gt_data) new_folder_path = outpath + "/" + f if not os.path.exists(new_folder_path): os.mkdir(new_folder_path) key = "pmin_"+ str(pmin) + "_minMemb_" + str(minMemb)+ "_minSeg_"+ str(minSeg) +"_sigMin_" + str(sigMin) + "_sigWeights_" + str(sigWeights) \ + "_sigSmooth_" + str(sigSmooth) + "_nodeNumStop_" + str(nodeNumStop) save_h5(seg, new_folder_path + "/segmentation.h5", key) save_h5(sup, new_folder_path + "/super_pixels.h5", key) seg_data = np.zeros((2,3)) seg_data[0,0] = ri seg_data[0,1] = voi seg_data[1,0] = nodes_in_dgt seg_data[1,1] = nodes_in_sup seg_data[1,2] = nodes_in_seg save_h5(seg_data, new_folder_path + "/seg_data", key, None)
def test(ilp, files, gt_path, dense_gt_path, labels="", loops=3, weights="", repeats=1, outpath= "", t_cache = "", p_cache = ""): """ Run test :param: ilp : path to ilp project to use for training :param: files : path to file to do batch prediction on :param: gt_path : path to trimap groundtruth :param: dense_gt_path : path to dense groundtruth :param: labels : amount of labeled pixels to use in training :param: loops : amount of autocontext loops :param: weights : weighting of the labels over the autocontext loops :param: repeats : amount of repeat runs of the test :param: outpath : outpath for test outputs :param: t_cache : path to training data cache :param: p_cache : path to prediction data cache """ hostname = socket.gethostname() print "test information:" print print "hostname:", hostname print print "ilp_file:", ilp print print "files to predict:", files print print "groundtruth:", gt_path print print "dense groundtruth:", dense_gt_path print print "labels:", labels print print "loops:", loops print print "weights:", weights print print "repeats:", repeats # Collect the test specifications ilp_split = ilp.split(".")[-2] training_file = "training file: "+ilp_split.split("/")[-1] gt_split = gt_path.split(".")[-2] trimap_file = "trimap file: " + gt_split.split("/")[-1] # Assign paths filesplit = files.split(".")[-2] filename = filesplit.split("/")[-1] prediction_file = "prediction file: "+ filename test_folder_path = assign_path(hostname)[5] if t_cache == "": t_cache = test_folder_path + "/t_cache" if p_cache == "": p_cache = test_folder_path + "/p_cache/" + filename if not os.path.exists(p_cache): os.mkdir(p_cache) if outpath == "": output = test_folder_path + "/q_data" else: output = outpath print print "outpath:", outpath print print "t_cache:", t_cache print print "p_cache:", p_cache # Create file tags if labels == "": label_tag = "all" else: # assert labels == check_ilp_labels(ilp) true_labels = check_ilp_labels(ilp) label_tag = true_labels if weights == "": weight_tag = "none" else: weight_tag = str(weights) # Make folder for quality data if "manual" in ilp: filename += "_manual" if "hand_drawn" in ilp: filename += "_hand_drawn" if "clever" in ilp: filename += "_clever" if "less_feat" in ilp: filename += "_less_feat" #change labels for every test # if labels != "": # print # print "reducing labels to " + str(labels) # ilp = reduce_labels_in_ilp(ilp, labels) file_dir = output + "/" + filename if not os.path.exists(file_dir): os.mkdir(file_dir) # Overwrite folder directory # file_dir = "/mnt/CLAWS1/stamilev/delme" # Check if file directory exists, if not make such directory if not os.path.exists(file_dir): print print "Output folder did not exist." os.mkdir(file_dir) print print "New one named " + file_dir + " was created." q_outpath = file_dir + "/n_" + str(loops) + "_l_" + str(label_tag) + "_w_" + weight_tag prob_folder = q_outpath + "/prob_files" q_data_outpath = q_outpath + "/n_" + str(loops) + "_l_" + str(label_tag) + "_w_" + weight_tag + ".h5" # Check if test directory exists, if not make such directory if not os.path.exists(q_outpath): print print "Output h5 file did not exist" os.mkdir(q_outpath) os.mkdir(prob_folder) print print "New one named " + q_outpath + " was created." if not os.path.exists(prob_folder): os.mkdir(prob_folder) # Run the test for i in range(repeats): print print "round of repeats %d of %d" % (i+1, repeats) #train on ilp project ac_train(ilp, labels, loops, weights, t_cache, outpath) print print "training completed" #batch predict files ac_batch_predict(files, t_cache, p_cache, overwrite = "") print print "batch prediction completed" #save prob files and quality data prob_file = [x for x in os.listdir(p_cache) if ("probs" in x)] predict_path = p_cache + "/" + prob_file[0] predict_data = adjust_predict(read_h5(predict_path)) prob_path = prob_folder + "/prob_"+ str(i+1)+ ".h5" # if os.path.exists(prob_path): # # TODO:not working yet # prob_path = prob_path.split("/")[-2] + "/new_" + prob_path.split("/")[-1] # print prob_path save_h5(predict_data, prob_path, "data") save_quality_values(predict_path, gt_path, dense_gt_path, q_data_outpath, (0,49,99)) #save test specification data save_h5([training_file, prediction_file, trimap_file], q_data_outpath, "used files", None) save_h5([labels],q_data_outpath, "autocontext_parameters/#labels") save_h5([loops], q_data_outpath, "autocontext_parameters/#loops") save_h5([str(weight_tag)], q_data_outpath, "autocontext_parameters/weights") print print "quality data saved" #save configuration data # call(["cp", predict_path, q_outpath]) save_h5(["pmin", "minMemb", "minSeg", "sigMin", "sigWeights", "sigSmooth", "cleanCloseSeeds", "returnSeedsOnly"], q_data_outpath, "segmentation/wsDt parameters", None) save_h5(["edge_weights", "edgeLengths", "nodeFeatures", "nodeSizes", "nodeLabels", "nodeNumStop", "beta", "metric", "wardness", "out"], q_data_outpath, "segmentation/agglCl parameters", None) print print "quality data saved"
def compress(input, ouput): file = read_h5(input[0], input[1]) save_h5(file, output[0], output[1], "lzf")
# for l in range(t-1): # data[i+l,j,k] = 1 # if data[i,j,k] == 1 and data[i+t,j,k] == 0: # for l in range(t): # data[i+l,j,k] = 1 # return data def label_neurons(data, neuron_label): for i in range(data.shape[0]): for j in range(data.shape[1]): for k in range(data.shape[2]): if data[i,j,k] != 1 and data[i,j,k] != 0: data[i,j,k] = neuron_label return data def filter_membrane(dense_gt_data): prepared_data = prepare_data(dense_gt_data) print "prepared data unique", np.unique(prepared_data) memb = create_membrane(prepared_data) memb_data = label_neurons(memb, 0) return memb_data, dense_gt_data if __name__ == '__main__': data = read_h5("/home/stamylew/volumes/groundtruth/dense_groundtruth/100p_cube1_dense_gt.h5", "data") memb = filter_membrane(data) save_h5(memb, "/home/stamylew/volumes/groundtruth/memb/100p_cube1_memb.h5", "data", "lzf") print "done"
# -*- coding: utf-8 -*- """ Created on Tue Sep 22 14:31:38 2015 @author: stamylew """ import numpy as np from python_functions.handle_h5.handle_h5 import read_h5, save_h5 def threshold(data, threshold_membrane, threshold_neurons): bin_data = np.zeros(data.shape) for i in range(data.shape[0]): for j in range(data.shape[1]): for k in range(data.shape[2]): if data[i,j,k] <= threshold_membrane: bin_data[i,j,k] = 0 if data[i,j,k] > threshold_membrane and data[i,j,k] <= threshold_neurons: bin_data[i,j,k] = 0.5 if data[i,j,k] > threshold_neurons: bin_data[i,j,k] = 1 return bin_data if __name__ == '__main__': f = read_h5("/home/stamylew/volumes/100x100block_of_validation_sample_Probabilities.h5", "exported_data") data = threshold(f, 0.4, 0.6) save_h5(data, "/home/stamylew/volumes/100x100block_of_validation_sample_Probabilities.h5", "bin", None) print "done"