def _train_model(self, model_file=None): print("Creating GALA feature manager...") fm = features.moments.Manager() fh = features.histogram.Manager(25, 0, 1, [0.1, 0.5, 0.9]) # Recommended numbers in the repo fg = features.graph.Manager() fc = features.contact.Manager() self.fm = features.base.Composite(children=[fm, fh, fg, fc]) if model_file is not None and os.path.isfile(model_file): print('Loading model from path ...') rf = classify.load_classifier(model_file) else: gt, pr, sv = (map(imio.read_h5_stack, [self.gt, self.mem, self.sp])) print("Creating training RAG...") g_train = agglo.Rag(sv, pr, feature_manager=self.fm) print("Learning agglomeration...") (X, y, w, merges) = g_train.learn_agglomerate(gt, self.fm, learning_mode='permissive', min_num_epochs=self.min_ep)[0] y = y[:, 0] rf = classify.DefaultRandomForest().fit(X, y) # Save if path requested if model_file is not None: classify.save_classifier(rf, model_file) self.model = agglo.classifier_probability(self.fm, rf)
gt_test, pr_test, p4_test, ws_test = map(imio.read_h5_stack, ['example-data/test-gt.lzf.h5', 'example-data/test-p1.lzf.h5', 'example-data/test-p4.lzf.h5', 'example-data/test-ws.lzf.h5']) fm = features.moments.Manager() fh = features.histogram.Manager() fc = features.base.Composite(children=[fm, fh]) g_train = agglo.Rag(ws_train, pr_train, feature_manager=fc) np.random.RandomState(0) (X, y, w, merges) = map(np.copy, map(np.ascontiguousarray, g_train.learn_agglomerate(gt_train, fc)[0])) print X.shape np.savez('example-data/train-set.npz', X=X, y=y) y = y[:, 0] rf = classify.DefaultRandomForest() X.shape np.random.RandomState(0) rf = rf.fit(X, y) classify.save_classifier(rf, 'example-data/rf-1.joblib') learned_policy = agglo.classifier_probability(fc, rf) g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc) g_test.agglomerate(0.5) seg_test1 = g_test.get_segmentation() imio.write_h5_stack(seg_test1, 'example-data/test-seg1.lzf.h5', compression='lzf') g_train4 = agglo.Rag(ws_train, p4_train, feature_manager=fc) np.random.RandomState(0) (X4, y4, w4, merges4) = map(np.copy, map(np.ascontiguousarray, g_train4.learn_agglomerate(gt_train, fc)[0])) print X4.shape np.savez('example-data/train-set4.npz', X=X4, y=y4) y4 = y4[:, 0] rf4 = classify.DefaultRandomForest() np.random.RandomState(0) rf4 = rf4.fit(X4, y4)
def train_and_save_classifier(training_data_file, filename, classifier_kind='random forest'): X, y = load_training_data(training_data_file) cl = classify.get_classifier(classifier_kind) cl.fit(X, y) classify.save_classifier(cl, filename, use_joblib=False)
im = scipy.io.loadmat(inFileImage)['im'] im = im.astype('int32') membraneTrain = scipy.io.loadmat(inFileMembrane)['membrane'] membraneTrain = membraneTrain.astype('float32') gt_train = scipy.io.loadmat(inFileTruth)['truth'] gt_train = gt_train.astype('int64') #just in case! ws_train = scipy.io.loadmat(inFileWatershed)['ws'] ws_train = ws_train.astype('int64') #just in case! xdim, ydim, zdim = (im.shape) fc = features.base.Composite(children=[features.moments.Manager(), features.histogram.Manager(25, 0, 1, [0.1, 0.5, 0.9]), features.graph.Manager(), features.contact.Manager([0.1, 0.5, 0.9]) ]) print "Creating RAG..." # create graph and obtain a training dataset g_train = agglo.Rag(ws_train, membraneTrain, feature_manager=fc) print 'Learning agglomeration...' (X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc,min_num_epochs=5)[0] y = y[:, 0] # gala has 3 truth labeling schemes, pick the first one print(X.shape, y.shape) # standard scikit-learn input format print "Training classifier..." # train a classifier, scikit-learn syntax rf = classify.DefaultRandomForest().fit(X, y) # a policy is the composition of a feature map and a classifier learned_policy = agglo.classifier_probability(fc, rf) classify.save_classifier(rf,'/mnt/pipeline/tools/i2g/packages/gala/ac4_full_classifier_v2.rf')