1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "../precomp.hpp"
44 #include "layers_common.hpp"
45 #include "../op_cuda.hpp"
46 #include "../op_inf_engine.hpp"
49 #include <opencv2/dnn/shape_utils.hpp>
52 #include "../cuda4dnn/primitives/reshape.hpp"
53 using namespace cv::dnn::cuda4dnn;
61 class FlattenLayerImpl CV_FINAL : public FlattenLayer
64 FlattenLayerImpl(const LayerParams ¶ms)
66 _startAxis = params.get<int>("axis", 1);
67 _endAxis = params.get<int>("end_axis", -1);
68 setParamsFrom(params);
71 virtual bool supportBackend(int backendId) CV_OVERRIDE
73 return backendId == DNN_BACKEND_OPENCV ||
74 backendId == DNN_BACKEND_CUDA ||
75 (backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine());
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
83 CV_Assert(inputs.size() > 0);
84 for (size_t i = 1; i < inputs.size(); i++)
86 CV_Assert(inputs[i] == inputs[0]);
89 int numAxes = inputs[0].size();
90 int startAxis = clamp(_startAxis, numAxes);
91 int endAxis = clamp(_endAxis, numAxes);
93 CV_Assert(startAxis >= 0);
94 CV_Assert(endAxis >= startAxis && endAxis < (int)numAxes);
96 size_t flattenedDimensionSize = total(inputs[0], startAxis, endAxis + 1);
98 MatShape outputShapeVec;
99 for (int i = 0; i < startAxis; i++)
101 outputShapeVec.push_back(inputs[0][i]);
103 outputShapeVec.push_back(flattenedDimensionSize);
104 for (size_t i = endAxis + 1; i < numAxes; i++)
106 outputShapeVec.push_back(inputs[0][i]);
108 CV_Assert(outputShapeVec.size() <= 4);
110 outputs.resize(inputs.size(), outputShapeVec);
115 void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays) CV_OVERRIDE
117 std::vector<Mat> inputs;
118 inputs_arr.getMatVector(inputs);
120 int numAxes = inputs[0].dims;
121 _startAxis = clamp(_startAxis, numAxes);
122 _endAxis = clamp(_endAxis, numAxes);
126 bool forward_ocl(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)
128 std::vector<UMat> inpvec;
129 std::vector<UMat> outputs;
131 inputs_arr.getUMatVector(inpvec);
132 outputs_arr.getUMatVector(outputs);
134 std::vector<UMat*> inputs(inpvec.size());
135 for (int i = 0; i < inpvec.size(); i++)
136 inputs[i] = &inpvec[i];
138 for (size_t i = 0; i < inputs.size(); i++)
140 MatShape outShape = shape(outputs[i]);
141 UMat& output = outputs_arr.getUMatRef(i);
142 output = inputs[i]->reshape(1, (int)outShape.size(), &outShape[0]);
149 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
152 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
154 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget) &&
155 outputs_arr.isUMatVector(),
156 forward_ocl(inputs_arr, outputs_arr, internals_arr))
158 std::vector<Mat> inputs, outputs;
159 inputs_arr.getMatVector(inputs);
160 outputs_arr.getMatVector(outputs);
162 for (size_t i = 0; i < inputs.size(); i++)
164 MatShape outShape = shape(outputs[i]);
165 if (inputs[i].data != outputs[i].data)
167 inputs[i].reshape(1, (int)outShape.size(), &outShape[0]).copyTo(outputs[i]);
173 Ptr<BackendNode> initCUDA(
175 const std::vector<Ptr<BackendWrapper>>& inputs,
176 const std::vector<Ptr<BackendWrapper>>& outputs
179 auto context = reinterpret_cast<csl::CSLContext*>(context_);
180 return make_cuda_node<cuda4dnn::ReshapeOp>(preferableTarget, std::move(context->stream));
184 #ifdef HAVE_INF_ENGINE
185 virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >& inputs) CV_OVERRIDE
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));
196 #endif // HAVE_INF_ENGINE
202 Ptr<FlattenLayer> FlattenLayer::create(const LayerParams& params)
204 return Ptr<FlattenLayer>(new FlattenLayerImpl(params));