2017-06-26 18:35:51 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
2017-06-28 16:15:22 +08:00
|
|
|
// Copyright (C) 2017, Intel Corporation, all rights reserved.
|
2017-06-26 18:35:51 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "../precomp.hpp"
|
|
|
|
#include "layers_common.hpp"
|
2018-03-16 21:36:11 +08:00
|
|
|
#include "../op_inf_engine.hpp"
|
2017-06-26 18:35:51 +08:00
|
|
|
#include <float.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <opencv2/dnn/shape_utils.hpp>
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
namespace dnn
|
|
|
|
{
|
|
|
|
|
2018-03-15 21:16:56 +08:00
|
|
|
class FlattenLayerImpl CV_FINAL : public FlattenLayer
|
2017-06-26 18:35:51 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
FlattenLayerImpl(const LayerParams ¶ms)
|
|
|
|
{
|
|
|
|
_startAxis = params.get<int>("axis", 1);
|
|
|
|
_endAxis = params.get<int>("end_axis", -1);
|
|
|
|
setParamsFrom(params);
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:16:56 +08:00
|
|
|
virtual bool supportBackend(int backendId) CV_OVERRIDE
|
2018-02-06 16:57:35 +08:00
|
|
|
{
|
2018-06-01 15:54:12 +08:00
|
|
|
return backendId == DNN_BACKEND_OPENCV ||
|
2018-11-15 04:25:23 +08:00
|
|
|
(backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine());
|
2018-02-06 16:57:35 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 18:35:51 +08:00
|
|
|
bool getMemoryShapes(const std::vector<MatShape> &inputs,
|
|
|
|
const int requiredOutputs,
|
|
|
|
std::vector<MatShape> &outputs,
|
2018-03-15 21:16:56 +08:00
|
|
|
std::vector<MatShape> &internals) const CV_OVERRIDE
|
2017-06-26 18:35:51 +08:00
|
|
|
{
|
|
|
|
CV_Assert(inputs.size() > 0);
|
|
|
|
for (size_t i = 1; i < inputs.size(); i++)
|
|
|
|
{
|
|
|
|
CV_Assert(inputs[i] == inputs[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int numAxes = inputs[0].size();
|
|
|
|
int startAxis = clamp(_startAxis, numAxes);
|
|
|
|
int endAxis = clamp(_endAxis, numAxes);
|
|
|
|
|
|
|
|
CV_Assert(startAxis >= 0);
|
|
|
|
CV_Assert(endAxis >= startAxis && endAxis < (int)numAxes);
|
|
|
|
|
|
|
|
size_t flattenedDimensionSize = total(inputs[0], startAxis, endAxis + 1);
|
|
|
|
|
|
|
|
MatShape outputShapeVec;
|
|
|
|
for (int i = 0; i < startAxis; i++)
|
|
|
|
{
|
|
|
|
outputShapeVec.push_back(inputs[0][i]);
|
|
|
|
}
|
|
|
|
outputShapeVec.push_back(flattenedDimensionSize);
|
|
|
|
for (size_t i = endAxis + 1; i < numAxes; i++)
|
|
|
|
{
|
|
|
|
outputShapeVec.push_back(inputs[0][i]);
|
|
|
|
}
|
|
|
|
CV_Assert(outputShapeVec.size() <= 4);
|
|
|
|
|
|
|
|
outputs.resize(inputs.size(), outputShapeVec);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-09 12:57:37 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
bool forward_ocl(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)
|
|
|
|
{
|
|
|
|
std::vector<UMat> inpvec;
|
|
|
|
std::vector<UMat> outputs;
|
|
|
|
|
|
|
|
inputs_arr.getUMatVector(inpvec);
|
|
|
|
outputs_arr.getUMatVector(outputs);
|
|
|
|
|
|
|
|
std::vector<UMat*> inputs(inpvec.size());
|
|
|
|
for (int i = 0; i < inpvec.size(); i++)
|
|
|
|
inputs[i] = &inpvec[i];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < inputs.size(); i++)
|
|
|
|
{
|
|
|
|
MatShape outShape = shape(outputs[i]);
|
|
|
|
UMat& output = outputs_arr.getUMatRef(i);
|
|
|
|
output = inputs[i]->reshape(1, (int)outShape.size(), &outShape[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-15 21:16:56 +08:00
|
|
|
void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
|
2017-11-09 12:57:37 +08:00
|
|
|
{
|
|
|
|
CV_TRACE_FUNCTION();
|
|
|
|
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
|
|
|
|
|
2018-04-26 19:22:29 +08:00
|
|
|
CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget) &&
|
2018-09-26 21:27:00 +08:00
|
|
|
outputs_arr.isUMatVector(),
|
2017-11-09 12:57:37 +08:00
|
|
|
forward_ocl(inputs_arr, outputs_arr, internals_arr))
|
|
|
|
|
2018-09-06 18:26:47 +08:00
|
|
|
std::vector<Mat> inputs, outputs;
|
|
|
|
inputs_arr.getMatVector(inputs);
|
|
|
|
outputs_arr.getMatVector(outputs);
|
2017-06-28 19:46:58 +08:00
|
|
|
|
2017-06-26 18:35:51 +08:00
|
|
|
for (size_t i = 0; i < inputs.size(); i++)
|
|
|
|
{
|
|
|
|
MatShape outShape = shape(outputs[i]);
|
2018-09-06 18:26:47 +08:00
|
|
|
if (inputs[i].data != outputs[i].data)
|
|
|
|
{
|
|
|
|
inputs[i].reshape(1, (int)outShape.size(), &outShape[0]).copyTo(outputs[i]);
|
|
|
|
}
|
2017-06-26 18:35:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 14:55:44 +08:00
|
|
|
virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >& inputs) CV_OVERRIDE
|
2018-02-06 16:57:35 +08:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INF_ENGINE
|
2019-01-14 14:55:44 +08:00
|
|
|
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2018R5)
|
|
|
|
InferenceEngine::Builder::Layer ieLayer(name);
|
|
|
|
ieLayer.setName(name);
|
|
|
|
ieLayer.setType("Flatten");
|
|
|
|
ieLayer.getParameters()["axis"] = _startAxis;
|
|
|
|
ieLayer.getParameters()["end_axis"] = _endAxis;
|
|
|
|
ieLayer.setInputPorts(std::vector<InferenceEngine::Port>(1));
|
|
|
|
ieLayer.setOutputPorts(std::vector<InferenceEngine::Port>(1));
|
|
|
|
return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
|
|
|
|
#else
|
2018-02-06 16:57:35 +08:00
|
|
|
InferenceEngine::LayerParams lp;
|
|
|
|
lp.name = name;
|
|
|
|
lp.type = "Flatten";
|
|
|
|
lp.precision = InferenceEngine::Precision::FP32;
|
|
|
|
std::shared_ptr<InferenceEngine::CNNLayer> ieLayer(new InferenceEngine::CNNLayer(lp));
|
|
|
|
ieLayer->params["axis"] = format("%d", _startAxis);
|
|
|
|
ieLayer->params["end_axis"] = format("%d", _endAxis);
|
|
|
|
return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
|
2019-01-14 14:55:44 +08:00
|
|
|
#endif
|
2018-02-06 16:57:35 +08:00
|
|
|
#endif // HAVE_INF_ENGINE
|
|
|
|
return Ptr<BackendNode>();
|
|
|
|
}
|
|
|
|
|
2017-06-26 18:35:51 +08:00
|
|
|
int _startAxis;
|
|
|
|
int _endAxis;
|
|
|
|
};
|
|
|
|
|
|
|
|
Ptr<FlattenLayer> FlattenLayer::create(const LayerParams& params)
|
|
|
|
{
|
|
|
|
return Ptr<FlattenLayer>(new FlattenLayerImpl(params));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|