use of org.deeplearning4j.nn.conf.ConvolutionMode in project deeplearning4j by deeplearning4j.
the class TestConvolutionModes method testStrictTruncateConvolutionModeOutput.
@Test
public void testStrictTruncateConvolutionModeOutput() {
//Idea: with convolution mode == Truncate, input size shouldn't matter (within the bounds of truncated edge),
// and edge data shouldn't affect the output
//Use: 9x9, kernel 3, stride 3, padding 0
// Should get same output for 10x10 and 11x11...
Nd4j.getRandom().setSeed(12345);
int[] minibatches = { 1, 3 };
int[] inDepths = { 1, 3 };
int[] inSizes = { 9, 10, 11 };
for (boolean isSubsampling : new boolean[] { false, true }) {
for (int minibatch : minibatches) {
for (int inDepth : inDepths) {
INDArray origData = Nd4j.rand(new int[] { minibatch, inDepth, 9, 9 });
for (int inSize : inSizes) {
for (ConvolutionMode cm : new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Truncate }) {
INDArray inputData = Nd4j.rand(new int[] { minibatch, inDepth, inSize, inSize });
inputData.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 9), NDArrayIndex.interval(0, 9)).assign(origData);
Layer layer;
if (isSubsampling) {
layer = new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).build();
} else {
layer = new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).nOut(3).build();
}
MultiLayerNetwork net = null;
try {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).convolutionMode(cm).list().layer(0, layer).layer(1, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3).build()).setInputType(InputType.convolutional(inSize, inSize, inDepth)).build();
net = new MultiLayerNetwork(conf);
net.init();
if (inSize > 9 && cm == ConvolutionMode.Strict) {
fail("Expected exception");
}
} catch (DL4JException e) {
if (inSize == 9 || cm != ConvolutionMode.Strict) {
e.printStackTrace();
fail("Unexpected exception");
}
//Expected exception
continue;
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
INDArray out = net.output(origData);
INDArray out2 = net.output(inputData);
assertEquals(out, out2);
}
}
}
}
}
}
use of org.deeplearning4j.nn.conf.ConvolutionMode in project deeplearning4j by deeplearning4j.
the class TestConvolutionModes method testGlobalLocalConfig.
@Test
public void testGlobalLocalConfig() {
for (ConvolutionMode cm : new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Truncate }) {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).convolutionMode(cm).list().layer(0, new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build()).layer(1, new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Strict).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build()).layer(2, new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Truncate).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build()).layer(3, new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Same).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build()).layer(4, new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).build()).layer(5, new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Strict).kernelSize(3, 3).stride(3, 3).padding(0, 0).build()).layer(6, new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Truncate).kernelSize(3, 3).stride(3, 3).padding(0, 0).build()).layer(7, new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Same).kernelSize(3, 3).stride(3, 3).padding(0, 0).build()).layer(8, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3).build()).build();
assertEquals(cm, ((ConvolutionLayer) conf.getConf(0).getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Strict, ((ConvolutionLayer) conf.getConf(1).getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Truncate, ((ConvolutionLayer) conf.getConf(2).getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Same, ((ConvolutionLayer) conf.getConf(3).getLayer()).getConvolutionMode());
assertEquals(cm, ((SubsamplingLayer) conf.getConf(4).getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Strict, ((SubsamplingLayer) conf.getConf(5).getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Truncate, ((SubsamplingLayer) conf.getConf(6).getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Same, ((SubsamplingLayer) conf.getConf(7).getLayer()).getConvolutionMode());
}
}
use of org.deeplearning4j.nn.conf.ConvolutionMode in project deeplearning4j by deeplearning4j.
the class TestConvolution method testCompareCudnnStandardOutputsVsMode.
@Test
public void testCompareCudnnStandardOutputsVsMode() throws Exception {
ConvolutionMode[] cm = new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Same };
for (ConvolutionMode c : cm) {
for (boolean conv : new boolean[] { true, false }) {
org.deeplearning4j.nn.conf.layers.Layer l;
if (conv) {
l = new ConvolutionLayer.Builder().nOut(4).kernelSize(4, 4).stride(2, 2).build();
} else {
l = new SubsamplingLayer.Builder().kernelSize(4, 4).stride(2, 2).build();
}
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).regularization(true).l2(0.0005).learningRate(.01).weightInit(WeightInit.XAVIER).convolutionMode(c).list().layer(0, l).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(10).activation(Activation.SOFTMAX).build()).setInputType(//See note below
InputType.convolutionalFlat(28, 28, 1)).backprop(true).pretrain(false).build();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork net1 = new MultiLayerNetwork(conf);
net1.init();
net1.initGradientsView();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork net2 = new MultiLayerNetwork(conf);
net2.init();
net2.initGradientsView();
Layer layerCudnn = net1.getLayer(0);
Layer layerStandard = net2.getLayer(0);
Field f = layerStandard.getClass().getDeclaredField("helper");
f.setAccessible(true);
f.set(layerStandard, null);
if (f.get(layerCudnn) == null)
throw new RuntimeException();
if (f.get(layerStandard) != null)
throw new RuntimeException();
//(20-4+0)/2 +1 = 9
INDArray in = Nd4j.rand(new int[] { 1, 1, 20, 20 });
INDArray outCudnn = layerCudnn.activate(in);
INDArray outStd = layerStandard.activate(in);
assertEquals(outStd, outCudnn);
//Check backprop:
INDArray epsilon = Nd4j.rand(outStd.shape());
Pair<Gradient, INDArray> pCudnn = layerCudnn.backpropGradient(epsilon);
Pair<Gradient, INDArray> pStd = layerStandard.backpropGradient(epsilon);
System.out.println(Arrays.toString(pStd.getSecond().data().asFloat()));
System.out.println(Arrays.toString(pCudnn.getSecond().data().asFloat()));
INDArray epsOutStd = pStd.getSecond();
INDArray epsOutCudnn = pCudnn.getSecond();
assertTrue(epsOutStd.equalsWithEps(epsOutCudnn, 1e-4));
INDArray gradStd = pStd.getFirst().gradient();
INDArray gradCudnn = pCudnn.getFirst().gradient();
assertTrue(gradStd.equalsWithEps(gradCudnn, 1e-4));
}
}
}
use of org.deeplearning4j.nn.conf.ConvolutionMode in project deeplearning4j by deeplearning4j.
the class KerasLayer method getConvolutionModeFromConfig.
/**
* Get convolution border mode from Keras layer configuration.
*
* @param layerConfig dictionary containing Keras layer configuration
* @return
* @throws InvalidKerasConfigurationException
*/
public static ConvolutionMode getConvolutionModeFromConfig(Map<String, Object> layerConfig) throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
Map<String, Object> innerConfig = getInnerLayerConfigFromConfig(layerConfig);
if (!innerConfig.containsKey(LAYER_FIELD_BORDER_MODE))
throw new InvalidKerasConfigurationException("Could not determine convolution border mode: no " + LAYER_FIELD_BORDER_MODE + " field found");
String borderMode = (String) innerConfig.get(LAYER_FIELD_BORDER_MODE);
ConvolutionMode convolutionMode = null;
switch(borderMode) {
/* Keras relies upon the Theano and TensorFlow border mode definitions
* and operations:
* - Theano: http://deeplearning.net/software/theano/library/tensor/nnet/conv.html#theano.tensor.nnet.conv.conv2d
* - TensorFlow: https://www.tensorflow.org/api_docs/python/nn/convolution#conv2d
*/
case LAYER_BORDER_MODE_SAME:
/* TensorFlow-only "same" mode is equivalent to DL4J Same mode. */
convolutionMode = ConvolutionMode.Same;
break;
case LAYER_BORDER_MODE_VALID:
/* TensorFlow and Theano "valid" modes apply filter only
* to complete patches within the image borders with no
* padding. That is equivalent to DL4J Truncate mode
* with no padding.
*/
case LAYER_BORDER_MODE_FULL:
/* Theano-only "full" mode zero pads the image so that
* outputs = (inputs + filters + 1) / stride. This should
* be equivalent to DL4J Truncate mode with padding
* equal to filters-1.
* TODO: verify this is correct.
*/
convolutionMode = ConvolutionMode.Truncate;
break;
default:
throw new UnsupportedKerasConfigurationException("Unsupported convolution border mode: " + borderMode);
}
return convolutionMode;
}
use of org.deeplearning4j.nn.conf.ConvolutionMode in project deeplearning4j by deeplearning4j.
the class TestConvolutionModes method testStrictTruncateConvolutionModeCompGraph.
@Test
public void testStrictTruncateConvolutionModeCompGraph() {
//Idea: with convolution mode == Truncate, input size shouldn't matter (within the bounds of truncated edge),
// and edge data shouldn't affect the output
//Use: 9x9, kernel 3, stride 3, padding 0
// Should get same output for 10x10 and 11x11...
Nd4j.getRandom().setSeed(12345);
int[] minibatches = { 1, 3 };
int[] inDepths = { 1, 3 };
int[] inSizes = { 9, 10, 11 };
for (boolean isSubsampling : new boolean[] { false, true }) {
for (int minibatch : minibatches) {
for (int inDepth : inDepths) {
INDArray origData = Nd4j.rand(new int[] { minibatch, inDepth, 9, 9 });
for (int inSize : inSizes) {
for (ConvolutionMode cm : new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Truncate }) {
INDArray inputData = Nd4j.rand(new int[] { minibatch, inDepth, inSize, inSize });
inputData.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 9), NDArrayIndex.interval(0, 9)).assign(origData);
Layer layer;
if (isSubsampling) {
layer = new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).build();
} else {
layer = new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).nOut(3).build();
}
ComputationGraph net = null;
try {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).convolutionMode(cm).graphBuilder().addInputs("in").addLayer("0", layer, "in").addLayer("1", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3).build(), "0").setOutputs("1").setInputTypes(InputType.convolutional(inSize, inSize, inDepth)).build();
net = new ComputationGraph(conf);
net.init();
if (inSize > 9 && cm == ConvolutionMode.Strict) {
fail("Expected exception");
}
} catch (DL4JException e) {
if (inSize == 9 || cm != ConvolutionMode.Strict) {
e.printStackTrace();
fail("Unexpected exception");
}
//Expected exception
continue;
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
INDArray out = net.outputSingle(origData);
INDArray out2 = net.outputSingle(inputData);
assertEquals(out, out2);
}
}
}
}
}
}
Aggregations