Search in sources :

Example 6 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class ClassifyProblem method run.

@Nonnull
@Override
public ClassifyProblem run(@Nonnull final NotebookOutput log) {
    @Nonnull final TrainingMonitor monitor = TestUtil.getMonitor(history);
    final Tensor[][] trainingData = getTrainingData(log);
    @Nonnull final DAGNetwork network = fwdFactory.imageToVector(log, categories);
    log.h3("Network Diagram");
    log.code(() -> {
        return Graphviz.fromGraph(TestUtil.toGraph(network)).height(400).width(600).render(Format.PNG).toImage();
    });
    log.h3("Training");
    @Nonnull final SimpleLossNetwork supervisedNetwork = new SimpleLossNetwork(network, new EntropyLossLayer());
    TestUtil.instrumentPerformance(supervisedNetwork);
    int initialSampleSize = Math.max(trainingData.length / 5, Math.min(10, trainingData.length / 2));
    @Nonnull final ValidatingTrainer trainer = optimizer.train(log, new SampledArrayTrainable(trainingData, supervisedNetwork, initialSampleSize, getBatchSize()), new ArrayTrainable(trainingData, supervisedNetwork, getBatchSize()), monitor);
    log.code(() -> {
        trainer.setTimeout(timeoutMinutes, TimeUnit.MINUTES).setMaxIterations(10000).run();
    });
    if (!history.isEmpty()) {
        log.code(() -> {
            return TestUtil.plot(history);
        });
        log.code(() -> {
            return TestUtil.plotTime(history);
        });
    }
    try {
        @Nonnull String filename = log.getName() + "_" + ClassifyProblem.modelNo++ + "_plot.png";
        ImageIO.write(Util.toImage(TestUtil.plot(history)), "png", log.file(filename));
        @Nonnull File file = new File(log.getResourceDir(), filename);
        log.appendFrontMatterProperty("result_plot", file.toString(), ";");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    TestUtil.extractPerformance(log, supervisedNetwork);
    @Nonnull final String modelName = "classification_model_" + ClassifyProblem.modelNo++ + ".json";
    log.appendFrontMatterProperty("result_model", modelName, ";");
    log.p("Saved model as " + log.file(network.getJson().toString(), modelName, modelName));
    log.h3("Validation");
    log.p("If we apply our model against the entire validation dataset, we get this accuracy:");
    log.code(() -> {
        return data.validationData().mapToDouble(labeledObject -> predict(network, labeledObject)[0] == parse(labeledObject.label) ? 1 : 0).average().getAsDouble() * 100;
    });
    log.p("Let's examine some incorrectly predicted results in more detail:");
    log.code(() -> {
        try {
            @Nonnull final TableOutput table = new TableOutput();
            Lists.partition(data.validationData().collect(Collectors.toList()), 100).stream().flatMap(batch -> {
                @Nonnull TensorList batchIn = TensorArray.create(batch.stream().map(x -> x.data).toArray(i -> new Tensor[i]));
                TensorList batchOut = network.eval(new ConstantResult(batchIn)).getData();
                return IntStream.range(0, batchOut.length()).mapToObj(i -> toRow(log, batch.get(i), batchOut.get(i).getData()));
            }).filter(x -> null != x).limit(10).forEach(table::putRow);
            return table;
        } catch (@Nonnull final IOException e) {
            throw new RuntimeException(e);
        }
    });
    return this;
}
Also used : IntStream(java.util.stream.IntStream) Graphviz(guru.nidi.graphviz.engine.Graphviz) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) Arrays(java.util.Arrays) TableOutput(com.simiacryptus.util.TableOutput) Tensor(com.simiacryptus.mindseye.lang.Tensor) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) Format(guru.nidi.graphviz.engine.Format) LabeledObject(com.simiacryptus.util.test.LabeledObject) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) ImageIO(javax.imageio.ImageIO) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) Layer(com.simiacryptus.mindseye.lang.Layer) ValidatingTrainer(com.simiacryptus.mindseye.opt.ValidatingTrainer) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Util(com.simiacryptus.util.Util) SimpleLossNetwork(com.simiacryptus.mindseye.network.SimpleLossNetwork) IOException(java.io.IOException) TestUtil(com.simiacryptus.mindseye.test.TestUtil) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TensorList(com.simiacryptus.mindseye.lang.TensorList) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) IOException(java.io.IOException) TensorList(com.simiacryptus.mindseye.lang.TensorList) SimpleLossNetwork(com.simiacryptus.mindseye.network.SimpleLossNetwork) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) TableOutput(com.simiacryptus.util.TableOutput) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) ValidatingTrainer(com.simiacryptus.mindseye.opt.ValidatingTrainer) File(java.io.File) Nonnull(javax.annotation.Nonnull)

Example 7 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class ImgCropLayerTest method getPerformanceTester.

@Nullable
@Override
public ComponentTest<ToleranceStatistics> getPerformanceTester() {
    @Nonnull ComponentTest<ToleranceStatistics> inner = new PerformanceTester().setSamples(100).setBatches(10);
    return new ComponentTestBase<ToleranceStatistics>() {

        @Override
        public ToleranceStatistics test(@Nonnull NotebookOutput log, Layer component, Tensor... inputPrototype) {
            @Nullable PrintStream apiLog = null;
            try {
                apiLog = new PrintStream(log.file("cuda_perf.log"));
                CudaSystem.addLog(apiLog);
                return inner.test(log, component, inputPrototype);
            } finally {
                log.p(log.file((String) null, "cuda_perf.log", "GPU Log"));
                if (null != apiLog) {
                    apiLog.close();
                    CudaSystem.apiLog.remove(apiLog);
                }
            }
        }

        @Override
        protected void _free() {
            inner.freeRef();
            super._free();
        }
    };
}
Also used : ComponentTestBase(com.simiacryptus.mindseye.test.unit.ComponentTestBase) PrintStream(java.io.PrintStream) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) PerformanceTester(com.simiacryptus.mindseye.test.unit.PerformanceTester) Layer(com.simiacryptus.mindseye.lang.Layer) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 8 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class ActivationLayerTestBase method run.

@Override
public void run(final NotebookOutput log) {
    super.run(log);
    log.h3("Function Plots");
    final Layer layer = getLayer(new int[][] { { 1 } }, new Random());
    final List<double[]> plotData = scan().mapToObj(x -> {
        @Nonnull Tensor tensor = new Tensor(x);
        @Nonnull final SimpleEval eval = SimpleEval.run(layer, tensor);
        tensor.freeRef();
        @Nonnull double[] doubles = { x, eval.getOutput().get(0), eval.getDerivative()[0].get(0) };
        eval.freeRef();
        return doubles;
    }).collect(Collectors.toList());
    log.code(() -> {
        return ActivationLayerTestBase.plot("Value Plot", plotData, x -> new double[] { x[0], x[1] });
    });
    log.code(() -> {
        return ActivationLayerTestBase.plot("Derivative Plot", plotData, x -> new double[] { x[0], x[2] });
    });
}
Also used : IntStream(java.util.stream.IntStream) LayerTestBase(com.simiacryptus.mindseye.layers.LayerTestBase) PlotCanvas(smile.plot.PlotCanvas) ScatterPlot(smile.plot.ScatterPlot) ComponentTest(com.simiacryptus.mindseye.test.unit.ComponentTest) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) TrainingTester(com.simiacryptus.mindseye.test.unit.TrainingTester) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) DoubleStream(java.util.stream.DoubleStream) List(java.util.List) Layer(com.simiacryptus.mindseye.lang.Layer) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) Layer(com.simiacryptus.mindseye.lang.Layer)

Example 9 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class EncodingUtil method decompositionSvg.

/**
 * To svg string.
 *
 * @param log              the log
 * @param baseline         the baseline
 * @param signedComponents the signed components
 * @return the string
 */
public static String decompositionSvg(@Nonnull final NotebookOutput log, @Nonnull final Tensor baseline, @Nonnull final List<Tensor> signedComponents) {
    final List<DoubleStatistics> componentStats = signedComponents.stream().map(t -> new DoubleStatistics().accept(t.getData())).collect(Collectors.toList());
    @Nonnull final CharSequence positiveFilter = IntStream.range(0, signedComponents.size()).mapToObj(i -> {
        String name;
        try {
            name = String.format("component_%s.png", EncodingUtil.imageNumber++);
            ImageIO.write(signedComponents.get(i).map(v -> v > 0 ? v * (0xFF / componentStats.get(i).getMax()) : 0).toImage(), "png", log.file(name));
        } catch (@Nonnull final IOException e) {
            throw new RuntimeException(e);
        }
        return String.format("  <feImage xlink:href=\"%s\" result=\"pos_image_%s\" />\n", name, i);
    }).reduce((a, b) -> a + "\n" + b).get();
    @Nonnull final CharSequence negativeFilter = IntStream.range(0, signedComponents.size()).mapToObj(i -> {
        String name;
        try {
            name = String.format("component_%s.png", EncodingUtil.imageNumber++);
            ImageIO.write(signedComponents.get(i).map(v -> v < 0 ? 0xFF - v * (0xFF / componentStats.get(i).getMin()) : 0).toImage(), "png", log.file(name));
        } catch (@Nonnull final IOException e) {
            throw new RuntimeException(e);
        }
        return String.format("  <feImage xlink:href=\"%s\" result=\"neg_image_%s\" />\n", name, i);
    }).reduce((a, b) -> a + "\n" + b).get();
    @Nonnull final CharSequence compositingFilters = IntStream.range(0, signedComponents.size()).mapToObj(i -> {
        final double fPos = componentStats.get(i).getMax() / 0xFF;
        final double fNeg = componentStats.get(i).getMin() / 0xFF;
        return "  <feComposite in=\"" + (i == 0 ? "FillPaint" : "lastResult") + "\" in2=\"neg_image_" + i + "\" result=\"lastResult\" operator=\"arithmetic\" k1=\"0.0\" k2=\"1.0\" k3=\"" + -fNeg + "\" k4=\"" + fNeg + "\"/>\n" + "  <feComposite in=\"lastResult\" in2=\"pos_image_" + i + "\" result=\"lastResult\" operator=\"arithmetic\" k1=\"0.0\" k2=\"1.0\" k3=\"" + fPos + "\" k4=\"" + 0.0 + "\"/>\n";
    }).reduce((a, b) -> a + "\n" + b).get();
    final int red = (int) baseline.get(0, 0, 0);
    final int green = (int) baseline.get(0, 0, 1);
    final int blue = (int) baseline.get(0, 0, 2);
    @Nonnull final CharSequence avgHexColor = Long.toHexString(red + (green << 8) + (blue << 16));
    return "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n" + ("<defs>\n" + "<filter id=\"image\" >\n" + (positiveFilter + "\n" + negativeFilter + "\n" + compositingFilters).replaceAll("\n", "\n\t") + "\n" + "</filter>\n" + "</defs>\n" + "<rect style=\"filter:url(#image);\" setByCoord=\"#" + avgHexColor + "\" width=\"256\" height=\"256\"/>").replaceAll("\n", "\n\t") + "\n</svg>";
}
Also used : PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) IntStream(java.util.stream.IntStream) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) Arrays(java.util.Arrays) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) GifSequenceWriter(com.simiacryptus.util.io.GifSequenceWriter) TableOutput(com.simiacryptus.util.TableOutput) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) Caltech101(com.simiacryptus.mindseye.test.data.Caltech101) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) ImgBandScaleLayer(com.simiacryptus.mindseye.layers.java.ImgBandScaleLayer) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) ImageIO(javax.imageio.ImageIO) Layer(com.simiacryptus.mindseye.lang.Layer) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) PCAUtil(com.simiacryptus.mindseye.test.PCAUtil) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintStream(java.io.PrintStream) MeanSqLossLayer(com.simiacryptus.mindseye.layers.java.MeanSqLossLayer) Logger(org.slf4j.Logger) SysOutInterceptor(com.simiacryptus.util.test.SysOutInterceptor) BufferedImage(java.awt.image.BufferedImage) ImgBandSelectLayer(com.simiacryptus.mindseye.layers.java.ImgBandSelectLayer) IOException(java.io.IOException) TestUtil(com.simiacryptus.mindseye.test.TestUtil) FastRandom(com.simiacryptus.util.FastRandom) Collectors(java.util.stream.Collectors) File(java.io.File) DoubleStream(java.util.stream.DoubleStream) ConvolutionLayer(com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer) List(java.util.List) Stream(java.util.stream.Stream) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) ToDoubleFunction(java.util.function.ToDoubleFunction) ImgReshapeLayer(com.simiacryptus.mindseye.layers.java.ImgReshapeLayer) ImgBandBiasLayer(com.simiacryptus.mindseye.layers.java.ImgBandBiasLayer) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) Step(com.simiacryptus.mindseye.opt.Step) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) IOException(java.io.IOException)

Example 10 with NotebookOutput

use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.

the class EncodingUtil method animatedGif.

/**
 * Animated gif string.
 *
 * @param log              the log
 * @param baseline         the baseline
 * @param signedComponents the signed components
 * @return the string
 */
public static CharSequence animatedGif(@Nonnull final NotebookOutput log, @Nonnull final Tensor baseline, @Nonnull final List<Tensor> signedComponents) {
    int loopTimeMs = 15000;
    int framerate = 12;
    int frames = loopTimeMs * framerate / 1000;
    try {
        double step = 2 * Math.PI / frames;
        @Nonnull String filename = EncodingUtil.gifNumber++ + ".gif";
        @Nonnull File file = new File(log.getResourceDir(), filename);
        GifSequenceWriter.write(file, loopTimeMs / frames, true, DoubleStream.iterate(0, x -> x + step).limit(frames).parallel().mapToObj(t -> {
            return IntStream.range(0, signedComponents.size()).mapToObj(i -> {
                return signedComponents.get(i).scale((1 + Math.sin((1 + i) * t)) / 2);
            }).reduce((a, b) -> {
                Tensor add = a.addAndFree(b);
                b.freeRef();
                return add;
            }).get().add(baseline).toImage();
        }).toArray(i -> new BufferedImage[i]));
        return String.format("<img src=\"etc/%s\" />", filename);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) IntStream(java.util.stream.IntStream) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) Arrays(java.util.Arrays) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) GifSequenceWriter(com.simiacryptus.util.io.GifSequenceWriter) TableOutput(com.simiacryptus.util.TableOutput) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) Caltech101(com.simiacryptus.mindseye.test.data.Caltech101) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) ImgBandScaleLayer(com.simiacryptus.mindseye.layers.java.ImgBandScaleLayer) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) ImageIO(javax.imageio.ImageIO) Layer(com.simiacryptus.mindseye.lang.Layer) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) PCAUtil(com.simiacryptus.mindseye.test.PCAUtil) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintStream(java.io.PrintStream) MeanSqLossLayer(com.simiacryptus.mindseye.layers.java.MeanSqLossLayer) Logger(org.slf4j.Logger) SysOutInterceptor(com.simiacryptus.util.test.SysOutInterceptor) BufferedImage(java.awt.image.BufferedImage) ImgBandSelectLayer(com.simiacryptus.mindseye.layers.java.ImgBandSelectLayer) IOException(java.io.IOException) TestUtil(com.simiacryptus.mindseye.test.TestUtil) FastRandom(com.simiacryptus.util.FastRandom) Collectors(java.util.stream.Collectors) File(java.io.File) DoubleStream(java.util.stream.DoubleStream) ConvolutionLayer(com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer) List(java.util.List) Stream(java.util.stream.Stream) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) ToDoubleFunction(java.util.function.ToDoubleFunction) ImgReshapeLayer(com.simiacryptus.mindseye.layers.java.ImgReshapeLayer) ImgBandBiasLayer(com.simiacryptus.mindseye.layers.java.ImgBandBiasLayer) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) Step(com.simiacryptus.mindseye.opt.Step) Comparator(java.util.Comparator) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Aggregations

NotebookOutput (com.simiacryptus.util.io.NotebookOutput)48 Nonnull (javax.annotation.Nonnull)48 Tensor (com.simiacryptus.mindseye.lang.Tensor)46 Nullable (javax.annotation.Nullable)40 Layer (com.simiacryptus.mindseye.lang.Layer)39 Arrays (java.util.Arrays)38 List (java.util.List)37 IntStream (java.util.stream.IntStream)31 TestUtil (com.simiacryptus.mindseye.test.TestUtil)25 Logger (org.slf4j.Logger)25 LoggerFactory (org.slf4j.LoggerFactory)25 Stream (java.util.stream.Stream)23 Collectors (java.util.stream.Collectors)22 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)21 DAGNetwork (com.simiacryptus.mindseye.network.DAGNetwork)20 PipelineNetwork (com.simiacryptus.mindseye.network.PipelineNetwork)19 TrainingMonitor (com.simiacryptus.mindseye.opt.TrainingMonitor)19 TimeUnit (java.util.concurrent.TimeUnit)19 StepRecord (com.simiacryptus.mindseye.test.StepRecord)18