Path: blob/master/modules/dnn/src/layers/reorg_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 "../op_inf_engine.hpp"44#include <opencv2/dnn/shape_utils.hpp>45#include <opencv2/dnn/all_layers.hpp>4647#ifdef HAVE_OPENCL48#include "opencl_kernels_dnn.hpp"49#endif5051namespace cv52{53namespace dnn54{5556class ReorgLayerImpl CV_FINAL : public ReorgLayer57{58int reorgStride;59public:6061ReorgLayerImpl(const LayerParams& params)62{63setParamsFrom(params);6465reorgStride = params.get<int>("reorg_stride", 2);66CV_Assert(reorgStride > 0);67}6869bool getMemoryShapes(const std::vector<MatShape> &inputs,70const int requiredOutputs,71std::vector<MatShape> &outputs,72std::vector<MatShape> &internals) const CV_OVERRIDE73{74CV_Assert(inputs.size() > 0);75outputs = std::vector<MatShape>(inputs.size(), shape(76inputs[0][0],77inputs[0][1] * reorgStride * reorgStride,78inputs[0][2] / reorgStride,79inputs[0][3] / reorgStride));8081CV_Assert(outputs[0][0] > 0 && outputs[0][1] > 0 && outputs[0][2] > 0 && outputs[0][3] > 0);82CV_Assert(total(outputs[0]) == total(inputs[0]));8384return false;85}8687virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE88{89std::vector<Mat> inputs, outputs;90inputs_arr.getMatVector(inputs);91outputs_arr.getMatVector(outputs);9293Mat inp = inputs[0];94Mat out = outputs[0];95int batchSize = inp.size[0];9697LayerParams permParams;98if (batchSize == 1)99{100int order[] = {1, 3, 0, 2};101permParams.set("order", DictValue::arrayInt(&order[0], 4));102103permuteInpShape.resize(4);104permuteInpShape[0] = inp.size[1] * inp.size[2] / (reorgStride * reorgStride); // (channels*height)/(r*r)105permuteInpShape[1] = reorgStride;106permuteInpShape[2] = inp.size[3]; // width107permuteInpShape[3] = reorgStride;108109permuteOutShape.resize(4);110for (int i = 0; i < 4; ++i)111permuteOutShape[i] = permuteInpShape[order[i]];112}113else114{115int order[] = {0, 2, 4, 1, 3};116permParams.set("order", DictValue::arrayInt(&order[0], 5));117118permuteInpShape.resize(5);119permuteInpShape[0] = batchSize;120permuteInpShape[1] = inp.size[1] * inp.size[2] / (reorgStride * reorgStride); // (channels*height)/(r*r)121permuteInpShape[2] = reorgStride;122permuteInpShape[3] = inp.size[3]; // width123permuteInpShape[4] = reorgStride;124125permuteOutShape.resize(5);126for (int i = 0; i < 5; ++i)127permuteOutShape[i] = permuteInpShape[order[i]];128}129permute = PermuteLayer::create(permParams);130std::vector<Mat> permuteInputs(1, inp.reshape(1, permuteInpShape));131std::vector<Mat> permuteOutputs(1, out.reshape(1, permuteOutShape));132permute->finalize(permuteInputs, permuteOutputs);133}134135virtual bool supportBackend(int backendId) CV_OVERRIDE136{137return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_INFERENCE_ENGINE;138}139140#ifdef HAVE_OPENCL141bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)142{143std::vector<UMat> inputs;144std::vector<UMat> outputs;145146inps.getUMatVector(inputs);147outs.getUMatVector(outputs);148149inputs[0] = inputs[0].reshape(1, permuteInpShape.size(), &permuteInpShape[0]);150outputs[0] = outputs[0].reshape(1, permuteOutShape.size(), &permuteOutShape[0]);151permute->preferableTarget = preferableTarget;152permute->forward(inputs, outputs, internals);153return true;154}155#endif156157void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE158{159CV_TRACE_FUNCTION();160CV_TRACE_ARG_VALUE(name, "name", name.c_str());161162CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),163forward_ocl(inputs_arr, outputs_arr, internals_arr))164165if (inputs_arr.depth() == CV_16S)166{167forward_fallback(inputs_arr, outputs_arr, internals_arr);168return;169}170171std::vector<Mat> inputs, outputs;172inputs_arr.getMatVector(inputs);173outputs_arr.getMatVector(outputs);174175inputs[0] = inputs[0].reshape(1, permuteInpShape);176outputs[0] = outputs[0].reshape(1, permuteOutShape);177permute->forward(inputs, outputs, internals_arr);178}179180virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE181{182#ifdef HAVE_INF_ENGINE183InferenceEngine::LayerParams lp;184lp.name = name;185lp.type = "ReorgYolo";186lp.precision = InferenceEngine::Precision::FP32;187std::shared_ptr<InferenceEngine::CNNLayer> ieLayer(new InferenceEngine::CNNLayer(lp));188ieLayer->params["stride"] = format("%d", reorgStride);189return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));190#endif // HAVE_INF_ENGINE191return Ptr<BackendNode>();192}193194virtual int64 getFLOPS(const std::vector<MatShape> &inputs,195const std::vector<MatShape> &outputs) const CV_OVERRIDE196{197CV_UNUSED(outputs); // suppress unused variable warning198199int64 flops = 0;200for(int i = 0; i < inputs.size(); i++)201{202flops += 21*total(inputs[i]);203}204return flops;205}206207private:208Ptr<PermuteLayer> permute;209std::vector<int> permuteInpShape, permuteOutShape;210};211212Ptr<ReorgLayer> ReorgLayer::create(const LayerParams& params)213{214return Ptr<ReorgLayer>(new ReorgLayerImpl(params));215}216217} // namespace dnn218} // namespace cv219220221