def test_slice(self): img = trios.Imageset([('a', 'b', None), ('c', 'b', None), ('d', 'b', None), ('e', 'b', None)]) img2 = img[:2] self.assertEqual(type(img2), trios.Imageset) self.assertEqual(img2.input_preffix, img.input_preffix) self.assertEqual(img2.output_preffix, img.output_preffix) self.assertEqual(img2.mask_preffix, img.mask_preffix)
def test_write_read(self): img = trios.Imageset([('a', 'b', None), ('c', 'b', 'er')]) img.write('test-imageset.txt') img2 = trios.Imageset.read('test-imageset.txt') self.assertEqual(len(img), len(img2)) self.assertEqual(img[0], img2[0]) self.assertEqual(img[1], img2[1])
def test_append(self): img = trios.Imageset() with self.assertRaises(TypeError) as context: img.append('image_name') with self.assertRaises(TypeError) as context: img.append(('image_name', )) with self.assertRaises(TypeError) as context: img.append(('image_name', 'out_name')) img.append(('image_name', 'out_name', 'mask_name')) self.assertEqual(img.image_list[-1], ('image_name', 'out_name', 'mask_name')) self.assertEqual(img[-1], ('image_name', 'out_name', 'mask_name'))
from trios.classifiers import SKClassifier from sklearn.tree import DecisionTreeClassifier from trios.contrib.features.sobel import SobelExtractor import trios import numpy as np import trios.shortcuts.persistence as p drive_location = 'datasets/drive' training = trios.Imageset([ ('%s/training/images/%2d_training.tif'%(drive_location, i), '%s/training/1st_manual/%2d_manual1.gif'%(drive_location, i), '%s/training/mask/%2d_training_mask.gif'%(drive_location, i)) for i in range(21, 41)]) testset = trios.Imageset([ ('%s/test/images/%02d_test.tif'%(drive_location, i), '%s/test/1st_manual/%02d_manual1.gif'%(drive_location, i), '%s/test/mask/%02d_test_mask.gif'%(drive_location, i)) for i in range(1, 21)]) if __name__ == '__main__': win = np.ones((9,9), np.uint8) sobel = SobelExtractor(window=win) op = trios.WOperator(win, SKClassifier(DecisionTreeClassifier()), sobel) print('Training') op.train(training) print('Evaluating') print('Accuracy:', 1 - op.eval(testset))
def test_creation_empty(self): img = trios.Imageset() self.assertEqual(img.image_list, []) self.assertEqual(img.input_preffix, '') self.assertEqual(img.output_preffix, '') self.assertEqual(img.mask_preffix, '')
def test_len(self): imgset = trios.Imageset() self.assertEqual(len(imgset), 0) imgset.append(('asdl', '1sdf', 'ldkasl')) self.assertEqual(len(imgset), 1)
def test_indexing(self): img = trios.Imageset([('a', 'b', None), ('c', 'b', None), ('d', 'b', None), ('e', 'b', None)]) self.assertEqual(img[0], ('a', 'b', None)) self.assertEqual(img[-1], ('e', 'b', None))
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')
import trios import trios.shortcuts.persistence as p if __name__ == '__main__': # Imageset can be defined directly in code, like below images = trios.Imageset([('input.png', 'output.png', None), ('input2.png', 'output2.png', 'mask.png')]) # Definitions in code can be put in a module and imported like regular # Python code. # It can also be saved to a gziped file using the persistence p.save_gzip(images, 'imageset.gz') # And loaded back images2 = p.load_gzip('imageset.gz') assert images[0] == images2[0] assert images[1] == images2[1] assert len(images) == len(images2) # Or saved to a text-only format using the read and write methods. images.write('imageset-text.txt') images3 = trios.Imageset.read('imageset-text.txt') assert images[0] == images3[0] assert images[1] == images3[1] assert len(images) == len(images3)