Search in sources :

Example 6 with ConvolutionLayer

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

the class ConvolutionLayerSetupTest method testLRN.

@Test
public void testLRN() throws Exception {
    List<String> labels = new ArrayList<>(Arrays.asList("Zico", "Ziwang_Xu"));
    String rootDir = new ClassPathResource("lfwtest").getFile().getAbsolutePath();
    RecordReader reader = new ImageRecordReader(28, 28, 3);
    reader.initialize(new FileSplit(new File(rootDir)));
    DataSetIterator recordReader = new RecordReaderDataSetIterator(reader, 10, 1, labels.size());
    labels.remove("lfwtest");
    NeuralNetConfiguration.ListBuilder builder = (NeuralNetConfiguration.ListBuilder) incompleteLRN();
    builder.setInputType(InputType.convolutional(28, 28, 3));
    MultiLayerConfiguration conf = builder.build();
    ConvolutionLayer layer2 = (ConvolutionLayer) conf.getConf(3).getLayer();
    assertEquals(6, layer2.getNIn());
}
Also used : RecordReader(org.datavec.api.records.reader.RecordReader) ImageRecordReader(org.datavec.image.recordreader.ImageRecordReader) ArrayList(java.util.ArrayList) RecordReaderDataSetIterator(org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) FileSplit(org.datavec.api.split.FileSplit) ClassPathResource(org.datavec.api.util.ClassPathResource) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) File(java.io.File) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) RecordReaderDataSetIterator(org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator) ImageRecordReader(org.datavec.image.recordreader.ImageRecordReader) Test(org.junit.Test)

Example 7 with ConvolutionLayer

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

the class ConvolutionLayerTest method testCNNBiasInit.

@Test
public void testCNNBiasInit() {
    ConvolutionLayer cnn = new ConvolutionLayer.Builder().nIn(1).nOut(3).biasInit(1).build();
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().layer(cnn).build();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    Layer layer = conf.getLayer().instantiate(conf, null, 0, params, true);
    assertEquals(1, layer.getParam("b").size(0));
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) Layer(org.deeplearning4j.nn.api.Layer) OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) DenseLayer(org.deeplearning4j.nn.conf.layers.DenseLayer) SubsamplingLayer(org.deeplearning4j.nn.conf.layers.SubsamplingLayer) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) Test(org.junit.Test)

Example 8 with ConvolutionLayer

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

the class TestConvolutionModes method testGlobalLocalConfigCompGraph.

@Test
public void testGlobalLocalConfigCompGraph() {
    for (ConvolutionMode cm : new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Truncate, ConvolutionMode.Same }) {
        ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).convolutionMode(cm).graphBuilder().addInputs("in").addLayer("0", new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "in").addLayer("1", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Strict).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "0").addLayer("2", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Truncate).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "1").addLayer("3", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Same).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "2").addLayer("4", new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "3").addLayer("5", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Strict).kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "4").addLayer("6", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Truncate).kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "5").addLayer("7", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Same).kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "6").addLayer("8", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3).build(), "7").setOutputs("8").build();
        assertEquals(cm, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("0")).getLayerConf().getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Strict, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("1")).getLayerConf().getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Truncate, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("2")).getLayerConf().getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Same, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("3")).getLayerConf().getLayer()).getConvolutionMode());
        assertEquals(cm, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("4")).getLayerConf().getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Strict, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("5")).getLayerConf().getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Truncate, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("6")).getLayerConf().getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Same, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("7")).getLayerConf().getLayer()).getConvolutionMode());
    }
}
Also used : ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionMode(org.deeplearning4j.nn.conf.ConvolutionMode) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) Test(org.junit.Test)

Example 9 with ConvolutionLayer

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

the class FineTuneConfiguration method applyToNeuralNetConfiguration.

public void applyToNeuralNetConfiguration(NeuralNetConfiguration nnc) {
    Layer l = nnc.getLayer();
    Updater originalUpdater = null;
    WeightInit origWeightInit = null;
    if (l != null) {
        originalUpdater = l.getUpdater();
        origWeightInit = l.getWeightInit();
        if (activationFn != null)
            l.setActivationFn(activationFn);
        if (weightInit != null)
            l.setWeightInit(weightInit);
        if (biasInit != null)
            l.setBiasInit(biasInit);
        if (dist != null)
            l.setDist(dist);
        if (learningRate != null) {
            //usually the same learning rate is applied to both bias and weights
            //so always overwrite the learning rate to both?
            l.setLearningRate(learningRate);
            l.setBiasLearningRate(learningRate);
        }
        if (biasLearningRate != null)
            l.setBiasLearningRate(biasLearningRate);
        if (learningRateSchedule != null)
            l.setLearningRateSchedule(learningRateSchedule);
        //        if(lrScoreBasedDecay != null)
        if (l1 != null)
            l.setL1(l1);
        if (l2 != null)
            l.setL2(l2);
        if (l1Bias != null)
            l.setL1Bias(l1Bias);
        if (l2Bias != null)
            l.setL2Bias(l2Bias);
        if (dropOut != null)
            l.setDropOut(dropOut);
        if (updater != null)
            l.setUpdater(updater);
        if (momentum != null)
            l.setMomentum(momentum);
        if (momentumSchedule != null)
            l.setMomentum(momentum);
        if (epsilon != null)
            l.setEpsilon(epsilon);
        if (rho != null)
            l.setRho(rho);
        if (rmsDecay != null)
            l.setRmsDecay(rmsDecay);
        if (adamMeanDecay != null)
            l.setAdamMeanDecay(adamMeanDecay);
        if (adamVarDecay != null)
            l.setAdamVarDecay(adamVarDecay);
    }
    if (miniBatch != null)
        nnc.setMiniBatch(miniBatch);
    if (numIterations != null)
        nnc.setNumIterations(numIterations);
    if (maxNumLineSearchIterations != null)
        nnc.setMaxNumLineSearchIterations(maxNumLineSearchIterations);
    if (seed != null)
        nnc.setSeed(seed);
    if (useRegularization != null)
        nnc.setUseRegularization(useRegularization);
    if (optimizationAlgo != null)
        nnc.setOptimizationAlgo(optimizationAlgo);
    if (stepFunction != null)
        nnc.setStepFunction(stepFunction);
    if (useDropConnect != null)
        nnc.setUseDropConnect(useDropConnect);
    if (minimize != null)
        nnc.setMinimize(minimize);
    if (gradientNormalization != null)
        l.setGradientNormalization(gradientNormalization);
    if (gradientNormalizationThreshold != null)
        l.setGradientNormalizationThreshold(gradientNormalizationThreshold);
    if (learningRatePolicy != null)
        nnc.setLearningRatePolicy(learningRatePolicy);
    if (lrPolicySteps != null)
        nnc.setLrPolicySteps(lrPolicySteps);
    if (lrPolicyPower != null)
        nnc.setLrPolicyPower(lrPolicyPower);
    if (convolutionMode != null && l instanceof ConvolutionLayer) {
        ((ConvolutionLayer) l).setConvolutionMode(convolutionMode);
    }
    if (convolutionMode != null && l instanceof SubsamplingLayer) {
        ((SubsamplingLayer) l).setConvolutionMode(convolutionMode);
    }
    //Check the updater config. If we change updaters, we want to remove the old config to avoid warnings
    if (l != null && updater != null && originalUpdater != null && updater != originalUpdater) {
        switch(originalUpdater) {
            case ADAM:
                if (adamMeanDecay == null)
                    l.setAdamMeanDecay(Double.NaN);
                if (adamVarDecay == null)
                    l.setAdamVarDecay(Double.NaN);
                break;
            case ADADELTA:
                if (rho == null)
                    l.setRho(Double.NaN);
                if (epsilon == null)
                    l.setEpsilon(Double.NaN);
                break;
            case NESTEROVS:
                if (momentum == null)
                    l.setMomentum(Double.NaN);
                if (momentumSchedule == null)
                    l.setMomentumSchedule(null);
                if (epsilon == null)
                    l.setEpsilon(Double.NaN);
                break;
            case ADAGRAD:
                if (epsilon == null)
                    l.setEpsilon(Double.NaN);
                break;
            case RMSPROP:
                if (rmsDecay == null)
                    l.setRmsDecay(Double.NaN);
                if (epsilon == null)
                    l.setEpsilon(Double.NaN);
                break;
        }
    }
    //Check weight init. Remove dist if originally was DISTRIBUTION, and isn't now -> remove no longer needed distribution
    if (l != null && origWeightInit == WeightInit.DISTRIBUTION && weightInit != null && weightInit != WeightInit.DISTRIBUTION) {
        l.setDist(null);
    }
    //Perform validation. This also sets the defaults for updaters. For example, Updater.RMSProp -> set rmsDecay
    if (l != null) {
        LayerValidation.updaterValidation(l.getLayerName(), l, momentum, momentumSchedule, adamMeanDecay, adamVarDecay, rho, rmsDecay, epsilon);
        boolean useDropCon = (useDropConnect == null ? nnc.isUseDropConnect() : useDropConnect);
        LayerValidation.generalValidation(l.getLayerName(), l, nnc.isUseRegularization(), useDropCon, dropOut, l2, l2Bias, l1, l1Bias, dist);
    }
    //Also: update the LR, L1 and L2 maps, based on current config (which might be different to original config)
    if (nnc.variables(false) != null) {
        for (String s : nnc.variables(false)) {
            nnc.setLayerParamLR(s);
        }
    }
}
Also used : SubsamplingLayer(org.deeplearning4j.nn.conf.layers.SubsamplingLayer) WeightInit(org.deeplearning4j.nn.weights.WeightInit) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) SubsamplingLayer(org.deeplearning4j.nn.conf.layers.SubsamplingLayer) Layer(org.deeplearning4j.nn.conf.layers.Layer) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer)

Aggregations

ConvolutionLayer (org.deeplearning4j.nn.conf.layers.ConvolutionLayer)9 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)7 Test (org.junit.Test)6 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)5 INDArray (org.nd4j.linalg.api.ndarray.INDArray)4 SubsamplingLayer (org.deeplearning4j.nn.conf.layers.SubsamplingLayer)3 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)2 ConvolutionMode (org.deeplearning4j.nn.conf.ConvolutionMode)2 Layer (org.deeplearning4j.nn.conf.layers.Layer)2 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)2 WeightInit (org.deeplearning4j.nn.weights.WeightInit)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 RecordReader (org.datavec.api.records.reader.RecordReader)1 FileSplit (org.datavec.api.split.FileSplit)1 ClassPathResource (org.datavec.api.util.ClassPathResource)1 ImageRecordReader (org.datavec.image.recordreader.ImageRecordReader)1 Persistable (org.deeplearning4j.api.storage.Persistable)1 RecordReaderDataSetIterator (org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator)1 MnistDataSetIterator (org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator)1