Path: blob/master/modules/dnn/src/layers/flatten_layer.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2013, OpenCV Foundation, all rights reserved.13// Copyright (C) 2017, Intel Corporation, all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "../precomp.hpp"43#include "layers_common.hpp"44#include "../op_inf_engine.hpp"45#include <float.h>46#include <algorithm>47#include <opencv2/dnn/shape_utils.hpp>4849namespace cv50{51namespace dnn52{5354class FlattenLayerImpl CV_FINAL : public FlattenLayer55{56public:57FlattenLayerImpl(const LayerParams ¶ms)58{59_startAxis = params.get<int>("axis", 1);60_endAxis = params.get<int>("end_axis", -1);61setParamsFrom(params);62}6364virtual bool supportBackend(int backendId) CV_OVERRIDE65{66return backendId == DNN_BACKEND_OPENCV ||67backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine();68}6970bool getMemoryShapes(const std::vector<MatShape> &inputs,71const int requiredOutputs,72std::vector<MatShape> &outputs,73std::vector<MatShape> &internals) const CV_OVERRIDE74{75CV_Assert(inputs.size() > 0);76for (size_t i = 1; i < inputs.size(); i++)77{78CV_Assert(inputs[i] == inputs[0]);79}8081int numAxes = inputs[0].size();82int startAxis = clamp(_startAxis, numAxes);83int endAxis = clamp(_endAxis, numAxes);8485CV_Assert(startAxis >= 0);86CV_Assert(endAxis >= startAxis && endAxis < (int)numAxes);8788size_t flattenedDimensionSize = total(inputs[0], startAxis, endAxis + 1);8990MatShape outputShapeVec;91for (int i = 0; i < startAxis; i++)92{93outputShapeVec.push_back(inputs[0][i]);94}95outputShapeVec.push_back(flattenedDimensionSize);96for (size_t i = endAxis + 1; i < numAxes; i++)97{98outputShapeVec.push_back(inputs[0][i]);99}100CV_Assert(outputShapeVec.size() <= 4);101102outputs.resize(inputs.size(), outputShapeVec);103104return true;105}106107#ifdef HAVE_OPENCL108bool forward_ocl(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)109{110std::vector<UMat> inpvec;111std::vector<UMat> outputs;112113inputs_arr.getUMatVector(inpvec);114outputs_arr.getUMatVector(outputs);115116std::vector<UMat*> inputs(inpvec.size());117for (int i = 0; i < inpvec.size(); i++)118inputs[i] = &inpvec[i];119120for (size_t i = 0; i < inputs.size(); i++)121{122MatShape outShape = shape(outputs[i]);123UMat& output = outputs_arr.getUMatRef(i);124output = inputs[i]->reshape(1, (int)outShape.size(), &outShape[0]);125}126127return true;128}129#endif130131void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE132{133CV_TRACE_FUNCTION();134CV_TRACE_ARG_VALUE(name, "name", name.c_str());135136CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget) &&137outputs_arr.isUMatVector(),138forward_ocl(inputs_arr, outputs_arr, internals_arr))139140std::vector<Mat> inputs, outputs;141inputs_arr.getMatVector(inputs);142outputs_arr.getMatVector(outputs);143144for (size_t i = 0; i < inputs.size(); i++)145{146MatShape outShape = shape(outputs[i]);147if (inputs[i].data != outputs[i].data)148{149inputs[i].reshape(1, (int)outShape.size(), &outShape[0]).copyTo(outputs[i]);150}151}152}153154virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE155{156#ifdef HAVE_INF_ENGINE157InferenceEngine::LayerParams lp;158lp.name = name;159lp.type = "Flatten";160lp.precision = InferenceEngine::Precision::FP32;161std::shared_ptr<InferenceEngine::CNNLayer> ieLayer(new InferenceEngine::CNNLayer(lp));162ieLayer->params["axis"] = format("%d", _startAxis);163ieLayer->params["end_axis"] = format("%d", _endAxis);164return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));165#endif // HAVE_INF_ENGINE166return Ptr<BackendNode>();167}168169int _startAxis;170int _endAxis;171};172173Ptr<FlattenLayer> FlattenLayer::create(const LayerParams& params)174{175return Ptr<FlattenLayer>(new FlattenLayerImpl(params));176}177178}179}180181182