Merge pull request #14827 from YashasSamaga:cuda4dnn-csl-low
[platform/upstream/opencv.git] / modules / dnn / src / layers / flatten_layer.cpp
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_cuda.hpp"
46 #include "../op_inf_engine.hpp"
47 #include <float.h>
48 #include <algorithm>
49 #include <opencv2/dnn/shape_utils.hpp>
50
51 #ifdef HAVE_CUDA
52 #include "../cuda4dnn/primitives/reshape.hpp"
53 using namespace cv::dnn::cuda4dnn;
54 #endif
55
56 namespace cv
57 {
58 namespace dnn
59 {
60
61 class FlattenLayerImpl CV_FINAL : public FlattenLayer
62 {
63 public:
64     FlattenLayerImpl(const LayerParams &params)
65     {
66         _startAxis = params.get<int>("axis", 1);
67         _endAxis = params.get<int>("end_axis", -1);
68         setParamsFrom(params);
69     }
70
71     virtual bool supportBackend(int backendId) CV_OVERRIDE
72     {
73         return backendId == DNN_BACKEND_OPENCV ||
74                backendId == DNN_BACKEND_CUDA ||
75                (backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine());
76     }
77
78     bool getMemoryShapes(const std::vector<MatShape> &inputs,
79                          const int requiredOutputs,
80                          std::vector<MatShape> &outputs,
81                          std::vector<MatShape> &internals) const CV_OVERRIDE
82     {
83         CV_Assert(inputs.size() > 0);
84         for (size_t i = 1; i < inputs.size(); i++)
85         {
86             CV_Assert(inputs[i] == inputs[0]);
87         }
88
89         int numAxes = inputs[0].size();
90         int startAxis = clamp(_startAxis, numAxes);
91         int endAxis = clamp(_endAxis, numAxes);
92
93         CV_Assert(startAxis >= 0);
94         CV_Assert(endAxis >= startAxis && endAxis < (int)numAxes);
95
96         size_t flattenedDimensionSize = total(inputs[0], startAxis, endAxis + 1);
97
98         MatShape outputShapeVec;
99         for (int i = 0; i < startAxis; i++)
100         {
101             outputShapeVec.push_back(inputs[0][i]);
102         }
103         outputShapeVec.push_back(flattenedDimensionSize);
104         for (size_t i = endAxis + 1; i < numAxes; i++)
105         {
106             outputShapeVec.push_back(inputs[0][i]);
107         }
108         CV_Assert(outputShapeVec.size() <= 4);
109
110         outputs.resize(inputs.size(), outputShapeVec);
111
112         return true;
113     }
114
115     void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays) CV_OVERRIDE
116     {
117         std::vector<Mat> inputs;
118         inputs_arr.getMatVector(inputs);
119
120         int numAxes = inputs[0].dims;
121         _startAxis = clamp(_startAxis, numAxes);
122         _endAxis = clamp(_endAxis, numAxes);
123     }
124
125 #ifdef HAVE_OPENCL
126     bool forward_ocl(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)
127     {
128         std::vector<UMat> inpvec;
129         std::vector<UMat> outputs;
130
131         inputs_arr.getUMatVector(inpvec);
132         outputs_arr.getUMatVector(outputs);
133
134         std::vector<UMat*> inputs(inpvec.size());
135         for (int i = 0; i < inpvec.size(); i++)
136             inputs[i] = &inpvec[i];
137
138         for (size_t i = 0; i < inputs.size(); i++)
139         {
140             MatShape outShape = shape(outputs[i]);
141             UMat& output = outputs_arr.getUMatRef(i);
142             output = inputs[i]->reshape(1, (int)outShape.size(), &outShape[0]);
143         }
144
145         return true;
146     }
147 #endif
148
149     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
150     {
151         CV_TRACE_FUNCTION();
152         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
153
154         CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget) &&
155                    outputs_arr.isUMatVector(),
156                    forward_ocl(inputs_arr, outputs_arr, internals_arr))
157
158         std::vector<Mat> inputs, outputs;
159         inputs_arr.getMatVector(inputs);
160         outputs_arr.getMatVector(outputs);
161
162         for (size_t i = 0; i < inputs.size(); i++)
163         {
164             MatShape outShape = shape(outputs[i]);
165             if (inputs[i].data != outputs[i].data)
166             {
167                 inputs[i].reshape(1, (int)outShape.size(), &outShape[0]).copyTo(outputs[i]);
168             }
169         }
170     }
171
172 #ifdef HAVE_CUDA
173     Ptr<BackendNode> initCUDA(
174         void *context_,
175         const std::vector<Ptr<BackendWrapper>>& inputs,
176         const std::vector<Ptr<BackendWrapper>>& outputs
177     ) override
178     {
179         auto context = reinterpret_cast<csl::CSLContext*>(context_);
180         return make_cuda_node<cuda4dnn::ReshapeOp>(preferableTarget, std::move(context->stream));
181     }
182 #endif
183
184 #ifdef HAVE_INF_ENGINE
185     virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >& inputs) CV_OVERRIDE
186     {
187         InferenceEngine::Builder::Layer ieLayer(name);
188         ieLayer.setName(name);
189         ieLayer.setType("Flatten");
190         ieLayer.getParameters()["axis"] = (size_t)_startAxis;
191         ieLayer.getParameters()["end_axis"] = _endAxis;  // Do not cast to size_t because it might be negative.
192         ieLayer.setInputPorts(std::vector<InferenceEngine::Port>(1));
193         ieLayer.setOutputPorts(std::vector<InferenceEngine::Port>(1));
194         return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
195     }
196 #endif  // HAVE_INF_ENGINE
197
198     int _startAxis;
199     int _endAxis;
200 };
201
202 Ptr<FlattenLayer> FlattenLayer::create(const LayerParams& params)
203 {
204     return Ptr<FlattenLayer>(new FlattenLayerImpl(params));
205 }
206
207 }
208 }