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 "../op_inf_engine.hpp"
45 #include <opencv2/dnn/shape_utils.hpp>
46 #include <opencv2/dnn/all_layers.hpp>
49 #include "opencl_kernels_dnn.hpp"
57 class ReorgLayerImpl CV_FINAL : public ReorgLayer
62 ReorgLayerImpl(const LayerParams& params)
64 setParamsFrom(params);
66 reorgStride = params.get<int>("reorg_stride", 2);
67 CV_Assert(reorgStride > 0);
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
75 CV_Assert(inputs.size() > 0);
76 outputs = std::vector<MatShape>(inputs.size(), shape(
78 inputs[0][1] * reorgStride * reorgStride,
79 inputs[0][2] / reorgStride,
80 inputs[0][3] / reorgStride));
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]));
88 virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
90 std::vector<Mat> inputs, outputs;
91 inputs_arr.getMatVector(inputs);
92 outputs_arr.getMatVector(outputs);
96 int batchSize = inp.size[0];
98 LayerParams permParams;
101 int order[] = {1, 3, 0, 2};
102 permParams.set("order", DictValue::arrayInt(&order[0], 4));
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;
110 permuteOutShape.resize(4);
111 for (int i = 0; i < 4; ++i)
112 permuteOutShape[i] = permuteInpShape[order[i]];
116 int order[] = {0, 2, 4, 1, 3};
117 permParams.set("order", DictValue::arrayInt(&order[0], 5));
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;
126 permuteOutShape.resize(5);
127 for (int i = 0; i < 5; ++i)
128 permuteOutShape[i] = permuteInpShape[order[i]];
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);
136 virtual bool supportBackend(int backendId) CV_OVERRIDE
138 return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_INFERENCE_ENGINE;
142 bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
144 std::vector<UMat> inputs;
145 std::vector<UMat> outputs;
147 inps.getUMatVector(inputs);
148 outs.getUMatVector(outputs);
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);
158 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
161 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
163 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
164 forward_ocl(inputs_arr, outputs_arr, internals_arr))
166 if (inputs_arr.depth() == CV_16S)
168 forward_fallback(inputs_arr, outputs_arr, internals_arr);
172 std::vector<Mat> inputs, outputs;
173 inputs_arr.getMatVector(inputs);
174 outputs_arr.getMatVector(outputs);
176 inputs[0] = inputs[0].reshape(1, permuteInpShape);
177 outputs[0] = outputs[0].reshape(1, permuteOutShape);
178 permute->forward(inputs, outputs, internals_arr);
181 #ifdef HAVE_INF_ENGINE
182 virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
184 InferenceEngine::Builder::ReorgYoloLayer ieLayer(name);
185 ieLayer.setStride(reorgStride);
186 return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
188 #endif // HAVE_INF_ENGINE
190 virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
191 const std::vector<MatShape> &outputs) const CV_OVERRIDE
193 CV_UNUSED(outputs); // suppress unused variable warning
196 for(int i = 0; i < inputs.size(); i++)
198 flops += 21*total(inputs[i]);
204 Ptr<PermuteLayer> permute;
205 std::vector<int> permuteInpShape, permuteOutShape;
208 Ptr<ReorgLayer> ReorgLayer::create(const LayerParams& params)
210 return Ptr<ReorgLayer>(new ReorgLayerImpl(params));