Search in sources :

Example 1 with BandAvgReducerLayer

use of com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer in project MindsEye by SimiaCryptus.

the class StyleTransfer method getStyleComponents.

/**
 * Gets style components.
 *
 * @param node          the node
 * @param network       the network
 * @param styleParams   the style params
 * @param mean          the mean
 * @param covariance    the covariance
 * @param centeringMode the centering mode
 * @return the style components
 */
@Nonnull
public ArrayList<Tuple2<Double, DAGNode>> getStyleComponents(final DAGNode node, final PipelineNetwork network, final LayerStyleParams styleParams, final Tensor mean, final Tensor covariance, final CenteringMode centeringMode) {
    ArrayList<Tuple2<Double, DAGNode>> styleComponents = new ArrayList<>();
    if (null != styleParams && (styleParams.cov != 0 || styleParams.mean != 0)) {
        double meanRms = mean.rms();
        double meanScale = 0 == meanRms ? 1 : (1.0 / meanRms);
        InnerNode negTarget = network.wrap(new ValueLayer(mean.scale(-1)), new DAGNode[] {});
        InnerNode negAvg = network.wrap(new BandAvgReducerLayer().setAlpha(-1), node);
        if (styleParams.cov != 0) {
            DAGNode recentered;
            switch(centeringMode) {
                case Origin:
                    recentered = node;
                    break;
                case Dynamic:
                    recentered = network.wrap(new GateBiasLayer(), node, negAvg);
                    break;
                case Static:
                    recentered = network.wrap(new GateBiasLayer(), node, negTarget);
                    break;
                default:
                    throw new RuntimeException();
            }
            int[] covDim = covariance.getDimensions();
            assert 0 < covDim[2] : Arrays.toString(covDim);
            int inputBands = mean.getDimensions()[2];
            assert 0 < inputBands : Arrays.toString(mean.getDimensions());
            int outputBands = covDim[2] / inputBands;
            assert 0 < outputBands : Arrays.toString(covDim) + " / " + inputBands;
            double covRms = covariance.rms();
            double covScale = 0 == covRms ? 1 : (1.0 / covRms);
            styleComponents.add(new Tuple2<>(styleParams.cov, network.wrap(new MeanSqLossLayer().setAlpha(covScale), network.wrap(new ValueLayer(covariance), new DAGNode[] {}), network.wrap(ArtistryUtil.wrapTilesAvg(new GramianLayer()), recentered))));
        }
        if (styleParams.mean != 0) {
            styleComponents.add(new Tuple2<>(styleParams.mean, network.wrap(new MeanSqLossLayer().setAlpha(meanScale), negAvg, negTarget)));
        }
    }
    return styleComponents;
}
Also used : ArrayList(java.util.ArrayList) ValueLayer(com.simiacryptus.mindseye.layers.cudnn.ValueLayer) DAGNode(com.simiacryptus.mindseye.network.DAGNode) RangeConstraint(com.simiacryptus.mindseye.opt.region.RangeConstraint) MeanSqLossLayer(com.simiacryptus.mindseye.layers.cudnn.MeanSqLossLayer) InnerNode(com.simiacryptus.mindseye.network.InnerNode) GramianLayer(com.simiacryptus.mindseye.layers.cudnn.GramianLayer) Tuple2(com.simiacryptus.util.lang.Tuple2) BandAvgReducerLayer(com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer) GateBiasLayer(com.simiacryptus.mindseye.layers.cudnn.GateBiasLayer) Nonnull(javax.annotation.Nonnull)

Example 2 with BandAvgReducerLayer

use of com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer in project MindsEye by SimiaCryptus.

the class ArtistryUtil method wrapTilesAvg.

/**
 * Wrap tiles avg layer.
 *
 * @param subnet     the subnet
 * @param borderX1   the border x 1
 * @param borderY1   the border y 1
 * @param borderX2   the border x 2
 * @param borderY2   the border y 2
 * @param tileWidth  the tile width
 * @param tileHeight the tile height
 * @return the layer
 */
protected static Layer wrapTilesAvg(final Layer subnet, final int borderX1, final int borderY1, final int borderX2, final int borderY2, final int tileWidth, final int tileHeight) {
    PipelineNetwork network1 = new PipelineNetwork(1);
    if (borderX1 != 0 || borderY1 != 0)
        network1.wrap(new com.simiacryptus.mindseye.layers.cudnn.ImgZeroPaddingLayer(borderX1, borderY1));
    network1.add(subnet);
    if (borderX2 != 0 || borderY2 != 0)
        network1.wrap(new com.simiacryptus.mindseye.layers.cudnn.ImgZeroPaddingLayer(-borderX2, -borderY2));
    PipelineNetwork network = new PipelineNetwork(1);
    network.wrap(new com.simiacryptus.mindseye.layers.cudnn.ImgTileSubnetLayer(network1, tileWidth, tileHeight, tileWidth - 2 * borderX1, tileHeight - 2 * borderY1));
    network.wrap(new BandAvgReducerLayer());
    return network;
}
Also used : PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) BandAvgReducerLayer(com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer)

Example 3 with BandAvgReducerLayer

use of com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer in project MindsEye by SimiaCryptus.

the class ArtistryUtil method squareAvg.

/**
 * Square avg pipeline network.
 *
 * @param network      the network
 * @param mean         the mean
 * @param pcaTransform the pca transform
 * @return the pipeline network
 */
@Nonnull
public static PipelineNetwork squareAvg(final PipelineNetwork network, Tensor mean, Tensor pcaTransform) {
    int[] dimensions = pcaTransform.getDimensions();
    int inputBands = mean.getDimensions()[2];
    int pcaBands = dimensions[2];
    int outputBands = pcaBands / inputBands;
    int width = dimensions[0];
    int height = dimensions[1];
    network.wrap(new ImgBandBiasLayer(mean.scale(-1)));
    network.wrap(new ConvolutionLayer(width, height, inputBands, outputBands).set(pcaTransform));
    network.wrap(new SquareActivationLayer());
    network.wrap(new BandAvgReducerLayer());
    return network;
}
Also used : ImgBandBiasLayer(com.simiacryptus.mindseye.layers.cudnn.ImgBandBiasLayer) SquareActivationLayer(com.simiacryptus.mindseye.layers.cudnn.SquareActivationLayer) BandAvgReducerLayer(com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer) ConvolutionLayer(com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer) Nonnull(javax.annotation.Nonnull)

Aggregations

BandAvgReducerLayer (com.simiacryptus.mindseye.layers.cudnn.BandAvgReducerLayer)3 Nonnull (javax.annotation.Nonnull)2 ConvolutionLayer (com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer)1 GateBiasLayer (com.simiacryptus.mindseye.layers.cudnn.GateBiasLayer)1 GramianLayer (com.simiacryptus.mindseye.layers.cudnn.GramianLayer)1 ImgBandBiasLayer (com.simiacryptus.mindseye.layers.cudnn.ImgBandBiasLayer)1 MeanSqLossLayer (com.simiacryptus.mindseye.layers.cudnn.MeanSqLossLayer)1 SquareActivationLayer (com.simiacryptus.mindseye.layers.cudnn.SquareActivationLayer)1 ValueLayer (com.simiacryptus.mindseye.layers.cudnn.ValueLayer)1 DAGNode (com.simiacryptus.mindseye.network.DAGNode)1 InnerNode (com.simiacryptus.mindseye.network.InnerNode)1 PipelineNetwork (com.simiacryptus.mindseye.network.PipelineNetwork)1 RangeConstraint (com.simiacryptus.mindseye.opt.region.RangeConstraint)1 Tuple2 (com.simiacryptus.util.lang.Tuple2)1 ArrayList (java.util.ArrayList)1