|
| 1 | +#! /usr/local/python-2.7.6/bin/python |
| 2 | +# |
| 3 | +# Copyright (C) 2015 by Howard Hughes Medical Institute. |
| 4 | +# |
| 5 | +# Purpose: perform 2D segmentation using GALA |
| 6 | +# then post-process results |
| 7 | +# |
| 8 | +# ------------------------- imports ------------------------- |
| 9 | + |
| 10 | +#from __future__ import absolute_import |
| 11 | +#from __future__ import print_function |
| 12 | +# imports |
| 13 | +import os, sys, re, optparse |
| 14 | +from gala import imio, classify, features, agglo, evaluate as ev |
| 15 | +from six.moves import map |
| 16 | +import numpy as np |
| 17 | + |
| 18 | +import MS_LIB_Dict |
| 19 | +import MS_LIB_Options |
| 20 | +import MS_LIB_IO |
| 21 | + |
| 22 | +from PIL import Image |
| 23 | + |
| 24 | +ms_home = os.environ['MS_HOME'] # where source code is located |
| 25 | +ms_data = os.environ['MS_DATA'] # dir for initial input data and final output |
| 26 | +ms_temp = os.environ['MS_TEMP'] # dir for intermediate data files |
| 27 | +gala_home = os.environ['GALA_HOME'] # where source code is located |
| 28 | +ilastik_home = os.environ['ILASTIK_HOME'] # where source code is located |
| 29 | + |
| 30 | +# ----------------------------------------------------------------------------- |
| 31 | + |
| 32 | +def perform_segmentation(training_raw, groundtruth_annot_dir, \ |
| 33 | + groundtruth_seg_dir, production_raw, \ |
| 34 | + options): |
| 35 | + raw_files = os.listdir(os.path.join(ms_data, training_raw)) |
| 36 | + labels = os.listdir(os.path.join(ms_data, groundtruth_annot_dir)) |
| 37 | + |
| 38 | + # Assume only one raw and one labels file for now |
| 39 | + command = "MS_UT_CreateH5LabelsForIlastik.py " + groundtruth_annot_dir + \ |
| 40 | + " groundtruth.h5" |
| 41 | + print "\nRunning the commmand: " , command |
| 42 | + os.system(command) |
| 43 | + |
| 44 | + # Generate Ilastik project file |
| 45 | + command = "python " + os.path.join(ilastik_home, "ilastik", "bin", "train_headless.py") + \ |
| 46 | + " training.ilp '" + training_raw + "/*.png' groundtruth.h5/main" |
| 47 | + print "\nRunning the commmand: " , command |
| 48 | + os.system(command) |
| 49 | + |
| 50 | + # Produce the probabilities and oversegmentation files from the inputs |
| 51 | + if not os.path.isdir('seg_data'): |
| 52 | + print "\nRunning the commmand: mkdir seg_data" |
| 53 | + os.mkdir('seg_data') |
| 54 | + else: |
| 55 | + print "\nRunning the commmand: rm -f seg_data/*" |
| 56 | + os.system("rm -rf seg_data/*") |
| 57 | + if os.path.isfile('STACKED_prediction.h5'): |
| 58 | + command = "rm -f STACKED_prediction.h5" |
| 59 | + print "\nRunning the commmand: " , command |
| 60 | + os.system(command) |
| 61 | + if not os.path.islink('gala-segmentation-pipeline'): |
| 62 | + os.symlink('gala-segmentation-pipeline', os.path.join(gala_home, 'gala',\ |
| 63 | + 'bin', 'gala-segmentation-pipeline')) |
| 64 | + command = "python gala-segmentation-pipeline " + \ |
| 65 | + " . -I '" + training_raw + "/*.png' --ilp-file training.ilp " + \ |
| 66 | + " --enable-gen-supervoxels --enable-gen-agglomeration " + \ |
| 67 | + " --enable-gen-pixel --seed-size 5 --segmentation-thresholds 0.0" |
| 68 | + print "\nRunning the commmand: " , command |
| 69 | + os.system(command) |
| 70 | + |
| 71 | + # Put probabilities in the right format: |
| 72 | + command = "MS_UT_ProbsNF2GALA.py STACKED_prediction.h5 probabilities_training.h5" |
| 73 | + print "\nRunning the commmand: ", command |
| 74 | + os.system(command) |
| 75 | + |
| 76 | + # Rename the oversegmentation training file |
| 77 | + for file in os.listdir("seg_data"): |
| 78 | + if re.search(".h5", file): |
| 79 | + oversegmentation_file = os.path.join("seg_data", file) |
| 80 | + command = "mv " + oversegmentation_file + " oversegmentation_training.h5" |
| 81 | + print "\nRunning the commmand: ", command |
| 82 | + os.system(command) |
| 83 | + |
| 84 | + # Create the groundtruuth segmentation labels file |
| 85 | + command = "MS_UT_CreateH5Groundtruth.py " + groundtruth_seg_dir + " seg_labels_file.h5" |
| 86 | + print "\nRunning the commmand: " , command |
| 87 | + os.system(command) |
| 88 | + |
| 89 | + # Read in training data |
| 90 | + print "\nReading training data into gala..." |
| 91 | + gt_train, pr_train, ws_train = (map(imio.read_h5_stack, |
| 92 | + ['seg_labels_file.h5', 'probabilities_training.h5', |
| 93 | + 'oversegmentation_training.h5'])) |
| 94 | + |
| 95 | + # create a feature manager |
| 96 | + fm = features.moments.Manager() |
| 97 | + fh = features.histogram.Manager() |
| 98 | + fc = features.base.Composite(children=[fm, fh]) |
| 99 | + |
| 100 | + # create graph and obtain a training dataset |
| 101 | + g_train = agglo.Rag(ws_train, pr_train, feature_manager=fc) |
| 102 | + (X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc)[0] |
| 103 | + y = y[:, 0] # gala has 3 truth labeling schemes, pick the first one |
| 104 | + print((X.shape, y.shape)) # standard scikit-learn input format |
| 105 | + |
| 106 | + # train a classifier, scikit-learn syntax |
| 107 | + rf = classify.DefaultRandomForest().fit(X, y) |
| 108 | + # a policy is the composition of a feature map and a classifier |
| 109 | + learned_policy = agglo.classifier_probability(fc, rf) |
| 110 | + |
| 111 | + if not os.path.isdir('seg_data'): |
| 112 | + print "\nRunning the commmand: mkdir seg_data" |
| 113 | + os.mkdir('seg_data') |
| 114 | + else: |
| 115 | + command = "rm -f seg_data/*" |
| 116 | + print "\nRunning the commmand: ", command |
| 117 | + os.system(command) |
| 118 | + if os.path.isfile('STACKED_prediction.h5'): |
| 119 | + command = "rm -f STACKED_prediction.h5" |
| 120 | + print "\nRunning the commmand: " , command |
| 121 | + os.system(command) |
| 122 | + |
| 123 | + # Produce the probabilities and oversegmentation files from the inputs |
| 124 | + command = "python " + os.path.join(gala_home, "gala", "bin", "gala-segmentation-pipeline") + \ |
| 125 | + " . -I '" + productiobn_raw + "/*.png' --ilp-file training.ilp " + \ |
| 126 | + " --enable-gen-supervoxels --enable-gen-agglomeration " + \ |
| 127 | + " --enable-gen-pixel --seed-size 5 --segmentation-thresholds 0.0" |
| 128 | + print "\nRunning the commmand: ", command |
| 129 | + os.system(command) |
| 130 | + |
| 131 | + # Put probabilities in the right format: |
| 132 | + if os.path.isdir("seg_data"): |
| 133 | + print "\nRunning the commmand: rm -rf seg_data" |
| 134 | + os.system("rm -rf seg_data") |
| 135 | + command = "MS_UT_ProbsNF2GALA.py STACKED_prediction.h5 probabilities_production.h5" |
| 136 | + print "\nRunning the commmand: ", command |
| 137 | + os.system(command) |
| 138 | + |
| 139 | + # Rename the oversegmentation production file |
| 140 | + for file in os.listdir("seg_data"): |
| 141 | + if re.search(".h5", file): |
| 142 | + oversegmentation_file = os.path.join("seg_data", file) |
| 143 | + command = "mv " + oversegmentation_file + " oversegmentation_production.h5" |
| 144 | + |
| 145 | + # get the production data and make a RAG with the trained policy |
| 146 | + pr_test, ws_test = (map(imio.read_h5_stack, |
| 147 | + ['probabilities_production.h5', 'oversegmentation_production.h5'])) |
| 148 | + g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc) |
| 149 | + g_test.agglomerate(0.5) # best expected segmentation |
| 150 | + seg_production = g_test.get_segmentation() |
| 151 | + |
| 152 | + # Output the segmentation file |
| 153 | + img = Image.fromarray(seg_production) |
| 154 | + img.save('segmentation_production.h5') |
| 155 | + |
| 156 | + |
| 157 | +# ----------------------------------------------------------------------------- |
| 158 | + |
| 159 | +def output_results(ws_test, gt_test, seg_test1): |
| 160 | + results = np.vstack(( |
| 161 | + ev.split_vi(ws_test, gt_test), |
| 162 | + ev.split_vi(seg_test1, gt_test), |
| 163 | + )) |
| 164 | + print(results) |
| 165 | + |
| 166 | +# ----------------------------------------------------------------------------- |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + |
| 170 | + usage = "Usage: \n\ |
| 171 | + %prog <raw_train_data> <gt_labels> <raw_data> [options (-h to list)]" |
| 172 | + |
| 173 | + parser = optparse.OptionParser(usage=usage, version="%%prog ") |
| 174 | + parser = MS_LIB_Options.GalaSegmentation2D_command_line_parser(parser) |
| 175 | + (options, args) = parser.parse_args() |
| 176 | + |
| 177 | + if len(args) == 4: |
| 178 | + training_raw = args[0] |
| 179 | + gt_annot_labels = args[1] |
| 180 | + gt_seg_labels = args[2] |
| 181 | + production_raw = args[3] |
| 182 | + perform_segmentation(training_raw, gt_annot_labels, gt_seg_labels, \ |
| 183 | + production_raw, options) |
| 184 | + else: |
| 185 | + parser.print_usage() |
| 186 | + sys.exit(2) |
| 187 | + |
0 commit comments