def PrintResults(prefix, vertex_ones, vertex_twos, edge_weights, maintained_edges, algorithm): # get the ground truth and print out the results seg2gold_mapping = seg2gold.Mapping(prefix) # get the number of edges nedges = edge_weights.shape[0] # see how multicut has changed the results labels = [] cnn_results = [] multicut_results = [] # go through each edge for ie in range(nedges): vertex_one = vertex_ones[ie] vertex_two = vertex_twos[ie] # skip if there is no ground truth if seg2gold_mapping[vertex_one] < 1 or seg2gold_mapping[vertex_two] < 1: continue # over 0.5 on edge weight means the edge should collapse cnn_results.append(edge_weights[ie] > 0.5) # since this edge has ground truth add to list # subtract one here since a maintained edge is one that should not be merged multicut_results.append(1 - maintained_edges[ie]) if seg2gold_mapping[vertex_one] == seg2gold_mapping[vertex_two]: labels.append(True) else: labels.append(False) print 'CNN Results:' PrecisionAndRecall(np.array(labels), np.array(cnn_results)) print 'Multicut Results' PrecisionAndRecall(np.array(labels), np.array(multicut_results))
def MergeGroundTruth(prefix, model_prefix): # read the segmentation data segmentation = dataIO.ReadSegmentationData(prefix) # get the multicut filename (with graph weights) multicut_filename = 'multicut/{}-{}.graph'.format(model_prefix, prefix) # read the gold data gold = dataIO.ReadGoldData(prefix) # read in the segmentation to gold mapping mapping = seg2gold.Mapping(segmentation, gold) # get the maximum segmentation value max_value = np.amax(segmentation) + 1 # create union find data structure union_find = [UnionFind.UnionFindElement(iv) for iv in range(max_value)] # read in all of the labels with open(multicut_filename, 'rb') as fd: # read the number of vertices and edges nvertices, nedges, = struct.unpack('QQ', fd.read(16)) # read in all of the edges for ie in range(nedges): # read in the two labels label_one, label_two, = struct.unpack('QQ', fd.read(16)) # skip over the reduced labels and edge weight fd.read(24) # if the labels are the same and the mapping is non zero if mapping[label_one] == mapping[label_two] and mapping[label_one]: UnionFind.Union(union_find[label_one], union_find[label_two]) # create a mapping mapping = np.zeros(max_value, dtype=np.int64) # update the segmentation for iv in range(max_value): label = UnionFind.Find(union_find[iv]).label mapping[iv] = label merged_segmentation = seg2seg.MapLabels(segmentation, mapping) gold_filename = 'gold/{}_gold.h5'.format(prefix) # TODO fix this code temporary filename truth_filename = 'multicut/{}-truth.h5'.format(prefix) # temporary write h5 file dataIO.WriteH5File(merged_segmentation, truth_filename, 'stack') import time start_time = time.time() print ('Ground truth: ') # create the command line command = '~/software/PixelPred2Seg/comparestacks --stack1 {} --stackbase {} --dilate1 1 --dilatebase 1 --relabel1 --relabelbase --filtersize 100 --anisotropic'.format(truth_filename, gold_filename) # execute the command os.system(command) print (time.time() - start_time)
# read the input segmentation data segmentation = dataIO.ReadSegmentationData(prefix) # subset is either training, validation, or testing subset = 'testing' # remove the singleton slices node_generation.RemoveSingletons(prefix, segmentation) print("Step 1 done") # need to update the prefix and segmentation # removesingletons writes a new h5 file to disk prefix = '{}-segmentation-wos'.format(prefix) segmentation = dataIO.ReadSegmentationData(prefix) # need to rerun seg2gold mapping since segmentation changed seg2gold_mapping = seg2gold.Mapping(prefix, segmentation, gold) print("Step 2 done") # generate locations for segments that are too small node_generation.GenerateNodes(prefix, segmentation, subset, seg2gold_mapping) print("Step 3 done") # run inference for node network node_model_prefix = '/n/home03/tanav/axonem/biologicalgraphs/neuronseg/architectures/nodes-400nm-3x20x60x60-Kasthuri/nodes' forward.Forward(prefix, node_model_prefix, segmentation, subset, seg2gold_mapping, evaluate=True)