Search in sources :

Example 1 with ConvolutionLayer

use of org.deeplearning4j.nn.layers.convolution.ConvolutionLayer in project deeplearning4j by deeplearning4j.

the class TestPreProcessors method testRnnToCnnPreProcessor.

@Test
public void testRnnToCnnPreProcessor() {
    //Two ways to test this:
    // (a) check that doing preProcess + backprop on a given input gives same result
    // (b) compare to ComposableInputPreProcessor(CNNtoFF, FFtoRNN)
    int[] miniBatchSizes = { 5, 1 };
    int[] timeSeriesLengths = { 9, 1 };
    int[] inputHeights = { 10, 30 };
    int[] inputWidths = { 10, 30 };
    int[] numChannels = { 1, 3, 6 };
    int cnnNChannelsIn = 3;
    Nd4j.getRandom().setSeed(12345);
    System.out.println();
    for (int miniBatchSize : miniBatchSizes) {
        for (int timeSeriesLength : timeSeriesLengths) {
            for (int inputHeight : inputHeights) {
                for (int inputWidth : inputWidths) {
                    for (int nChannels : numChannels) {
                        InputPreProcessor proc = new RnnToCnnPreProcessor(inputHeight, inputWidth, nChannels);
                        NeuralNetConfiguration nnc = new NeuralNetConfiguration.Builder().layer(new org.deeplearning4j.nn.conf.layers.ConvolutionLayer.Builder(inputWidth, inputHeight).nIn(cnnNChannelsIn).nOut(nChannels).build()).build();
                        int numParams = nnc.getLayer().initializer().numParams(nnc);
                        INDArray params = Nd4j.create(1, numParams);
                        ConvolutionLayer layer = (ConvolutionLayer) nnc.getLayer().instantiate(nnc, null, 0, params, true);
                        layer.setInputMiniBatchSize(miniBatchSize);
                        int[] shape_rnn = new int[] { miniBatchSize, nChannels * inputHeight * inputWidth, timeSeriesLength };
                        INDArray rand = Nd4j.rand(shape_rnn);
                        INDArray activationsRnn_c = Nd4j.create(shape_rnn, 'c');
                        INDArray activationsRnn_f = Nd4j.create(shape_rnn, 'f');
                        activationsRnn_c.assign(rand);
                        activationsRnn_f.assign(rand);
                        assertEquals(activationsRnn_c, activationsRnn_f);
                        //Check shape of outputs:
                        INDArray activationsCnn_c = proc.preProcess(activationsRnn_c, miniBatchSize);
                        INDArray activationsCnn_f = proc.preProcess(activationsRnn_f, miniBatchSize);
                        int[] shape_cnn = new int[] { miniBatchSize * timeSeriesLength, nChannels, inputHeight, inputWidth };
                        assertArrayEquals(shape_cnn, activationsCnn_c.shape());
                        assertArrayEquals(shape_cnn, activationsCnn_f.shape());
                        assertEquals(activationsCnn_c, activationsCnn_f);
                        //Check backward pass. Given that activations and epsilons have same shape, they should
                        //be opposite operations - i.e., get the same thing back out
                        INDArray twiceProcessed_c = proc.backprop(activationsCnn_c, miniBatchSize);
                        INDArray twiceProcessed_f = proc.backprop(activationsCnn_c, miniBatchSize);
                        assertArrayEquals(shape_rnn, twiceProcessed_c.shape());
                        assertArrayEquals(shape_rnn, twiceProcessed_f.shape());
                        assertEquals(activationsRnn_c, twiceProcessed_c);
                        assertEquals(activationsRnn_c, twiceProcessed_f);
                        //Second way to check: compare to ComposableInputPreProcessor(RNNtoFF, FFtoCNN)
                        InputPreProcessor compProc = new ComposableInputPreProcessor(new RnnToFeedForwardPreProcessor(), new FeedForwardToCnnPreProcessor(inputHeight, inputWidth, nChannels));
                        INDArray activationsCnnComp_c = compProc.preProcess(activationsRnn_c, miniBatchSize);
                        INDArray activationsCnnComp_f = compProc.preProcess(activationsRnn_f, miniBatchSize);
                        assertEquals(activationsCnnComp_c, activationsCnn_c);
                        assertEquals(activationsCnnComp_f, activationsCnn_f);
                        int[] epsilonShape = new int[] { miniBatchSize * timeSeriesLength, nChannels, inputHeight, inputWidth };
                        rand = Nd4j.rand(epsilonShape);
                        INDArray epsilonsCnn_c = Nd4j.create(epsilonShape, 'c');
                        INDArray epsilonsCnn_f = Nd4j.create(epsilonShape, 'f');
                        epsilonsCnn_c.assign(rand);
                        epsilonsCnn_f.assign(rand);
                        INDArray epsilonsRnnComp_c = compProc.backprop(epsilonsCnn_c, miniBatchSize);
                        INDArray epsilonsRnnComp_f = compProc.backprop(epsilonsCnn_f, miniBatchSize);
                        assertEquals(epsilonsRnnComp_c, epsilonsRnnComp_f);
                        INDArray epsilonsRnn_c = proc.backprop(epsilonsCnn_c, miniBatchSize);
                        INDArray epsilonsRnn_f = proc.backprop(epsilonsCnn_f, miniBatchSize);
                        assertEquals(epsilonsRnn_c, epsilonsRnn_f);
                        if (!epsilonsRnn_c.equals(epsilonsRnnComp_c)) {
                            System.out.println(miniBatchSize + "\t" + timeSeriesLength + "\t" + inputHeight + "\t" + inputWidth + "\t" + nChannels);
                            System.out.println("expected - epsilonsRnnComp");
                            System.out.println(Arrays.toString(epsilonsRnnComp_c.shape()));
                            System.out.println(epsilonsRnnComp_c);
                            System.out.println("actual - epsilonsRnn");
                            System.out.println(Arrays.toString(epsilonsRnn_c.shape()));
                            System.out.println(epsilonsRnn_c);
                        }
                        assertEquals(epsilonsRnnComp_c, epsilonsRnn_c);
                        assertEquals(epsilonsRnnComp_c, epsilonsRnn_f);
                    }
                }
            }
        }
    }
}
Also used : InputPreProcessor(org.deeplearning4j.nn.conf.InputPreProcessor) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 2 with ConvolutionLayer

use of org.deeplearning4j.nn.layers.convolution.ConvolutionLayer in project deeplearning4j by deeplearning4j.

the class TestPreProcessors method testCnnToRnnPreProcessor.

@Test
public void testCnnToRnnPreProcessor() {
    //Two ways to test this:
    // (a) check that doing preProcess + backprop on a given input gives same result
    // (b) compare to ComposableInputPreProcessor(CNNtoFF, FFtoRNN)
    int[] miniBatchSizes = { 5, 1 };
    int[] timeSeriesLengths = { 9, 1 };
    int[] inputHeights = { 10, 30 };
    int[] inputWidths = { 10, 30 };
    int[] numChannels = { 1, 3, 6 };
    int cnnNChannelsIn = 3;
    Nd4j.getRandom().setSeed(12345);
    System.out.println();
    for (int miniBatchSize : miniBatchSizes) {
        for (int timeSeriesLength : timeSeriesLengths) {
            for (int inputHeight : inputHeights) {
                for (int inputWidth : inputWidths) {
                    for (int nChannels : numChannels) {
                        String msg = "miniBatch=" + miniBatchSize + ", tsLength=" + timeSeriesLength + ", h=" + inputHeight + ", w=" + inputWidth + ", ch=" + nChannels;
                        InputPreProcessor proc = new CnnToRnnPreProcessor(inputHeight, inputWidth, nChannels);
                        NeuralNetConfiguration nnc = new NeuralNetConfiguration.Builder().layer(new org.deeplearning4j.nn.conf.layers.ConvolutionLayer.Builder(inputWidth, inputHeight).nIn(cnnNChannelsIn).nOut(nChannels).build()).build();
                        int numParams = nnc.getLayer().initializer().numParams(nnc);
                        INDArray params = Nd4j.create(1, numParams);
                        ConvolutionLayer layer = (ConvolutionLayer) nnc.getLayer().instantiate(nnc, null, 0, params, true);
                        layer.setInputMiniBatchSize(miniBatchSize);
                        INDArray activationsCnn = Nd4j.rand(new int[] { miniBatchSize * timeSeriesLength, nChannels, inputHeight, inputWidth });
                        //Check shape of outputs:
                        int prod = nChannels * inputHeight * inputWidth;
                        INDArray activationsRnn = proc.preProcess(activationsCnn, miniBatchSize);
                        assertArrayEquals(msg, new int[] { miniBatchSize, prod, timeSeriesLength }, activationsRnn.shape());
                        //Check backward pass. Given that activations and epsilons have same shape, they should
                        //be opposite operations - i.e., get the same thing back out
                        INDArray twiceProcessed = proc.backprop(activationsRnn, miniBatchSize);
                        assertArrayEquals(msg, activationsCnn.shape(), twiceProcessed.shape());
                        assertEquals(msg, activationsCnn, twiceProcessed);
                        //Second way to check: compare to ComposableInputPreProcessor(CNNtoFF, FFtoRNN)
                        InputPreProcessor compProc = new ComposableInputPreProcessor(new CnnToFeedForwardPreProcessor(inputHeight, inputWidth, nChannels), new FeedForwardToRnnPreProcessor());
                        INDArray activationsRnnComp = compProc.preProcess(activationsCnn, miniBatchSize);
                        assertEquals(msg, activationsRnnComp, activationsRnn);
                        INDArray epsilonsRnn = Nd4j.rand(new int[] { miniBatchSize, nChannels * inputHeight * inputWidth, timeSeriesLength });
                        INDArray epsilonsCnnComp = compProc.backprop(epsilonsRnn, miniBatchSize);
                        INDArray epsilonsCnn = proc.backprop(epsilonsRnn, miniBatchSize);
                        if (!epsilonsCnn.equals(epsilonsCnnComp)) {
                            System.out.println(miniBatchSize + "\t" + timeSeriesLength + "\t" + inputHeight + "\t" + inputWidth + "\t" + nChannels);
                            System.out.println("expected - epsilonsCnnComp");
                            System.out.println(Arrays.toString(epsilonsCnnComp.shape()));
                            System.out.println(epsilonsCnnComp);
                            System.out.println("actual - epsilonsCnn");
                            System.out.println(Arrays.toString(epsilonsCnn.shape()));
                            System.out.println(epsilonsCnn);
                        }
                        assertEquals(msg, epsilonsCnnComp, epsilonsCnn);
                    }
                }
            }
        }
    }
}
Also used : InputPreProcessor(org.deeplearning4j.nn.conf.InputPreProcessor) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 3 with ConvolutionLayer

use of org.deeplearning4j.nn.layers.convolution.ConvolutionLayer in project deeplearning4j by deeplearning4j.

the class RemoteConvolutionalIterationListener method iterationDone.

/**
     * Event listener for each iteration
     *
     * @param model     the model iterating
     * @param iteration the iteration number
     */
@Override
public void iterationDone(Model model, int iteration) {
    if (iteration % freq == 0) {
        List<INDArray> tensors = new ArrayList<>();
        int cnt = 0;
        Random rnd = new Random();
        MultiLayerNetwork l = (MultiLayerNetwork) model;
        BufferedImage sourceImage = null;
        int sampleDim = -1;
        for (Layer layer : l.getLayers()) {
            if (layer.type() == Layer.Type.CONVOLUTIONAL) {
                INDArray output = layer.activate();
                if (sampleDim < 0)
                    sampleDim = rnd.nextInt(output.shape()[0] - 1) + 1;
                if (cnt == 0) {
                    INDArray inputs = ((ConvolutionLayer) layer).input();
                    try {
                        sourceImage = restoreRGBImage(inputs.tensorAlongDimension(sampleDim, new int[] { 3, 2, 1 }));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                INDArray tad = output.tensorAlongDimension(sampleDim, 3, 2, 1);
                tensors.add(tad);
                cnt++;
            }
        }
        BufferedImage render = rasterizeConvoLayers(tensors, sourceImage);
        try {
            File tempFile = File.createTempFile("cnn_activations", ".png");
            tempFile.deleteOnExit();
            ImageIO.write(render, "png", tempFile);
            PathUpdate update = new PathUpdate();
            //ensure path is set
            update.setPath(tempFile.getPath());
            //ensure the server is hooked up with the path
            //target.request(MediaType.APPLICATION_JSON).post(Entity.entity(update, MediaType.APPLICATION_JSON));
            WebReporter.getInstance().queueReport(target, Entity.entity(update, MediaType.APPLICATION_JSON));
            if (firstIteration) {
                firstIteration = false;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        minibatchNum++;
    }
}
Also used : ArrayList(java.util.ArrayList) Layer(org.deeplearning4j.nn.api.Layer) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) BufferedImage(java.awt.image.BufferedImage) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) IOException(java.io.IOException) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Random(java.util.Random) PathUpdate(org.deeplearning4j.ui.activation.PathUpdate) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) File(java.io.File)

Example 4 with ConvolutionLayer

use of org.deeplearning4j.nn.layers.convolution.ConvolutionLayer in project deeplearning4j by deeplearning4j.

the class ConvolutionalIterationListener method iterationDone.

/**
     * Event listener for each iteration
     *
     * @param model     the model iterating
     * @param iteration the iteration number
     */
@Override
public void iterationDone(Model model, int iteration) {
    if (iteration % freq == 0) {
        List<INDArray> tensors = new ArrayList<>();
        int cnt = 0;
        Random rnd = new Random();
        BufferedImage sourceImage = null;
        if (model instanceof MultiLayerNetwork) {
            MultiLayerNetwork l = (MultiLayerNetwork) model;
            for (Layer layer : l.getLayers()) {
                if (layer.type() == Layer.Type.CONVOLUTIONAL) {
                    INDArray output = layer.activate();
                    int sampleDim = rnd.nextInt(output.shape()[0] - 1) + 1;
                    if (cnt == 0) {
                        INDArray inputs = ((ConvolutionLayer) layer).input();
                        try {
                            sourceImage = restoreRGBImage(inputs.tensorAlongDimension(sampleDim, new int[] { 3, 2, 1 }));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                    INDArray tad = output.tensorAlongDimension(sampleDim, 3, 2, 1);
                    tensors.add(tad);
                    cnt++;
                }
            }
        } else if (model instanceof ComputationGraph) {
            ComputationGraph l = (ComputationGraph) model;
            for (Layer layer : l.getLayers()) {
                if (layer.type() == Layer.Type.CONVOLUTIONAL) {
                    INDArray output = layer.activate();
                    int sampleDim = rnd.nextInt(output.shape()[0] - 1) + 1;
                    if (cnt == 0) {
                        INDArray inputs = ((ConvolutionLayer) layer).input();
                        try {
                            sourceImage = restoreRGBImage(inputs.tensorAlongDimension(sampleDim, new int[] { 3, 2, 1 }));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                    INDArray tad = output.tensorAlongDimension(sampleDim, 3, 2, 1);
                    tensors.add(tad);
                    cnt++;
                }
            }
        }
        BufferedImage render = rasterizeConvoLayers(tensors, sourceImage);
        Persistable p = new ConvolutionListenerPersistable(sessionID, workerID, System.currentTimeMillis(), render);
        ssr.putStaticInfo(p);
        minibatchNum++;
    }
}
Also used : Persistable(org.deeplearning4j.api.storage.Persistable) ArrayList(java.util.ArrayList) Layer(org.deeplearning4j.nn.api.Layer) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) BufferedImage(java.awt.image.BufferedImage) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) IOException(java.io.IOException) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Random(java.util.Random) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork)

Aggregations

ConvolutionLayer (org.deeplearning4j.nn.layers.convolution.ConvolutionLayer)4 INDArray (org.nd4j.linalg.api.ndarray.INDArray)4 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Layer (org.deeplearning4j.nn.api.Layer)2 InputPreProcessor (org.deeplearning4j.nn.conf.InputPreProcessor)2 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)2 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)2 Test (org.junit.Test)2 File (java.io.File)1 Persistable (org.deeplearning4j.api.storage.Persistable)1 ComputationGraph (org.deeplearning4j.nn.graph.ComputationGraph)1 PathUpdate (org.deeplearning4j.ui.activation.PathUpdate)1