/
TestSameDiffConv.java
189 lines (161 loc) · 9.64 KB
/
TestSameDiffConv.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.deeplearning4j.nn.layers.samediff;
import lombok.extern.slf4j.Slf4j;
import org.deeplearning4j.TestUtils;
import org.deeplearning4j.nn.conf.ConvolutionMode;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.ConvolutionLayer;
import org.deeplearning4j.nn.layers.samediff.testlayers.SameDiffConv;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.params.ConvolutionParamInitializer;
import org.deeplearning4j.nn.weights.WeightInit;
import org.junit.Test;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import static org.junit.Assert.*;
@Slf4j
public class TestSameDiffConv {
@Test
public void testSameDiffConvBasic() {
int nIn = 3;
int nOut = 4;
int kH = 2;
int kW = 3;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.list()
.layer(new SameDiffConv.Builder().nIn(nIn).nOut(nOut).kernelSize(kH, kW).build())
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
Map<String, INDArray> pt1 = net.getLayer(0).paramTable();
assertNotNull(pt1);
assertEquals(2, pt1.size());
assertNotNull(pt1.get(ConvolutionParamInitializer.WEIGHT_KEY));
assertNotNull(pt1.get(ConvolutionParamInitializer.BIAS_KEY));
assertArrayEquals(new int[]{nOut, nIn, kH, kW}, pt1.get(ConvolutionParamInitializer.WEIGHT_KEY).shape());
assertArrayEquals(new int[]{1, nOut}, pt1.get(ConvolutionParamInitializer.BIAS_KEY).shape());
TestUtils.testModelSerialization(net);
}
@Test
public void testSameDiffConvForward() {
int imgH = 16;
int imgW = 20;
int count = 0;
//Note: to avoid the exporential number of tests here, we'll randomly run every Nth test only.
//With n=1, m=3 this is 1 out of every 3 tests (on average)
Random r = new Random(12345);
int n = 1;
int m = 5;
for (int minibatch : new int[]{5, 1}) {
Activation[] afns = new Activation[]{
Activation.TANH,
Activation.SIGMOID,
Activation.ELU,
Activation.IDENTITY,
Activation.SOFTPLUS,
Activation.SOFTSIGN,
Activation.CUBE,
Activation.HARDTANH,
Activation.RELU
};
for (boolean hasBias : new boolean[]{true, false}) {
for (int nIn : new int[]{3, 4}) {
for (int nOut : new int[]{4, 5}) {
for (int[] kernel : new int[][]{{2, 2}, {2, 1}, {3, 2}}) {
for (int[] strides : new int[][]{{1, 1}, {2, 2}, {2, 1}}) {
for (int[] dilation : new int[][]{{1, 1}, {2, 2}, {1, 2}}) {
for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Truncate, ConvolutionMode.Same}) {
for (Activation a : afns) {
int i = r.nextInt(m);
if (i >= n) {
//Example: n=2, m=3... skip on i=2, run test on i=0, i=1
continue;
}
String msg = "Test " + (count++) + " - minibatch=" + minibatch + ", nIn=" + nIn
+ ", nOut=" + nOut + ", kernel=" + Arrays.toString(kernel) + ", stride="
+ Arrays.toString(strides) + ", dilation=" + Arrays.toString(dilation)
+ ", ConvolutionMode=" + cm + ", ActFn=" + a + ", hasBias=" + hasBias;
log.info("Starting test: " + msg);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(12345)
.list()
.layer(new SameDiffConv.Builder()
.weightInit(WeightInit.XAVIER)
.nIn(nIn)
.nOut(nOut)
.kernelSize(kernel)
.stride(strides)
.dilation(dilation)
.convolutionMode(cm)
.activation(a)
.hasBias(hasBias)
.build())
.layer(new SameDiffConv.Builder()
.weightInit(WeightInit.XAVIER)
.nIn(nOut)
.nOut(nOut)
.kernelSize(kernel)
.stride(strides)
.dilation(dilation)
.convolutionMode(cm)
.activation(a)
.hasBias(hasBias)
.build())
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
assertNotNull(net.paramTable());
MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder()
.weightInit(WeightInit.XAVIER)
.seed(12345)
.list()
.layer(new ConvolutionLayer.Builder()
.nIn(nIn)
.nOut(nOut)
.kernelSize(kernel)
.stride(strides)
.dilation(dilation)
.convolutionMode(cm)
.activation(a)
.hasBias(hasBias)
.build())
.layer(new ConvolutionLayer.Builder()
.nIn(nOut)
.nOut(nOut)
.kernelSize(kernel)
.stride(strides)
.dilation(dilation)
.convolutionMode(cm)
.activation(a)
.hasBias(hasBias)
.build())
.build();
MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
net2.init();
//Check params:
Map<String, INDArray> params1 = net.paramTable();
Map<String, INDArray> params2 = net2.paramTable();
assertEquals(msg, params2, params1);
INDArray in = Nd4j.rand(new int[]{minibatch, nIn, imgH, imgW});
INDArray out = net.output(in);
INDArray outExp = net2.output(in);
assertEquals(msg, outExp, out);
//Also check serialization:
MultiLayerNetwork netLoaded = TestUtils.testModelSerialization(net);
INDArray outLoaded = netLoaded.output(in);
assertEquals(msg, outExp, outLoaded);
}
}
}
}
}
}
}
}
}
}
}