659a79521a24eb1541ef1b90d54fb003b449cd61
[platform/upstream/opencv.git] / modules / dnn / src / layers / reorg_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 "../op_inf_engine.hpp"
45 #include <opencv2/dnn/shape_utils.hpp>
46 #include <opencv2/dnn/all_layers.hpp>
47
48 #ifdef HAVE_OPENCL
49 #include "opencl_kernels_dnn.hpp"
50 #endif
51
52 namespace cv
53 {
54 namespace dnn
55 {
56
57 class ReorgLayerImpl CV_FINAL : public ReorgLayer
58 {
59     int reorgStride;
60 public:
61
62     ReorgLayerImpl(const LayerParams& params)
63     {
64         setParamsFrom(params);
65
66         reorgStride = params.get<int>("reorg_stride", 2);
67         CV_Assert(reorgStride > 0);
68     }
69
70     bool getMemoryShapes(const std::vector<MatShape> &inputs,
71                          const int requiredOutputs,
72                          std::vector<MatShape> &outputs,
73                          std::vector<MatShape> &internals) const CV_OVERRIDE
74     {
75         CV_Assert(inputs.size() > 0);
76         outputs = std::vector<MatShape>(inputs.size(), shape(
77             inputs[0][0],
78             inputs[0][1] * reorgStride * reorgStride,
79             inputs[0][2] / reorgStride,
80             inputs[0][3] / reorgStride));
81
82         CV_Assert(outputs[0][0] > 0 && outputs[0][1] > 0 && outputs[0][2] > 0 && outputs[0][3] > 0);
83         CV_Assert(total(outputs[0]) == total(inputs[0]));
84
85         return false;
86     }
87
88     virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
89     {
90         std::vector<Mat> inputs, outputs;
91         inputs_arr.getMatVector(inputs);
92         outputs_arr.getMatVector(outputs);
93
94         Mat inp = inputs[0];
95         Mat out = outputs[0];
96         int batchSize = inp.size[0];
97
98         LayerParams permParams;
99         if (batchSize == 1)
100         {
101             int order[] = {1, 3, 0, 2};
102             permParams.set("order", DictValue::arrayInt(&order[0], 4));
103
104             permuteInpShape.resize(4);
105             permuteInpShape[0] = inp.size[1] * inp.size[2] / (reorgStride * reorgStride);  // (channels*height)/(r*r)
106             permuteInpShape[1] = reorgStride;
107             permuteInpShape[2] = inp.size[3];  // width
108             permuteInpShape[3] = reorgStride;
109
110             permuteOutShape.resize(4);
111             for (int i = 0; i < 4; ++i)
112                 permuteOutShape[i] = permuteInpShape[order[i]];
113         }
114         else
115         {
116             int order[] = {0, 2, 4, 1, 3};
117             permParams.set("order", DictValue::arrayInt(&order[0], 5));
118
119             permuteInpShape.resize(5);
120             permuteInpShape[0] = batchSize;
121             permuteInpShape[1] = inp.size[1] * inp.size[2] / (reorgStride * reorgStride);  // (channels*height)/(r*r)
122             permuteInpShape[2] = reorgStride;
123             permuteInpShape[3] = inp.size[3];  // width
124             permuteInpShape[4] = reorgStride;
125
126             permuteOutShape.resize(5);
127             for (int i = 0; i < 5; ++i)
128                 permuteOutShape[i] = permuteInpShape[order[i]];
129         }
130         permute = PermuteLayer::create(permParams);
131         std::vector<Mat> permuteInputs(1, inp.reshape(1, permuteInpShape));
132         std::vector<Mat> permuteOutputs(1, out.reshape(1, permuteOutShape));
133         permute->finalize(permuteInputs, permuteOutputs);
134     }
135
136     virtual bool supportBackend(int backendId) CV_OVERRIDE
137     {
138         return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_INFERENCE_ENGINE;
139     }
140
141 #ifdef HAVE_OPENCL
142     bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
143     {
144         std::vector<UMat> inputs;
145         std::vector<UMat> outputs;
146
147         inps.getUMatVector(inputs);
148         outs.getUMatVector(outputs);
149
150         inputs[0] = inputs[0].reshape(1, permuteInpShape.size(), &permuteInpShape[0]);
151         outputs[0] = outputs[0].reshape(1, permuteOutShape.size(), &permuteOutShape[0]);
152         permute->preferableTarget = preferableTarget;
153         permute->forward(inputs, outputs, internals);
154         return true;
155     }
156 #endif
157
158     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
159     {
160         CV_TRACE_FUNCTION();
161         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
162
163         CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
164                    forward_ocl(inputs_arr, outputs_arr, internals_arr))
165
166         if (inputs_arr.depth() == CV_16S)
167         {
168             forward_fallback(inputs_arr, outputs_arr, internals_arr);
169             return;
170         }
171
172         std::vector<Mat> inputs, outputs;
173         inputs_arr.getMatVector(inputs);
174         outputs_arr.getMatVector(outputs);
175
176         inputs[0] = inputs[0].reshape(1, permuteInpShape);
177         outputs[0] = outputs[0].reshape(1, permuteOutShape);
178         permute->forward(inputs, outputs, internals_arr);
179     }
180
181 #ifdef HAVE_INF_ENGINE
182     virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
183     {
184         InferenceEngine::Builder::ReorgYoloLayer ieLayer(name);
185         ieLayer.setStride(reorgStride);
186         return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
187     }
188 #endif  // HAVE_INF_ENGINE
189
190     virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
191                            const std::vector<MatShape> &outputs) const CV_OVERRIDE
192     {
193         CV_UNUSED(outputs); // suppress unused variable warning
194
195         int64 flops = 0;
196         for(int i = 0; i < inputs.size(); i++)
197         {
198             flops += 21*total(inputs[i]);
199         }
200         return flops;
201     }
202
203 private:
204     Ptr<PermuteLayer> permute;
205     std::vector<int> permuteInpShape, permuteOutShape;
206 };
207
208 Ptr<ReorgLayer> ReorgLayer::create(const LayerParams& params)
209 {
210     return Ptr<ReorgLayer>(new ReorgLayerImpl(params));
211 }
212
213 }  // namespace dnn
214 }  // namespace cv