/
SubsamplingLayer.java
459 lines (397 loc) · 19.5 KB
/
SubsamplingLayer.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
package org.deeplearning4j.nn.layers.convolution.subsampling;
import lombok.extern.slf4j.Slf4j;
import org.deeplearning4j.exception.DL4JInvalidInputException;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.api.MaskState;
import org.deeplearning4j.nn.conf.ConvolutionMode;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.PoolingType;
import org.deeplearning4j.nn.gradient.DefaultGradient;
import org.deeplearning4j.nn.gradient.Gradient;
import org.deeplearning4j.nn.layers.AbstractLayer;
import org.deeplearning4j.nn.layers.LayerHelper;
import org.deeplearning4j.nn.layers.mkldnn.MKLDNNSubsamplingHelper;
import org.deeplearning4j.util.ConvolutionUtils;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.DynamicCustomOp;
import org.nd4j.linalg.api.ops.Op;
import org.nd4j.linalg.api.ops.impl.layers.convolution.LegacyPooling2D;
import org.nd4j.linalg.api.ops.impl.transforms.any.IsMax;
import org.nd4j.linalg.api.shape.Shape;
import org.nd4j.linalg.convolution.Convolution;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.ops.transforms.Transforms;
import org.nd4j.linalg.primitives.Pair;
import org.nd4j.linalg.util.ArrayUtil;
import org.deeplearning4j.nn.workspace.LayerWorkspaceMgr;
import org.deeplearning4j.nn.workspace.ArrayType;
import org.nd4j.util.OneTimeLogger;
import java.util.Arrays;
import java.util.Properties;
/**
* Subsampling layer.
*
* Used for downsampling a convolution
*
* @author Adam Gibson
*/
@Slf4j
public class SubsamplingLayer extends AbstractLayer<org.deeplearning4j.nn.conf.layers.SubsamplingLayer> {
protected SubsamplingHelper helper = null;
protected int helperCountFail = 0;
protected ConvolutionMode convolutionMode;
public SubsamplingLayer(NeuralNetConfiguration conf, DataType dataType) {
super(conf, dataType);
initializeHelper();
this.convolutionMode =
((org.deeplearning4j.nn.conf.layers.SubsamplingLayer) conf.getLayer()).getConvolutionMode();
}
void initializeHelper() {
String backend = Nd4j.getExecutioner().getEnvironmentInformation().getProperty("backend");
if("CUDA".equalsIgnoreCase(backend)) {
try {
helper = Class.forName("org.deeplearning4j.nn.layers.convolution.subsampling.CudnnSubsamplingHelper")
.asSubclass(SubsamplingHelper.class).getConstructor(DataType.class).newInstance(dataType);
log.debug("CudnnSubsamplingHelper successfully initialized");
if (!helper.checkSupported()) {
helper = null;
}
} catch (Throwable t) {
if (!(t instanceof ClassNotFoundException)) {
log.warn("Could not initialize CudnnSubsamplingHelper", t);
} else {
OneTimeLogger.info(log, "cuDNN not found: "
+ "use cuDNN for better GPU performance by including the deeplearning4j-cuda module. "
+ "For more information, please refer to: https://deeplearning4j.org/docs/latest/deeplearning4j-config-cudnn", t);
}
}
} else if("CPU".equalsIgnoreCase(backend) ){
helper = new MKLDNNSubsamplingHelper(dataType);
log.debug("Created MKL-DNN helper: MKLDNNSubsamplingHelper, layer {}", layerConf().getLayerName());
}
if (helper != null && !helper.checkSupported()) {
log.debug("Removed helper {} as not supported", helper.getClass());
helper = null;
}
}
@Override
public double calcRegularizationScore(boolean backpropOnlyParams) {
return 0;
}
@Override
public Type type() {
return Type.SUBSAMPLING;
}
@Override
public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon, LayerWorkspaceMgr workspaceMgr) {
assertInputSet(true);
INDArray input = this.input.castTo(dataType);
if(epsilon.dataType() != dataType)
epsilon = epsilon.castTo(dataType);
// FIXME: int cast
int miniBatch = (int) input.size(0);
int inDepth = (int) input.size(1);
int inH = (int) input.size(2);
int inW = (int) input.size(3);
int[] kernel = layerConf().getKernelSize();
int[] strides = layerConf().getStride();
int[] dilation = layerConf().getDilation();
int[] pad;
int[] outSize;
if (convolutionMode == ConvolutionMode.Same) {
outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, null, convolutionMode, dilation); //Also performs validation
pad = ConvolutionUtils.getSameModeTopLeftPadding(outSize, new int[] {inH, inW}, kernel, strides, dilation);
} else {
pad = layerConf().getPadding();
outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, pad, convolutionMode, dilation); //Also performs validation
}
int outH = outSize[0];
int outW = outSize[1];
if (helper != null && (helperCountFail == 0 || !layerConf().isCudnnAllowFallback())) {
Pair<Gradient, INDArray> ret = null;
try{
ret = helper.backpropGradient(input, epsilon, kernel, strides, pad,
layerConf().getPoolingType(), convolutionMode, dilation, workspaceMgr);
} catch (Exception e){
if(e.getMessage() != null && e.getMessage().contains("Failed to allocate")){
//This is a memory exception - don't fallback to built-in implementation
throw e;
}
if(layerConf().isCudnnAllowFallback()){
helperCountFail++;
if(helper instanceof MKLDNNSubsamplingHelper){
log.warn("MKL-DNN execution failed - falling back on built-in implementation",e);
} else {
log.warn("CuDNN execution failed - falling back on built-in implementation",e);
}
} else {
throw new RuntimeException(e);
}
}
if (ret != null) {
return ret;
}
}
//subsampling doesn't have weights and thus gradients are not calculated for this layer
//only scale and reshape epsilon
// FIXME: int cast
int inputHeight = (int) input().size(-2);
int inputWidth = (int) input().size(-1);
Gradient retGradient = new DefaultGradient();
//Epsilons in shape: [miniBatch, channels, outH, outW]
//Epsilons out shape: [miniBatch, channels, inH, inW]
//Two possibilities here for the epsilons:
//(a) Epsilons come from a dense/output layer above, with c order and strides [channels*H*W, H*W, W, 1]
//(b) Epsilons come from CNN layer above, with c order and strides [H*W, channels*H*W, W, 1] (i.e., due to permute)
//We want to reshape epsilons to 1d here, but to do this without a copy: we end up with different orders of
// element in the buffer, for the "dense above" and "cnn above" cases.
//Fortunately, we can just permute things when we do the im2col reshaping; then, the order of the rows in
// col2d will match the order of the 1d epsilons...
//With the 1d epsilons order matching the rows order for the 2d im2col: we can just do a muliColumnVector op,
// instead of a slower broadcast muli op
boolean cOrderStrides = false;
if (epsilon.ordering() != 'c') {
epsilon = epsilon.dup('c');
cOrderStrides = true;
}
if (!cOrderStrides && Shape.strideDescendingCAscendingF(epsilon)) {
cOrderStrides = true;
} else if (!Arrays.equals(new long[] {outH * outW, inDepth * outH * outW, outW, 1}, epsilon.stride())) {
//Unexpected/unusual strides, not either (a) or (b) cases above
epsilon = epsilon.dup('c');
cOrderStrides = true;
}
INDArray col6d;
INDArray col6dPermuted;
INDArray epsilon1d;
if (cOrderStrides) {
//"Dense/Output layer above strides... i.e., standard c-order strides
col6d = Nd4j.create(dataType, new long[] {miniBatch, inDepth, outH, outW, kernel[0], kernel[1]}, 'c');
col6dPermuted = col6d.permute(0, 1, 4, 5, 2, 3);
epsilon1d = epsilon.reshape('c', ArrayUtil.prod(epsilon.length()), 1); //zero copy reshape
} else {
//"CNN layer above" strides...
col6d = Nd4j.create(dataType, new long[] {inDepth, miniBatch, outH, outW, kernel[0], kernel[1]}, 'c');
col6dPermuted = col6d.permute(1, 0, 4, 5, 2, 3);
INDArray epsilonTemp = epsilon.permute(1, 0, 2, 3);
epsilon1d = epsilonTemp.reshape('c', new int[] {ArrayUtil.prod(epsilon.length()), 1}); //Should be a zero-copy reshape always
}
INDArray col2d = col6d.reshape('c', miniBatch * inDepth * outH * outW, kernel[0] * kernel[1]);
switch (layerConf().getPoolingType()) {
case MAX:
//Execute im2col, then reshape to 2d. Note rows are in a different order for cOrderStrides true vs false cases
DynamicCustomOp op = DynamicCustomOp.builder("im2col")
.addIntegerArguments(kernel[0], kernel[1], strides[0], strides[1], pad[0], pad[1], dilation[0], dilation[1],
ArrayUtil.fromBoolean(convolutionMode == ConvolutionMode.Same))
.addFloatingPointArguments(minValue())
.addInputs(input)
.addOutputs(col6dPermuted)
.build();
Nd4j.getExecutioner().exec(op);
INDArray isMax = Nd4j.getExecutioner().exec(new IsMax(col2d, col2d, 1));
isMax.muliColumnVector(epsilon1d);
break;
case AVG:
//TODO: We could further optimize this by creating an uninitialized array, and doing a 'putiColumnVector' operation
// instead of a zero initialization + an addiColumnVector op
col2d.addiColumnVector(epsilon1d);
break;
case PNORM:
int pnorm = layerConf().getPnorm();
//First: do forward pass to get pNorm array
Convolution.im2col(input, kernel[0], kernel[1], strides[0], strides[1], pad[0], pad[1], dilation[0], dilation[1],
convolutionMode == ConvolutionMode.Same, col6dPermuted);
INDArray pNorm = Transforms.abs(col2d, true); //dup as we need col2d again later
Transforms.pow(pNorm, pnorm, false);
pNorm = pNorm.sum(1).reshape(pNorm.size(0), 1);
Transforms.pow(pNorm, (1.0 / pnorm), false);
//dL/dIn = dL/dOut * dOut/dIn
//dOut/dIn = in .* |in|^(p-2) / ||in||_p^(p-1), where ||in||_p is the output p-norm
INDArray numerator;
if (pnorm == 2) {
numerator = col2d;
} else {
INDArray absp2 = Transforms.pow(Transforms.abs(col2d, true), pnorm - 2, false);
numerator = col2d.muli(absp2);
}
INDArray denom = Transforms.pow(pNorm, pnorm - 1, false);
double eps = layerConf().getEps();
Transforms.max(denom, eps, false); // in case of 0
numerator.muliColumnVector(denom.rdivi(epsilon1d));
break;
default:
throw new IllegalStateException("Unknown or unsupported pooling type: " + layerConf().getPoolingType()
+ " " + layerId());
}
//Finally: we want the output strides for the epsilons to match the strides in the activations from the layer below
//Assuming the layer below is a CNN layer (very likely) we want [H*W, channels*H*W, W, 1] instead of the standard
// c-order [channels*H*W, H*W, W, 1] strides
//To achieve this: [channels, miniBatch, H, W] in c order, then permute to [miniBatch, channels, H, W]
//This gives us proper strides of 1 on the muli...
INDArray tempEpsilon = workspaceMgr.create(ArrayType.ACTIVATION_GRAD, dataType, new long[] {inDepth, miniBatch, inH, inW}, 'c');
INDArray outEpsilon = tempEpsilon.permute(1, 0, 2, 3);
Convolution.col2im(col6dPermuted, outEpsilon, strides[0], strides[1], pad[0], pad[1], inputHeight, inputWidth, dilation[0], dilation[1]);
if (layerConf().getPoolingType() == PoolingType.AVG)
outEpsilon.divi(ArrayUtil.prod(layerConf().getKernelSize()));
return new Pair<>(retGradient, outEpsilon);
}
private static double minValue(){
switch (Nd4j.dataType()){
case DOUBLE:
return -Double.MAX_VALUE;
case FLOAT:
return -Float.MAX_VALUE;
case HALF:
return -65504.0;
default:
throw new IllegalStateException("Unexpected data type: " + Nd4j.dataType());
}
}
@Override
public INDArray activate(boolean training, LayerWorkspaceMgr workspaceMgr) {
assertInputSet(false);
//Normally we would apply dropout first. However, dropout on subsampling layers is not something that users typically expect
// consequently, we'll skip it here
//Input validation: expect rank 4 matrix
if (input.rank() != 4) {
throw new DL4JInvalidInputException("Got rank " + input.rank()
+ " array as input to SubsamplingLayer with shape " + Arrays.toString(input.shape())
+ ". Expected rank 4 array with shape [minibatchSize, channels, inputHeight, inputWidth]. "
+ layerId());
}
INDArray input = this.input.castTo(dataType);
// FIXME: int cast
int miniBatch = (int) input.size(0);
int inDepth = (int) input.size(1);
int inH = (int) input.size(2);
int inW = (int) input.size(3);
int[] kernel = layerConf().getKernelSize();
int[] strides = layerConf().getStride();
int[] dilation = layerConf().getDilation();
int[] pad;
int[] outSize;
if (convolutionMode == ConvolutionMode.Same) {
outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, null, convolutionMode, dilation); //Also performs validation
pad = ConvolutionUtils.getSameModeTopLeftPadding(outSize, new int[] {inH, inW}, kernel, strides, dilation);
} else {
pad = layerConf().getPadding();
outSize = ConvolutionUtils.getOutputSize(input, kernel, strides, pad, convolutionMode, dilation); //Also performs validation
}
int outH = outSize[0];
int outW = outSize[1];
if (helper != null && (helperCountFail == 0 || !layerConf().isCudnnAllowFallback())) {
INDArray ret = null;
try {
ret = helper.activate(input, training, kernel, strides, pad, layerConf().getPoolingType(),
convolutionMode, dilation, workspaceMgr);
} catch (Exception e){
if(layerConf().isCudnnAllowFallback()){
helperCountFail++;
if(helper instanceof MKLDNNSubsamplingHelper){
log.warn("MKL-DNN execution failed - falling back on built-in implementation",e);
} else {
log.warn("CuDNN execution failed - falling back on built-in implementation",e);
}
} else {
throw new RuntimeException(e);
}
}
if (ret != null) {
return ret;
}
}
//Similar to convolution layer forward pass: do im2col, but permute so that pooling can be done with efficient strides...
//Current im2col implementation expects input with shape [miniBatch,channels,kH,kW,outH,outW]
INDArray output = workspaceMgr.createUninitialized(ArrayType.ACTIVATIONS, input.dataType(), new long[]{miniBatch, inDepth, outH, outW}, 'c');
LegacyPooling2D.Pooling2DType pt;
double extra = 0.0;
switch (layerConf().getPoolingType()){
case MAX:
pt = LegacyPooling2D.Pooling2DType.MAX;
break;
case AVG:
pt = LegacyPooling2D.Pooling2DType.AVG;
extra = 1.0; //Divide by kH*kW not "number present" to match backward pass
break;
case PNORM:
pt = LegacyPooling2D.Pooling2DType.PNORM;
extra = layerConf().getPnorm();
break;
default:
throw new UnsupportedOperationException("Not supported: " + layerConf().getPoolingType());
}
Op op = new LegacyPooling2D(input, kernel[0], kernel[1], strides[0], strides[1], pad[0], pad[1], dilation[0], dilation[1],
convolutionMode == ConvolutionMode.Same, pt, extra, output);
Nd4j.getExecutioner().exec(op);
return output;
}
@Override
public boolean isPretrainLayer() {
return false;
}
@Override
public void clearNoiseWeightParams() {
//no op
}
@Override
public LayerHelper getHelper() {
return helper;
}
@Override
public Gradient gradient() {
throw new UnsupportedOperationException("Not supported - no parameters");
}
@Override
public void fit() {
}
@Override
public long numParams() {
return 0;
}
@Override
public void fit(INDArray input, LayerWorkspaceMgr workspaceMgr) {}
@Override
public double score() {
return 0;
}
@Override
public void update(INDArray gradient, String paramType) {
}
@Override
public INDArray params() {
return null;
}
@Override
public INDArray getParam(String param) {
return params();
}
@Override
public void setParams(INDArray params) {
}
@Override
public Pair<INDArray, MaskState> feedForwardMaskArray(INDArray maskArray, MaskState currentMaskState, int minibatchSize) {
if (maskArray == null) {
//For same mode (with stride 1): output activations size is always same size as input activations size -> mask array is same size
return new Pair<>(maskArray, currentMaskState);
}
INDArray outMask = ConvolutionUtils.cnn2dMaskReduction(maskArray, layerConf().getKernelSize(), layerConf().getStride(),
layerConf().getPadding(), layerConf().getDilation(), layerConf().getConvolutionMode());
return super.feedForwardMaskArray(outMask, currentMaskState, minibatchSize);
}
}