Merge pull request #14951 from alalek:issue_14937
[platform/upstream/opencv.git] / modules / dnn / src / layers / convolution_layer.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Copyright (C) 2017, Intel Corporation, all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "../precomp.hpp"
44 #include "layers_common.hpp"
45 #include "../op_halide.hpp"
46 #include "../op_inf_engine.hpp"
47 #include "../op_vkcom.hpp"
48 #include "opencv2/core/hal/hal.hpp"
49 #include "opencv2/core/hal/intrin.hpp"
50 #include <iostream>
51
52 #ifdef HAVE_OPENCL
53 #include "opencl_kernels_dnn.hpp"
54 using namespace cv::dnn::ocl4dnn;
55 #endif
56
57 namespace cv
58 {
59 namespace dnn
60 {
61
62 class BaseConvolutionLayerImpl : public ConvolutionLayer
63 {
64 public:
65     bool fusedWeights, fusedBias;
66     std::vector<double> weightsMultipliers;
67     BaseConvolutionLayerImpl(const LayerParams &params)
68     {
69         setParamsFrom(params);
70         getConvolutionKernelParams(params, kernel_size, pads_begin, pads_end, strides, dilations, padMode);
71
72         numOutput = params.get<int>("num_output");
73         int ngroups = params.get<int>("group", 1);
74         CV_Assert(numOutput % ngroups == 0);
75
76         if (kernel_size.size() == 2) {
77             kernel = Size(kernel_size[1], kernel_size[0]);
78             stride = Size(strides[1], strides[0]);
79             for (int i = 0; i < pads_begin.size(); i++) {
80                 if (pads_begin[i] != pads_end[i])
81                     CV_Error(Error::StsNotImplemented, "Unsupported asymmetric padding in convolution layer");
82             }
83             pad = Size(pads_begin[1], pads_begin[0]);
84             dilation = Size(dilations[1], dilations[0]);
85
86             adjust_pads.push_back(params.get<int>("adj_h", 0));
87             adjust_pads.push_back(params.get<int>("adj_w", 0));
88
89             adjustPad.height = adjust_pads[0];
90             adjustPad.width = adjust_pads[1];
91             CV_Assert(adjustPad.width < stride.width &&
92                       adjustPad.height < stride.height);
93         }
94         fusedWeights = false;
95         fusedBias = false;
96     }
97
98     virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
99     {
100         std::vector<Mat> inputs, outputs;
101         inputs_arr.getMatVector(inputs);
102         outputs_arr.getMatVector(outputs);
103
104         CV_Assert(inputs.size() > 0);
105
106         CV_Assert(blobs.size() == 1 || blobs.size() == 2);
107         CV_Assert(inputs[0].dims == outputs[0].dims);
108         CV_Assert(blobs[0].dims == kernel_size.size() + 2);
109         for (int i = 0; i < kernel_size.size(); i++) {
110             CV_Assert(blobs[0].size[i + 2] == kernel_size[i]);
111         }
112
113         const Mat &input = inputs[0];
114         CV_Assert((input.dims == 4 || input.dims == 5) && (input.type() == CV_32F || input.type() == CV_16S));
115         for (size_t i = 0; i < inputs.size(); i++)
116         {
117             CV_Assert(inputs[i].type() == input.type());
118             CV_Assert((inputs[i].dims == 4 || inputs[i].dims == 5) && inputs[i].size[1] == input.size[1]);
119             for (int j = 0; j < inputs[i].dims; j++) {
120                 CV_Assert(inputs[i].size[j] == input.size[j]);
121             }
122         }
123
124         std::vector<int> inpShape;
125         std::vector<int> outShape;
126         for (int i = 2; i < inputs[0].dims; i++) {
127             inpShape.push_back(inputs[0].size[i]);
128             outShape.push_back(outputs[0].size[i]);
129         }
130         getConvPoolPaddings(inpShape, kernel_size, strides, padMode, pads_begin, pads_end);
131         if (pads_begin.size() == 2) {
132             for (int i = 0; i < pads_begin.size(); i++) {
133                 if (pads_begin[i] != pads_end[i])
134                     CV_Error(Error::StsNotImplemented, "Unsupported asymmetric padding in convolution layer");
135             }
136             pad = Size(pads_begin[1], pads_begin[0]);
137         }
138         fusedWeights = false;
139         fusedBias = false;
140     }
141
142     bool hasBias() const
143     {
144         return blobs.size() >= 2;
145     }
146
147     virtual MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const = 0;
148     bool is1x1() const
149     {
150         return (kernel.height == 1 && kernel.width == 1) &&
151                (stride.height == 1 && stride.width == 1) &&
152                (dilation.height == 1 && dilation.width == 1);
153     }
154
155     virtual bool tryFuse(Ptr<Layer>& top) CV_OVERRIDE
156     {
157         Mat w, b;
158         top->getScaleShift(w, b);
159         if (!w.empty() || !b.empty())
160         {
161             fuseWeights(w, b);
162             fusedWeights = fusedWeights || !w.empty();
163             fusedBias = fusedBias || (hasBias() && !w.empty()) || !b.empty();
164             return true;
165         }
166         return false;
167     }
168
169     virtual void fuseWeights(const Mat& w_, const Mat& b_) = 0;
170
171     virtual void applyHalideScheduler(Ptr<BackendNode>& node,
172                                       const std::vector<Mat*> &inputs,
173                                       const std::vector<Mat> &outputs,
174                                       int targetId) const CV_OVERRIDE
175     {
176 #ifdef HAVE_HALIDE
177         if (targetId != DNN_TARGET_CPU)
178         {
179             Layer::applyHalideScheduler(node, inputs, outputs, targetId);
180             return;
181         }
182         Halide::Var x("x"), y("y"), c("c"), n("n"), tile("tile"), yi("yi"), yo("yo"), co("co"), ci("ci");
183         Halide::Func& top = node.dynamicCast<HalideBackendNode>()->funcs[1];
184         Halide::Func& padded_input = node.dynamicCast<HalideBackendNode>()->funcs[0];
185
186         int outW, outH, outC, outN;
187         getCanonicalSize(outputs[0].size, &outW, &outH, &outC, &outN);
188
189         if (outW == 1 || outH <= 2)
190             return;
191
192         if (is1x1() || outC <= 16)
193             top.reorder(x, c, y)
194                .split(y, yo, yi, 2)
195                .fuse(yo, n, tile)
196                .parallel(tile)
197                .unroll(yi)
198                .vectorize(x, outW >= 16 ? 16 : outW);
199         else
200             top.reorder(x, c, y)
201                .split(y, yo, yi, 2)
202                .split(c, co, ci, 16)
203                .fuse(yo, co, tile).fuse(n, tile, tile)
204                .parallel(tile)
205                .unroll(yi)
206                .vectorize(x, outW >= 16 ? 16 : outW);
207         padded_input.compute_at(top, yi);
208 #endif  // HAVE_HALIDE
209     }
210 };
211
212
213 #define IS_POWER_LAYER(layer) \
214             (!layer.empty() && !layer->type.compare("Power"))
215 //TODO: simultaneously convolution and bias addition for cache optimization
216 class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
217 {
218 public:
219     enum { VEC_ALIGN = 8, DFT_TYPE = CV_32F };
220     Mat weightsMat;
221     std::vector<float> biasvec;
222     std::vector<float> reluslope;
223     Ptr<ActivationLayer> activ;
224
225 #ifdef HAVE_OPENCL
226     Ptr<OCL4DNNConvSpatial<float> > convolutionOp;
227     std::vector<UMat> umat_blobs;
228     bool newActiv;
229     ocl4dnnFusedActiv_t activType;
230     float power;
231 #endif
232     ConvolutionLayerImpl(const LayerParams &params) : BaseConvolutionLayerImpl(params)
233     {
234 #ifdef HAVE_OPENCL
235         newActiv = false;
236         activType = OCL4DNN_CONV_FUSED_ACTIV_NONE;
237         power = 0.f;
238 #endif
239     }
240
241     MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const CV_OVERRIDE
242     {
243         Size out(outShape[3], outShape[2]);
244         int inpGroupCn = blobs[0].size[1];
245         int ksize = inpGroupCn * kernel.height * kernel.width;
246         return shape(out.area(), ksize);
247     }
248
249     virtual bool supportBackend(int backendId) CV_OVERRIDE
250     {
251 #ifdef HAVE_INF_ENGINE
252         if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
253         {
254             if (kernel_size.size() == 3)
255                 return preferableTarget == DNN_TARGET_CPU;
256             return (preferableTarget != DNN_TARGET_MYRIAD || dilation.width == dilation.height);
257         }
258         else
259 #endif
260         {
261             if (kernel_size.size() != 2)
262                 return false;
263             return backendId == DNN_BACKEND_OPENCV ||
264                    backendId == DNN_BACKEND_HALIDE ||
265                    (backendId == DNN_BACKEND_VKCOM && haveVulkan());
266         }
267     }
268
269     bool getMemoryShapes(const std::vector<MatShape> &inputs,
270                          const int requiredOutputs,
271                          std::vector<MatShape> &outputs,
272                          std::vector<MatShape> &internals) const CV_OVERRIDE
273     {
274         CV_Assert(blobs.size() != 0);
275         CV_Assert(!hasBias() || blobs[1].total() == (size_t)blobs[0].size[0]);
276         CV_Assert(inputs.size() == (size_t)1);
277
278         internals.clear();
279
280         CV_Assert(inputs.size() != 0);
281         std::vector<int> inpShape(inputs[0].begin() + 2, inputs[0].end());
282
283         int outCn = blobs[0].size[0];
284         std::vector<int> outShape;
285         outShape.push_back(inputs[0][0]);
286         outShape.push_back(outCn);
287
288         int inpCn = inputs[0][1];
289         if (padMode.empty())
290         {
291             for (int i = 0; i < inpShape.size(); i++)
292                 outShape.push_back((inpShape[i] + pads_begin[i] + pads_end[i] - dilations[i] * (kernel_size[i] - 1) - 1) / strides[i] + 1);
293         }
294         else
295         {
296             getConvPoolOutParams(inpShape, kernel_size, strides, padMode, dilations, outShape);
297         }
298
299         int ngroups = inpCn / blobs[0].size[1];
300         if (ngroups == 0 || ngroups * blobs[0].size[1] != inpCn)
301             CV_Error(Error::StsError, format("Number of input channels should "
302                      "be multiple of %d but got %d", blobs[0].size[1], inpCn));
303         CV_Assert(ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0);
304
305         outputs.resize(1, outShape);
306
307         return false;
308     }
309
310     virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
311     {
312         BaseConvolutionLayerImpl::finalize(inputs_arr, outputs_arr);
313
314         CV_Assert(!blobs.empty());
315         const int outCn = blobs[0].size[0];
316         // prepare weightsMat where each row is aligned and has enough zero padding on the right to
317         // use vectorized (i.e. with intrinsics) loops without tail processing
318         Mat wm = blobs[0].reshape(1, outCn);
319         if( wm.step1() % VEC_ALIGN != 0 )
320         {
321             int newcols = (int)alignSize(wm.step1(), VEC_ALIGN);
322             Mat wm_buffer = Mat(outCn, newcols, wm.type());
323             Mat wm_padding = wm_buffer.colRange(wm.cols, newcols);
324             wm_padding.setTo(Scalar::all(0.));
325             Mat wm_aligned = wm_buffer.colRange(0, wm.cols);
326             wm.copyTo(wm_aligned);
327             wm = wm_aligned;
328         }
329         weightsMat = wm;
330         weightsMultipliers.assign(outCn, 1.0);
331
332         Mat biasMat = hasBias() ? blobs[1].reshape(1, outCn) : Mat();
333         biasvec.resize(outCn+2);
334         if( biasMat.empty() )
335         {
336             for(int i = 0; i < outCn; i++ )
337                 biasvec[i] = 0.f;
338         }
339         else
340         {
341             for(int i = 0; i < outCn; i++ )
342                 biasvec[i] = biasMat.at<float>(i);
343         }
344 #ifdef HAVE_OPENCL
345         convolutionOp.release();
346 #endif
347     }
348
349     bool setActivation(const Ptr<ActivationLayer>& layer) CV_OVERRIDE
350     {
351         if (!activ.empty() && !layer.empty())
352             return false;
353
354         activ = layer;
355         if (activ.empty())
356             reluslope.clear();
357 #ifdef HAVE_OPENCL
358         newActiv = true;
359         activType = OCL4DNN_CONV_FUSED_ACTIV_NONE;
360
361         if (IS_DNN_OPENCL_TARGET(preferableTarget))
362         {
363             Ptr<PowerLayer> activ_power = activ.dynamicCast<PowerLayer>();
364             if (!activ_power.empty())
365             {
366                 if (activ_power->scale != 1.f || activ_power->shift != 0.f)
367                 {
368                     const int outCh = blobs[0].size[0];
369                     fuseWeights(Mat(1, outCh, CV_32F, Scalar(activ_power->scale)),
370                                 Mat(1, outCh, CV_32F, Scalar(activ_power->shift)));
371                 }
372
373                 power = activ_power->power;
374                 activType = OCL4DNN_CONV_FUSED_ACTIV_POWER;
375             }
376             Ptr<TanHLayer> activ_tanh = activ.dynamicCast<TanHLayer>();
377             if (!activ_tanh.empty())
378             {
379                 activType = OCL4DNN_CONV_FUSED_ACTIV_TANH;
380             }
381         }
382 #endif
383         return !activ.empty();
384     }
385
386     void fuseWeights(const Mat& w_, const Mat& b_) CV_OVERRIDE
387     {
388         // Convolution weights have OIHW data layout. Parameters fusion in case of
389         // (conv(I) + b1 ) * w + b2
390         // means to replace convolution's weights to [w*conv(I)] and bias to [b1 * w + b2]
391         const int outCn = weightsMat.size[0];
392         Mat w = w_.total() == 1 ? Mat(1, outCn, CV_32F, Scalar(w_.at<float>(0))) : w_;
393         Mat b = b_.total() == 1 ? Mat(1, outCn, CV_32F, Scalar(b_.at<float>(0))) : b_;
394         CV_Assert_N(!weightsMat.empty(), biasvec.size() == outCn + 2,
395                     w.empty() || outCn == w.total(), b.empty() || outCn == b.total());
396
397         if (!w.empty())
398         {
399             // Keep origin weights unchanged.
400             if (weightsMat.data == blobs[0].data)
401                 weightsMat = weightsMat.clone();
402
403             Mat originWeights = blobs[0].reshape(1, outCn);
404             for (int i = 0; i < outCn; ++i)
405             {
406                 double wi = w.at<float>(i);
407                 weightsMultipliers[i] *= wi;
408                 cv::multiply(originWeights.row(i), weightsMultipliers[i], weightsMat.row(i));
409                 biasvec[i] *= wi;
410             }
411         }
412
413         if (!b.empty())
414         {
415             for (int i = 0; i < outCn; ++i)
416                 biasvec[i] += b.at<float>(i);
417         }
418         biasvec[outCn] = biasvec[outCn+1] = biasvec[outCn-1];
419     }
420
421     virtual Ptr<BackendNode> initVkCom(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
422     {
423 #ifdef HAVE_VULKAN
424         int out_channel = blobs[0].size[0];
425         bool has_bias = hasBias() || fusedBias;
426         int filter_size[2] = {kernel.height, kernel.width};
427         int pad_size[2] = {pad.height, pad.width};
428         int stride_size[2] = {stride.height, stride.width};
429         int dilation_size[2] = {dilation.height, dilation.width};
430         int activation = 0;
431         vkcom::Tensor input_tensor = VkComTensor(inputs[0]);
432         int in_channel = input_tensor.dimSize(1);
433         int group = in_channel / blobs[0].size[1];
434
435         // TODO: support group > 1
436         if (group != 1)
437             return Ptr<BackendNode>();
438
439         int padding_mode;
440         if (padMode.empty())
441         {
442             padding_mode = vkcom::kPaddingModeCaffe;
443         }
444         else if (padMode == "VALID")
445         {
446             padding_mode = vkcom::kPaddingModeValid;
447         }
448         else if (padMode == "SAME")
449         {
450             padding_mode = vkcom::kPaddingModeSame;
451         }
452         else
453             CV_Error(Error::StsError, "Unsupported padding mode " + padMode);
454
455         std::shared_ptr<vkcom::OpBase> op(new vkcom::OpConv(out_channel, has_bias,
456                     filter_size, pad_size,
457                     stride_size, dilation_size,
458                     activation, group,
459                     padding_mode));
460
461         std::vector<Ptr<BackendWrapper> > blobsWrapper;
462
463         if (fusedWeights)
464         {
465             Mat wm;
466             weightsMat.copyTo(wm); // to handle the case of isContinuous() == false
467             wm = wm.reshape(1, blobs[0].dims, blobs[0].size);
468             blobsWrapper.push_back(Ptr<BackendWrapper>(new VkComBackendWrapper(wm)));
469         }
470         else
471         {
472             blobsWrapper.push_back(Ptr<BackendWrapper>(new VkComBackendWrapper(blobs[0])));
473         }
474
475         if (has_bias)
476         {
477             Mat biasesMat({out_channel}, CV_32F, &biasvec[0]);
478             blobsWrapper.push_back(Ptr<BackendWrapper>(new VkComBackendWrapper(biasesMat)));
479         }
480
481         return Ptr<BackendNode>(new VkComBackendNode(inputs, op, blobsWrapper));
482 #endif  // HAVE_VULKAN
483         return Ptr<BackendNode>();
484     }
485
486
487
488     virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
489     {
490 #ifdef HAVE_HALIDE
491         Halide::Buffer<float> inputBuffer = halideBuffer(inputs[0]);
492
493         const int inpCn = inputBuffer.channels();
494         const int outCn = blobs[0].size[0];
495         const int inpGroupCn = blobs[0].size[1];
496         const int group = inpCn / inpGroupCn;
497         const int outGroupCn = outCn / group;
498
499         Halide::Buffer<float> weights = wrapToHalideBuffer(blobs[0]);
500
501         Halide::Var x("x"), y("y"), c("c"), n("n");
502         Halide::Func top = (name.empty() ? Halide::Func() : Halide::Func(name));
503         Halide::Func padded_input(name + "_constant_exterior");
504         if (pad.width || pad.height)
505         {
506             Halide::Func bounded =
507                 Halide::BoundaryConditions::constant_exterior(inputBuffer, 0);
508             padded_input(x, y, c, n) = bounded(x, y, c, n);
509         }
510         else
511         {
512             padded_input(x, y, c, n) = inputBuffer(x, y, c, n);
513         }
514
515         Halide::RDom r(0, kernel.width, 0, kernel.height, 0, inpGroupCn);
516         Halide::Expr kx = x * stride.width - pad.width + r.x * dilation.width;
517         Halide::Expr ky = y * stride.height - pad.height + r.y * dilation.height;
518         Halide::Expr kc = r.z;
519         for (int i = 1; i < group; ++i)
520         {
521             kc = select(c < outGroupCn * i, kc, inpGroupCn * i + r.z);
522         }
523         Halide::Expr topExpr = sum(padded_input(kx, ky, kc, n) *
524                                    weights(r.x, r.y, r.z, c));
525         if (hasBias())
526         {
527             Halide::Buffer<float> bias = wrapToHalideBuffer(blobs[1], {outCn});
528             topExpr += bias(c);
529         }
530         top(x, y, c, n) = topExpr;
531         return Ptr<BackendNode>(new HalideBackendNode({ padded_input, top }));
532 #endif  // HAVE_HALIDE
533         return Ptr<BackendNode>();
534     }
535
536 #ifdef HAVE_INF_ENGINE
537     virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
538     {
539         InferenceEngine::DataPtr input = infEngineDataNode(inputs[0]);
540         CV_Assert(input->dims.size() == 4 || input->dims.size() == 5);
541
542         const int inpCn = input->dims[input->dims.size() - 2];  // NOTE: input->dims are reversed (WHIO or WHDIO)
543         const int outCn = blobs[0].size[0];
544         const int inpGroupCn = blobs[0].size[1];
545         const int group = inpCn / inpGroupCn;
546
547         InferenceEngine::Layout layout = (input->dims.size() == 4) ? InferenceEngine::Layout::OIHW :
548                                                                      InferenceEngine::Layout::NCDHW;
549
550         auto ieWeights = wrapToInfEngineBlob(blobs[0], layout);
551         if (fusedWeights)
552         {
553             if (weightsMat.isContinuous())
554             {
555                 Mat cvWeights = weightsMat.reshape(1, blobs[0].dims, blobs[0].size);
556                 ieWeights = wrapToInfEngineBlob(cvWeights, layout);
557             }
558             else
559             {
560                 ieWeights = InferenceEngine::make_shared_blob<float>(
561                                     InferenceEngine::Precision::FP32, layout,
562                                     ieWeights->dims());
563                 ieWeights->allocate();
564
565                 Mat newWeights = infEngineBlobToMat(ieWeights).reshape(1, outCn);
566                 Mat cvWeights = weightsMat.colRange(0, newWeights.cols);
567                 cvWeights.copyTo(newWeights);
568             }
569         }
570         InferenceEngine::Blob::Ptr ieBiases;
571         if (hasBias() || fusedBias)
572         {
573             Mat biasesMat({outCn}, CV_32F, &biasvec[0]);
574             ieBiases = wrapToInfEngineBlob(biasesMat, {(size_t)outCn}, InferenceEngine::Layout::C);
575         }
576
577         InferenceEngine::Builder::ConvolutionLayer ieLayer(name);
578
579         ieLayer.setKernel(kernel_size);
580         ieLayer.setStrides(strides);
581         ieLayer.setDilation(dilations);
582         ieLayer.setPaddingsBegin(pads_begin);
583         ieLayer.setPaddingsEnd(pads_end);
584         ieLayer.setGroup((size_t)group);
585         ieLayer.setOutDepth((size_t)outCn);
586
587         InferenceEngine::Builder::Layer l = ieLayer;
588         addConstantData("weights", ieWeights, l);
589         if (ieBiases)
590             addConstantData("biases", ieBiases, l);
591
592         if (!padMode.empty())
593             l.getParameters()["auto_pad"] = padMode == "VALID" ? std::string("valid") : std::string("same_upper");
594
595         return Ptr<BackendNode>(new InfEngineBackendNode(l));
596     }
597 #endif  // HAVE_INF_ENGINE
598
599     class ParallelConv : public cv::ParallelLoopBody
600     {
601     public:
602         enum { BLK_SIZE = 32, BLK_SIZE_CN = 64 };
603
604         const Mat* input_;
605         const Mat* weights_;
606         Mat* output_;
607         int outShape[4];
608         Size kernel_, pad_, stride_, dilation_;
609         int ngroups_, nstripes_;
610         std::vector<int> ofstab_;
611         const std::vector<float>* biasvec_;
612         const std::vector<float>* reluslope_;
613         const ActivationLayer* activ_;
614         bool is1x1_;
615         bool useAVX;
616         bool useAVX2;
617         bool useAVX512;
618
619         ParallelConv()
620             : input_(0), weights_(0), output_(0), ngroups_(0), nstripes_(0),
621               biasvec_(0), reluslope_(0), activ_(0), is1x1_(false), useAVX(false), useAVX2(false), useAVX512(false)
622         {}
623
624         static void run( const Mat& input, Mat& output, const Mat& weights,
625                          const std::vector<float>& biasvec,
626                          const std::vector<float>& reluslope,
627                          Size kernel, Size pad, Size stride, Size dilation,
628                          const ActivationLayer* activ, int ngroups, int nstripes )
629         {
630             CV_Assert_N(
631                        input.dims == 4 && output.dims == 4,
632                        input.size[0] == output.size[0],
633                        weights.rows == output.size[1],
634                        weights.cols == (input.size[1]/ngroups)*kernel.width*kernel.height,
635                        input.type() == output.type(),
636                        input.type() == weights.type(),
637                        input.type() == CV_32FC1,
638                        input.isContinuous(),
639                        output.isContinuous(),
640                        biasvec.size() == (size_t)output.size[1]+2);
641             ParallelConv p;
642
643             p.input_ = &input;
644             p.weights_ = &weights;
645             p.output_ = &output;
646             for( int i = 0; i < 4; i++ ) p.outShape[i] = output.size[i];
647             p.outShape[1] /= ngroups;
648             p.kernel_ = kernel; p.pad_ = pad; p.stride_ = stride; p.dilation_ = dilation;
649             p.ngroups_ = ngroups;
650             p.nstripes_ = nstripes;
651
652             int inpCnAll = input.size[1], width = input.size[3], height = input.size[2];
653             int inpCn = inpCnAll / ngroups;
654             p.is1x1_ = kernel == Size(1,1) && pad == Size(0, 0);
655             p.useAVX = checkHardwareSupport(CPU_AVX);
656             p.useAVX2 = checkHardwareSupport(CPU_AVX2);
657             p.useAVX512 = CV_CPU_HAS_SUPPORT_AVX512_SKX;
658
659             int ncn = std::min(inpCn, (int)BLK_SIZE_CN);
660             p.ofstab_.resize(kernel.width*kernel.height*ncn);
661             int* ofstab = &p.ofstab_[0];
662
663             for( int k = 0; k < ncn; k++ )
664                 for( int k_r = 0; k_r < kernel.height; k_r++ )
665                     for( int k_c = 0; k_c < kernel.width; k_c++ )
666                         ofstab[(k*kernel.height + k_r)*kernel.width + k_c] =
667                         (k*height + k_r*dilation.height)*width + k_c*dilation.width;
668
669             p.biasvec_ = &biasvec;
670             p.reluslope_ = &reluslope;
671             p.activ_ = p.reluslope_->empty() ? activ : 0;
672
673             parallel_for_(Range(0, nstripes), p, nstripes);
674         }
675
676         virtual void operator ()(const Range &r0) const CV_OVERRIDE
677         {
678             const int valign = ConvolutionLayerImpl::VEC_ALIGN;
679             int ngroups = ngroups_, batchSize = input_->size[0]*ngroups;
680             int outW = output_->size[3], outH = output_->size[2], outCn = output_->size[1]/ngroups;
681             int width = input_->size[3], height = input_->size[2], inpCn = input_->size[1]/ngroups;
682             const int nstripes = nstripes_;
683             int kernel_w = kernel_.width, kernel_h = kernel_.height;
684             int pad_w = pad_.width, pad_h = pad_.height;
685             int stride_w = stride_.width, stride_h = stride_.height;
686             int dilation_w = dilation_.width, dilation_h = dilation_.height;
687             int karea = kernel_w*kernel_h;
688             int i, j, k;
689             size_t inpPlaneSize = width*height;
690             size_t outPlaneSize = outW*outH;
691             bool is1x1 = is1x1_;
692
693             int stripesPerSample;
694             size_t stripeSize;
695             Range r = r0;
696
697             if( nstripes >= batchSize*2 )
698             {
699                 stripesPerSample = nstripes/batchSize;
700                 stripeSize = alignSize((outPlaneSize + stripesPerSample - 1)/stripesPerSample, valign);
701                 stripeSize = std::min(stripeSize, outPlaneSize);
702             }
703             else
704             {
705                 stripesPerSample = 1;
706                 int samplesPerStripe = std::max((batchSize + nstripes - 1)/nstripes, 1);
707                 r.start *= samplesPerStripe;
708                 r.end *= samplesPerStripe;
709                 stripeSize = outPlaneSize;
710             }
711
712             const float* data_inp0_ = input_->ptr<float>();
713             const int* ofstab = &ofstab_[0];
714             const float* wptr_orig_ = weights_->ptr<float>();
715             size_t wstep = weights_->step1();
716             const float* biasptr_ = &biasvec_->at(0);
717             const float* reluptr_ = reluslope_->empty() ? 0 : &reluslope_->at(0);
718             float* data_out0_ = output_->ptr<float>();
719             size_t rowbufsz = (size_t)karea*BLK_SIZE_CN*BLK_SIZE;
720             AutoBuffer<float> rowbuf0_(rowbufsz + valign);
721             float* rowbuf0 = alignPtr(rowbuf0_.data(), (int)(valign*sizeof(float)));
722
723             // we clear the buffer once; ultimately, it lets us to avoid
724             // tail processing after running the unrolled/vectorized loop.
725             // the main idea is to make sure that the tail (a.k.a. padding) of each row
726             // (i.e. the elements with indices between vsz=karea*ncn and vsz_a)
727             // does not contain NaNs or Infs. Because the padding in the weights
728             // matrix is explicitly initialized with 0's, we handle all other
729             // cases nicely, i.e. we can skip expliciting re-initialization
730             // of the padding - we just retain elements from the previous iteration
731             // of the loop over channels (cn0).
732             memset(rowbuf0, 0, rowbufsz*sizeof(rowbuf0[0]) );
733
734             for( int stripe = r.start; stripe < r.end; stripe++ )
735             {
736                 int subsampleIdx = stripe/stripesPerSample;
737                 if( subsampleIdx >= batchSize )
738                     break;
739                 int stripeStart = (int)((stripe - subsampleIdx*stripesPerSample)*stripeSize);
740                 int stripeEnd = (int)std::min(stripeStart + stripeSize, outPlaneSize);
741                 const float* data_inp0 = data_inp0_ + subsampleIdx*inpPlaneSize*inpCn;
742                 float* data_out0 = data_out0_ + subsampleIdx*outPlaneSize*outCn;
743                 int startOutCn = (subsampleIdx % ngroups)*outCn;
744                 const float* wptr_orig = wptr_orig_ + wstep*startOutCn;
745                 const float* biasptr = biasptr_ + startOutCn;
746
747                 for( int cn0 = 0; cn0 < inpCn; cn0 += BLK_SIZE_CN )
748                 {
749                     int cn1 = std::min(cn0 + BLK_SIZE_CN, inpCn);
750                     int ncn = cn1 - cn0, vsz = karea*ncn;
751                     int vsz_a = (int)alignSize(vsz, valign);
752                     const float* wptr = wptr_orig + cn0*karea;
753                     // we apply [Channels][P]ReLU (if any) during the final pass only.
754                     const float* relu = cn1 == inpCn && reluptr_ ? reluptr_ + startOutCn : 0;
755
756                     for( int ofs0 = stripeStart; ofs0 < stripeEnd; ofs0 += BLK_SIZE )
757                     {
758                         int ofs, ofs1 = std::min(ofs0 + BLK_SIZE, stripeEnd);
759                         int out_i = ofs0 / outW;
760                         int out_j = ofs0 - out_i * outW;
761
762                         // do im2row for a part of input tensor
763                         float* rowbuf = rowbuf0;
764                         for( ofs = ofs0; ofs < ofs1; out_j = 0, ++out_i )
765                         {
766                             int delta = std::min(ofs1 - ofs, outW - out_j);
767                             int out_j1 = out_j + delta;
768                             int in_i = out_i * stride_h - pad_h;
769                             int in_j = out_j * stride_w - pad_w;
770                             const float* imgptr = data_inp0 + (cn0*height + in_i)*width + in_j;
771                             ofs += delta;
772
773                             // do im2row for a part of input tensor
774                             if( is1x1 )
775                             {
776                                 for( ; out_j < out_j1; out_j++, rowbuf += vsz_a, imgptr += stride_w )
777                                 {
778                                     for( k = 0; k < vsz; k++ )
779                                         rowbuf[k] = imgptr[k*inpPlaneSize];
780                                 }
781                             }
782                             else
783                             {
784                                 bool ok_i = 0 <= in_i && in_i < height - (kernel_h-1)*dilation_h;
785                                 int i0 = std::max(0, (-in_i + dilation_h-1)/dilation_h);
786                                 int i1 = std::min(kernel_h, (height - in_i + dilation_h-1)/dilation_h);
787
788                                 for( ; out_j < out_j1; out_j++, rowbuf += vsz_a, imgptr += stride_w, in_j += stride_w )
789                                 {
790                                     // this condition should be true for most of the tensor elements, i.e.
791                                     // most of the time the kernel aperture is inside the tensor X-Y plane.
792                                     if( ok_i && out_j + 2 <= out_j1 && 0 <= in_j && in_j + stride_w*2 <= width - (kernel_w-1)*dilation_w )
793                                     {
794                                         for( k = 0; k < vsz; k++ )
795                                         {
796                                             int k1 = ofstab[k];
797                                             float v0 = imgptr[k1];
798                                             float v1 = imgptr[k1 + stride_w];
799                                             rowbuf[k] = v0;
800                                             rowbuf[k+vsz_a] = v1;
801                                         }
802                                         out_j++;
803                                         rowbuf += vsz_a;
804                                         imgptr += stride_w;
805                                         in_j += stride_w;
806                                     }
807                                     else
808                                     {
809                                         int j0 = std::max(0, (-in_j + dilation_w-1)/dilation_w);
810                                         int j1 = std::min(kernel_w, (width - in_j + dilation_w-1)/dilation_w);
811
812                                         // here some non-continuous sub-row of the row will not be
813                                         // filled from the tensor; we need to make sure that the uncovered
814                                         // elements are explicitly set to 0's. the easiest way is to
815                                         // set all the elements to 0's before the loop.
816                                         memset(rowbuf, 0, vsz*sizeof(rowbuf[0]));
817                                         for( k = 0; k < ncn; k++ )
818                                         {
819                                             for( i = i0; i < i1; i++ )
820                                             {
821                                                 for( j = j0; j < j1; j++ )
822                                                 {
823                                                     int imgofs = k*(width*height) + i*(dilation_h*width) + j*dilation_w;
824                                                     rowbuf[(k*kernel_h + i)*kernel_w + j] = imgptr[imgofs];
825                                                 }
826                                             }
827                                         }
828                                     }
829                                 }
830                             }
831                         }
832
833                         // now compute dot product of the weights
834                         // and im2row-transformed part of the tensor
835                         int bsz = ofs1 - ofs0;
836                     #if CV_TRY_AVX512_SKX
837                         /* AVX512 convolution requires an alignment of 16, and ROI is only there for larger vector sizes */
838                         if(useAVX512)
839                             opt_AVX512_SKX::fastConv(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
840                                           outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
841                         else
842                     #endif
843                     #if CV_TRY_AVX2
844                         if(useAVX2)
845                             opt_AVX2::fastConv(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
846                                           outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
847                         else
848                     #endif
849                     #if CV_TRY_AVX
850                         if(useAVX)
851                             opt_AVX::fastConv(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
852                                          outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
853                         else
854                     #endif
855                         for( int i = 0; i < outCn; i += 2 )
856                         {
857                             const float* wptr0 = wptr + i*wstep;
858                             const float* wptr1 = wptr0 + wstep;
859                             float* outptr0 = data_out0 + ofs0 + i*outPlaneSize;
860                             float* outptr1 = outptr0 + outPlaneSize;
861                             float bias0 = biasptr[i], bias1 = biasptr[i+1];
862                             float r0 = 1.f, r1 = 1.f;
863
864                             if( i+1 >= outCn )
865                             {
866                                 wptr1 = wptr0;
867                                 outptr1 = outptr0;
868                                 bias1 = bias0;
869                             }
870
871                             if( relu )
872                             {
873                                 r0 = relu[i]; r1 = relu[i+1];
874                                 if( i+1 >= outCn )
875                                     r1 = r0;
876                             }
877
878                             int j = 0;
879                         #if CV_SIMD128
880                             v_float32x4 vr0 = v_setall_f32(r0), vr1 = v_setall_f32(r1), z = v_setzero_f32();
881
882                             for( ; j <= bsz - 4; j += 4 )
883                             {
884                                 const float* rptr = rowbuf0 + j*vsz_a;
885                                 v_float32x4 s0, s1;
886
887                                 if( cn0 == 0 )
888                                 {
889                                     s0 = v_setall_f32(bias0);
890                                     s1 = v_setall_f32(bias1);
891                                 }
892                                 else
893                                 {
894                                     s0 = v_load(outptr0 + j);
895                                     s1 = v_load(outptr1 + j);
896                                 }
897
898                                 v_float32x4 vs00 = v_setzero_f32(), vs01 = v_setzero_f32(),
899                                             vs02 = v_setzero_f32(), vs03 = v_setzero_f32(),
900                                             vs10 = v_setzero_f32(), vs11 = v_setzero_f32(),
901                                             vs12 = v_setzero_f32(), vs13 = v_setzero_f32();
902                                 for( k = 0; k < vsz; k += 4, rptr += 4 )
903                                 {
904                                     v_float32x4 w0 = v_load_aligned(wptr0 + k), w1 = v_load_aligned(wptr1 + k);
905                                     v_float32x4 r0 = v_load_aligned(rptr), r1 = v_load_aligned(rptr + vsz_a),
906                                                 r2 = v_load_aligned(rptr + vsz_a*2), r3 = v_load_aligned(rptr + vsz_a*3);
907
908                                     vs00 += w0*r0;
909                                     vs01 += w0*r1;
910                                     vs02 += w0*r2;
911                                     vs03 += w0*r3;
912
913                                     vs10 += w1*r0;
914                                     vs11 += w1*r1;
915                                     vs12 += w1*r2;
916                                     vs13 += w1*r3;
917                                 }
918                                 s0 += v_reduce_sum4(vs00, vs01, vs02, vs03);
919                                 s1 += v_reduce_sum4(vs10, vs11, vs12, vs13);
920                                 if( relu )
921                                 {
922                                     s0 = v_select(s0 > z, s0, s0*vr0);
923                                     s1 = v_select(s1 > z, s1, s1*vr1);
924                                 }
925
926                                 v_store(outptr0 + j, s0);
927                                 v_store(outptr1 + j, s1);
928                             }
929                         #endif
930                             for( ; j < bsz; j++ )
931                             {
932                                 const float* rptr = rowbuf0 + j*vsz_a;
933                                 float s00, s10;
934
935                                 if( cn0 == 0 )
936                                 {
937                                     s00 = bias0;
938                                     s10 = bias1;
939                                 }
940                                 else
941                                 {
942                                     s00 = outptr0[j];
943                                     s10 = outptr1[j];
944                                 }
945
946                                 for( k = 0; k < vsz; k++ )
947                                 {
948                                     float r0 = rptr[k];
949                                     s00 += wptr0[k]*r0;
950                                     s10 += wptr1[k]*r0;
951                                 }
952                                 if( relu )
953                                 {
954                                     s00 = s00 > 0.f ? s00 : s00*r0;
955                                     s10 = s10 > 0.f ? s10 : s10*r1;
956                                 }
957
958                                 outptr0[j] = s00;
959                                 outptr1[j] = s10;
960                             }
961                         }
962                     }
963                 }
964
965                 if( activ_ )
966                     activ_->forwardSlice(data_out0 + stripeStart, data_out0 + stripeStart,
967                                          (int)(stripeEnd - stripeStart),
968                                          outPlaneSize, startOutCn, startOutCn + outCn);
969             }
970         }
971     };
972
973 #ifdef HAVE_OPENCL
974     bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
975     {
976         std::vector<UMat> inputs;
977         std::vector<UMat> outputs;
978
979         bool use_half = (inps.depth() == CV_16S);
980         inps.getUMatVector(inputs);
981         outs.getUMatVector(outputs);
982
983         CV_Assert(outputs.size() == 1);
984         for (int i = 0; i < inputs.size(); ++i)
985             CV_Assert(inputs[i].u != outputs[0].u);
986
987         if (umat_blobs.empty())
988         {
989             size_t n = blobs.size();
990             umat_blobs.resize(n);
991             for (size_t i = 0; i < n; i++)
992             {
993                 blobs[i].copyTo(umat_blobs[i]);
994             }
995         }
996
997         if (convolutionOp.empty())
998         {
999             OCL4DNNConvConfig config;
1000             config.in_shape = shape(inputs[0]);
1001             config.out_shape = shape(outputs[0]);
1002             config.kernel = kernel;
1003             config.pad = pad;
1004             config.stride = stride;
1005             config.dilation = dilation;
1006             config.group = inputs[0].size[1] / umat_blobs[0].size[1];
1007             config.bias_term = (hasBias()) ? true : false;
1008             config.use_half = use_half;
1009
1010             convolutionOp = Ptr<OCL4DNNConvSpatial<float> >(new OCL4DNNConvSpatial<float>(config));
1011         }
1012
1013         int outCn = umat_blobs[0].size[0];
1014
1015         reluslope.clear();
1016         if( activ )
1017         {
1018             Ptr<ReLULayer> activ_relu = activ.dynamicCast<ReLULayer>();
1019             if( !activ_relu.empty() )
1020             {
1021                 reluslope.assign(outCn+2, activ_relu->negativeSlope);
1022                 activType = OCL4DNN_CONV_FUSED_ACTIV_RELU;
1023             }
1024
1025             Ptr<ReLU6Layer> activ_relu6 = activ.dynamicCast<ReLU6Layer>();
1026             if( !activ_relu6.empty() )
1027             {
1028                 reluslope.resize(2);
1029                 reluslope[0] = activ_relu6->minValue;
1030                 reluslope[1] = activ_relu6->maxValue;
1031                 activType = OCL4DNN_CONV_FUSED_ACTIV_RELU6;
1032             }
1033
1034             Ptr<ChannelsPReLULayer> activ_chprelu = activ.dynamicCast<ChannelsPReLULayer>();
1035             if( !activ_chprelu.empty() )
1036             {
1037                 const Mat& m = activ_chprelu->blobs[0];
1038                 CV_Assert(m.isContinuous() && m.type() == CV_32F && (int)m.total() == outCn);
1039                 const float* mdata = m.ptr<float>();
1040                 reluslope.resize(outCn+2);
1041                 std::copy(mdata, mdata + outCn, reluslope.begin());
1042                 reluslope[outCn] = reluslope[outCn+1] = reluslope[outCn-1];
1043                 activType = OCL4DNN_CONV_FUSED_ACTIV_PRELU;
1044             }
1045         }
1046
1047         if (fusedWeights)
1048         {
1049             weightsMat.copyTo(umat_blobs[0]);
1050             fusedWeights = false;
1051         }
1052         if (fusedBias)
1053         {
1054             if ( umat_blobs.size() < 2 )
1055                 umat_blobs.resize(2);
1056             umat_blobs[1] = UMat(biasvec, true);
1057             convolutionOp->setBias(true);
1058             fusedBias = false;
1059         }
1060
1061         if ( newActiv )
1062         {
1063             if ( activType == OCL4DNN_CONV_FUSED_ACTIV_RELU )
1064             {
1065                 CV_Assert(!reluslope.empty());
1066                 convolutionOp->setActivReLU(true, reluslope[0]);
1067             }
1068             else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_PRELU)
1069             {
1070                 CV_Assert(!reluslope.empty());
1071                 convolutionOp->setActivPReLU(true, reluslope);
1072             }
1073             else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_POWER)
1074             {
1075                 convolutionOp->setActivPower(true, power);
1076             }
1077             else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_TANH)
1078             {
1079                 convolutionOp->setActivTanh(true);
1080             }
1081             else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_RELU6)
1082             {
1083                 convolutionOp->setActivReLU6(true, reluslope[0], reluslope[1]);
1084             }
1085             else
1086             {
1087                 convolutionOp->setActivReLU(false, 0);
1088                 convolutionOp->setActivPReLU(false, reluslope);
1089                 convolutionOp->setActivPower(false, 1.f);
1090                 convolutionOp->setActivTanh(false);
1091                 convolutionOp->setActivReLU6(false, 0, 0);
1092             }
1093             newActiv = false;
1094         }
1095
1096         UMat& inpMat = inputs[0];
1097         UMat& outMat = outputs[0];
1098         int batch_size = inpMat.size[0];
1099
1100         return convolutionOp->Forward(inpMat,
1101                                       inputs.size() == 2 ? inputs[1] : UMat(),
1102                                       umat_blobs[0],
1103                                       umat_blobs.size() > 1 ? umat_blobs[1] : UMat(),
1104                                       outMat,
1105                                       batch_size);
1106     }
1107 #endif
1108
1109     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
1110     {
1111         CV_TRACE_FUNCTION();
1112         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
1113
1114         CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
1115                    forward_ocl(inputs_arr, outputs_arr, internals_arr))
1116
1117         if (inputs_arr.depth() == CV_16S)
1118         {
1119             forward_fallback(inputs_arr, outputs_arr, internals_arr);
1120             return;
1121         }
1122
1123         std::vector<Mat> inputs, outputs;
1124         inputs_arr.getMatVector(inputs);
1125         outputs_arr.getMatVector(outputs);
1126
1127         /*printf("conv %s: input (%d x %d x %d x %d), kernel (%d x %d), pad (%d x %d), stride (%d x %d), dilation (%d x %d)\n",
1128                name.c_str(), inputs[0].size[0], inputs[0].size[1], inputs[0].size[2], inputs[0].size[3],
1129                kernel.width, kernel.height, pad.width, pad.height,
1130                stride.width, stride.height, dilation.width, dilation.height);*/
1131         CV_Assert_N(inputs.size() == (size_t)1, inputs[0].size[1] % blobs[0].size[1] == 0,
1132                     outputs.size() == 1, inputs[0].data != outputs[0].data);
1133
1134         if (inputs[0].dims == 5) {
1135             CV_Error(Error::StsNotImplemented, "Convolution3D layer is not supported on OCV backend");
1136         }
1137
1138         int ngroups = inputs[0].size[1]/blobs[0].size[1];
1139         CV_Assert(outputs[0].size[1] % ngroups == 0);
1140         int outCn = blobs[0].size[0];
1141
1142         reluslope.clear();
1143         if( activ )
1144         {
1145             Ptr<ReLULayer> activ_relu = activ.dynamicCast<ReLULayer>();
1146             if( !activ_relu.empty() )
1147             {
1148                 reluslope.assign(outCn+2, activ_relu->negativeSlope);
1149             }
1150
1151             Ptr<ChannelsPReLULayer> activ_chprelu = activ.dynamicCast<ChannelsPReLULayer>();
1152             if( !activ_chprelu.empty() )
1153             {
1154                 const Mat& m = activ_chprelu->blobs[0];
1155                 CV_Assert(m.isContinuous() && m.type() == CV_32F && (int)m.total() == outCn);
1156                 const float* mdata = m.ptr<float>();
1157                 reluslope.resize(outCn+2);
1158                 std::copy(mdata, mdata + outCn, reluslope.begin());
1159                 reluslope[outCn] = reluslope[outCn+1] = reluslope[outCn-1];
1160             }
1161         }
1162
1163         int nstripes = std::max(getNumThreads(), 1);
1164
1165         ParallelConv::run(inputs[0], outputs[0], weightsMat, biasvec, reluslope,
1166                           kernel, pad, stride, dilation, activ.get(), ngroups, nstripes);
1167     }
1168
1169     virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
1170                            const std::vector<MatShape> &outputs) const CV_OVERRIDE
1171     {
1172         CV_Assert(inputs.size() == outputs.size());
1173
1174         int64 flops = 0;
1175         for (int i = 0; i < inputs.size(); i++)
1176         {
1177             flops += total(outputs[i])*(CV_BIG_INT(2)*kernel.area()*inputs[i][1] + 1);
1178         }
1179
1180         return flops;
1181     }
1182 };
1183
1184 class DeConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
1185 {
1186 public:
1187     Mat weightsMat, biasesMat;
1188     UMat umat_weights;
1189     UMat umat_biases;
1190
1191     DeConvolutionLayerImpl(const LayerParams& params) : BaseConvolutionLayerImpl(params) {}
1192
1193     MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const CV_OVERRIDE
1194     {
1195         int inpCn = inpShape[1];
1196         int inpH = inpShape[2];
1197         int inpW = inpShape[3];
1198         int outCn = outShape[1];
1199         int ngroups = inpCn / blobs[0].size[0];
1200         int outGroupCn = outCn / ngroups;
1201         int ksize = outGroupCn * kernel.height * kernel.width;
1202         return shape(ksize, inpH * inpW);
1203     }
1204
1205     virtual bool supportBackend(int backendId) CV_OVERRIDE
1206     {
1207 #ifdef HAVE_INF_ENGINE
1208         const int outGroupCn = blobs[0].size[1];  // Weights are in IOHW layout
1209         const int group = numOutput / outGroupCn;
1210
1211         if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
1212         {
1213             if (kernel_size.size() == 3)
1214                 CV_Error(Error::StsNotImplemented, "Unsupported deconvolution3D layer");
1215
1216             if (adjustPad.height || adjustPad.width)
1217             {
1218                 if (padMode.empty())
1219                 {
1220                     if (preferableTarget != DNN_TARGET_CPU && group != 1)
1221                     {
1222                         if ((adjustPad.height && pad.height) || (adjustPad.width && pad.width))
1223                             return false;
1224                     }
1225                     return pad.width >= adjustPad.width && pad.height >= adjustPad.height;
1226                 }
1227                 else if (padMode == "SAME")
1228                 {
1229                     return kernel.width >= pad.width + 1 + adjustPad.width &&
1230                            kernel.height >= pad.height + 1 + adjustPad.height;
1231                 }
1232                 else if (padMode == "VALID")
1233                     return false;
1234             }
1235
1236             if (group != 1)
1237             {
1238                 return preferableTarget == DNN_TARGET_CPU;
1239             }
1240             if (preferableTarget == DNN_TARGET_OPENCL || preferableTarget == DNN_TARGET_OPENCL_FP16)
1241                 return dilation.width == 1 && dilation.height == 1;
1242             return true;
1243         }
1244         else
1245 #endif  // HAVE_INF_ENGINE
1246             return kernel_size.size() == 2 && (backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_HALIDE);
1247     }
1248
1249     bool getMemoryShapes(const std::vector<MatShape> &inputs,
1250                          const int requiredOutputs,
1251                          std::vector<MatShape> &outputs,
1252                          std::vector<MatShape> &internals) const CV_OVERRIDE
1253     {
1254         CV_Assert(!hasBias() || blobs[1].total() == (size_t)numOutput);
1255         CV_Assert(inputs.size() != 0);
1256
1257         int outCn = numOutput;
1258         std::vector<int> outShape;
1259         outShape.push_back(inputs[0][0]);  // batch
1260         outShape.push_back(outCn);
1261         if (padMode.empty())
1262         {
1263             for (int i = 0; i < kernel_size.size(); i++)
1264                 outShape.push_back(strides[i] * (inputs[0][2 + i] - 1) + kernel_size[i] - pads_begin[i] - pads_end[i] + adjust_pads[i]);
1265         }
1266         else if (padMode == "VALID")
1267         {
1268             for (int i = 0; i < kernel_size.size(); i++)
1269                 outShape.push_back(strides[i] * (inputs[0][2 + i] - 1) + kernel_size[i] + adjust_pads[i]);
1270         }
1271         else if (padMode == "SAME")
1272         {
1273             for (int i = 0; i < kernel_size.size(); i++)
1274                 outShape.push_back(strides[i] * (inputs[0][2 + i] - 1) + 1 + adjust_pads[i]);
1275         }
1276         else
1277             CV_Error(Error::StsError, "Unsupported padding mode " + padMode);
1278
1279         CV_Assert(outCn % blobs[0].size[1] == 0);
1280         int ngroups = outCn / blobs[0].size[1];
1281
1282         int inpCn = inputs[0][1];
1283         CV_Assert(inpCn % ngroups == 0 && outCn % ngroups == 0);
1284         CV_Assert(blobs[0].size[0] == inpCn);
1285
1286         outputs.resize(1, outShape);
1287
1288         if (!is1x1())
1289             internals.push_back(computeColRowShape(inputs[0], outputs[0]));
1290
1291         return false;
1292     }
1293
1294     void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
1295     {
1296         BaseConvolutionLayerImpl::finalize(inputs_arr, outputs_arr);
1297
1298         std::vector<Mat> inputs, outputs;
1299         inputs_arr.getMatVector(inputs);
1300         outputs_arr.getMatVector(outputs);
1301
1302         std::vector<int> inpShape;
1303         std::vector<int> outShape;
1304         for (int i = 2; i < inputs[0].dims; i++) {
1305             inpShape.push_back(inputs[0].size[i]);
1306             outShape.push_back(outputs[0].size[i]);
1307         }
1308         getConvPoolPaddings(outShape, kernel_size, strides, padMode, pads_begin, pads_end);
1309         if (pads_begin.size() == 2) {
1310             for (int i = 0; i < pads_begin.size(); i++) {
1311                 if (pads_begin[i] != pads_end[i])
1312                     CV_Error(Error::StsNotImplemented, "Unsupported asymmetric padding in deconvolution layer");
1313             }
1314             pad = Size(pads_begin[1], pads_begin[0]);
1315         }
1316
1317         weightsMultipliers.assign(numOutput, 1.0);
1318         if (weightsMat.empty())
1319         {
1320             transpose(blobs[0].reshape(1, blobs[0].size[0]), weightsMat);
1321             biasesMat = hasBias() ? blobs[1].reshape(1, numOutput)
1322                                   : Mat::zeros(numOutput, 1, CV_32F);
1323         }
1324     }
1325
1326     void fuseWeights(const Mat& w_, const Mat& b_) CV_OVERRIDE
1327     {
1328         Mat w = w_.total() == 1 ? Mat(1, numOutput, CV_32F, Scalar(w_.at<float>(0))) : w_;
1329         Mat b = b_.total() == 1 ? Mat(1, numOutput, CV_32F, Scalar(b_.at<float>(0))) : b_;
1330
1331         CV_Assert_N(!weightsMat.empty(),
1332                      w.empty() || numOutput == w.total(),
1333                      b.empty() || numOutput == b.total());
1334
1335         if (!w.empty())
1336         {
1337             transpose(blobs[0].reshape(1, blobs[0].size[0]), weightsMat);
1338             weightsMat = weightsMat.reshape(1, numOutput);
1339             for (int i = 0; i < numOutput; ++i)
1340             {
1341                 double wi = w.at<float>(i);
1342                 weightsMultipliers[i] *= wi;
1343                 cv::multiply(weightsMat.row(i), weightsMultipliers[i], weightsMat.row(i));
1344                 biasesMat.at<float>(i) *= wi;
1345             }
1346             weightsMat = weightsMat.reshape(1, weightsMat.total() / blobs[0].size[0]);
1347         }
1348
1349         if (!b.empty())
1350         {
1351             cv::add(biasesMat, b.reshape(1, numOutput), biasesMat);
1352         }
1353     }
1354
1355     class MatMulInvoker : public ParallelLoopBody
1356     {
1357     public:
1358         MatMulInvoker(const Mat& a, const Mat& b, Mat& c, int nstripes)
1359         {
1360             a_ = &a;
1361             b_ = &b;
1362             c_ = &c;
1363             nstripes_ = nstripes;
1364             useAVX = checkHardwareSupport(CPU_AVX);
1365             useAVX2 = checkHardwareSupport(CPU_AVX2);
1366             useAVX512 = CV_CPU_HAS_SUPPORT_AVX512_SKX;
1367         }
1368
1369         void operator()(const Range& range_) const CV_OVERRIDE
1370         {
1371             int stripeSize = (int)alignSize((b_->cols + nstripes_ - 1)/nstripes_, 16);
1372             Range range(range_.start*stripeSize, std::min(range_.end*stripeSize, b_->cols));
1373             int mmax = a_->rows;
1374             int nmax = range.end - range.start;
1375             int kmax = a_->cols;
1376             int m, n, k;
1377             const float* aptr = a_->ptr<float>();
1378             const float* bptr = b_->ptr<float>() + range.start;
1379             float* cptr = c_->ptr<float>() + range.start;
1380             size_t astep = a_->step1();
1381             size_t bstep = b_->step1();
1382             size_t cstep = c_->step1();
1383
1384         #if CV_TRY_AVX512_SKX
1385             if( useAVX512 )
1386                 opt_AVX512_SKX::fastGEMM( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
1387             else
1388         #endif
1389         #if CV_TRY_AVX2
1390             if( useAVX2 )
1391                 opt_AVX2::fastGEMM( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
1392             else
1393         #endif
1394         #if CV_TRY_AVX
1395             if( useAVX )
1396                 opt_AVX::fastGEMM( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
1397             else
1398         #endif
1399             for( m = 0; m < mmax; m += 2 )
1400             {
1401                 float* dst0 = cptr + cstep*m;
1402                 float* dst1 = cptr + cstep*std::min(m+1, mmax-1);
1403                 const float* aptr0 = aptr + astep*m;
1404                 const float* aptr1 = aptr + astep*std::min(m+1, mmax-1);
1405
1406                 for( n = 0; n < nmax; n++ )
1407                 {
1408                     dst0[n] = 0.f;
1409                     dst1[n] = 0.f;
1410                 }
1411
1412                 for( k = 0; k < kmax; k += 4 )
1413                 {
1414                     float alpha00 = aptr0[k];
1415                     float alpha01 = aptr1[k];
1416                     float alpha10 = 0.f, alpha11 = 0.f;
1417                     float alpha20 = 0.f, alpha21 = 0.f;
1418                     float alpha30 = 0.f, alpha31 = 0.f;
1419                     const float* bptr0 = bptr + k*bstep;
1420                     const float* bptr1 = bptr0;
1421                     const float* bptr2 = bptr0;
1422                     const float* bptr3 = bptr0;
1423
1424                     if( k+1 < kmax )
1425                     {
1426                         alpha10 = aptr0[k+1];
1427                         alpha11 = aptr1[k+1];
1428                         bptr1 = bptr0 + bstep;
1429                         if( k+2 < kmax )
1430                         {
1431                             alpha20 = aptr0[k+2];
1432                             alpha21 = aptr1[k+2];
1433                             bptr2 = bptr1 + bstep;
1434                             if( k+3 < kmax )
1435                             {
1436                                 alpha30 = aptr0[k+3];
1437                                 alpha31 = aptr1[k+3];
1438                                 bptr3 = bptr2 + bstep;
1439                             }
1440                         }
1441                     }
1442                     n = 0;
1443
1444                 #if CV_SIMD128
1445                     v_float32x4 a00 = v_setall_f32(alpha00);
1446                     v_float32x4 a01 = v_setall_f32(alpha01);
1447                     v_float32x4 a10 = v_setall_f32(alpha10);
1448                     v_float32x4 a11 = v_setall_f32(alpha11);
1449                     v_float32x4 a20 = v_setall_f32(alpha20);
1450                     v_float32x4 a21 = v_setall_f32(alpha21);
1451                     v_float32x4 a30 = v_setall_f32(alpha30);
1452                     v_float32x4 a31 = v_setall_f32(alpha31);
1453
1454                     for( ; n <= nmax - 4; n += 4 )
1455                     {
1456                         v_float32x4 b0 = v_load(bptr0 + n);
1457                         v_float32x4 b1 = v_load(bptr1 + n);
1458                         v_float32x4 b2 = v_load(bptr2 + n);
1459                         v_float32x4 b3 = v_load(bptr3 + n);
1460                         v_float32x4 d0 = v_load(dst0 + n);
1461                         v_float32x4 d1 = v_load(dst1 + n);
1462                         d0 += b0*a00;
1463                         d1 += b0*a01;
1464                         d0 += b1*a10;
1465                         d1 += b1*a11;
1466                         d0 += b2*a20;
1467                         d1 += b2*a21;
1468                         d0 += b3*a30;
1469                         d1 += b3*a31;
1470                         v_store(dst0 + n, d0);
1471                         v_store(dst1 + n, d1);
1472                     }
1473                 #endif
1474
1475                     for( ; n < nmax; n++ )
1476                     {
1477                         float b0 = bptr0[n], b1 = bptr1[n];
1478                         float b2 = bptr2[n], b3 = bptr3[n];
1479                         float d0 = dst0[n] + alpha00*b0 + alpha10*b1 + alpha20*b2 + alpha30*b3;
1480                         float d1 = dst1[n] + alpha01*b0 + alpha11*b1 + alpha21*b2 + alpha31*b3;
1481                         dst0[n] = d0;
1482                         dst1[n] = d1;
1483                     }
1484                 }
1485             }
1486         }
1487
1488         const Mat *a_, *b_;
1489         Mat* c_;
1490         int nstripes_;
1491         bool useAVX;
1492         bool useAVX2;
1493         bool useAVX512;
1494     };
1495
1496     class Col2ImInvoker : public cv::ParallelLoopBody
1497     {
1498     public:
1499         const float* data_col;
1500         const float* biasvec;
1501         int channels, height, width;
1502         int kernel_h, kernel_w;
1503         int pad_h, pad_w;
1504         int stride_h, stride_w;
1505         float* data_im;
1506         int height_col, width_col;
1507         int nstripes;
1508         bool is1x1;
1509
1510         Col2ImInvoker()
1511             : data_col(0), biasvec(0), channels(0), height(0), width(0),
1512               kernel_h(0), kernel_w(0), pad_h(0), pad_w(0), stride_h(0), stride_w(0), data_im(0),
1513               height_col(0), width_col(0), nstripes(0), is1x1(0)
1514         {}
1515
1516         static void run(const float* data_col,
1517                         int channels, int height, int width,
1518                         int kernel_h, int kernel_w,
1519                         int pad_h, int pad_w,
1520                         int stride_h, int stride_w,
1521                         int height_col, int width_col,
1522                         float* data_im,
1523                         const float* biasvec,
1524                         bool is1x1)
1525         {
1526             const int nstripes = getNumThreads();
1527
1528             Col2ImInvoker t;
1529             t.data_col = data_col;
1530             t.data_im = data_im;
1531             t.channels = channels; t.height = height; t.width = width;
1532             t.kernel_h = kernel_h; t.kernel_w = kernel_w;
1533             t.pad_h = pad_h; t.pad_w = pad_w;
1534             t.stride_h = stride_h; t.stride_w = stride_w;
1535             t.height_col = height_col;
1536             t.width_col = width_col;
1537             t.nstripes = nstripes;
1538             t.is1x1 = is1x1;
1539             t.biasvec = biasvec;
1540
1541             parallel_for_(Range(0, nstripes), t, nstripes);
1542         }
1543
1544         virtual void operator ()(const Range &r) const CV_OVERRIDE
1545         {
1546             const float* data_col_ = data_col;
1547             float* data_im_ = data_im;
1548             int coeff_h = (1 - stride_h * kernel_w * height_col) * width_col;
1549             int coeff_w = (1 - stride_w * height_col * width_col);
1550             size_t total = (size_t)channels * height * width;
1551             size_t stripeSize = (total + nstripes - 1)/nstripes;
1552             size_t startIndex = r.start*stripeSize;
1553             size_t endIndex = std::min(r.end*stripeSize, total);
1554             int w = (int)(startIndex % width + pad_w);
1555             int h = (int)((startIndex / width) % height + pad_h);
1556             int c = (int)(startIndex / (width * height));
1557             int h_col_start = (h < kernel_h) ? 0 : (h - kernel_h) / stride_h + 1;
1558             int h_col_end = std::min(h / stride_h + 1, height_col);
1559             int plane_size_col = height_col * width_col;
1560             int offset = (c * kernel_h * kernel_w + h * kernel_w + w) * plane_size_col;
1561             bool is1x1_ = is1x1;
1562             const float* biasvec_ = biasvec;
1563
1564             for (size_t index = startIndex; index < endIndex; index++)
1565             {
1566                 // compute the start and end of the output
1567                 int w_col_start = (w < kernel_w) ? 0 : (w - kernel_w) / stride_w + 1;
1568                 int w_col_end = std::min(w / stride_w + 1, width_col);
1569                 float val;
1570
1571                 if( is1x1_ )
1572                     val = data_im_[index];
1573                 else
1574                 {
1575                     val = 0.f;
1576                     for (int h_col = h_col_start; h_col < h_col_end; ++h_col) {
1577                         for (int w_col = w_col_start; w_col < w_col_end; ++w_col) {
1578                             val += data_col_[offset + h_col * coeff_h + w_col * coeff_w];
1579                         }
1580                     }
1581                 }
1582                 data_im_[index] = val + biasvec_[c];
1583
1584                 offset += plane_size_col;
1585                 if( ++w >= width + pad_w )
1586                 {
1587                     w = (int)((index + 1)% width + pad_w);
1588                     h = (int)(((index + 1) / width) % height + pad_h);
1589                     c = (int)((index + 1) / (width * height));
1590                     h_col_start = (h < kernel_h) ? 0 : (h - kernel_h) / stride_h + 1;
1591                     h_col_end = std::min(h / stride_h + 1, height_col);
1592                     offset = (c * kernel_h * kernel_w + h * kernel_w + w) * plane_size_col;
1593                 }
1594             }
1595         }
1596     };
1597
1598 #ifdef HAVE_OPENCL
1599     bool forward_ocl(InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, OutputArrayOfArrays internals_)
1600     {
1601         std::vector<UMat> inputs;
1602         std::vector<UMat> outputs;
1603         std::vector<UMat> internals;
1604
1605         if (inputs_.depth() == CV_16S)
1606             return false;
1607
1608         inputs_.getUMatVector(inputs);
1609         outputs_.getUMatVector(outputs);
1610         internals_.getUMatVector(internals);
1611
1612         int outCn = numOutput;
1613         int inpCn = inputs[0].size[1];
1614
1615         if (is1x1())
1616             return false;
1617
1618         if (umat_weights.empty())
1619         {
1620             if (fusedWeights)
1621                 weightsMat.copyTo(umat_weights);
1622             else
1623                 transpose(blobs[0].reshape(1, inpCn), umat_weights);
1624
1625             if (fusedBias)
1626                 biasesMat.copyTo(umat_biases);
1627             else
1628             {
1629                 if (hasBias())
1630                     blobs[1].reshape(1, outCn).copyTo(umat_biases);
1631                 else
1632                     umat_biases = UMat::zeros(outCn, 1, CV_32F);
1633             }
1634         }
1635
1636         String buildopt = format("-DT=%s ", ocl::typeToStr(inputs[0].type()));
1637         buildopt += format("-DPAD_H=%d -DPAD_W=%d -DKERNEL_H=%d -DKERNEL_W=%d -DSTRIDE_H=%d -DSTRIDE_W=%d ",
1638                            pad.height, pad.width, kernel.height, kernel.width, stride.height, stride.width);
1639
1640         for (size_t ii = 0; ii < outputs.size(); ii++)
1641         {
1642             int ngroups = outCn / blobs[0].size[1];
1643             int inpGroupCn = inpCn / ngroups;
1644             int outGroupCn = blobs[0].size[1];
1645             const UMat& inp = inputs[ii];
1646             UMat& out = outputs[ii];
1647             int numImg = inp.size[0];
1648             int inpH = inp.size[2], inpW = inp.size[3];
1649             int outH = out.size[2], outW = out.size[3];
1650
1651             MatShape inpshape = shape(numImg*inpCn, inpH*inpW);
1652             MatShape outshape = shape(numImg*outCn, outH*outW);
1653             UMat convBlob = inputs[ii].reshape(1, inpshape.size(), &inpshape[0]);
1654             UMat decnBlob = out.reshape(1, outshape.size(), &outshape[0]);
1655             int rows = internals[0].rows / ngroups;
1656
1657             for (int n = 0; n < numImg; n++)
1658             {
1659                 for (int g = 0; g < ngroups; g++)
1660                 {
1661                     UMat colMat = internals[0].rowRange(_Range(g * rows, rows));
1662                     UMat convMat = convBlob.rowRange(_Range((g + n * ngroups) * inpGroupCn, inpGroupCn));
1663                     UMat wghtMat = umat_weights.colRange(_Range(g * inpGroupCn, inpGroupCn));
1664                     gemm(wghtMat, convMat, 1, noArray(), 0, colMat, 0);
1665                 }
1666
1667                 for (int g = 0; g < ngroups; g++)
1668                 {
1669                     int total = outGroupCn * decnBlob.cols;
1670                     int index = 0;
1671                     int height_col = inpH;
1672                     int width_col = inpW;
1673                     int coeff_h = (1 - stride.height * kernel.width * height_col) * width_col;
1674                     int coeff_w = (1 - stride.width * height_col * width_col);
1675
1676                     ocl::Kernel k("col2im", ocl::dnn::col2im_oclsrc, buildopt);
1677                     k.set(index++, total);
1678                     k.set(index++, ocl::KernelArg::PtrReadOnly(internals[0]));
1679                     k.set(index++, (int)(g * rows * internals[0].cols));
1680                     k.set(index++, outGroupCn);
1681                     k.set(index++, outH);
1682                     k.set(index++, outW);
1683                     k.set(index++, height_col);
1684                     k.set(index++, width_col);
1685                     k.set(index++, coeff_h);
1686                     k.set(index++, coeff_w);
1687                     k.set(index++, ocl::KernelArg::PtrReadOnly(umat_biases));
1688                     k.set(index++, (int)(g * outGroupCn * umat_biases.cols));
1689                     k.set(index++, ocl::KernelArg::PtrWriteOnly(decnBlob));
1690                     k.set(index++, (int)((g + n * ngroups) * outGroupCn * decnBlob.cols));
1691
1692                     size_t global[] = { (size_t)total };
1693                     bool ret = k.run(1, global, NULL, false);
1694                     if (!ret)
1695                         return false;
1696                 }
1697             }
1698         }
1699
1700         return true;
1701     }
1702 #endif
1703
1704     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
1705     {
1706         CV_TRACE_FUNCTION();
1707         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
1708
1709         CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
1710                    forward_ocl(inputs_arr, outputs_arr, internals_arr));
1711
1712         if (inputs_arr.depth() == CV_16S)
1713         {
1714             forward_fallback(inputs_arr, outputs_arr, internals_arr);
1715             return;
1716         }
1717
1718         std::vector<Mat> inputs, outputs, internals;
1719         inputs_arr.getMatVector(inputs);
1720         outputs_arr.getMatVector(outputs);
1721         internals_arr.getMatVector(internals);
1722
1723         int outCn = numOutput;
1724         int inpCn = inputs[0].size[1];
1725         bool is1x1flag = is1x1();
1726         int nstripes = getNumThreads();
1727
1728         if( weightsMat.empty() )
1729         {
1730             transpose(blobs[0].reshape(1, inpCn), weightsMat);
1731             biasesMat = hasBias() ? blobs[1].reshape(1, outCn) : Mat::zeros(outCn, 1, CV_32F);
1732         }
1733
1734         for (size_t ii = 0; ii < outputs.size(); ii++)
1735         {
1736             int ngroups = outCn / blobs[0].size[1];
1737             int inpGroupCn = inpCn / ngroups;
1738             int outGroupCn = blobs[0].size[1];
1739             const Mat& inp = inputs[ii];
1740             Mat& out = outputs[ii];
1741             int numImg = inp.size[0];
1742             int inpH = inp.size[2], inpW = inp.size[3];
1743             int outH = out.size[2], outW = out.size[3];
1744
1745             Mat convBlob = inputs[ii].reshape(1, numImg*inpCn);
1746             Mat decnBlob = out.reshape(1, numImg*outCn);
1747
1748             for (int n = 0; n < numImg; n++)
1749             {
1750                 for (int g = 0; g < ngroups; g++)
1751                 {
1752                     Mat dstMat = decnBlob.rowRange(_Range((g + n * ngroups) * outGroupCn, outGroupCn));
1753                     Mat &colMat = is1x1flag ? dstMat : internals[0];
1754
1755                     Mat convMat = convBlob.rowRange(_Range((g + n * ngroups) * inpGroupCn, inpGroupCn));
1756                     Mat wghtMat = weightsMat.colRange(_Range(g * inpGroupCn, inpGroupCn));
1757                     Mat curBiasMat = biasesMat.rowRange(_Range(g * outGroupCn, outGroupCn));
1758
1759                     //gemm(wghtMat, convMat, 1, colMat, 0, colMat, 0);
1760                     MatMulInvoker mminvoker(wghtMat, convMat, colMat, nstripes);
1761                     parallel_for_(Range(0, nstripes), mminvoker, nstripes);
1762
1763                     Col2ImInvoker::run(colMat.ptr<float>(), outGroupCn, outH, outW,
1764                                        kernel.height, kernel.width, pad.height, pad.width,
1765                                        stride.height, stride.width, inpH, inpW, dstMat.ptr<float>(),
1766                                        curBiasMat.ptr<float>(), is1x1flag);
1767                 }
1768             }
1769         }
1770     }
1771
1772     virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
1773     {
1774 #ifdef HAVE_HALIDE
1775         Halide::Buffer<float> inputBuffer = halideBuffer(inputs[0]);
1776
1777         int inW, inH, inC, inN;
1778         getCanonicalSize(inputBuffer, &inW, &inH, &inC, &inN);
1779         const int outGroupCn = blobs[0].size[1];
1780         const int group = numOutput / outGroupCn;
1781         const int inpGroupCn = blobs[0].size[0] / group;
1782
1783         Halide::Var x("x"), y("y"), c("c"), n("n");
1784         Halide::Func top = (name.empty() ? Halide::Func() : Halide::Func(name));
1785         Halide::Func padded_input(name + "_constant_exterior");
1786         auto weights = wrapToHalideBuffer(blobs[0]);
1787
1788         Halide::Func dilated_input("dilated_input");
1789         dilated_input(x, y, c, n) = 0.0f;
1790         Halide::RDom r1(0, inW, 0, inH);
1791         dilated_input(r1.x * stride.width, r1.y * stride.height, c, n) =
1792               inputBuffer(r1.x, r1.y, c, n);
1793         dilated_input.compute_root();
1794
1795         Halide::Func bounded =
1796             Halide::BoundaryConditions::constant_exterior(dilated_input, 0,
1797                                                           0, (inW - 1) * stride.width + 1,
1798                                                           0, (inH - 1) * stride.height + 1,
1799                                                           0, inC, 0, inN);
1800         padded_input(x, y, c, n) = bounded(x, y, c, n);
1801
1802         Halide::RDom r(0, kernel.width, 0, kernel.height, 0, inpGroupCn);
1803         Halide::Expr kx = x + pad.width - r.x;
1804         Halide::Expr ky = y + pad.height - r.y;
1805         Halide::Expr kInC = r.z;
1806         Halide::Expr kOutC = c;
1807         for (int i = 1; i < group; ++i)
1808         {
1809             kInC = select(c < outGroupCn * i, kInC, inpGroupCn * i + r.z);
1810             kOutC = select(c < outGroupCn * i, kOutC, c - outGroupCn * i);
1811         }
1812         Halide::Expr topExpr = sum(padded_input(kx, ky, kInC, n) *
1813                                    weights(r.x, r.y, kOutC, kInC));
1814         if (hasBias())
1815         {
1816             auto bias = wrapToHalideBuffer(blobs[1], {numOutput});
1817             topExpr += bias(c);
1818         }
1819         top(x, y, c, n) = topExpr;
1820         return Ptr<BackendNode>(new HalideBackendNode({ padded_input, top }));
1821 #endif  // HAVE_HALIDE
1822         return Ptr<BackendNode>();
1823     }
1824
1825 #ifdef HAVE_INF_ENGINE
1826     virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> > &) CV_OVERRIDE
1827     {
1828         auto ieWeights = wrapToInfEngineBlob(blobs[0], InferenceEngine::Layout::OIHW);
1829         if (fusedWeights)
1830         {
1831             ieWeights = InferenceEngine::make_shared_blob<float>(
1832                                 InferenceEngine::Precision::FP32, InferenceEngine::Layout::OIHW,
1833                                 ieWeights->dims());
1834             ieWeights->allocate();
1835
1836             int inpCn = blobs[0].size[0];
1837             Mat newWeights = infEngineBlobToMat(ieWeights).reshape(1, inpCn);
1838             transpose(weightsMat, newWeights);
1839         }
1840
1841         const int outGroupCn = blobs[0].size[1];  // Weights are in IOHW layout
1842         const int group = numOutput / outGroupCn;
1843
1844         InferenceEngine::Builder::DeconvolutionLayer ieLayer(name);
1845
1846         ieLayer.setKernel(kernel_size);
1847         ieLayer.setStrides(strides);
1848         ieLayer.setDilation(dilations);
1849         ieLayer.setPaddingsBegin(pads_begin);
1850
1851         if (padMode.empty())
1852         {
1853             ieLayer.setPaddingsEnd({pads_end[0] - adjust_pads[0], pads_end[1] - adjust_pads[1]});
1854         }
1855         else if (padMode == "SAME")
1856         {
1857             ieLayer.setPaddingsEnd({kernel_size[0] - pads_begin[0] - 1 - adjust_pads[0],
1858                                     kernel_size[1] - pads_begin[1] - 1 - adjust_pads[1]});
1859         }
1860         ieLayer.setGroup((size_t)group);
1861         ieLayer.setOutDepth((size_t)numOutput);
1862
1863         InferenceEngine::Builder::Layer l = ieLayer;
1864         addConstantData("weights", ieWeights, l);
1865         if (hasBias())
1866             addConstantData("biases", wrapToInfEngineBlob(biasesMat, {(size_t)numOutput}, InferenceEngine::Layout::C), l);
1867         return Ptr<BackendNode>(new InfEngineBackendNode(l));
1868     }
1869 #endif  // HAVE_INF_ENGINE
1870
1871     virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
1872                            const std::vector<MatShape> &outputs) const CV_OVERRIDE
1873     {
1874         CV_Assert(inputs.size() == outputs.size());
1875
1876         float flops = 0;
1877         int outChannels = blobs[0].size[0];
1878
1879         for (int i = 0; i < inputs.size(); i++)
1880         {
1881             flops += CV_BIG_INT(2)*outChannels*kernel.area()*total(inputs[i]);
1882         }
1883
1884         return flops;
1885     }
1886 };
1887
1888 Ptr<BaseConvolutionLayer> ConvolutionLayer::create(const LayerParams &params)
1889 {
1890     Ptr<ConvolutionLayerImpl> l(new ConvolutionLayerImpl(params));
1891     return l;
1892 }
1893
1894 Ptr<BaseConvolutionLayer> DeconvolutionLayer::create(const LayerParams &params)
1895 {
1896     return Ptr<BaseConvolutionLayer>(new DeConvolutionLayerImpl(params));
1897 }
1898
1899 }
1900 }