Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/layers/resize_layer.cpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
5
// Copyright (C) 2017, Intel Corporation, all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
#include "../precomp.hpp"
8
#include "layers_common.hpp"
9
#include "../op_inf_engine.hpp"
10
#include <opencv2/imgproc.hpp>
11
12
namespace cv { namespace dnn {
13
14
class ResizeLayerImpl : public ResizeLayer
15
{
16
public:
17
ResizeLayerImpl(const LayerParams& params) : zoomFactorWidth(0), zoomFactorHeight(0), scaleWidth(0), scaleHeight(0)
18
{
19
setParamsFrom(params);
20
outWidth = params.get<float>("width", 0);
21
outHeight = params.get<float>("height", 0);
22
if (params.has("zoom_factor"))
23
{
24
CV_Assert(!params.has("zoom_factor_x") && !params.has("zoom_factor_y"));
25
zoomFactorWidth = zoomFactorHeight = params.get<int>("zoom_factor");
26
}
27
else if (params.has("zoom_factor_x") || params.has("zoom_factor_y"))
28
{
29
CV_Assert(params.has("zoom_factor_x") && params.has("zoom_factor_y"));
30
zoomFactorWidth = params.get<int>("zoom_factor_x");
31
zoomFactorHeight = params.get<int>("zoom_factor_y");
32
}
33
interpolation = params.get<String>("interpolation");
34
CV_Assert(interpolation == "nearest" || interpolation == "bilinear");
35
36
alignCorners = params.get<bool>("align_corners", false);
37
}
38
39
bool getMemoryShapes(const std::vector<MatShape> &inputs,
40
const int requiredOutputs,
41
std::vector<MatShape> &outputs,
42
std::vector<MatShape> &internals) const CV_OVERRIDE
43
{
44
CV_Assert_N(inputs.size() == 1, inputs[0].size() == 4);
45
outputs.resize(1, inputs[0]);
46
outputs[0][2] = outHeight > 0 ? outHeight : (outputs[0][2] * zoomFactorHeight);
47
outputs[0][3] = outWidth > 0 ? outWidth : (outputs[0][3] * zoomFactorWidth);
48
// We can work in-place (do nothing) if input shape == output shape.
49
return (outputs[0][2] == inputs[0][2]) && (outputs[0][3] == inputs[0][3]);
50
}
51
52
virtual bool supportBackend(int backendId) CV_OVERRIDE
53
{
54
if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
55
return interpolation == "nearest" && preferableTarget != DNN_TARGET_MYRIAD;
56
else
57
return backendId == DNN_BACKEND_OPENCV;
58
}
59
60
virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
61
{
62
std::vector<Mat> inputs, outputs;
63
inputs_arr.getMatVector(inputs);
64
outputs_arr.getMatVector(outputs);
65
66
if (!outWidth && !outHeight)
67
{
68
outHeight = outputs[0].size[2];
69
outWidth = outputs[0].size[3];
70
}
71
if (alignCorners && outHeight > 1)
72
scaleHeight = static_cast<float>(inputs[0].size[2] - 1) / (outHeight - 1);
73
else
74
scaleHeight = static_cast<float>(inputs[0].size[2]) / outHeight;
75
76
if (alignCorners && outWidth > 1)
77
scaleWidth = static_cast<float>(inputs[0].size[3] - 1) / (outWidth - 1);
78
else
79
scaleWidth = static_cast<float>(inputs[0].size[3]) / outWidth;
80
}
81
82
void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
83
{
84
CV_TRACE_FUNCTION();
85
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
86
87
if (inputs_arr.depth() == CV_16S)
88
{
89
forward_fallback(inputs_arr, outputs_arr, internals_arr);
90
return;
91
}
92
93
std::vector<Mat> inputs, outputs, internals;
94
inputs_arr.getMatVector(inputs);
95
outputs_arr.getMatVector(outputs);
96
internals_arr.getMatVector(internals);
97
98
if (outHeight == inputs[0].size[2] && outWidth == inputs[0].size[3])
99
return;
100
101
Mat& inp = inputs[0];
102
Mat& out = outputs[0];
103
if (interpolation == "nearest")
104
{
105
for (size_t n = 0; n < inputs[0].size[0]; ++n)
106
{
107
for (size_t ch = 0; ch < inputs[0].size[1]; ++ch)
108
{
109
resize(getPlane(inp, n, ch), getPlane(out, n, ch),
110
Size(outWidth, outHeight), 0, 0, INTER_NEAREST);
111
}
112
}
113
}
114
else if (interpolation == "bilinear")
115
{
116
const int inpHeight = inp.size[2];
117
const int inpWidth = inp.size[3];
118
const int inpSpatialSize = inpHeight * inpWidth;
119
const int outSpatialSize = outHeight * outWidth;
120
const int numPlanes = inp.size[0] * inp.size[1];
121
CV_Assert_N(inp.isContinuous(), out.isContinuous());
122
123
Mat inpPlanes = inp.reshape(1, numPlanes * inpHeight);
124
Mat outPlanes = out.reshape(1, numPlanes * outHeight);
125
for (int y = 0; y < outHeight; ++y)
126
{
127
float input_y = y * scaleHeight;
128
int y0 = static_cast<int>(input_y);
129
const float* inpData_row0 = inpPlanes.ptr<float>(y0);
130
const float* inpData_row1 = inpPlanes.ptr<float>(std::min(y0 + 1, inpHeight - 1));
131
for (int x = 0; x < outWidth; ++x)
132
{
133
float input_x = x * scaleWidth;
134
int x0 = static_cast<int>(input_x);
135
int x1 = std::min(x0 + 1, inpWidth - 1);
136
137
float* outData = outPlanes.ptr<float>(y, x);
138
const float* inpData_row0_c = inpData_row0;
139
const float* inpData_row1_c = inpData_row1;
140
for (int c = 0; c < numPlanes; ++c)
141
{
142
*outData = inpData_row0_c[x0] +
143
(input_y - y0) * (inpData_row1_c[x0] - inpData_row0_c[x0]) +
144
(input_x - x0) * (inpData_row0_c[x1] - inpData_row0_c[x0] +
145
(input_y - y0) * (inpData_row1_c[x1] - inpData_row0_c[x1] - inpData_row1_c[x0] + inpData_row0_c[x0]));
146
147
inpData_row0_c += inpSpatialSize;
148
inpData_row1_c += inpSpatialSize;
149
outData += outSpatialSize;
150
}
151
}
152
}
153
}
154
else
155
CV_Error(Error::StsNotImplemented, "Unknown interpolation: " + interpolation);
156
}
157
158
virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
159
{
160
#ifdef HAVE_INF_ENGINE
161
InferenceEngine::LayerParams lp;
162
lp.name = name;
163
lp.type = "Resample";
164
lp.precision = InferenceEngine::Precision::FP32;
165
166
std::shared_ptr<InferenceEngine::CNNLayer> ieLayer(new InferenceEngine::CNNLayer(lp));
167
ieLayer->params["type"] = "caffe.ResampleParameter.NEAREST";
168
ieLayer->params["antialias"] = "0";
169
ieLayer->params["width"] = cv::format("%d", outWidth);
170
ieLayer->params["height"] = cv::format("%d", outHeight);
171
172
return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
173
#endif // HAVE_INF_ENGINE
174
return Ptr<BackendNode>();
175
}
176
177
protected:
178
int outWidth, outHeight, zoomFactorWidth, zoomFactorHeight;
179
String interpolation;
180
float scaleWidth, scaleHeight;
181
bool alignCorners;
182
};
183
184
185
Ptr<ResizeLayer> ResizeLayer::create(const LayerParams& params)
186
{
187
return Ptr<ResizeLayer>(new ResizeLayerImpl(params));
188
}
189
190
class InterpLayerImpl CV_FINAL : public ResizeLayerImpl
191
{
192
public:
193
InterpLayerImpl(const LayerParams& params) : ResizeLayerImpl(params) {}
194
195
bool getMemoryShapes(const std::vector<MatShape> &inputs,
196
const int requiredOutputs,
197
std::vector<MatShape> &outputs,
198
std::vector<MatShape> &internals) const CV_OVERRIDE
199
{
200
CV_Assert_N(inputs.size() == 1, inputs[0].size() == 4);
201
outputs.resize(1, inputs[0]);
202
outputs[0][2] = outHeight > 0 ? outHeight : (1 + zoomFactorHeight * (outputs[0][2] - 1));
203
outputs[0][3] = outWidth > 0 ? outWidth : (1 + zoomFactorWidth * (outputs[0][3] - 1));
204
// We can work in-place (do nothing) if input shape == output shape.
205
return (outputs[0][2] == inputs[0][2]) && (outputs[0][3] == inputs[0][3]);
206
}
207
208
virtual bool supportBackend(int backendId) CV_OVERRIDE
209
{
210
return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_INFERENCE_ENGINE;
211
}
212
213
virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
214
{
215
std::vector<Mat> inputs, outputs;
216
inputs_arr.getMatVector(inputs);
217
outputs_arr.getMatVector(outputs);
218
219
if (!outWidth && !outHeight)
220
{
221
outHeight = outputs[0].size[2];
222
outWidth = outputs[0].size[3];
223
}
224
int inpHeight = inputs[0].size[2];
225
int inpWidth = inputs[0].size[3];
226
scaleHeight = (outHeight > 1) ? (static_cast<float>(inpHeight - 1) / (outHeight - 1)) : 0.f;
227
scaleWidth = (outWidth > 1) ? (static_cast<float>(inpWidth - 1) / (outWidth - 1)) : 0.f;
228
}
229
230
virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
231
{
232
#ifdef HAVE_INF_ENGINE
233
InferenceEngine::LayerParams lp;
234
lp.name = name;
235
lp.type = "Interp";
236
lp.precision = InferenceEngine::Precision::FP32;
237
238
std::shared_ptr<InferenceEngine::CNNLayer> ieLayer(new InferenceEngine::CNNLayer(lp));
239
ieLayer->params["pad_beg"] = "0";
240
ieLayer->params["pad_end"] = "0";
241
return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
242
#endif // HAVE_INF_ENGINE
243
return Ptr<BackendNode>();
244
}
245
};
246
247
Ptr<Layer> InterpLayer::create(const LayerParams& params)
248
{
249
LayerParams lp(params);
250
lp.set("interpolation", "bilinear");
251
return Ptr<Layer>(new InterpLayerImpl(lp));
252
}
253
254
} // namespace dnn
255
} // namespace cv
256
257