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_Stratify(): samples = [('pos', 1)] * 1000 + [('neg', 0)] * 100 dist = samples >> CountValues(1) stratify = Stratify(1, dist, rand=StableRandom(0)) stratified1 = samples >> stratify >> Collect() stratified2 = samples >> stratify >> Collect() assert stratified1 != stratified2 dist1 = stratified1 >> Get(1) >> CountValues() print(dist1) assert dist1[0] == 100 assert 90 < dist1[1] < 110 dist2 = stratified2 >> Get(1) >> CountValues() print(dist2) assert dist1[0] == 100 assert 90 < dist1[1] < 110
def EvalNut(batches, network, metrics, compute, predcol=None, targetcol=-1): """ batches >> EvalNut(network, metrics) Create nut to evaluate network performance for given metrics. Returned when network.evaluate() is called. :param iterable over batches batches: Batches to evaluate :param nutmsml.Network network: :param list of functions metrics: List of functions that compute some metric, e.g. accuracy, F1, kappa-score. Each metric function must take vectors with true and predicted classes/probabilities and must compute the metric over the entire input (not per sample/mini-batch). :param function compute: Function of the form f(metric, targets, preds) that computes the given metric (e.g. mean accuracy) for the given targets and predictions. :param int|None predcol: Index of column in prediction to extract for evaluation. If None a single prediction output is expected. :param int targetcol: Index of batch column that contain targets. :return: Result(s) of evaluation, e.g. accuracy, precision, ... :rtype: float or tuple of floats if there is more than one metric """ targets = [] def accumulate(batch): p_batch, target = batch[:targetcol], batch[targetcol] targets.extend(target) return p_batch preds = (batches >> Map(accumulate) >> network.predict(flatten=False) >> Get(predcol) >> Flatten() >> Collect()) targets, preds = np.vstack(targets), np.vstack(preds) targets = targets.astype(np.float) results = tuple(compute(m, targets, preds) for m in metrics) return results if len(results) > 1 else results[0]
""" 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()
""" 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()
""" .. 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 if __name__ == "__main__": from cnn_train import load_samples, load_names train_samples, _ = load_samples() names = load_names() id2name = MapCol(1, lambda i: names[int(i)]) fnames = (Enumerate() >> Zip(train_samples >> Get(1)) >> id2name >> Format('{0}_{1}') >> Print()) imagepath = 'images/img*.png' train_samples >> Take(10) >> WriteImage(0, imagepath, fnames) >> Consume()
""" .. module:: write_images :synopsis: Example for writing of image data """ from six.moves import zip from nutsflow import Take, Consume, Enumerate, Zip, Format, Get, Print from nutsml import WriteImage def load_samples(): from keras.datasets import mnist (X_train, y_train), (X_test, y_test) = mnist.load_data() return list(zip(X_train, y_train)), list(zip(X_test, y_test)) if __name__ == '__main__': train_samples, _ = load_samples() imagepath = 'images/*.png' names = Enumerate() >> Zip(train_samples >> Get(1)) >> Format('{1}/img{0}') names = names >> Print() train_samples >> Take(30) >> WriteImage(0, imagepath, names) >> Consume()