def test_ImageWriter(): samples = [('nut_color', 1), ('nut_grayscale', 2)] inpath = 'tests/data/img_formats/*.bmp' img_samples = samples >> ReadImage(0, inpath) >> Collect() imagepath = 'tests/data/test_*.bmp' names = samples >> Get(0) >> Collect() img_samples >> WriteImage(0, imagepath, names) >> Consume() for sample, name in zip(img_samples, names): filepath = 'tests/data/test_{}.bmp'.format(name) arr = load_image(filepath) assert np.array_equal(arr, sample[0]) os.remove(filepath) pathfunc = lambda sample, name: 'tests/data/test_{}.jpg'.format(name) img_samples >> WriteImage(0, pathfunc) >> Consume() for i, sample in enumerate(img_samples): filepath = 'tests/data/test_{}.jpg'.format(i) os.path.exists(filepath) os.remove(filepath) with pytest.raises(ValueError) as ex: img_samples >> WriteImage(0, ()) >> Consume() assert str(ex.value).startswith('Expect path or function')
def test_CheckNaN(): assert [1, 2] >> CheckNaN() >> Collect() == [1, 2] with pytest.raises(RuntimeError) as ex: [1, np.NaN, 3] >> CheckNaN() >> Consume() assert str(ex.value).startswith('NaN encountered') with pytest.raises(RuntimeError) as ex: [(1, np.NaN), (2, 4)] >> CheckNaN() >> Consume() assert str(ex.value).startswith('NaN encountered')
def test_BuildBatch_verbose(): with Redirect() as out: samples = [[1], [2], [3]] build_batch = (nb.BuildBatch(2, verbose=True).input(0, 'number', 'uint8')) samples >> build_batch >> Consume() assert out.getvalue() == '[2:uint8]\n[1:uint8]\n' with Redirect() as out: samples = [(np.array([1, 2, 3]), 0), (np.array([4, 5, 6]), 1), (np.array([7, 8, 9]), 1)] build_batch = (nb.BuildBatch(2, verbose=True).input( 0, 'vector', 'float32').output(1, 'one_hot', 'uint8', 2)) samples >> build_batch >> Consume() expected = '[[2x3:float32], [2x2:uint8]]\n[[1x3:float32], [1x2:uint8]]\n' assert out.getvalue() == expected
""" .. module:: read_images :synopsis: Example for reading and viewing of image data """ from nutsflow import Consume, Print from nutsml import ReadLabelDirs, ReadImage, ViewImageAnnotation, PrintColType if __name__ == "__main__": show_image = ViewImageAnnotation(0, 1, pause=1, figsize=(3, 3)) (ReadLabelDirs('images', '*.png') >> Print() >> ReadImage(0) >> PrintColType() >> show_image >> Consume())
""" from __future__ import print_function from glob import glob from nutsflow import Collect, Consume, Get, Zip, Map, ArgMax, Print from nutsml import (TransformImage, BuildBatch, ReadImage, ViewImageAnnotation, ConvertLabel) BATCH_SIZE = 128 if __name__ == "__main__": from cnn_train import create_network, load_names convert_label = ConvertLabel(None, load_names()) rerange = TransformImage(0).by('rerange', 0, 255, 0, 1, 'float32') show_image = ViewImageAnnotation(0, 1, pause=1, figsize=(3, 3), interpolation='spline36') pred_batch = BuildBatch(BATCH_SIZE).input(0, 'image', 'float32') print('loading network...') network = create_network() network.load_weights() print('predicting...') samples = glob('images/*.png') >> Print() >> ReadImage(None) >> Collect() predictions = (samples >> rerange >> pred_batch >> network.predict() >> convert_label) samples >> Get(0) >> Zip(predictions) >> show_image >> Consume()
""" .. module:: view_augmented_images :synopsis: Example nuts-ml pipeline for viewing augmented image data """ from nutsflow import Take, Consume from nutsml import ViewImageAnnotation, AugmentImage if __name__ == "__main__": from cnn_train import load_samples train_samples, _ = load_samples() p = 0.5 augment = (AugmentImage(0) .by('identical', 1.0) .by('brightness', p, [0.7, 1.3]) .by('rotate', p, [-10, 10]) .by('fliplr', p)) show_image = ViewImageAnnotation(0, 1, pause=1, figsize=(2, 2), interpolation='spline36') (train_samples >> Take(10) >> augment >> show_image >> Consume())
""" .. module:: read_images :synopsis: Example nuts-ml pipeline for reading and viewing image data """ from glob import glob from nutsflow import Consume from nutsml import ReadImage, ViewImage, PrintColType if __name__ == "__main__": show_image = ViewImage(0, pause=1, figsize=(2, 2), interpolation='spline36') paths = glob('images/*.png') paths >> ReadImage(None) >> PrintColType() >> show_image >> Consume()
""" .. module:: view_train_images :synopsis: Example nuts-ml pipeline reading and viewing image data """ from nutsflow import Take, Consume, MapCol from nutsml import ViewImageAnnotation, PrintColType if __name__ == "__main__": from cnn_train import load_samples, load_names train_samples, _ = load_samples() names = load_names() id2name = MapCol(1, lambda i: names[i]) show_image = ViewImageAnnotation(0, 1, pause=1, figsize=(2, 2), interpolation='spline36') (train_samples >> Take(10) >> id2name >> PrintColType() >> show_image >> Consume())
""" from __future__ import print_function from nutsflow import Collect, Consume, Get, Zip, Map, Format, ArgMax from nutsml import (TransformImage, BuildBatch, ReadImage, ReadLabelDirs, ViewImageAnnotation) BATCH_SIZE = 128 if __name__ == "__main__": from mlp_train import create_network TransformImage.register('flatten', lambda img: img.flatten()) transform = (TransformImage(0).by('rerange', 0, 255, 0, 1, 'float32').by('flatten')) show_image = ViewImageAnnotation(0, (1, 2), pause=1, figsize=(4, 4)) pred_batch = BuildBatch(BATCH_SIZE).by(0, 'vector', 'float32') print('loading network...') network = create_network() network.load_weights() print('predicting...') samples = ReadLabelDirs('images', '*.png') >> ReadImage(0) >> Collect() truelabels = samples >> Get(1) >> Format('true: {}') predictions = (samples >> transform >> pred_batch >> network.predict() >> Map(ArgMax()) >> Format('pred: {}')) samples >> Get(0) >> Zip(predictions, truelabels) >> show_image >> Consume()
""" .. module:: write_images :synopsis: Example nuts-ml pipeline for writing of images """ from nutsflow import (Take, Print, Consume, Enumerate, Zip, Format, MapCol, Get) from nutsml import WriteImage, ConvertLabel if __name__ == "__main__": from cnn_train import load_samples, load_names train_samples, _ = load_samples() convert_label = ConvertLabel(1, load_names()) fnames = (Enumerate() >> Zip(train_samples >> Get(1)) >> convert_label >> Format('{0}_{1}') >> Print()) imagepath = 'images/img*.png' train_samples >> Take(10) >> WriteImage(0, imagepath, fnames) >> Consume()
from __future__ import print_function from nutsflow import Consume, Zip, Unzip, Map, ArgMax, nut_filter from nutsml import TransformImage, BuildBatch, ViewImageAnnotation BATCH_SIZE = 128 if __name__ == "__main__": from mlp_train import create_network, load_samples TransformImage.register('flatten', lambda img: img.flatten()) transform = (TransformImage(0).by('rerange', 0, 255, 0, 1, 'float32').by('flatten')) show_image = ViewImageAnnotation(0, (1, 2), pause=3, figsize=(3, 3)) pred_batch = BuildBatch(BATCH_SIZE).by(0, 'vector', 'float32') IsMisclassified = nut_filter(lambda (i, t, p): p != t) print('loading samples ...') train_samples, test_samples = load_samples() print('loading network...') network = create_network() network.load_weights() print('predicting...') samples = train_samples + test_samples images, trues = samples >> Unzip() preds = (samples >> transform >> pred_batch >> network.predict() >> Map( ArgMax())) images >> Zip(trues, preds) >> IsMisclassified() >> show_image >> Consume()
""" .. module:: view_train_images :synopsis: Example nuts-ml pipeline reading and viewing image data """ from nutsflow import Take, Consume, MapCol from nutsml import ViewImageAnnotation, PrintColType, ConvertLabel if __name__ == "__main__": from cnn_train import load_samples, load_names train_samples, _ = load_samples() convert_label = ConvertLabel(1, load_names()) show_image = ViewImageAnnotation(0, 1, pause=1, figsize=(2, 2), interpolation='spline36') (train_samples >> Take(10) >> convert_label >> PrintColType() >> show_image >> Consume())
""" .. module:: view_data :synopsis: Example nuts-ml pipeline viewing CIFAR-10 image data """ from nutsflow import Take, Consume from nutsml import ViewImage if __name__ == "__main__": from cnn_train import load_samples train_samples, test_samples = load_samples() samples = train_samples + test_samples show_image = ViewImage(0, pause=1, figsize=(2, 2), interpolation='spline36') samples >> Take(10) >> show_image >> Consume()
""" .. module:: view_train_images :synopsis: Example for showing images with transformation """ from nutsflow import Take, Consume, GetCols from nutsml import ViewImage, TransformImage if __name__ == "__main__": from mlp_train import load_samples samples, _ = load_samples() transform = (TransformImage(0).by('elastic', 5, 100)) (samples >> GetCols(0, 0, 1) >> Take(1000) >> transform >> ViewImage( (0, 1), pause=1) >> Consume())
""" .. module:: view_train_images :synopsis: Example for showing images with annotation """ from nutsflow import Take, Consume from nutsml import ViewImageAnnotation if __name__ == "__main__": from mlp_train import load_samples samples, _ = load_samples() (samples >> Take(10) >> ViewImageAnnotation(0, 1, pause=1) >> Consume())
img = img.copy() msk = np.zeros(img.shape[:2]) img[sr:er, sc:ec, ...] = color msk[sr:er, sc:ec] = C_MASK return img, msk def gen_samples(config, ir, ic): for _ in range(config['samples']): img = create_background(ir, ic) sr, sc = rnd.uniform(0.7, 0.8), rnd.uniform(0.7, 0.8) rp, cp = int(ir * sr), int(ic * sc) img, ball_msk = draw_ball(img, rp, cp, 10, C_RED) sr, sc = rnd.uniform(0.1, 0.4), rnd.uniform(0.1, 0.4) rp, cp = int(ir * sr), int(ic * sc) img, box_msk = draw_box(img, rp, cp, 10, C_GREEN) yield (img, box_msk, ball_msk) if __name__ == '__main__': from nutsflow import Consume, Map from nutsml import ViewImage, PrintColType ir, ic = 64, 64 config = {'samples': 20, 'objects': {'box': [1, 1], 'ball': [1, 1]}} gen_samples(config, ir, ic) >> ViewImage((0, 1, 2), pause=10) >> Consume()