1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
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"
53 #include "opencl_kernels_dnn.hpp"
54 using namespace cv::dnn::ocl4dnn;
62 class BaseConvolutionLayerImpl : public ConvolutionLayer
65 bool fusedWeights, fusedBias;
66 std::vector<double> weightsMultipliers;
67 BaseConvolutionLayerImpl(const LayerParams ¶ms)
69 setParamsFrom(params);
70 getConvolutionKernelParams(params, kernel_size, pads_begin, pads_end, strides, dilations, padMode);
72 numOutput = params.get<int>("num_output");
73 int ngroups = params.get<int>("group", 1);
74 CV_Assert(numOutput % ngroups == 0);
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");
83 pad = Size(pads_begin[1], pads_begin[0]);
84 dilation = Size(dilations[1], dilations[0]);
86 adjust_pads.push_back(params.get<int>("adj_h", 0));
87 adjust_pads.push_back(params.get<int>("adj_w", 0));
89 adjustPad.height = adjust_pads[0];
90 adjustPad.width = adjust_pads[1];
91 CV_Assert(adjustPad.width < stride.width &&
92 adjustPad.height < stride.height);
98 virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
100 std::vector<Mat> inputs, outputs;
101 inputs_arr.getMatVector(inputs);
102 outputs_arr.getMatVector(outputs);
104 CV_Assert(inputs.size() > 0);
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]);
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++)
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]);
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]);
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");
136 pad = Size(pads_begin[1], pads_begin[0]);
138 fusedWeights = false;
144 return blobs.size() >= 2;
147 virtual MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const = 0;
150 return (kernel.height == 1 && kernel.width == 1) &&
151 (stride.height == 1 && stride.width == 1) &&
152 (dilation.height == 1 && dilation.width == 1);
155 virtual bool tryFuse(Ptr<Layer>& top) CV_OVERRIDE
158 top->getScaleShift(w, b);
159 if (!w.empty() || !b.empty())
162 fusedWeights = fusedWeights || !w.empty();
163 fusedBias = fusedBias || (hasBias() && !w.empty()) || !b.empty();
169 virtual void fuseWeights(const Mat& w_, const Mat& b_) = 0;
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
177 if (targetId != DNN_TARGET_CPU)
179 Layer::applyHalideScheduler(node, inputs, outputs, targetId);
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];
186 int outW, outH, outC, outN;
187 getCanonicalSize(outputs[0].size, &outW, &outH, &outC, &outN);
189 if (outW == 1 || outH <= 2)
192 if (is1x1() || outC <= 16)
198 .vectorize(x, outW >= 16 ? 16 : outW);
202 .split(c, co, ci, 16)
203 .fuse(yo, co, tile).fuse(n, tile, tile)
206 .vectorize(x, outW >= 16 ? 16 : outW);
207 padded_input.compute_at(top, yi);
208 #endif // HAVE_HALIDE
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
219 enum { VEC_ALIGN = 8, DFT_TYPE = CV_32F };
221 std::vector<float> biasvec;
222 std::vector<float> reluslope;
223 Ptr<ActivationLayer> activ;
226 Ptr<OCL4DNNConvSpatial<float> > convolutionOp;
227 std::vector<UMat> umat_blobs;
229 ocl4dnnFusedActiv_t activType;
232 ConvolutionLayerImpl(const LayerParams ¶ms) : BaseConvolutionLayerImpl(params)
236 activType = OCL4DNN_CONV_FUSED_ACTIV_NONE;
241 MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const CV_OVERRIDE
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);
249 virtual bool supportBackend(int backendId) CV_OVERRIDE
251 #ifdef HAVE_INF_ENGINE
252 if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
254 if (kernel_size.size() == 3)
255 return preferableTarget == DNN_TARGET_CPU;
256 return (preferableTarget != DNN_TARGET_MYRIAD || dilation.width == dilation.height);
261 if (kernel_size.size() != 2)
263 return backendId == DNN_BACKEND_OPENCV ||
264 backendId == DNN_BACKEND_HALIDE ||
265 (backendId == DNN_BACKEND_VKCOM && haveVulkan());
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
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);
280 CV_Assert(inputs.size() != 0);
281 std::vector<int> inpShape(inputs[0].begin() + 2, inputs[0].end());
283 int outCn = blobs[0].size[0];
284 std::vector<int> outShape;
285 outShape.push_back(inputs[0][0]);
286 outShape.push_back(outCn);
288 int inpCn = inputs[0][1];
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);
296 getConvPoolOutParams(inpShape, kernel_size, strides, padMode, dilations, outShape);
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);
305 outputs.resize(1, outShape);
310 virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
312 BaseConvolutionLayerImpl::finalize(inputs_arr, outputs_arr);
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 )
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);
330 weightsMultipliers.assign(outCn, 1.0);
332 Mat biasMat = hasBias() ? blobs[1].reshape(1, outCn) : Mat();
333 biasvec.resize(outCn+2);
334 if( biasMat.empty() )
336 for(int i = 0; i < outCn; i++ )
341 for(int i = 0; i < outCn; i++ )
342 biasvec[i] = biasMat.at<float>(i);
345 convolutionOp.release();
349 bool setActivation(const Ptr<ActivationLayer>& layer) CV_OVERRIDE
351 if (!activ.empty() && !layer.empty())
359 activType = OCL4DNN_CONV_FUSED_ACTIV_NONE;
361 if (IS_DNN_OPENCL_TARGET(preferableTarget))
363 Ptr<PowerLayer> activ_power = activ.dynamicCast<PowerLayer>();
364 if (!activ_power.empty())
366 if (activ_power->scale != 1.f || activ_power->shift != 0.f)
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)));
373 power = activ_power->power;
374 activType = OCL4DNN_CONV_FUSED_ACTIV_POWER;
376 Ptr<TanHLayer> activ_tanh = activ.dynamicCast<TanHLayer>();
377 if (!activ_tanh.empty())
379 activType = OCL4DNN_CONV_FUSED_ACTIV_TANH;
383 return !activ.empty();
386 void fuseWeights(const Mat& w_, const Mat& b_) CV_OVERRIDE
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());
399 // Keep origin weights unchanged.
400 if (weightsMat.data == blobs[0].data)
401 weightsMat = weightsMat.clone();
403 Mat originWeights = blobs[0].reshape(1, outCn);
404 for (int i = 0; i < outCn; ++i)
406 double wi = w.at<float>(i);
407 weightsMultipliers[i] *= wi;
408 cv::multiply(originWeights.row(i), weightsMultipliers[i], weightsMat.row(i));
415 for (int i = 0; i < outCn; ++i)
416 biasvec[i] += b.at<float>(i);
418 biasvec[outCn] = biasvec[outCn+1] = biasvec[outCn-1];
421 virtual Ptr<BackendNode> initVkCom(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
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};
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];
435 // TODO: support group > 1
437 return Ptr<BackendNode>();
442 padding_mode = vkcom::kPaddingModeCaffe;
444 else if (padMode == "VALID")
446 padding_mode = vkcom::kPaddingModeValid;
448 else if (padMode == "SAME")
450 padding_mode = vkcom::kPaddingModeSame;
453 CV_Error(Error::StsError, "Unsupported padding mode " + padMode);
455 std::shared_ptr<vkcom::OpBase> op(new vkcom::OpConv(out_channel, has_bias,
456 filter_size, pad_size,
457 stride_size, dilation_size,
461 std::vector<Ptr<BackendWrapper> > blobsWrapper;
463 if (newWeightAndBias)
466 weightsMat.copyTo(wm); // to handle the case of isContinuous() == false
467 wm.reshape(1, blobs[0].dims, blobs[0].size);
468 blobsWrapper.push_back(Ptr<BackendWrapper>(new VkComBackendWrapper(wm)));
472 blobsWrapper.push_back(Ptr<BackendWrapper>(new VkComBackendWrapper(blobs[0])));
477 Mat biasesMat({out_channel}, CV_32F, &biasvec[0]);
478 blobsWrapper.push_back(Ptr<BackendWrapper>(new VkComBackendWrapper(biasesMat)));
481 return Ptr<BackendNode>(new VkComBackendNode(inputs, op, blobsWrapper));
482 #endif // HAVE_VULKAN
483 return Ptr<BackendNode>();
488 virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
491 Halide::Buffer<float> inputBuffer = halideBuffer(inputs[0]);
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;
499 Halide::Buffer<float> weights = wrapToHalideBuffer(blobs[0]);
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)
506 Halide::Func bounded =
507 Halide::BoundaryConditions::constant_exterior(inputBuffer, 0);
508 padded_input(x, y, c, n) = bounded(x, y, c, n);
512 padded_input(x, y, c, n) = inputBuffer(x, y, c, n);
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)
521 kc = select(c < outGroupCn * i, kc, inpGroupCn * i + r.z);
523 Halide::Expr topExpr = sum(padded_input(kx, ky, kc, n) *
524 weights(r.x, r.y, r.z, c));
527 Halide::Buffer<float> bias = wrapToHalideBuffer(blobs[1], {outCn});
530 top(x, y, c, n) = topExpr;
531 return Ptr<BackendNode>(new HalideBackendNode({ padded_input, top }));
532 #endif // HAVE_HALIDE
533 return Ptr<BackendNode>();
536 #ifdef HAVE_INF_ENGINE
537 virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
539 InferenceEngine::DataPtr input = infEngineDataNode(inputs[0]);
540 CV_Assert(input->dims.size() == 4 || input->dims.size() == 5);
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;
547 InferenceEngine::Layout layout = (input->dims.size() == 4) ? InferenceEngine::Layout::OIHW :
548 InferenceEngine::Layout::NCDHW;
550 auto ieWeights = wrapToInfEngineBlob(blobs[0], layout);
553 if (weightsMat.isContinuous())
555 Mat cvWeights = weightsMat.reshape(1, blobs[0].dims, blobs[0].size);
556 ieWeights = wrapToInfEngineBlob(cvWeights, layout);
560 ieWeights = InferenceEngine::make_shared_blob<float>(
561 InferenceEngine::Precision::FP32, layout,
563 ieWeights->allocate();
565 Mat newWeights = infEngineBlobToMat(ieWeights).reshape(1, outCn);
566 Mat cvWeights = weightsMat.colRange(0, newWeights.cols);
567 cvWeights.copyTo(newWeights);
570 InferenceEngine::Blob::Ptr ieBiases;
571 if (hasBias() || fusedBias)
573 Mat biasesMat({outCn}, CV_32F, &biasvec[0]);
574 ieBiases = wrapToInfEngineBlob(biasesMat, {(size_t)outCn}, InferenceEngine::Layout::C);
577 InferenceEngine::Builder::ConvolutionLayer ieLayer(name);
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);
587 InferenceEngine::Builder::Layer l = ieLayer;
588 addConstantData("weights", ieWeights, l);
590 addConstantData("biases", ieBiases, l);
592 if (!padMode.empty())
593 l.getParameters()["auto_pad"] = padMode == "VALID" ? std::string("valid") : std::string("same_upper");
595 return Ptr<BackendNode>(new InfEngineBackendNode(l));
597 #endif // HAVE_INF_ENGINE
599 class ParallelConv : public cv::ParallelLoopBody
602 enum { BLK_SIZE = 32, BLK_SIZE_CN = 64 };
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_;
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)
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 )
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);
644 p.weights_ = &weights;
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;
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;
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];
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;
669 p.biasvec_ = &biasvec;
670 p.reluslope_ = &reluslope;
671 p.activ_ = p.reluslope_->empty() ? activ : 0;
673 parallel_for_(Range(0, nstripes), p, nstripes);
676 virtual void operator ()(const Range &r0) const CV_OVERRIDE
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;
689 size_t inpPlaneSize = width*height;
690 size_t outPlaneSize = outW*outH;
693 int stripesPerSample;
697 if( nstripes >= batchSize*2 )
699 stripesPerSample = nstripes/batchSize;
700 stripeSize = alignSize((outPlaneSize + stripesPerSample - 1)/stripesPerSample, valign);
701 stripeSize = std::min(stripeSize, outPlaneSize);
705 stripesPerSample = 1;
706 int samplesPerStripe = std::max((batchSize + nstripes - 1)/nstripes, 1);
707 r.start *= samplesPerStripe;
708 r.end *= samplesPerStripe;
709 stripeSize = outPlaneSize;
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)));
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]) );
734 for( int stripe = r.start; stripe < r.end; stripe++ )
736 int subsampleIdx = stripe/stripesPerSample;
737 if( subsampleIdx >= batchSize )
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;
747 for( int cn0 = 0; cn0 < inpCn; cn0 += BLK_SIZE_CN )
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;
756 for( int ofs0 = stripeStart; ofs0 < stripeEnd; ofs0 += BLK_SIZE )
758 int ofs, ofs1 = std::min(ofs0 + BLK_SIZE, stripeEnd);
759 int out_i = ofs0 / outW;
760 int out_j = ofs0 - out_i * outW;
762 // do im2row for a part of input tensor
763 float* rowbuf = rowbuf0;
764 for( ofs = ofs0; ofs < ofs1; out_j = 0, ++out_i )
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;
773 // do im2row for a part of input tensor
776 for( ; out_j < out_j1; out_j++, rowbuf += vsz_a, imgptr += stride_w )
778 for( k = 0; k < vsz; k++ )
779 rowbuf[k] = imgptr[k*inpPlaneSize];
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);
788 for( ; out_j < out_j1; out_j++, rowbuf += vsz_a, imgptr += stride_w, in_j += stride_w )
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 )
794 for( k = 0; k < vsz; k++ )
797 float v0 = imgptr[k1];
798 float v1 = imgptr[k1 + stride_w];
800 rowbuf[k+vsz_a] = v1;
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);
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++ )
819 for( i = i0; i < i1; i++ )
821 for( j = j0; j < j1; j++ )
823 int imgofs = k*(width*height) + i*(dilation_h*width) + j*dilation_w;
824 rowbuf[(k*kernel_h + i)*kernel_w + j] = imgptr[imgofs];
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 */
839 opt_AVX512_SKX::fastConv(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
840 outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
845 opt_AVX2::fastConv(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
846 outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
851 opt_AVX::fastConv(wptr, wstep, biasptr, rowbuf0, data_out0 + ofs0,
852 outShape, bsz, vsz, vsz_a, relu, cn0 == 0);
855 for( int i = 0; i < outCn; i += 2 )
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;
873 r0 = relu[i]; r1 = relu[i+1];
880 v_float32x4 vr0 = v_setall_f32(r0), vr1 = v_setall_f32(r1), z = v_setzero_f32();
882 for( ; j <= bsz - 4; j += 4 )
884 const float* rptr = rowbuf0 + j*vsz_a;
889 s0 = v_setall_f32(bias0);
890 s1 = v_setall_f32(bias1);
894 s0 = v_load(outptr0 + j);
895 s1 = v_load(outptr1 + j);
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 )
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);
918 s0 += v_reduce_sum4(vs00, vs01, vs02, vs03);
919 s1 += v_reduce_sum4(vs10, vs11, vs12, vs13);
922 s0 = v_select(s0 > z, s0, s0*vr0);
923 s1 = v_select(s1 > z, s1, s1*vr1);
926 v_store(outptr0 + j, s0);
927 v_store(outptr1 + j, s1);
930 for( ; j < bsz; j++ )
932 const float* rptr = rowbuf0 + j*vsz_a;
946 for( k = 0; k < vsz; k++ )
954 s00 = s00 > 0.f ? s00 : s00*r0;
955 s10 = s10 > 0.f ? s10 : s10*r1;
966 activ_->forwardSlice(data_out0 + stripeStart, data_out0 + stripeStart,
967 (int)(stripeEnd - stripeStart),
968 outPlaneSize, startOutCn, startOutCn + outCn);
974 bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
976 std::vector<UMat> inputs;
977 std::vector<UMat> outputs;
979 bool use_half = (inps.depth() == CV_16S);
980 inps.getUMatVector(inputs);
981 outs.getUMatVector(outputs);
983 CV_Assert(outputs.size() == 1);
984 for (int i = 0; i < inputs.size(); ++i)
985 CV_Assert(inputs[i].u != outputs[0].u);
987 if (umat_blobs.empty())
989 size_t n = blobs.size();
990 umat_blobs.resize(n);
991 for (size_t i = 0; i < n; i++)
993 blobs[i].copyTo(umat_blobs[i]);
997 if (convolutionOp.empty())
999 OCL4DNNConvConfig config;
1000 config.in_shape = shape(inputs[0]);
1001 config.out_shape = shape(outputs[0]);
1002 config.kernel = kernel;
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;
1010 convolutionOp = Ptr<OCL4DNNConvSpatial<float> >(new OCL4DNNConvSpatial<float>(config));
1013 int outCn = umat_blobs[0].size[0];
1018 Ptr<ReLULayer> activ_relu = activ.dynamicCast<ReLULayer>();
1019 if( !activ_relu.empty() )
1021 reluslope.assign(outCn+2, activ_relu->negativeSlope);
1022 activType = OCL4DNN_CONV_FUSED_ACTIV_RELU;
1025 Ptr<ReLU6Layer> activ_relu6 = activ.dynamicCast<ReLU6Layer>();
1026 if( !activ_relu6.empty() )
1028 reluslope.resize(2);
1029 reluslope[0] = activ_relu6->minValue;
1030 reluslope[1] = activ_relu6->maxValue;
1031 activType = OCL4DNN_CONV_FUSED_ACTIV_RELU6;
1034 Ptr<ChannelsPReLULayer> activ_chprelu = activ.dynamicCast<ChannelsPReLULayer>();
1035 if( !activ_chprelu.empty() )
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;
1049 weightsMat.copyTo(umat_blobs[0]);
1050 fusedWeights = false;
1054 if ( umat_blobs.size() < 2 )
1055 umat_blobs.resize(2);
1056 umat_blobs[1] = UMat(biasvec, true);
1057 convolutionOp->setBias(true);
1063 if ( activType == OCL4DNN_CONV_FUSED_ACTIV_RELU )
1065 CV_Assert(!reluslope.empty());
1066 convolutionOp->setActivReLU(true, reluslope[0]);
1068 else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_PRELU)
1070 CV_Assert(!reluslope.empty());
1071 convolutionOp->setActivPReLU(true, reluslope);
1073 else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_POWER)
1075 convolutionOp->setActivPower(true, power);
1077 else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_TANH)
1079 convolutionOp->setActivTanh(true);
1081 else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_RELU6)
1083 convolutionOp->setActivReLU6(true, reluslope[0], reluslope[1]);
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);
1096 UMat& inpMat = inputs[0];
1097 UMat& outMat = outputs[0];
1098 int batch_size = inpMat.size[0];
1100 return convolutionOp->Forward(inpMat,
1101 inputs.size() == 2 ? inputs[1] : UMat(),
1103 umat_blobs.size() > 1 ? umat_blobs[1] : UMat(),
1109 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
1111 CV_TRACE_FUNCTION();
1112 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
1114 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
1115 forward_ocl(inputs_arr, outputs_arr, internals_arr))
1117 if (inputs_arr.depth() == CV_16S)
1119 forward_fallback(inputs_arr, outputs_arr, internals_arr);
1123 std::vector<Mat> inputs, outputs;
1124 inputs_arr.getMatVector(inputs);
1125 outputs_arr.getMatVector(outputs);
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);
1134 if (inputs[0].dims == 5) {
1135 CV_Error(Error::StsNotImplemented, "Convolution3D layer is not supported on OCV backend");
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];
1145 Ptr<ReLULayer> activ_relu = activ.dynamicCast<ReLULayer>();
1146 if( !activ_relu.empty() )
1148 reluslope.assign(outCn+2, activ_relu->negativeSlope);
1151 Ptr<ChannelsPReLULayer> activ_chprelu = activ.dynamicCast<ChannelsPReLULayer>();
1152 if( !activ_chprelu.empty() )
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];
1163 int nstripes = std::max(getNumThreads(), 1);
1165 ParallelConv::run(inputs[0], outputs[0], weightsMat, biasvec, reluslope,
1166 kernel, pad, stride, dilation, activ.get(), ngroups, nstripes);
1169 virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
1170 const std::vector<MatShape> &outputs) const CV_OVERRIDE
1172 CV_Assert(inputs.size() == outputs.size());
1175 for (int i = 0; i < inputs.size(); i++)
1177 flops += total(outputs[i])*(CV_BIG_INT(2)*kernel.area()*inputs[i][1] + 1);
1184 class DeConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
1187 Mat weightsMat, biasesMat;
1191 DeConvolutionLayerImpl(const LayerParams& params) : BaseConvolutionLayerImpl(params) {}
1193 MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const CV_OVERRIDE
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);
1205 virtual bool supportBackend(int backendId) CV_OVERRIDE
1207 #ifdef HAVE_INF_ENGINE
1208 const int outGroupCn = blobs[0].size[1]; // Weights are in IOHW layout
1209 const int group = numOutput / outGroupCn;
1211 if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
1213 if (kernel_size.size() == 3)
1214 CV_Error(Error::StsNotImplemented, "Unsupported deconvolution3D layer");
1216 if (adjustPad.height || adjustPad.width)
1218 if (padMode.empty())
1220 if (preferableTarget != DNN_TARGET_CPU && group != 1)
1222 if ((adjustPad.height && pad.height) || (adjustPad.width && pad.width))
1225 return pad.width >= adjustPad.width && pad.height >= adjustPad.height;
1227 else if (padMode == "SAME")
1229 return kernel.width >= pad.width + 1 + adjustPad.width &&
1230 kernel.height >= pad.height + 1 + adjustPad.height;
1232 else if (padMode == "VALID")
1238 return preferableTarget == DNN_TARGET_CPU;
1240 if (preferableTarget == DNN_TARGET_OPENCL || preferableTarget == DNN_TARGET_OPENCL_FP16)
1241 return dilation.width == 1 && dilation.height == 1;
1245 #endif // HAVE_INF_ENGINE
1246 return kernel_size.size() == 2 && (backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_HALIDE);
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
1254 CV_Assert(!hasBias() || blobs[1].total() == (size_t)numOutput);
1255 CV_Assert(inputs.size() != 0);
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())
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]);
1266 else if (padMode == "VALID")
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]);
1271 else if (padMode == "SAME")
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]);
1277 CV_Error(Error::StsError, "Unsupported padding mode " + padMode);
1279 CV_Assert(outCn % blobs[0].size[1] == 0);
1280 int ngroups = outCn / blobs[0].size[1];
1282 int inpCn = inputs[0][1];
1283 CV_Assert(inpCn % ngroups == 0 && outCn % ngroups == 0);
1284 CV_Assert(blobs[0].size[0] == inpCn);
1286 outputs.resize(1, outShape);
1289 internals.push_back(computeColRowShape(inputs[0], outputs[0]));
1294 void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
1296 BaseConvolutionLayerImpl::finalize(inputs_arr, outputs_arr);
1298 std::vector<Mat> inputs, outputs;
1299 inputs_arr.getMatVector(inputs);
1300 outputs_arr.getMatVector(outputs);
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]);
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");
1314 pad = Size(pads_begin[1], pads_begin[0]);
1317 weightsMultipliers.assign(numOutput, 1.0);
1318 if (weightsMat.empty())
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);
1326 void fuseWeights(const Mat& w_, const Mat& b_) CV_OVERRIDE
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_;
1331 CV_Assert_N(!weightsMat.empty(),
1332 w.empty() || numOutput == w.total(),
1333 b.empty() || numOutput == b.total());
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)
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;
1346 weightsMat = weightsMat.reshape(1, weightsMat.total() / blobs[0].size[0]);
1351 cv::add(biasesMat, b.reshape(1, numOutput), biasesMat);
1355 class MatMulInvoker : public ParallelLoopBody
1358 MatMulInvoker(const Mat& a, const Mat& b, Mat& c, int nstripes)
1363 nstripes_ = nstripes;
1364 useAVX = checkHardwareSupport(CPU_AVX);
1365 useAVX2 = checkHardwareSupport(CPU_AVX2);
1366 useAVX512 = CV_CPU_HAS_SUPPORT_AVX512_SKX;
1369 void operator()(const Range& range_) const CV_OVERRIDE
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;
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();
1384 #if CV_TRY_AVX512_SKX
1386 opt_AVX512_SKX::fastGEMM( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
1391 opt_AVX2::fastGEMM( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
1396 opt_AVX::fastGEMM( aptr, astep, bptr, bstep, cptr, cstep, mmax, kmax, nmax );
1399 for( m = 0; m < mmax; m += 2 )
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);
1406 for( n = 0; n < nmax; n++ )
1412 for( k = 0; k < kmax; k += 4 )
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;
1426 alpha10 = aptr0[k+1];
1427 alpha11 = aptr1[k+1];
1428 bptr1 = bptr0 + bstep;
1431 alpha20 = aptr0[k+2];
1432 alpha21 = aptr1[k+2];
1433 bptr2 = bptr1 + bstep;
1436 alpha30 = aptr0[k+3];
1437 alpha31 = aptr1[k+3];
1438 bptr3 = bptr2 + bstep;
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);
1454 for( ; n <= nmax - 4; n += 4 )
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);
1470 v_store(dst0 + n, d0);
1471 v_store(dst1 + n, d1);
1475 for( ; n < nmax; n++ )
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;
1496 class Col2ImInvoker : public cv::ParallelLoopBody
1499 const float* data_col;
1500 const float* biasvec;
1501 int channels, height, width;
1502 int kernel_h, kernel_w;
1504 int stride_h, stride_w;
1506 int height_col, width_col;
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)
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,
1523 const float* biasvec,
1526 const int nstripes = getNumThreads();
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;
1539 t.biasvec = biasvec;
1541 parallel_for_(Range(0, nstripes), t, nstripes);
1544 virtual void operator ()(const Range &r) const CV_OVERRIDE
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;
1564 for (size_t index = startIndex; index < endIndex; index++)
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);
1572 val = data_im_[index];
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];
1582 data_im_[index] = val + biasvec_[c];
1584 offset += plane_size_col;
1585 if( ++w >= width + pad_w )
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;
1599 bool forward_ocl(InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, OutputArrayOfArrays internals_)
1601 std::vector<UMat> inputs;
1602 std::vector<UMat> outputs;
1603 std::vector<UMat> internals;
1605 if (inputs_.depth() == CV_16S)
1608 inputs_.getUMatVector(inputs);
1609 outputs_.getUMatVector(outputs);
1610 internals_.getUMatVector(internals);
1612 int outCn = numOutput;
1613 int inpCn = inputs[0].size[1];
1618 if (umat_weights.empty())
1621 weightsMat.copyTo(umat_weights);
1623 transpose(blobs[0].reshape(1, inpCn), umat_weights);
1626 biasesMat.copyTo(umat_biases);
1630 blobs[1].reshape(1, outCn).copyTo(umat_biases);
1632 umat_biases = UMat::zeros(outCn, 1, CV_32F);
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);
1640 for (size_t ii = 0; ii < outputs.size(); ii++)
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];
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;
1657 for (int n = 0; n < numImg; n++)
1659 for (int g = 0; g < ngroups; g++)
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);
1667 for (int g = 0; g < ngroups; g++)
1669 int total = outGroupCn * decnBlob.cols;
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);
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));
1692 size_t global[] = { (size_t)total };
1693 bool ret = k.run(1, global, NULL, false);
1704 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
1706 CV_TRACE_FUNCTION();
1707 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
1709 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
1710 forward_ocl(inputs_arr, outputs_arr, internals_arr));
1712 if (inputs_arr.depth() == CV_16S)
1714 forward_fallback(inputs_arr, outputs_arr, internals_arr);
1718 std::vector<Mat> inputs, outputs, internals;
1719 inputs_arr.getMatVector(inputs);
1720 outputs_arr.getMatVector(outputs);
1721 internals_arr.getMatVector(internals);
1723 int outCn = numOutput;
1724 int inpCn = inputs[0].size[1];
1725 bool is1x1flag = is1x1();
1726 int nstripes = getNumThreads();
1728 if( weightsMat.empty() )
1730 transpose(blobs[0].reshape(1, inpCn), weightsMat);
1731 biasesMat = hasBias() ? blobs[1].reshape(1, outCn) : Mat::zeros(outCn, 1, CV_32F);
1734 for (size_t ii = 0; ii < outputs.size(); ii++)
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];
1745 Mat convBlob = inputs[ii].reshape(1, numImg*inpCn);
1746 Mat decnBlob = out.reshape(1, numImg*outCn);
1748 for (int n = 0; n < numImg; n++)
1750 for (int g = 0; g < ngroups; g++)
1752 Mat dstMat = decnBlob.rowRange(_Range((g + n * ngroups) * outGroupCn, outGroupCn));
1753 Mat &colMat = is1x1flag ? dstMat : internals[0];
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));
1759 //gemm(wghtMat, convMat, 1, colMat, 0, colMat, 0);
1760 MatMulInvoker mminvoker(wghtMat, convMat, colMat, nstripes);
1761 parallel_for_(Range(0, nstripes), mminvoker, nstripes);
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);
1772 virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
1775 Halide::Buffer<float> inputBuffer = halideBuffer(inputs[0]);
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;
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]);
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();
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,
1800 padded_input(x, y, c, n) = bounded(x, y, c, n);
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)
1809 kInC = select(c < outGroupCn * i, kInC, inpGroupCn * i + r.z);
1810 kOutC = select(c < outGroupCn * i, kOutC, c - outGroupCn * i);
1812 Halide::Expr topExpr = sum(padded_input(kx, ky, kInC, n) *
1813 weights(r.x, r.y, kOutC, kInC));
1816 auto bias = wrapToHalideBuffer(blobs[1], {numOutput});
1819 top(x, y, c, n) = topExpr;
1820 return Ptr<BackendNode>(new HalideBackendNode({ padded_input, top }));
1821 #endif // HAVE_HALIDE
1822 return Ptr<BackendNode>();
1825 #ifdef HAVE_INF_ENGINE
1826 virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> > &) CV_OVERRIDE
1828 auto ieWeights = wrapToInfEngineBlob(blobs[0], InferenceEngine::Layout::OIHW);
1831 ieWeights = InferenceEngine::make_shared_blob<float>(
1832 InferenceEngine::Precision::FP32, InferenceEngine::Layout::OIHW,
1834 ieWeights->allocate();
1836 int inpCn = blobs[0].size[0];
1837 Mat newWeights = infEngineBlobToMat(ieWeights).reshape(1, inpCn);
1838 transpose(weightsMat, newWeights);
1841 const int outGroupCn = blobs[0].size[1]; // Weights are in IOHW layout
1842 const int group = numOutput / outGroupCn;
1844 InferenceEngine::Builder::DeconvolutionLayer ieLayer(name);
1846 ieLayer.setKernel(kernel_size);
1847 ieLayer.setStrides(strides);
1848 ieLayer.setDilation(dilations);
1849 ieLayer.setPaddingsBegin(pads_begin);
1851 if (padMode.empty())
1853 ieLayer.setPaddingsEnd({pads_end[0] - adjust_pads[0], pads_end[1] - adjust_pads[1]});
1855 else if (padMode == "SAME")
1857 ieLayer.setPaddingsEnd({kernel_size[0] - pads_begin[0] - 1 - adjust_pads[0],
1858 kernel_size[1] - pads_begin[1] - 1 - adjust_pads[1]});
1860 ieLayer.setGroup((size_t)group);
1861 ieLayer.setOutDepth((size_t)numOutput);
1863 InferenceEngine::Builder::Layer l = ieLayer;
1864 addConstantData("weights", ieWeights, l);
1866 addConstantData("biases", wrapToInfEngineBlob(biasesMat, {(size_t)numOutput}, InferenceEngine::Layout::C), l);
1867 return Ptr<BackendNode>(new InfEngineBackendNode(l));
1869 #endif // HAVE_INF_ENGINE
1871 virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
1872 const std::vector<MatShape> &outputs) const CV_OVERRIDE
1874 CV_Assert(inputs.size() == outputs.size());
1877 int outChannels = blobs[0].size[0];
1879 for (int i = 0; i < inputs.size(); i++)
1881 flops += CV_BIG_INT(2)*outChannels*kernel.area()*total(inputs[i]);
1888 Ptr<BaseConvolutionLayer> ConvolutionLayer::create(const LayerParams ¶ms)
1890 Ptr<ConvolutionLayerImpl> l(new ConvolutionLayerImpl(params));
1894 Ptr<BaseConvolutionLayer> DeconvolutionLayer::create(const LayerParams ¶ms)
1896 return Ptr<BaseConvolutionLayer>(new DeConvolutionLayerImpl(params));