Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/layers/reshape_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 "layers_common.hpp"
45
#include "../op_inf_engine.hpp"
46
#include <opencv2/dnn/shape_utils.hpp>
47
48
namespace cv
49
{
50
namespace dnn
51
{
52
53
static void computeShapeByReshapeMask(const MatShape &srcShape,
54
const MatShape &maskShape,
55
Range srcRange /*= Range::all()*/,
56
MatShape& dstShape)
57
{
58
int srcShapeSize = (int)srcShape.size();
59
int maskShapeSize = (int)maskShape.size();
60
61
if (srcRange == Range::all())
62
srcRange = Range(0, srcShapeSize);
63
else
64
{
65
int sz = srcRange.size();
66
srcRange.start = clamp(srcRange.start, srcShapeSize);
67
srcRange.end = srcRange.end == INT_MAX ? srcShapeSize : srcRange.start + sz;
68
}
69
70
bool explicitMask = !maskShape.empty(); // All mask values are positive.
71
for (int i = 0, n = maskShape.size(); i < n && explicitMask; ++i)
72
{
73
explicitMask = maskShape[i] > 0;
74
}
75
// Working range of source shape is a range where area(src) == area(mask).
76
if (explicitMask)
77
{
78
int maskTotal = total(maskShape);
79
// Go from the end of mask until we collect required total.
80
bool matched = false;
81
for (int i = srcRange.end - 1; i >= srcRange.start; --i)
82
{
83
if (matched)
84
{
85
if (total(srcShape, i, srcRange.end) != maskTotal)
86
{
87
srcRange.start = i + 1;
88
break;
89
}
90
else if (i == 0)
91
{
92
srcRange.start = 0;
93
break;
94
}
95
}
96
else
97
{
98
matched = total(srcShape, i, srcRange.end) == maskTotal;
99
}
100
}
101
while (total(srcShape, srcRange.start, srcRange.end) != maskTotal && srcRange.start > 0)
102
{
103
srcRange.start -= 1;
104
}
105
CV_Assert(total(srcShape, srcRange.start, srcRange.end) == maskTotal);
106
}
107
108
CV_Assert(0 <= srcRange.start && srcRange.start <= srcRange.end && srcRange.end <= srcShapeSize);
109
int dstShapeSize = srcShapeSize - srcRange.size() + maskShapeSize;
110
dstShape.resize(dstShapeSize);
111
112
std::copy(srcShape.begin(), srcShape.begin() + srcRange.start, dstShape.begin());
113
std::copy(srcShape.begin() + srcRange.end, srcShape.begin() + srcShapeSize, dstShape.begin() + srcRange.start + maskShapeSize);
114
115
int inferDim = -1;
116
for (int i = 0; i < maskShapeSize; i++)
117
{
118
if (maskShape[i] > 0)
119
{
120
dstShape[srcRange.start + i] = maskShape[i];
121
}
122
else if (maskShape[i] == 0)
123
{
124
if (srcRange.start + i >= srcShapeSize)
125
CV_Error(Error::StsBadArg, format("Copy dim[%d] (which has zero size) is out of the source shape bounds", srcRange.start + i));
126
dstShape[srcRange.start + i] = srcShape[srcRange.start + i];
127
}
128
else if (maskShape[i] == -1)
129
{
130
if (inferDim != -1)
131
CV_Error(Error::StsAssert, "Duplicate of inferred dim (which is denoted by -1)");
132
inferDim = srcRange.start + i;
133
dstShape[inferDim] = 1;
134
}
135
else
136
CV_Error(Error::StsBadArg, "maskShape[i] >= -1");
137
}
138
139
size_t srcTotal = total(srcShape);
140
size_t dstTotal = total(dstShape);
141
142
if (inferDim != -1)
143
{
144
if (srcTotal % dstTotal != 0)
145
CV_Error(Error::StsBackTrace, "Can't infer a dim denoted by -1");
146
147
dstShape[inferDim] = (int)(srcTotal / dstTotal);
148
}
149
else
150
{
151
CV_Assert(srcTotal == dstTotal);
152
}
153
}
154
155
156
class ReshapeLayerImpl CV_FINAL : public ReshapeLayer
157
{
158
public:
159
ReshapeLayerImpl(const LayerParams& params)
160
{
161
setParamsFrom(params);
162
int axis = params.get<int>("axis", 0);
163
int numAxes = params.get<int>("num_axes", -1);
164
CV_Assert(numAxes >= -1);
165
newShapeRange = (numAxes == -1) ? Range(axis, INT_MAX) : Range(axis, axis + numAxes);
166
167
newShapeDesc.clear();
168
if (params.has("dim"))
169
{
170
const DictValue &paramShape = params.get("dim");
171
int i, dims = paramShape.size();
172
newShapeDesc.resize(dims);
173
for (i = 0; i < dims; i++)
174
newShapeDesc[i] = paramShape.get<int>(i);
175
}
176
}
177
178
virtual bool supportBackend(int backendId) CV_OVERRIDE
179
{
180
return backendId == DNN_BACKEND_OPENCV ||
181
backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine();
182
}
183
184
bool getMemoryShapes(const std::vector<MatShape> &inputs,
185
const int requiredOutputs,
186
std::vector<MatShape> &outputs,
187
std::vector<MatShape> &internals) const CV_OVERRIDE
188
{
189
if (inputs.size() == 1 || inputs.size() == requiredOutputs)
190
{
191
outputs.clear();
192
for (size_t i = 0; i < inputs.size(); i++)
193
{
194
outputs.push_back(MatShape());
195
computeShapeByReshapeMask(inputs[i], newShapeDesc, newShapeRange, outputs.back());
196
}
197
}
198
else
199
{
200
CV_Assert_N(inputs.size() == 2, total(inputs[0]) == total(inputs[1]));
201
outputs.assign(1, inputs[1]);
202
}
203
return true;
204
}
205
206
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
207
{
208
std::vector<UMat> inputs;
209
std::vector<UMat> outputs;
210
211
inps.getUMatVector(inputs);
212
outs.getUMatVector(outputs);
213
214
for (size_t i = 0; i < outputs.size(); i++)
215
{
216
UMat srcBlob = inputs[i];
217
void *src_handle = inputs[i].handle(ACCESS_READ);
218
void *dst_handle = outputs[i].handle(ACCESS_WRITE);
219
if (src_handle != dst_handle)
220
{
221
MatShape outShape = shape(outputs[i]);
222
UMat umat = srcBlob.reshape(1, (int)outShape.size(), &outShape[0]);
223
umat.copyTo(outputs[i]);
224
}
225
}
226
outs.assign(outputs);
227
228
return true;
229
}
230
231
void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
232
{
233
CV_TRACE_FUNCTION();
234
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
235
236
CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
237
forward_ocl(inputs_arr, outputs_arr, internals_arr))
238
239
std::vector<Mat> inputs, outputs;
240
inputs_arr.getMatVector(inputs);
241
outputs_arr.getMatVector(outputs);
242
for (size_t i = 0; i < outputs.size(); i++)
243
{
244
Mat srcBlob = inputs[i];
245
if (outputs[i].data != srcBlob.data)
246
srcBlob.reshape(1, shape(outputs[i])).copyTo(outputs[i]);
247
}
248
}
249
250
virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >& inputs) CV_OVERRIDE
251
{
252
#ifdef HAVE_INF_ENGINE
253
InferenceEngine::LayerParams lp;
254
lp.name = name;
255
lp.type = "Reshape";
256
lp.precision = InferenceEngine::Precision::FP32;
257
std::shared_ptr<InferenceEngine::ReshapeLayer> ieLayer(new InferenceEngine::ReshapeLayer(lp));
258
if (!newShapeDesc.empty())
259
ieLayer->shape = newShapeDesc;
260
else
261
{
262
CV_Assert(inputs.size() == 2);
263
InferenceEngine::DataPtr shapeSrc = infEngineDataNode(inputs[1]);
264
// NOTE: shapeSrc->dims are reversed
265
ieLayer->shape = std::vector<int>(shapeSrc->dims.rbegin(), shapeSrc->dims.rend());
266
}
267
return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
268
#endif // HAVE_INF_ENGINE
269
return Ptr<BackendNode>();
270
}
271
};
272
273
Ptr<ReshapeLayer> ReshapeLayer::create(const LayerParams& params)
274
{
275
return Ptr<ReshapeLayer>(new ReshapeLayerImpl(params));
276
}
277
278
279
}
280
}
281
282