Path: blob/master/modules/dnn/src/layers/crop_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 "layers_common.hpp"4546namespace cv47{48namespace dnn49{5051class CropLayerImpl CV_FINAL : public CropLayer52{53public:54CropLayerImpl(const LayerParams& params)55{56setParamsFrom(params);57startAxis = params.get<int>("axis", 2);58const DictValue *paramOffset = params.ptr("offset");5960if (paramOffset)61{62for (int i = 0; i < paramOffset->size(); i++)63offset.push_back(paramOffset->get<int>(i));64}65}6667virtual bool supportBackend(int backendId) CV_OVERRIDE68{69return backendId == DNN_BACKEND_OPENCV ||70backendId == DNN_BACKEND_INFERENCE_ENGINE && crop_ranges.size() == 4;71}7273bool getMemoryShapes(const std::vector<MatShape> &inputs,74const int requiredOutputs,75std::vector<MatShape> &outputs,76std::vector<MatShape> &internals) const CV_OVERRIDE77{78CV_Assert(inputs.size() == 2);7980MatShape dstShape = inputs[0];81int start = clamp(startAxis, dstShape);82for (int i = start; i < dstShape.size(); i++)83{84dstShape[i] = inputs[1][i];85}8687outputs.resize(1, dstShape);8889return false;90}9192void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays) CV_OVERRIDE93{94std::vector<Mat> inputs;95inputs_arr.getMatVector(inputs);96CV_Assert(2 == inputs.size());9798const Mat &inpBlob = inputs[0];99const Mat &inpSzBlob = inputs[1];100101int dims = inpBlob.dims;102int start_axis = clamp(startAxis, dims);103104std::vector<int> offset_final(dims, 0);105if (offset.size() == 1)106{107for (int i = start_axis; i < dims; i++)108offset_final[i] = offset[0];109}110else if (offset.size() > 1)111{112if ((int)offset.size() != dims - start_axis)113CV_Error(Error::StsBadArg, "number of offset values specified must be "114"equal to the number of dimensions following axis.");115116for (int i = start_axis; i < dims; i++)117offset_final[i] = offset[i - start_axis];118}119120crop_ranges.resize(dims);121for (int i = 0; i < start_axis; i++)122{123crop_ranges[i] = Range(0, inpBlob.size[i]);124}125for (int i = start_axis; i < dims; i++)126{127if (offset_final[i] < 0 || offset_final[i] + inpSzBlob.size[i] > inpBlob.size[i])128CV_Error(Error::StsBadArg, "invalid crop parameters or blob sizes");129130crop_ranges[i] = Range(offset_final[i], offset_final[i] + inpSzBlob.size[i]);131}132}133134void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE135{136CV_TRACE_FUNCTION();137CV_TRACE_ARG_VALUE(name, "name", name.c_str());138139std::vector<Mat> inputs, outputs;140inputs_arr.getMatVector(inputs);141outputs_arr.getMatVector(outputs);142143Mat &input = inputs[0];144input(&crop_ranges[0]).copyTo(outputs[0]);145}146147virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE148{149#ifdef HAVE_INF_ENGINE150InferenceEngine::LayerParams lp;151lp.name = name;152lp.type = "Crop";153lp.precision = InferenceEngine::Precision::FP32;154std::shared_ptr<InferenceEngine::CropLayer> ieLayer(new InferenceEngine::CropLayer(lp));155156CV_Assert(crop_ranges.size() == 4);157158ieLayer->axis.push_back(0); // batch159ieLayer->offset.push_back(crop_ranges[0].start);160ieLayer->dim.push_back(crop_ranges[0].end - crop_ranges[0].start);161162ieLayer->axis.push_back(1); // channels163ieLayer->offset.push_back(crop_ranges[1].start);164ieLayer->dim.push_back(crop_ranges[1].end - crop_ranges[1].start);165166ieLayer->axis.push_back(3); // height167ieLayer->offset.push_back(crop_ranges[2].start);168ieLayer->dim.push_back(crop_ranges[2].end - crop_ranges[2].start);169170ieLayer->axis.push_back(2); // width171ieLayer->offset.push_back(crop_ranges[3].start);172ieLayer->dim.push_back(crop_ranges[3].end - crop_ranges[3].start);173174return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));175#endif // HAVE_INF_ENGINE176return Ptr<BackendNode>();177}178179std::vector<Range> crop_ranges;180};181182183Ptr<CropLayer> CropLayer::create(const LayerParams& params)184{185return Ptr<CropLayer>(new CropLayerImpl(params));186}187188}189}190191192