Search in sources :

Example 1 with TrustRegionStrategy

use of com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy in project MindsEye by SimiaCryptus.

the class LinearSumConstraintTest method train.

@Override
public void train(@Nonnull final NotebookOutput log, @Nonnull final Layer network, @Nonnull final Tensor[][] trainingData, final TrainingMonitor monitor) {
    log.code(() -> {
        @Nonnull final SimpleLossNetwork supervisedNetwork = new SimpleLossNetwork(network, new EntropyLossLayer());
        @Nonnull final Trainable trainable = new SampledArrayTrainable(trainingData, supervisedNetwork, 10000);
        @Nonnull final TrustRegionStrategy trustRegionStrategy = new TrustRegionStrategy() {

            @Override
            public TrustRegion getRegionPolicy(final Layer layer) {
                return new LinearSumConstraint();
            }
        };
        return new IterativeTrainer(trainable).setIterationsPerSample(100).setMonitor(monitor).setOrientation(trustRegionStrategy).setTimeout(3, TimeUnit.MINUTES).setMaxIterations(500).runAndFree();
    });
}
Also used : IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) SimpleLossNetwork(com.simiacryptus.mindseye.network.SimpleLossNetwork) Trainable(com.simiacryptus.mindseye.eval.Trainable) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) Layer(com.simiacryptus.mindseye.lang.Layer) TrustRegionStrategy(com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy)

Example 2 with TrustRegionStrategy

use of com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy in project MindsEye by SimiaCryptus.

the class SingleOrthantTrustRegionTest method train.

@Override
public void train(@Nonnull final NotebookOutput log, @Nonnull final Layer network, @Nonnull final Tensor[][] trainingData, final TrainingMonitor monitor) {
    log.code(() -> {
        @Nonnull final SimpleLossNetwork supervisedNetwork = new SimpleLossNetwork(network, new EntropyLossLayer());
        @Nonnull final Trainable trainable = new SampledArrayTrainable(trainingData, supervisedNetwork, 10000);
        @Nonnull final TrustRegionStrategy trustRegionStrategy = new TrustRegionStrategy() {

            @Override
            public TrustRegion getRegionPolicy(final Layer layer) {
                return new SingleOrthant();
            }
        };
        return new IterativeTrainer(trainable).setIterationsPerSample(100).setMonitor(monitor).setOrientation(trustRegionStrategy).setTimeout(3, TimeUnit.MINUTES).setMaxIterations(500).runAndFree();
    });
}
Also used : IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) SimpleLossNetwork(com.simiacryptus.mindseye.network.SimpleLossNetwork) Trainable(com.simiacryptus.mindseye.eval.Trainable) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) Layer(com.simiacryptus.mindseye.lang.Layer) TrustRegionStrategy(com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy)

Example 3 with TrustRegionStrategy

use of com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy in project MindsEye by SimiaCryptus.

the class DeepDream method train.

/**
 * Train buffered image.
 *
 * @param server          the server
 * @param log             the log
 * @param canvasImage     the canvas image
 * @param network         the network
 * @param precision       the precision
 * @param trainingMinutes the training minutes
 * @return the buffered image
 */
@Nonnull
public BufferedImage train(final StreamNanoHTTPD server, @Nonnull final NotebookOutput log, final BufferedImage canvasImage, final PipelineNetwork network, final Precision precision, final int trainingMinutes) {
    System.gc();
    Tensor canvas = Tensor.fromRGB(canvasImage);
    TestUtil.monitorImage(canvas, false, false);
    network.setFrozen(true);
    ArtistryUtil.setPrecision(network, precision);
    @Nonnull Trainable trainable = new ArrayTrainable(network, 1).setVerbose(true).setMask(true).setData(Arrays.asList(new Tensor[][] { { canvas } }));
    TestUtil.instrumentPerformance(network);
    if (null != server)
        ArtistryUtil.addLayersHandler(network, server);
    log.code(() -> {
        @Nonnull ArrayList<StepRecord> history = new ArrayList<>();
        new IterativeTrainer(trainable).setMonitor(TestUtil.getMonitor(history)).setIterationsPerSample(100).setOrientation(new TrustRegionStrategy() {

            @Override
            public TrustRegion getRegionPolicy(final Layer layer) {
                return new RangeConstraint();
            }
        }).setLineSearchFactory(name -> new BisectionSearch().setSpanTol(1e-1).setCurrentRate(1e3)).setTimeout(trainingMinutes, TimeUnit.MINUTES).setTerminateThreshold(Double.NEGATIVE_INFINITY).runAndFree();
        return TestUtil.plot(history);
    });
    return canvas.toImage();
}
Also used : TrustRegion(com.simiacryptus.mindseye.opt.region.TrustRegion) PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) Arrays(java.util.Arrays) TrustRegion(com.simiacryptus.mindseye.opt.region.TrustRegion) MeanSqLossLayer(com.simiacryptus.mindseye.layers.cudnn.MeanSqLossLayer) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) TrustRegionStrategy(com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy) HashMap(java.util.HashMap) NullNotebookOutput(com.simiacryptus.util.io.NullNotebookOutput) MultiLayerImageNetwork(com.simiacryptus.mindseye.models.MultiLayerImageNetwork) ArrayList(java.util.ArrayList) Trainable(com.simiacryptus.mindseye.eval.Trainable) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) Tuple2(com.simiacryptus.util.lang.Tuple2) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) SquareActivationLayer(com.simiacryptus.mindseye.layers.cudnn.SquareActivationLayer) Logger(org.slf4j.Logger) BufferedImage(java.awt.image.BufferedImage) AvgReducerLayer(com.simiacryptus.mindseye.layers.cudnn.AvgReducerLayer) ValueLayer(com.simiacryptus.mindseye.layers.cudnn.ValueLayer) TestUtil(com.simiacryptus.mindseye.test.TestUtil) UUID(java.util.UUID) DAGNode(com.simiacryptus.mindseye.network.DAGNode) StreamNanoHTTPD(com.simiacryptus.util.StreamNanoHTTPD) TimeUnit(java.util.concurrent.TimeUnit) BisectionSearch(com.simiacryptus.mindseye.opt.line.BisectionSearch) List(java.util.List) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) BinarySumLayer(com.simiacryptus.mindseye.layers.cudnn.BinarySumLayer) MultiLayerVGG16(com.simiacryptus.mindseye.models.MultiLayerVGG16) RangeConstraint(com.simiacryptus.mindseye.opt.region.RangeConstraint) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) LayerEnum(com.simiacryptus.mindseye.models.LayerEnum) MultiLayerVGG19(com.simiacryptus.mindseye.models.MultiLayerVGG19) Tensor(com.simiacryptus.mindseye.lang.Tensor) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) MeanSqLossLayer(com.simiacryptus.mindseye.layers.cudnn.MeanSqLossLayer) Layer(com.simiacryptus.mindseye.lang.Layer) SquareActivationLayer(com.simiacryptus.mindseye.layers.cudnn.SquareActivationLayer) AvgReducerLayer(com.simiacryptus.mindseye.layers.cudnn.AvgReducerLayer) ValueLayer(com.simiacryptus.mindseye.layers.cudnn.ValueLayer) BinarySumLayer(com.simiacryptus.mindseye.layers.cudnn.BinarySumLayer) StepRecord(com.simiacryptus.mindseye.test.StepRecord) RangeConstraint(com.simiacryptus.mindseye.opt.region.RangeConstraint) BisectionSearch(com.simiacryptus.mindseye.opt.line.BisectionSearch) Trainable(com.simiacryptus.mindseye.eval.Trainable) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TrustRegionStrategy(com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy) Nonnull(javax.annotation.Nonnull)

Example 4 with TrustRegionStrategy

use of com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy in project MindsEye by SimiaCryptus.

the class StyleTransfer method styleTransfer.

/**
 * Style transfer buffered image.
 *
 * @param server          the server
 * @param log             the log
 * @param canvasImage     the canvas image
 * @param styleParameters the style parameters
 * @param trainingMinutes the training minutes
 * @param measureStyle    the measure style
 * @return the buffered image
 */
public BufferedImage styleTransfer(final StreamNanoHTTPD server, @Nonnull final NotebookOutput log, final BufferedImage canvasImage, final StyleSetup<T> styleParameters, final int trainingMinutes, final NeuralSetup measureStyle) {
    BufferedImage result = ArtistryUtil.logExceptionWithDefault(log, () -> {
        log.p("Input Content:");
        log.p(log.image(styleParameters.contentImage, "Content Image"));
        log.p("Style Content:");
        styleParameters.styleImages.forEach((file, styleImage) -> {
            log.p(log.image(styleImage, file));
        });
        log.p("Input Canvas:");
        log.p(log.image(canvasImage, "Input Canvas"));
        System.gc();
        Tensor canvas = Tensor.fromRGB(canvasImage);
        TestUtil.monitorImage(canvas, false, false);
        log.p("Input Parameters:");
        log.code(() -> {
            return ArtistryUtil.toJson(styleParameters);
        });
        Trainable trainable = log.code(() -> {
            PipelineNetwork network = fitnessNetwork(measureStyle);
            network.setFrozen(true);
            ArtistryUtil.setPrecision(network, styleParameters.precision);
            TestUtil.instrumentPerformance(network);
            if (null != server)
                ArtistryUtil.addLayersHandler(network, server);
            return new ArrayTrainable(network, 1).setVerbose(true).setMask(true).setData(Arrays.asList(new Tensor[][] { { canvas } }));
        });
        log.code(() -> {
            @Nonnull ArrayList<StepRecord> history = new ArrayList<>();
            new IterativeTrainer(trainable).setMonitor(TestUtil.getMonitor(history)).setOrientation(new TrustRegionStrategy() {

                @Override
                public TrustRegion getRegionPolicy(final Layer layer) {
                    return new RangeConstraint().setMin(1e-2).setMax(256);
                }
            }).setIterationsPerSample(100).setLineSearchFactory(name -> new BisectionSearch().setSpanTol(1e-1).setCurrentRate(1e6)).setTimeout(trainingMinutes, TimeUnit.MINUTES).setTerminateThreshold(Double.NEGATIVE_INFINITY).runAndFree();
            return TestUtil.plot(history);
        });
        return canvas.toImage();
    }, canvasImage);
    log.p("Output Canvas:");
    log.p(log.image(result, "Output Canvas"));
    return result;
}
Also used : PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) TrustRegion(com.simiacryptus.mindseye.opt.region.TrustRegion) MeanSqLossLayer(com.simiacryptus.mindseye.layers.cudnn.MeanSqLossLayer) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) TrustRegionStrategy(com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy) HashMap(java.util.HashMap) NullNotebookOutput(com.simiacryptus.util.io.NullNotebookOutput) MultiLayerImageNetwork(com.simiacryptus.mindseye.models.MultiLayerImageNetwork) ArrayList(java.util.ArrayList) JsonUtil(com.simiacryptus.util.io.JsonUtil) Trainable(com.simiacryptus.mindseye.eval.Trainable) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) Tuple2(com.simiacryptus.util.lang.Tuple2) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) GateBiasLayer(com.simiacryptus.mindseye.layers.cudnn.GateBiasLayer) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) Logger(org.slf4j.Logger) BufferedImage(java.awt.image.BufferedImage) ValueLayer(com.simiacryptus.mindseye.layers.cudnn.ValueLayer) TestUtil(com.simiacryptus.mindseye.test.TestUtil) UUID(java.util.UUID) DAGNode(com.simiacryptus.mindseye.network.DAGNode) Collectors(java.util.stream.Collectors) BandAvgReducerLayer(com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer) StreamNanoHTTPD(com.simiacryptus.util.StreamNanoHTTPD) TimeUnit(java.util.concurrent.TimeUnit) BisectionSearch(com.simiacryptus.mindseye.opt.line.BisectionSearch) List(java.util.List) GramianLayer(com.simiacryptus.mindseye.layers.cudnn.GramianLayer) Stream(java.util.stream.Stream) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) BinarySumLayer(com.simiacryptus.mindseye.layers.cudnn.BinarySumLayer) InnerNode(com.simiacryptus.mindseye.network.InnerNode) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) MultiLayerVGG16(com.simiacryptus.mindseye.models.MultiLayerVGG16) RangeConstraint(com.simiacryptus.mindseye.opt.region.RangeConstraint) LayerEnum(com.simiacryptus.mindseye.models.LayerEnum) MultiLayerVGG19(com.simiacryptus.mindseye.models.MultiLayerVGG19) Tensor(com.simiacryptus.mindseye.lang.Tensor) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) MeanSqLossLayer(com.simiacryptus.mindseye.layers.cudnn.MeanSqLossLayer) Layer(com.simiacryptus.mindseye.lang.Layer) GateBiasLayer(com.simiacryptus.mindseye.layers.cudnn.GateBiasLayer) ValueLayer(com.simiacryptus.mindseye.layers.cudnn.ValueLayer) BandAvgReducerLayer(com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer) GramianLayer(com.simiacryptus.mindseye.layers.cudnn.GramianLayer) BinarySumLayer(com.simiacryptus.mindseye.layers.cudnn.BinarySumLayer) BufferedImage(java.awt.image.BufferedImage) StepRecord(com.simiacryptus.mindseye.test.StepRecord) RangeConstraint(com.simiacryptus.mindseye.opt.region.RangeConstraint) BisectionSearch(com.simiacryptus.mindseye.opt.line.BisectionSearch) Trainable(com.simiacryptus.mindseye.eval.Trainable) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TrustRegionStrategy(com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy)

Example 5 with TrustRegionStrategy

use of com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy in project MindsEye by SimiaCryptus.

the class TrustSphereTest method train.

@Override
public void train(@Nonnull final NotebookOutput log, @Nonnull final Layer network, @Nonnull final Tensor[][] trainingData, final TrainingMonitor monitor) {
    log.code(() -> {
        @Nonnull final SimpleLossNetwork supervisedNetwork = new SimpleLossNetwork(network, new EntropyLossLayer());
        @Nonnull final Trainable trainable = new SampledArrayTrainable(trainingData, supervisedNetwork, 10000);
        @Nonnull final TrustRegionStrategy trustRegionStrategy = new TrustRegionStrategy() {

            @Override
            public TrustRegion getRegionPolicy(final Layer layer) {
                return new AdaptiveTrustSphere();
            }
        };
        return new IterativeTrainer(trainable).setIterationsPerSample(100).setMonitor(monitor).setOrientation(trustRegionStrategy).setTimeout(3, TimeUnit.MINUTES).setMaxIterations(500).runAndFree();
    });
}
Also used : IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) SimpleLossNetwork(com.simiacryptus.mindseye.network.SimpleLossNetwork) Trainable(com.simiacryptus.mindseye.eval.Trainable) SampledArrayTrainable(com.simiacryptus.mindseye.eval.SampledArrayTrainable) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) Layer(com.simiacryptus.mindseye.lang.Layer) TrustRegionStrategy(com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy)

Aggregations

Trainable (com.simiacryptus.mindseye.eval.Trainable)5 Layer (com.simiacryptus.mindseye.lang.Layer)5 IterativeTrainer (com.simiacryptus.mindseye.opt.IterativeTrainer)5 TrustRegionStrategy (com.simiacryptus.mindseye.opt.orient.TrustRegionStrategy)5 Nonnull (javax.annotation.Nonnull)5 SampledArrayTrainable (com.simiacryptus.mindseye.eval.SampledArrayTrainable)3 EntropyLossLayer (com.simiacryptus.mindseye.layers.java.EntropyLossLayer)3 SimpleLossNetwork (com.simiacryptus.mindseye.network.SimpleLossNetwork)3 ArrayTrainable (com.simiacryptus.mindseye.eval.ArrayTrainable)2 Tensor (com.simiacryptus.mindseye.lang.Tensor)2 Precision (com.simiacryptus.mindseye.lang.cudnn.Precision)2 BinarySumLayer (com.simiacryptus.mindseye.layers.cudnn.BinarySumLayer)2 MeanSqLossLayer (com.simiacryptus.mindseye.layers.cudnn.MeanSqLossLayer)2 ValueLayer (com.simiacryptus.mindseye.layers.cudnn.ValueLayer)2 LayerEnum (com.simiacryptus.mindseye.models.LayerEnum)2 MultiLayerImageNetwork (com.simiacryptus.mindseye.models.MultiLayerImageNetwork)2 MultiLayerVGG16 (com.simiacryptus.mindseye.models.MultiLayerVGG16)2 MultiLayerVGG19 (com.simiacryptus.mindseye.models.MultiLayerVGG19)2 DAGNode (com.simiacryptus.mindseye.network.DAGNode)2 PipelineNetwork (com.simiacryptus.mindseye.network.PipelineNetwork)2