Search in sources :

Example 41 with NotebookOutput

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

the class SingleDerivativeTester method test.

/**
 * Test tolerance statistics.
 *
 * @param output
 * @param component      the component
 * @param inputPrototype the input prototype
 * @return the tolerance statistics
 */
@Override
public ToleranceStatistics test(@Nonnull final NotebookOutput output, @Nonnull final Layer component, @Nonnull final Tensor... inputPrototype) {
    output.h1("Differential Validation");
    ToleranceStatistics _statistics = new ToleranceStatistics();
    final Tensor outputPrototype = SimpleEval.run(component, inputPrototype).getOutputAndFree();
    try {
        if (verbose) {
            output.code(() -> {
                log.info(String.format("Inputs: %s", Arrays.stream(inputPrototype).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).orElse("")));
                log.info(String.format("Inputs Statistics: %s", Arrays.stream(inputPrototype).map(x -> new ScalarStatistics().add(x.getData()).toString()).reduce((a, b) -> a + ",\n" + b).orElse("")));
                log.info(String.format("Output: %s", null == outputPrototype ? null : outputPrototype.prettyPrint()));
                log.info(String.format("Outputs Statistics: %s", new ScalarStatistics().add(outputPrototype.getData())));
            });
        }
        if (isTestFeedback()) {
            output.h2("Feedback Validation");
            output.p("We validate the agreement between the implemented derivative _of the inputs_ apply finite difference estimations:");
            final ToleranceStatistics statistics = _statistics;
            _statistics = output.code(() -> {
                return testFeedback(statistics, component, inputPrototype, outputPrototype);
            });
        }
        if (isTestLearning()) {
            output.h2("Learning Validation");
            output.p("We validate the agreement between the implemented derivative _of the internal weights_ apply finite difference estimations:");
            final ToleranceStatistics statistics = _statistics;
            _statistics = output.code(() -> {
                return testLearning(statistics, component, inputPrototype, outputPrototype);
            });
        }
    } finally {
        outputPrototype.freeRef();
    }
    output.h2("Total Accuracy");
    output.p("The overall agreement accuracy between the implemented derivative and the finite difference estimations:");
    final ToleranceStatistics statistics = _statistics;
    output.code(() -> {
        // log.info(String.format("Component: %s\nInputs: %s\noutput=%s", component, Arrays.toString(inputPrototype), outputPrototype));
        log.info(String.format("Finite-Difference Derivative Accuracy:"));
        log.info(String.format("absoluteTol: %s", statistics.absoluteTol));
        log.info(String.format("relativeTol: %s", statistics.relativeTol));
    });
    output.h2("Frozen and Alive Status");
    output.code(() -> {
        testFrozen(component, inputPrototype);
        testUnFrozen(component, inputPrototype);
    });
    return _statistics;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DoubleBuffer(com.simiacryptus.mindseye.lang.DoubleBuffer) Result(com.simiacryptus.mindseye.lang.Result) Collectors(java.util.stream.Collectors) Delta(com.simiacryptus.mindseye.lang.Delta) List(java.util.List) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) Optional(java.util.Optional) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) 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) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics)

Example 42 with NotebookOutput

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

the class ImageClassificationBase method run.

/**
 * Test.
 *
 * @param log the log
 */
public void run(@Nonnull NotebookOutput log) {
    log.h1("Model");
    log.p("In this demonstration, we will show how to load an image recognition network and use it to identify object in images.");
    log.p("We start by loading the VGG16 pretrained model using the HD5 importer. This downloads, if needed, the weights from a file in S3 and re-constructs the network architecture by custom code.");
    log.p("Next, we need an example image to analyze:");
    log.p("We pass this image to the categorization network, and get the following top-10 results. Note that multiple objects may be detected, and the total percentage may be greater than 100%.");
    log.p("Once we have categories identified, we can attempt to localize each object category within the image. We do this via a pipeline starting with the backpropagated input signal delta and applying several filters e.g. blurring and normalization to produce an alpha channel. When applied to the input image, we highlight the image areas related to the object type in question. Note that this produces a fuzzy blob, which does indicate object location but is a poor indicator of object boundaries. Below we perform this task for the top 5 object categories:");
    ImageClassifier vgg16 = loadModel(log);
    log.h1("Data");
    Tensor[] images = loadData(log);
    log.h1("Prediction");
    List<LinkedHashMap<CharSequence, Double>> predictions = log.code(() -> {
        return vgg16.predict(5, images);
    });
    log.h1("Results");
    log.code(() -> {
        @Nonnull TableOutput tableOutput = new TableOutput();
        for (int i = 0; i < images.length; i++) {
            @Nonnull HashMap<CharSequence, Object> row = new HashMap<>();
            row.put("Image", log.image(images[i].toImage(), ""));
            row.put("Prediction", predictions.get(i).entrySet().stream().map(e -> String.format("%s -> %.2f", e.getKey(), 100 * e.getValue())).reduce((a, b) -> a + "<br/>" + b).get());
            tableOutput.putRow(row);
        }
        return tableOutput;
    }, 256 * 1024);
    log.setFrontMatterProperty("status", "OK");
}
Also used : BufferedImage(java.awt.image.BufferedImage) TableOutput(com.simiacryptus.util.TableOutput) Tensor(com.simiacryptus.mindseye.lang.Tensor) HashMap(java.util.HashMap) TestUtil(com.simiacryptus.mindseye.test.TestUtil) Caltech101(com.simiacryptus.mindseye.test.data.Caltech101) LinkedHashMap(java.util.LinkedHashMap) ImageClassifier(com.simiacryptus.mindseye.applications.ImageClassifier) List(java.util.List) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) TableOutput(com.simiacryptus.util.TableOutput) ImageClassifier(com.simiacryptus.mindseye.applications.ImageClassifier)

Example 43 with NotebookOutput

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

the class EncodingUtil method validationReport.

/**
 * Validation report.
 *
 * @param log          the log
 * @param data         the data
 * @param dataPipeline the data pipeline
 * @param maxRows      the max rows
 */
public static void validationReport(@Nonnull final NotebookOutput log, @Nonnull final Tensor[][] data, @Nonnull final List<Layer> dataPipeline, final int maxRows) {
    log.out("Current dataset and evaluation results: ");
    log.code(() -> {
        @Nonnull final TableOutput table = new TableOutput();
        Arrays.stream(data).limit(maxRows).map(tensorArray -> {
            @Nonnull final LinkedHashMap<CharSequence, Object> row = new LinkedHashMap<>();
            for (int col = 1; col < tensorArray.length; col++) {
                EncodingUtil.renderLayer(log, dataPipeline, row, col, tensorArray[col]);
            }
            return row;
        }).filter(x -> null != x).limit(10).forEach(table::putRow);
        return table;
    });
}
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) TableOutput(com.simiacryptus.util.TableOutput) Nonnull(javax.annotation.Nonnull) LinkedHashMap(java.util.LinkedHashMap)

Example 44 with NotebookOutput

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

the class EncodingUtil method printModel.

/**
 * Print model.
 *
 * @param log     the log
 * @param network the network
 * @param modelNo the model no
 */
public static void printModel(@Nonnull final NotebookOutput log, @Nonnull final Layer network, final int modelNo) {
    log.out("Learned Model Statistics: ");
    log.code(() -> {
        @Nonnull final ScalarStatistics scalarStatistics = new ScalarStatistics();
        network.state().stream().flatMapToDouble(x -> Arrays.stream(x)).forEach(v -> scalarStatistics.add(v));
        return scalarStatistics.getMetrics();
    });
    @Nonnull final String modelName = "model" + modelNo + ".json";
    log.p("Saved model as " + log.file(network.getJson().toString(), modelName, modelName));
}
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) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics)

Example 45 with NotebookOutput

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

the class ActivationLayerTest method run.

@Override
public void run(final NotebookOutput log) {
    @Nonnull String logName = "cuda_" + log.getName() + "_all.log";
    log.p(log.file((String) null, logName, "GPU Log"));
    CudaSystem.addLog(new PrintStream(log.file(logName)));
    super.run(log);
    log.h3("Function Plots");
    @Nonnull final Layer layer = getLayer(new int[][] { { 1, 1, 1 } }, new Random());
    final List<double[]> plotData = IntStream.range(-1000, 1000).mapToDouble(x -> x / 300.0).mapToObj(x -> {
        @Nonnull Tensor input = new Tensor(new double[] { x }, 1, 1, 1);
        @Nonnull final SimpleEval eval = SimpleEval.run(layer, input);
        input.freeRef();
        @Nonnull double[] doubles = { x, eval.getOutput().get(0), eval.getDerivative()[0].get(0) };
        eval.freeRef();
        return doubles;
    }).collect(Collectors.toList());
    layer.freeRef();
    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 : PrintStream(java.io.PrintStream) IntStream(java.util.stream.IntStream) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) ActivationLayerTestBase(com.simiacryptus.mindseye.layers.java.ActivationLayerTestBase) Collectors(java.util.stream.Collectors) ReLuActivationLayer(com.simiacryptus.mindseye.layers.java.ReLuActivationLayer) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) List(java.util.List) CudaSystem(com.simiacryptus.mindseye.lang.cudnn.CudaSystem) Layer(com.simiacryptus.mindseye.lang.Layer) SingleDerivativeTester(com.simiacryptus.mindseye.test.unit.SingleDerivativeTester) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) SigmoidActivationLayer(com.simiacryptus.mindseye.layers.java.SigmoidActivationLayer) Nonnull(javax.annotation.Nonnull) PrintStream(java.io.PrintStream) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) Nonnull(javax.annotation.Nonnull) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) ReLuActivationLayer(com.simiacryptus.mindseye.layers.java.ReLuActivationLayer) Layer(com.simiacryptus.mindseye.lang.Layer) SigmoidActivationLayer(com.simiacryptus.mindseye.layers.java.SigmoidActivationLayer)

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