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_inf_engine.hpp"
48 #include <opencv2/dnn/shape_utils.hpp>
55 class FlattenLayerImpl CV_FINAL : public FlattenLayer
58 FlattenLayerImpl(const LayerParams ¶ms)
60 _startAxis = params.get<int>("axis", 1);
61 _endAxis = params.get<int>("end_axis", -1);
62 setParamsFrom(params);
65 virtual bool supportBackend(int backendId) CV_OVERRIDE
67 return backendId == DNN_BACKEND_OPENCV ||
68 (backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine());
71 bool getMemoryShapes(const std::vector<MatShape> &inputs,
72 const int requiredOutputs,
73 std::vector<MatShape> &outputs,
74 std::vector<MatShape> &internals) const CV_OVERRIDE
76 CV_Assert(inputs.size() > 0);
77 for (size_t i = 1; i < inputs.size(); i++)
79 CV_Assert(inputs[i] == inputs[0]);
82 int numAxes = inputs[0].size();
83 int startAxis = clamp(_startAxis, numAxes);
84 int endAxis = clamp(_endAxis, numAxes);
86 CV_Assert(startAxis >= 0);
87 CV_Assert(endAxis >= startAxis && endAxis < (int)numAxes);
89 size_t flattenedDimensionSize = total(inputs[0], startAxis, endAxis + 1);
91 MatShape outputShapeVec;
92 for (int i = 0; i < startAxis; i++)
94 outputShapeVec.push_back(inputs[0][i]);
96 outputShapeVec.push_back(flattenedDimensionSize);
97 for (size_t i = endAxis + 1; i < numAxes; i++)
99 outputShapeVec.push_back(inputs[0][i]);
101 CV_Assert(outputShapeVec.size() <= 4);
103 outputs.resize(inputs.size(), outputShapeVec);
108 void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays) CV_OVERRIDE
110 std::vector<Mat> inputs;
111 inputs_arr.getMatVector(inputs);
113 int numAxes = inputs[0].dims;
114 _startAxis = clamp(_startAxis, numAxes);
115 _endAxis = clamp(_endAxis, numAxes);
119 bool forward_ocl(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)
121 std::vector<UMat> inpvec;
122 std::vector<UMat> outputs;
124 inputs_arr.getUMatVector(inpvec);
125 outputs_arr.getUMatVector(outputs);
127 std::vector<UMat*> inputs(inpvec.size());
128 for (int i = 0; i < inpvec.size(); i++)
129 inputs[i] = &inpvec[i];
131 for (size_t i = 0; i < inputs.size(); i++)
133 MatShape outShape = shape(outputs[i]);
134 UMat& output = outputs_arr.getUMatRef(i);
135 output = inputs[i]->reshape(1, (int)outShape.size(), &outShape[0]);
142 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
145 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
147 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget) &&
148 outputs_arr.isUMatVector(),
149 forward_ocl(inputs_arr, outputs_arr, internals_arr))
151 std::vector<Mat> inputs, outputs;
152 inputs_arr.getMatVector(inputs);
153 outputs_arr.getMatVector(outputs);
155 for (size_t i = 0; i < inputs.size(); i++)
157 MatShape outShape = shape(outputs[i]);
158 if (inputs[i].data != outputs[i].data)
160 inputs[i].reshape(1, (int)outShape.size(), &outShape[0]).copyTo(outputs[i]);
165 #ifdef HAVE_INF_ENGINE
166 virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >& inputs) CV_OVERRIDE
168 InferenceEngine::Builder::Layer ieLayer(name);
169 ieLayer.setName(name);
170 ieLayer.setType("Flatten");
171 ieLayer.getParameters()["axis"] = (size_t)_startAxis;
172 ieLayer.getParameters()["end_axis"] = _endAxis; // Do not cast to size_t because it might be negative.
173 ieLayer.setInputPorts(std::vector<InferenceEngine::Port>(1));
174 ieLayer.setOutputPorts(std::vector<InferenceEngine::Port>(1));
175 return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
177 #endif // HAVE_INF_ENGINE
183 Ptr<FlattenLayer> FlattenLayer::create(const LayerParams& params)
185 return Ptr<FlattenLayer>(new FlattenLayerImpl(params));