339f2b7932ebd16207906af972c3d999174d848c
[platform/upstream/opencv.git] / modules / dnn / src / layers / resize_layer.cpp
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 #ifdef HAVE_INF_ENGINE
55         if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
56         {
57             return (interpolation == "nearest" && scaleWidth == scaleHeight) ||
58                    (interpolation == "bilinear");
59         }
60 #endif
61         return backendId == DNN_BACKEND_OPENCV;
62     }
63
64     virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
65     {
66         std::vector<Mat> inputs, outputs;
67         inputs_arr.getMatVector(inputs);
68         outputs_arr.getMatVector(outputs);
69
70         if (!outWidth && !outHeight)
71         {
72             outHeight = outputs[0].size[2];
73             outWidth = outputs[0].size[3];
74         }
75         if (alignCorners && outHeight > 1)
76             scaleHeight = static_cast<float>(inputs[0].size[2] - 1) / (outHeight - 1);
77         else
78             scaleHeight = static_cast<float>(inputs[0].size[2]) / outHeight;
79
80         if (alignCorners && outWidth > 1)
81             scaleWidth = static_cast<float>(inputs[0].size[3] - 1) / (outWidth - 1);
82         else
83             scaleWidth = static_cast<float>(inputs[0].size[3]) / outWidth;
84     }
85
86     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
87     {
88         CV_TRACE_FUNCTION();
89         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
90
91         if (inputs_arr.depth() == CV_16S)
92         {
93             forward_fallback(inputs_arr, outputs_arr, internals_arr);
94             return;
95         }
96
97         std::vector<Mat> inputs, outputs, internals;
98         inputs_arr.getMatVector(inputs);
99         outputs_arr.getMatVector(outputs);
100         internals_arr.getMatVector(internals);
101
102         if (outHeight == inputs[0].size[2] && outWidth == inputs[0].size[3])
103             return;
104
105         Mat& inp = inputs[0];
106         Mat& out = outputs[0];
107         if (interpolation == "nearest")
108         {
109             for (size_t n = 0; n < inputs[0].size[0]; ++n)
110             {
111                 for (size_t ch = 0; ch < inputs[0].size[1]; ++ch)
112                 {
113                     resize(getPlane(inp, n, ch), getPlane(out, n, ch),
114                            Size(outWidth, outHeight), 0, 0, INTER_NEAREST);
115                 }
116             }
117         }
118         else if (interpolation == "bilinear")
119         {
120             const int inpHeight = inp.size[2];
121             const int inpWidth = inp.size[3];
122             const int inpSpatialSize = inpHeight * inpWidth;
123             const int outSpatialSize = outHeight * outWidth;
124             const int numPlanes = inp.size[0] * inp.size[1];
125             CV_Assert_N(inp.isContinuous(), out.isContinuous());
126
127             Mat inpPlanes = inp.reshape(1, numPlanes * inpHeight);
128             Mat outPlanes = out.reshape(1, numPlanes * outHeight);
129             for (int y = 0; y < outHeight; ++y)
130             {
131                 float input_y = y * scaleHeight;
132                 int y0 = static_cast<int>(input_y);
133                 const float* inpData_row0 = inpPlanes.ptr<float>(y0);
134                 const float* inpData_row1 = inpPlanes.ptr<float>(std::min(y0 + 1, inpHeight - 1));
135                 for (int x = 0; x < outWidth; ++x)
136                 {
137                     float input_x = x * scaleWidth;
138                     int x0 = static_cast<int>(input_x);
139                     int x1 = std::min(x0 + 1, inpWidth - 1);
140
141                     float* outData = outPlanes.ptr<float>(y, x);
142                     const float* inpData_row0_c = inpData_row0;
143                     const float* inpData_row1_c = inpData_row1;
144                     for (int c = 0; c < numPlanes; ++c)
145                     {
146                         *outData = inpData_row0_c[x0] +
147                             (input_y - y0) * (inpData_row1_c[x0] - inpData_row0_c[x0]) +
148                             (input_x - x0) * (inpData_row0_c[x1] - inpData_row0_c[x0] +
149                             (input_y - y0) * (inpData_row1_c[x1] - inpData_row0_c[x1] - inpData_row1_c[x0] + inpData_row0_c[x0]));
150
151                         inpData_row0_c += inpSpatialSize;
152                         inpData_row1_c += inpSpatialSize;
153                         outData += outSpatialSize;
154                     }
155                 }
156             }
157         }
158         else
159             CV_Error(Error::StsNotImplemented, "Unknown interpolation: " + interpolation);
160     }
161
162     virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
163     {
164 #ifdef HAVE_INF_ENGINE
165         InferenceEngine::Builder::Layer ieLayer(name);
166         ieLayer.setName(name);
167         if (interpolation == "nearest")
168         {
169             ieLayer.setType("Resample");
170             ieLayer.getParameters()["type"] = std::string("caffe.ResampleParameter.NEAREST");
171             ieLayer.getParameters()["antialias"] = false;
172             if (scaleWidth != scaleHeight)
173                 CV_Error(Error::StsNotImplemented, "resample with sw != sh");
174             ieLayer.getParameters()["factor"] = 1.0f / scaleWidth;
175         }
176         else if (interpolation == "bilinear")
177         {
178             ieLayer.setType("Interp");
179             ieLayer.getParameters()["pad_beg"] = 0;
180             ieLayer.getParameters()["pad_end"] = 0;
181             ieLayer.getParameters()["align_corners"] = false;
182         }
183         else
184             CV_Error(Error::StsNotImplemented, "Unsupported interpolation: " + interpolation);
185         ieLayer.getParameters()["width"] = outWidth;
186         ieLayer.getParameters()["height"] = outHeight;
187         ieLayer.setInputPorts(std::vector<InferenceEngine::Port>(1));
188         ieLayer.setOutputPorts(std::vector<InferenceEngine::Port>(1));
189         return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
190 #endif  // HAVE_INF_ENGINE
191         return Ptr<BackendNode>();
192     }
193
194 protected:
195     int outWidth, outHeight, zoomFactorWidth, zoomFactorHeight;
196     String interpolation;
197     float scaleWidth, scaleHeight;
198     bool alignCorners;
199 };
200
201
202 Ptr<ResizeLayer> ResizeLayer::create(const LayerParams& params)
203 {
204     return Ptr<ResizeLayer>(new ResizeLayerImpl(params));
205 }
206
207 class InterpLayerImpl CV_FINAL : public ResizeLayerImpl
208 {
209 public:
210     InterpLayerImpl(const LayerParams& params) : ResizeLayerImpl(params) {}
211
212     bool getMemoryShapes(const std::vector<MatShape> &inputs,
213                          const int requiredOutputs,
214                          std::vector<MatShape> &outputs,
215                          std::vector<MatShape> &internals) const CV_OVERRIDE
216     {
217         CV_Assert_N(inputs.size() == 1, inputs[0].size() == 4);
218         outputs.resize(1, inputs[0]);
219         outputs[0][2] = outHeight > 0 ? outHeight : (1 + zoomFactorHeight * (outputs[0][2] - 1));
220         outputs[0][3] = outWidth > 0 ? outWidth : (1 + zoomFactorWidth * (outputs[0][3] - 1));
221         // We can work in-place (do nothing) if input shape == output shape.
222         return (outputs[0][2] == inputs[0][2]) && (outputs[0][3] == inputs[0][3]);
223     }
224
225     virtual bool supportBackend(int backendId) CV_OVERRIDE
226     {
227         return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_INFERENCE_ENGINE;
228     }
229
230     virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
231     {
232         std::vector<Mat> inputs, outputs;
233         inputs_arr.getMatVector(inputs);
234         outputs_arr.getMatVector(outputs);
235
236         if (!outWidth && !outHeight)
237         {
238             outHeight = outputs[0].size[2];
239             outWidth = outputs[0].size[3];
240         }
241         int inpHeight = inputs[0].size[2];
242         int inpWidth = inputs[0].size[3];
243         scaleHeight = (outHeight > 1) ? (static_cast<float>(inpHeight - 1) / (outHeight - 1)) : 0.f;
244         scaleWidth = (outWidth > 1) ? (static_cast<float>(inpWidth - 1) / (outWidth - 1)) : 0.f;
245     }
246
247 #ifdef HAVE_INF_ENGINE
248     virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
249     {
250         InferenceEngine::Builder::Layer ieLayer(name);
251         ieLayer.setName(name);
252         ieLayer.setType("Interp");
253         ieLayer.getParameters()["pad_beg"] = 0;
254         ieLayer.getParameters()["pad_end"] = 0;
255         ieLayer.getParameters()["width"] = outWidth;
256         ieLayer.getParameters()["height"] = outHeight;
257         ieLayer.setInputPorts(std::vector<InferenceEngine::Port>(1));
258         ieLayer.setOutputPorts(std::vector<InferenceEngine::Port>(1));
259         return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
260     }
261 #endif  // HAVE_INF_ENGINE
262
263 };
264
265 Ptr<Layer> InterpLayer::create(const LayerParams& params)
266 {
267     LayerParams lp(params);
268     lp.set("interpolation", "bilinear");
269     return Ptr<Layer>(new InterpLayerImpl(lp));
270 }
271
272 }  // namespace dnn
273 }  // namespace cv