use of com.simiacryptus.mindseye.test.StepRecord in project MindsEye by SimiaCryptus.
the class EncodingUtil method getMonitor.
/**
* Gets monitor.
*
* @param history the history
* @return the monitor
*/
public static TrainingMonitor getMonitor(@Nonnull final List<StepRecord> history) {
return new TrainingMonitor() {
@Override
public void clear() {
super.clear();
}
@Override
public void log(final String msg) {
// Logged MnistProblemData
log.info(msg);
// Realtime MnistProblemData
EncodingUtil.rawOut.println(msg);
}
@Override
public void onStepComplete(@Nonnull final Step currentPoint) {
history.add(new StepRecord(currentPoint.point.getMean(), currentPoint.time, currentPoint.iteration));
}
};
}
use of com.simiacryptus.mindseye.test.StepRecord 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();
}
use of com.simiacryptus.mindseye.test.StepRecord in project MindsEye by SimiaCryptus.
the class ImageClassifier method deepDream.
/**
* Deep dream.
*
* @param log the log
* @param image the image
*/
public void deepDream(@Nonnull final NotebookOutput log, final Tensor image) {
log.code(() -> {
@Nonnull ArrayList<StepRecord> history = new ArrayList<>();
@Nonnull PipelineNetwork clamp = new PipelineNetwork(1);
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(255).setScale(-1).freeze());
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(255).setScale(-1).freeze());
@Nonnull PipelineNetwork supervised = new PipelineNetwork(1);
supervised.add(getNetwork().freeze(), supervised.wrap(clamp, supervised.getInput(0)));
// CudaTensorList gpuInput = CudnnHandle.apply(gpu -> {
// Precision precision = Precision.Float;
// return CudaTensorList.wrap(gpu.getPtr(TensorArray.wrap(image), precision, MemoryType.Managed), 1, image.getDimensions(), precision);
// });
// @Nonnull Trainable trainable = new TensorListTrainable(supervised, gpuInput).setVerbosity(1).setMask(true);
@Nonnull Trainable trainable = new ArrayTrainable(supervised, 1).setVerbose(true).setMask(true, false).setData(Arrays.<Tensor[]>asList(new Tensor[] { image }));
new IterativeTrainer(trainable).setMonitor(getTrainingMonitor(history, supervised)).setOrientation(new QQN()).setLineSearchFactory(name -> new ArmijoWolfeSearch()).setTimeout(60, TimeUnit.MINUTES).runAndFree();
return TestUtil.plot(history);
});
}
use of com.simiacryptus.mindseye.test.StepRecord 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;
}
use of com.simiacryptus.mindseye.test.StepRecord in project MindsEye by SimiaCryptus.
the class TrainingTester method trainGD.
/**
* Train gd list.
*
* @param log the log
* @param trainable the trainable
* @return the list
*/
@Nonnull
public List<StepRecord> trainGD(@Nonnull final NotebookOutput log, final Trainable trainable) {
log.p("First, we train using basic gradient descent method apply weak line search conditions.");
@Nonnull final List<StepRecord> history = new ArrayList<>();
@Nonnull final TrainingMonitor monitor = TrainingTester.getMonitor(history);
try {
log.code(() -> {
return new IterativeTrainer(trainable).setLineSearchFactory(label -> new ArmijoWolfeSearch()).setOrientation(new GradientDescent()).setMonitor(monitor).setTimeout(30, TimeUnit.SECONDS).setMaxIterations(250).setTerminateThreshold(0).runAndFree();
});
} catch (Throwable e) {
if (isThrowExceptions())
throw new RuntimeException(e);
}
return history;
}
Aggregations