Skip to content

Commit 0f01b40

Browse files
committed
Reset OpenCL kernels if batch size changes
1 parent 0a61ebd commit 0f01b40

File tree

6 files changed

+34
-8
lines changed

6 files changed

+34
-8
lines changed

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
273273
for(int i = 0; i < outCn; i++ )
274274
biasvec[i] = biasMat.at<float>(i);
275275
}
276+
#ifdef HAVE_OPENCL
277+
convolutionOp.release();
278+
#endif
276279
}
277280

278281
bool setActivation(const Ptr<ActivationLayer>& layer)

modules/dnn/src/layers/fully_connected_layer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ class FullyConnectedLayerImpl : public InnerProductLayer
267267
};
268268

269269
#ifdef HAVE_OPENCL
270+
void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
271+
{
272+
innerProductOp.release();
273+
}
274+
270275
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, InputArrayOfArrays internals)
271276
{
272277
std::vector<UMat> inputs;

modules/dnn/src/layers/lrn_layer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class LRNLayerImpl : public LRNLayer
9696
}
9797

9898
#ifdef HAVE_OPENCL
99+
void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
100+
{
101+
lrnOp.release();
102+
}
103+
99104
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
100105
{
101106
std::vector<UMat> inputs;

modules/dnn/src/layers/pooling_layer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ class PoolingLayerImpl : public PoolingLayer
126126
}
127127

128128
getConvPoolPaddings(inp, out, kernel, stride, padMode, Size(1, 1), pad);
129+
130+
#ifdef HAVE_OPENCL
131+
poolOp.release();
132+
#endif
129133
}
130134

131135
virtual bool supportBackend(int backendId)

modules/dnn/src/layers/softmax_layer.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,7 @@ class SoftMaxLayerImpl : public SoftmaxLayer
9595
#ifdef HAVE_OPENCL
9696
virtual void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
9797
{
98-
OCL4DNNSoftmaxConfig config;
99-
100-
config.in_shape = shape(*inputs[0]);
101-
config.axis = axisRaw;
102-
config.channels = inputs[0]->size[axisRaw];
103-
config.logsoftmax = logSoftMax;
104-
105-
softmaxOp = Ptr<OCL4DNNSoftmax<float> >(new OCL4DNNSoftmax<float>(config));
98+
softmaxOp.release();
10699
}
107100

108101
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays itns)
@@ -115,6 +108,18 @@ class SoftMaxLayerImpl : public SoftmaxLayer
115108
outs.getUMatVector(outputs);
116109
itns.getUMatVector(internals);
117110

111+
if (softmaxOp.empty())
112+
{
113+
OCL4DNNSoftmaxConfig config;
114+
115+
config.in_shape = shape(inputs[0]);
116+
config.axis = axisRaw;
117+
config.channels = inputs[0].size[axisRaw];
118+
config.logsoftmax = logSoftMax;
119+
120+
softmaxOp = Ptr<OCL4DNNSoftmax<float> >(new OCL4DNNSoftmax<float>(config));
121+
}
122+
118123
UMat& src = inputs[0];
119124
UMat& dstMat = outputs[0];
120125

modules/dnn/test/test_googlenet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ OCL_TEST(Reproducibility_GoogLeNet, Accuracy)
7777
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
7878
net.setPreferableTarget(DNN_TARGET_OPENCL);
7979

80+
// Initialize network for a single image in the batch but test with batch size=2.
81+
net.setInput(blobFromImage(Mat(224, 224, CV_8UC3)));
82+
net.forward();
83+
8084
std::vector<Mat> inpMats;
8185
inpMats.push_back( imread(_tf("googlenet_0.png")) );
8286
inpMats.push_back( imread(_tf("googlenet_1.png")) );

0 commit comments

Comments
 (0)