Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
classify.save_classifier(rf4, 'example-data/rf-4.joblib')
learned_policy4 = agglo.classifier_probability(fc, rf4)
g_test4 = agglo.Rag(ws_test, p4_test, learned_policy4, feature_manager=fc)
(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)
classify.save_classifier(rf4, 'example-data/rf-4.joblib')
learned_policy4 = agglo.classifier_probability(fc, rf4)
g_test4 = agglo.Rag(ws_test, p4_test, learned_policy4, feature_manager=fc)
g_test4.agglomerate(0.5)
seg_test4 = g_test4.get_segmentation()
imio.write_h5_stack(seg_test4, 'example-data/test-seg4.lzf.h5', compression='lzf')
def testAggoBuild(self):
from gala import agglo
watershed, boundary, dummy = self.gen_watershed()
stack = agglo.Rag(watershed, boundary, nozeros=True)
self.assertEqual(stack.number_of_nodes(), 3630)
stack.agglomerate(0.5)
self.assertEqual(stack.number_of_nodes(), 61)
stack.remove_inclusions()
self.assertEqual(stack.number_of_nodes(), 61)
def test_snemi():
feat = default.snemi3d()
g = agglo.Rag(ws, prob, feature_manager=feat, use_slow=True)
# contact are edge features, so they are inserted just before the 8
# difference features in the base paper_em vector.
expected = np.concatenate((ans12[:-8], contact, ans12[-8:]))
assert_allclose(feat(g, 1, 2), expected, atol=0.01)
def testAggoBuild(self):
from gala import agglo
watershed, boundary, dummy = self.gen_watershed()
stack = agglo.Rag(watershed, boundary, nozeros=True)
self.assertEqual(stack.number_of_nodes(), 3630)
stack.agglomerate(0.5)
self.assertEqual(stack.number_of_nodes(), 61)
stack.remove_inclusions()
self.assertEqual(stack.number_of_nodes(), 61)
def run_matched(f, fn, c=1,
edges=[(1, 2), (6, 3), (7, 4)],
merges=[(1, 2), (6, 3)]):
p = probs1 if c == 1 else probs2
g = agglo.Rag(wss1, p, feature_manager=f, use_slow=True)
o = list_of_feature_arrays(g, f, edges, merges)
with open(fn, 'rb') as fin:
r = pck.load(fin, encoding='bytes')
assert_equal_lists_or_arrays(o, r)
def test_ladder_agglomeration():
i = 2
g = agglo.Rag(wss[i], probs[i], agglo.boundary_mean,
normalize_probabilities=True, use_slow=True,
update_unchanged_edges=True)
g.agglomerate_ladder(3)
g.agglomerate(0.51)
assert_allclose(ev.vi(g.get_segmentation(), results[i]), 0.0,
err_msg='Ladder agglomeration failed.')
def bench_suite():
times = OrderedDict()
memory = OrderedDict()
wstr, prtr, gttr = trdata()
with timer() as t_build_rag:
g = agglo.Rag(wstr, prtr)
times['build RAG'] = t_build_rag[0]
memory['base RAG'] = asizeof(g)
with timer() as t_features:
g.set_feature_manager(em)
times['build feature caches'] = t_features[0]
memory['feature caches'] = asizeof(g) - memory['base RAG']
with timer() as t_flat:
_ignore = g.learn_flat(gttr, em)
times['learn flat'] = t_flat[0]
with timer() as t_gala:
(X, y, w, e), allepochs = g.learn_agglomerate(gttr, em,
min_num_epochs=5)
y = y[:, 0] # ignore rand-sign and vi-sign schemes
memory['training data'] = asizeof((X, y, w, e))
times['learn agglo'] = t_gala[0]
with timer() as t_train_classifier: