b9e38769f079a9ef9677755664d7d9bfc727a8ba
[platform/upstream/opencv.git] / modules / dnn / src / layers / lrn_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/imgproc.hpp"
49 #include "opencv2/dnn/shape_utils.hpp"
50 #include "opencv2/core/hal/hal.hpp"
51 #include <algorithm>
52
53 #ifdef HAVE_OPENCL
54 #include "opencl_kernels_dnn.hpp"
55 using namespace cv::dnn::ocl4dnn;
56 #endif
57
58 namespace cv
59 {
60 namespace dnn
61 {
62
63 class LRNLayerImpl CV_FINAL : public LRNLayer
64 {
65 public:
66     LRNLayerImpl(const LayerParams& params)
67     {
68         setParamsFrom(params);
69         type = -1;
70         String nrmType = params.get<String>("norm_region", "ACROSS_CHANNELS");
71         if (nrmType == "ACROSS_CHANNELS")
72             type = CHANNEL_NRM;
73         else if (nrmType == "WITHIN_CHANNEL")
74             type = SPATIAL_NRM;
75         else
76             CV_Error(Error::StsBadArg, "Unknown region type \"" + nrmType + "\"");
77
78         size = params.get<int>("local_size", 5);
79         if (size % 2 != 1 || size <= 0)
80             CV_Error(Error::StsBadArg, "LRN layer supports only positive odd values for local_size");
81
82         alpha = params.get<double>("alpha", 1);
83         beta = params.get<double>("beta", 0.75);
84         bias = params.get<double>("bias", 1);
85         normBySize = params.get<bool>("norm_by_size", true);
86     }
87
88 #ifdef HAVE_OPENCL
89     Ptr<OCL4DNNLRN<float> > lrnOp;
90 #endif
91
92     virtual bool supportBackend(int backendId) CV_OVERRIDE
93     {
94         if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
95             return bias == (int)bias;
96         return backendId == DNN_BACKEND_OPENCV ||
97                backendId == DNN_BACKEND_HALIDE ||
98                (backendId == DNN_BACKEND_VKCOM && haveVulkan() && (size % 2 == 1) && (type == CHANNEL_NRM));
99     }
100
101 #ifdef HAVE_OPENCL
102     virtual void finalize(InputArrayOfArrays, OutputArrayOfArrays) CV_OVERRIDE
103     {
104         lrnOp.release();
105     }
106
107     bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
108     {
109         std::vector<UMat> inputs;
110         std::vector<UMat> outputs;
111
112         bool use_half = (inps.depth() == CV_16S);
113         inps.getUMatVector(inputs);
114         outs.getUMatVector(outputs);
115
116         if (lrnOp.empty())
117         {
118             OCL4DNNLRNConfig config;
119             config.lrn_type = type == CHANNEL_NRM ?
120                               LRNParameter_NormRegion_ACROSS_CHANNELS :
121                               LRNParameter_NormRegion_WITHIN_CHANNEL;
122
123             CHECK_EQ(size % 2, 1)<< "LRN only supports odd values for local_size";
124             config.local_size = size;
125             config.alpha = alpha;
126             config.beta = beta;
127             config.k = bias;
128             CHECK_EQ(4, inputs[0].dims) << "Input must have 4 axes, "
129                      << "corresponding to (num, channels, height, width)";
130             config.batch_size = inputs[0].size[0];
131             config.channels = inputs[0].size[1];
132             config.height = inputs[0].size[2];
133             config.width = inputs[0].size[3];
134             config.norm_by_size = normBySize;
135             config.use_half = use_half;
136
137             lrnOp = Ptr<OCL4DNNLRN<float> >(new OCL4DNNLRN<float>(config));
138         }
139
140         if (!lrnOp->Forward(inputs[0], outputs[0]))
141             return false;
142
143         return true;
144     }
145 #endif
146
147     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
148     {
149         CV_TRACE_FUNCTION();
150         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
151
152         CV_Assert(inputs_arr.total() == outputs_arr.total());
153
154         CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
155                    forward_ocl(inputs_arr, outputs_arr, internals_arr))
156
157         if (inputs_arr.depth() == CV_16S)
158         {
159             forward_fallback(inputs_arr, outputs_arr, internals_arr);
160             return;
161         }
162
163         std::vector<Mat> inputs, outputs;
164         inputs_arr.getMatVector(inputs);
165         outputs_arr.getMatVector(outputs);
166
167         CV_Assert(inputs.size() == outputs.size());
168
169         for (int i = 0; i < inputs.size(); i++)
170         {
171             CV_Assert(inputs[i].dims == 4);
172
173             Mat &src = inputs[i];
174             Mat &dst = outputs[i];
175
176             switch (type)
177             {
178                 case CHANNEL_NRM:
179                     channelNormalization(src, dst);
180                     break;
181                 case SPATIAL_NRM:
182                     spatialNormalization(src, dst);
183                     break;
184                 default:
185                     CV_Error(Error::StsNotImplemented, "Unimplemented mode of LRN layer");
186                     break;
187             }
188         }
189     }
190
191     class ChannelLRN : public ParallelLoopBody
192     {
193     public:
194         ChannelLRN(const float* src, float* dst, int channels, int ksize,
195                    float alpha1, float bias1, float beta1,
196                    size_t planeSize, int nsamples, int nstripes)
197         {
198             src_ = src; dst_ = dst;
199             channels_ = channels;
200             ksize_ = ksize;
201             alpha1_ = alpha1; bias1_ = bias1; beta1_ = beta1;
202             planeSize_ = planeSize; nsamples_ = nsamples; nstripes_ = nstripes;
203         }
204
205         void operator()(const Range& r) const CV_OVERRIDE
206         {
207             int nsamples = nsamples_, nstripes = nstripes_;
208             size_t planeSize = planeSize_, planeSize_n = planeSize * nsamples;
209             size_t elemsPerStripe = (planeSize_n + nstripes - 1)/nstripes;
210             size_t rstart = r.start*elemsPerStripe;
211             size_t rend = r.end == nstripes ? planeSize_n : r.end*elemsPerStripe;
212             rstart = std::min(rstart, planeSize_n);
213             rend = std::min(rend, planeSize_n);
214             float alpha1 = alpha1_, bias1 = bias1_, beta1 = beta1_;
215             int k, channels = channels_, ksize = ksize_;
216
217             AutoBuffer<float> buf_((channels + ksize + 1)*2);
218             float* acc = buf_.data();
219             float* buf = acc + channels + ksize + 1;
220             for( k = 0; k <= ksize; k++ )
221                 buf[-k-1] = buf[channels + k] = 0.f;
222
223             for( size_t ofs = rstart; ofs < rend; )
224             {
225                 int sampleIdx = (int)(ofs/planeSize);
226                 if( sampleIdx >= nsamples )
227                     break;
228                 size_t ofs0 = ofs - sampleIdx*planeSize;
229                 size_t ofs1 = std::min(planeSize - ofs0, rend - ofs) + ofs;
230                 const float* src = src_ + sampleIdx*planeSize*channels + ofs0;
231                 float* dst = dst_ + sampleIdx*planeSize*channels + ofs0;
232
233                 for( ; ofs < ofs1; ofs++, src++, dst++ )
234                 {
235                     for( k = 0; k < channels; k++ )
236                         buf[k] = src[k*planeSize];
237                     float s = 0;
238                     for( k = 0; k < ksize; k++ )
239                         s += buf[k]*buf[k];
240                     for( k = 0; k < channels; k++ )
241                     {
242                         float x1 = buf[k + ksize];
243                         float x0 = buf[k - ksize - 1];
244                         s = std::max(s + (x1 + x0)*(x1 - x0), 0.f);
245                         acc[k] = (float)(alpha1*s + bias1);
246                     }
247
248                     hal::log32f(acc, acc, channels);
249                     for( k = 0; k < channels; k++ )
250                         acc[k] *= beta1;
251                     hal::exp32f(acc, acc, channels);
252
253                     for( k = 0; k < channels; k++ )
254                         dst[k*planeSize] = buf[k]*acc[k];
255                 }
256             }
257         }
258
259         const float* src_;
260         float* dst_;
261         float alpha1_, bias1_, beta1_;
262         size_t planeSize_;
263         int channels_, ksize_, nsamples_, nstripes_;
264     };
265
266     void channelNormalization(Mat &srcBlob, Mat &dstBlob)
267     {
268         int num = srcBlob.size[0];
269         int channels = srcBlob.size[1];
270         int ksize = (size - 1) / 2;
271         int sizeNormFactor = normBySize ? size : 1;
272         size_t planeSize = srcBlob.size[2]*srcBlob.size[3];
273
274         int nstripes = std::max(getNumThreads(), 1);
275
276         ChannelLRN clrn(srcBlob.ptr<float>(), dstBlob.ptr<float>(), channels,
277                         ksize, alpha/sizeNormFactor, bias, -beta, planeSize, num, nstripes);
278         parallel_for_(Range(0, nstripes), clrn, nstripes);
279     }
280
281     void sqrBoxFilter_(const Mat &src, Mat &dst)
282     {
283         Mat srcRawWrapper(src.rows, src.cols, src.type(), src.data, src.step[0]);
284         cv::sqrBoxFilter(srcRawWrapper, dst, dst.depth(), Size(size, size), Point(-1, -1), false, BORDER_CONSTANT);
285     }
286
287     void spatialNormalization(Mat &srcBlob, Mat &dstBlob)
288     {
289         int num = srcBlob.size[0];
290         int channels = srcBlob.size[1];
291         int sizeNormFactor = normBySize ? size*size : 1;
292
293         Mat srcMat = srcBlob;
294         Mat dstMat = dstBlob;
295
296         for (int n = 0; n < num; n++)
297         {
298             for (int cn = 0; cn < channels; cn++)
299             {
300                 Mat src = getPlane(srcMat, n, cn);
301                 Mat dst = getPlane(dstMat, n, cn);
302
303                 sqrBoxFilter_(src, dst);
304
305                 dst.convertTo(dst, dst.type(), alpha/sizeNormFactor, bias);
306                 cv::pow(dst, beta, dst);
307                 cv::divide(src, dst, dst);
308             }
309         }
310     }
311
312     virtual Ptr<BackendNode> initVkCom(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
313     {
314 #ifdef HAVE_VULKAN
315         std::shared_ptr<vkcom::OpBase> op(new vkcom::OpLRN(size / 2, bias, alpha, beta, normBySize));
316         return Ptr<BackendNode>(new VkComBackendNode(inputs, op));
317 #endif
318         return Ptr<BackendNode>();
319     }
320
321     virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
322     {
323 #ifdef HAVE_HALIDE
324         float alphaSize = alpha;
325         if (normBySize)
326             alphaSize /= (type == CHANNEL_NRM ? size : size * size);
327         int width, height, channels, numImgs;
328         Halide::Buffer<float> inputBuffer = halideBuffer(inputs[0]);
329         getCanonicalSize(inputBuffer, &width, &height, &channels, &numImgs);
330
331         Halide::Var x("x"), y("y"), c("c"), n("n");
332         Halide::Func top = (name.empty() ? Halide::Func() : Halide::Func(name));
333         Halide::Func padded_sq(name + "_padded_sq");
334         Halide::Func sq("sq");
335         sq(x, y, c, n) = inputBuffer(x, y, c, n) * inputBuffer(x, y, c, n);
336
337         Halide::Func bounded =
338             Halide::BoundaryConditions::constant_exterior(sq, 0, 0, width,
339                                                           0, height,
340                                                           0, channels,
341                                                           0, numImgs);
342         padded_sq(x, y, c, n) = bounded(x, y, c, n);
343
344         Halide::Expr base;
345         if (type == CHANNEL_NRM)
346         {
347             Halide::RDom r((1 - size) / 2, size);
348             base = alphaSize * sum(padded_sq(x, y, c + r, n));
349         }
350         else  // SPATIAL_NRM
351         {
352             Halide::RDom r((1 - size) / 2, size, (1 - size) / 2, size);
353             base = alphaSize * sum(padded_sq(x + r.x, y + r.y, c, n));
354         }
355         base += static_cast<float>(bias);
356         top(x, y, c, n) = inputBuffer(x, y, c, n) / pow(base, beta);
357         return Ptr<BackendNode>(new HalideBackendNode({ padded_sq, top }));
358 #endif  // HAVE_HALIDE
359         return Ptr<BackendNode>();
360     }
361
362     virtual void applyHalideScheduler(Ptr<BackendNode>& node,
363                                       const std::vector<Mat*> &inputs,
364                                       const std::vector<Mat> &outputs,
365                                       int targetId) const CV_OVERRIDE
366     {
367 #ifdef  HAVE_HALIDE
368         if (targetId != DNN_TARGET_CPU)
369         {
370             Layer::applyHalideScheduler(node, inputs, outputs, targetId);
371             return;
372         }
373         int outW, outH, outC, outN;
374         getCanonicalSize(outputs[0].size, &outW, &outH, &outC, &outN);
375
376         Halide::Var x("x"), y("y"), c("c"), n("n"), yo("yo"), yi("yi"), tile("tile");
377         Halide::Func& top = node.dynamicCast<HalideBackendNode>()->funcs[1];
378         Halide::Func& padded_sq = node.dynamicCast<HalideBackendNode>()->funcs[0];
379
380         if (outW < 8 || outH <= 2)
381             return;
382
383         top.reorder(x, c, y, n)
384            .split(y, yo, yi, 2)
385            .fuse(yo, n, tile)
386            .parallel(tile)
387            .unroll(yi)
388            .vectorize(x, 8);
389         padded_sq.store_at(top, tile)
390                  .compute_at(top, yi);
391 #endif  // HAVE_HALIDE
392     }
393
394 #ifdef HAVE_INF_ENGINE
395     virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
396     {
397         float alphaSize = alpha;
398         if (!normBySize)
399             alphaSize *= (type == SPATIAL_NRM ? size*size : size);
400
401         InferenceEngine::Builder::NormLayer ieLayer(name);
402         ieLayer.setSize(size);
403         ieLayer.setAlpha(alphaSize);
404         ieLayer.setBeta(beta);
405         ieLayer.setAcrossMaps(type == CHANNEL_NRM);
406
407         InferenceEngine::Builder::Layer l = ieLayer;
408         l.getParameters()["k"] = bias;
409         return Ptr<BackendNode>(new InfEngineBackendNode(l));
410     }
411 #endif  // HAVE_INF_ENGINE
412
413     virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
414                            const std::vector<MatShape> &outputs) const CV_OVERRIDE
415     {
416         CV_UNUSED(outputs); // suppress unused variable warning
417         CV_Assert(inputs.size() > 0);
418         long flops = 0;
419
420         for(int i = 0; i < inputs.size(); i++)
421         {
422             if (type == CHANNEL_NRM)
423             {
424                 int channels = inputs[i][1];
425                 int ksize = (size - 1) / 2;
426
427                 flops += inputs[i][0]*(std::min(ksize, channels)*2*total(inputs[i], 2) + channels*4*total(inputs[i], 2));
428
429                 if (ksize < channels)
430                 {
431                     flops += (size + 2*(channels - size))*total(inputs[i], 2);
432                 }
433             }
434             else
435             {
436                 flops += total(inputs[i])*(2*size*size + 2);
437             }
438         }
439         return flops;
440     }
441
442 private:
443     enum Type
444     {
445         CHANNEL_NRM,
446         SPATIAL_NRM
447     };
448 };
449
450 Ptr<LRNLayer> LRNLayer::create(const LayerParams& params)
451 {
452     return Ptr<LRNLayer>(new LRNLayerImpl(params));
453 }
454
455 }
456 }