def apply_batch(operator, testset, folder_prefix, procs=None): ''' Applies an operator in all images of a given testset. The images are saved in folder_prefix with the same name as the groundtruth images. ''' for (i, o, m) in testset: inp = p.load_image(i) msk = p.load_mask_image(m, inp.shape, operator.window) o_name = os.path.split(o)[1] res = operator.apply(inp, msk) p.save_image(res, '%s/%s' % (folder_prefix, o_name))
for i in range(5): win = w.random_win(domain, 40, True) op = trios.WOperator(win, SKClassifier(DecisionTreeClassifier()), RAWFeatureExtractor) print('Training...', i) op.train(images) ops.append(op) comb = CombinationPattern(*ops) wop2 = trios.WOperator(comb.window, SKClassifier(DecisionTreeClassifier(), ordered=True), comb, batch=True) print('Training 2nd level') wop2.train(images2) # save trained operator p.save_gzip(wop2, 'dt-tl-jung.op') # and load it later wop2 = p.load_gzip('dt-tl-jung.op') # load image and apply operator. Second argument is application mask. img = p.load_image('images/jung-1a.png') print('Applying to image jung-1a.png') out = wop2.apply(img, img) p.save_image(out, 'out-isi-jung-1a.png') print('Accuracy', wop2.eval(test, procs=1)) print(wop2.cite_me())
from trios.classifiers import SKClassifier from sklearn.tree import DecisionTreeClassifier from trios.feature_extractors import Aperture import trios import numpy as np import trios.shortcuts.persistence as p if __name__ == '__main__': images = trios.Imageset([('images_gl/einstein-noise.png', 'images_gl/einstein-original.png', None)]) win = np.ones((7, 7), np.uint8) # use Decision Tree Classifier and raw pixels as features. ap = Aperture(win, 10, mul=0.5) op = trios.WOperator(win, SKClassifier(DecisionTreeClassifier()), ap) print('Training...') op.train(images) # save trained operator p.save_gzip(op, 'ap-einstein.op') # and load it later op2 = p.load_gzip('ap-einstein.op') # load image and apply operator. Second argument is application mask. img = p.load_image('images_gl/einstein-noise.png') print('Applying to image images_gl/einstein-noise.png') out = op.apply(img, img) p.save_image(out, 'out-ap-einstein.png')
images = trios.Imageset.read('images/level1.set') win = np.ones((5, 5), np.uint8) # use Decision Tree Classifier and raw pixels as features. op = trios.WOperator(win, SKClassifier(DecisionTreeClassifier()), RAWFeatureExtractor) print('Training...') op.train(images) # save trained operator p.save_gzip(op, 'dt-jung.op') # and load it later op2 = p.load_gzip('dt-jung.op') # load image and apply operator. Second argument is application mask. img = p.load_image('images/jung-1a.png') print('Applying to image jung-1a.png') out = op.apply(img, img) p.save_image(out, 'out-dt-jung-1a.png') test = trios.Imageset.read('images/test.set') print('Accuracy', op.eval(test, procs=1, per_image=True)) print('Accuracy', op.eval(test, procs=2, per_image=True)) print('Accuracy', op.eval(test, procs=8, per_image=True)) op.extractor.batch_size = 100 out_5000 = op.apply(img, img) assert np.sum(out_5000 != out) == 0 p.save_image(out, 'out-dt-jung-1a-2.png') #print(op2.cite_me())
import trios.shortcuts.persistence as p import trios import numpy as np if __name__ == '__main__': jung_op = p.load_gzip('trained-jung.op.gz') input13 = p.load_image('jung-images/jung-15b.png') # The jung operator uses the input image as mask. Thus, it erases # all pixels that do not belong to an s. result = jung_op.apply(input13, input13) p.save_image(result, 'jung13-output.png') testset = trios.Imageset.read('jung-images/test.set') print('Error:', jung_op.eval(testset))
from trios.classifiers import SKClassifier from sklearn.tree import DecisionTreeClassifier from trios.contrib.features.lbp import LBPExtractor import trios import numpy as np import trios.shortcuts.persistence as p if __name__ == '__main__': images = trios.Imageset.read('images/training.set') win = np.ones((5, 5), np.uint8) op = trios.WOperator(win, SKClassifier(DecisionTreeClassifier()), LBPExtractor) op.train(images) img = p.load_image('images/jung-1a.png') lbp = op.apply(img, img) p.save_image(lbp, 'lbp-out.png') test = trios.Imageset.read('images/test.set') print('Accuracy', op.eval(test, procs=7))
import pyximport pyximport.install() from trios.classifiers import SKClassifier from sklearn.tree import DecisionTreeClassifier from sklearn import svm from trios.contrib.features.sobel import SobelExtractor import trios import numpy as np import trios.shortcuts.persistence as p if __name__ == '__main__': images = trios.Imageset.read('images/training.set') win = np.ones((5, 5), np.uint8) sobel = SobelExtractor(window=win, batch_size=1000) op = trios.WOperator(win, SKClassifier(svm.LinearSVC()), sobel) op.train(images) img= p.load_image('images/jung-1a.png') lbp = op.apply(img, img) p.save_image(lbp, 'sobel-out.png') test = trios.Imageset.read('images/test.set') print('Accuracy', op.eval(test, procs=7))