Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/layers/reorg_layer.cpp
16337 views
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
virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
182
{
183
#ifdef HAVE_INF_ENGINE
184
InferenceEngine::LayerParams lp;
185
lp.name = name;
186
lp.type = "ReorgYolo";
187
lp.precision = InferenceEngine::Precision::FP32;
188
std::shared_ptr<InferenceEngine::CNNLayer> ieLayer(new InferenceEngine::CNNLayer(lp));
189
ieLayer->params["stride"] = format("%d", reorgStride);
190
return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
191
#endif // HAVE_INF_ENGINE
192
return Ptr<BackendNode>();
193
}
194
195
virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
196
const std::vector<MatShape> &outputs) const CV_OVERRIDE
197
{
198
CV_UNUSED(outputs); // suppress unused variable warning
199
200
int64 flops = 0;
201
for(int i = 0; i < inputs.size(); i++)
202
{
203
flops += 21*total(inputs[i]);
204
}
205
return flops;
206
}
207
208
private:
209
Ptr<PermuteLayer> permute;
210
std::vector<int> permuteInpShape, permuteOutShape;
211
};
212
213
Ptr<ReorgLayer> ReorgLayer::create(const LayerParams& params)
214
{
215
return Ptr<ReorgLayer>(new ReorgLayerImpl(params));
216
}
217
218
} // namespace dnn
219
} // namespace cv
220
221