Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/layers/crop_layer.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
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.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
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.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
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.
26
//
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.
29
//
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.
40
//
41
//M*/
42
43
#include "../precomp.hpp"
44
#include "../op_inf_engine.hpp"
45
#include "layers_common.hpp"
46
47
namespace cv
48
{
49
namespace dnn
50
{
51
52
class CropLayerImpl CV_FINAL : public CropLayer
53
{
54
public:
55
CropLayerImpl(const LayerParams& params)
56
{
57
setParamsFrom(params);
58
startAxis = params.get<int>("axis", 2);
59
const DictValue *paramOffset = params.ptr("offset");
60
61
if (paramOffset)
62
{
63
for (int i = 0; i < paramOffset->size(); i++)
64
offset.push_back(paramOffset->get<int>(i));
65
}
66
}
67
68
virtual bool supportBackend(int backendId) CV_OVERRIDE
69
{
70
return backendId == DNN_BACKEND_OPENCV ||
71
backendId == DNN_BACKEND_INFERENCE_ENGINE && crop_ranges.size() == 4;
72
}
73
74
bool getMemoryShapes(const std::vector<MatShape> &inputs,
75
const int requiredOutputs,
76
std::vector<MatShape> &outputs,
77
std::vector<MatShape> &internals) const CV_OVERRIDE
78
{
79
CV_Assert(inputs.size() == 2);
80
81
MatShape dstShape = inputs[0];
82
int start = clamp(startAxis, dstShape);
83
for (int i = start; i < dstShape.size(); i++)
84
{
85
dstShape[i] = inputs[1][i];
86
}
87
88
outputs.resize(1, dstShape);
89
90
return false;
91
}
92
93
void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays) CV_OVERRIDE
94
{
95
std::vector<Mat> inputs;
96
inputs_arr.getMatVector(inputs);
97
CV_Assert(2 == inputs.size());
98
99
const Mat &inpBlob = inputs[0];
100
const Mat &inpSzBlob = inputs[1];
101
102
int dims = inpBlob.dims;
103
int start_axis = clamp(startAxis, dims);
104
105
std::vector<int> offset_final(dims, 0);
106
if (offset.size() == 1)
107
{
108
for (int i = start_axis; i < dims; i++)
109
offset_final[i] = offset[0];
110
}
111
else if (offset.size() > 1)
112
{
113
if ((int)offset.size() != dims - start_axis)
114
CV_Error(Error::StsBadArg, "number of offset values specified must be "
115
"equal to the number of dimensions following axis.");
116
117
for (int i = start_axis; i < dims; i++)
118
offset_final[i] = offset[i - start_axis];
119
}
120
121
crop_ranges.resize(dims);
122
for (int i = 0; i < start_axis; i++)
123
{
124
crop_ranges[i] = Range(0, inpBlob.size[i]);
125
}
126
for (int i = start_axis; i < dims; i++)
127
{
128
if (offset_final[i] < 0 || offset_final[i] + inpSzBlob.size[i] > inpBlob.size[i])
129
CV_Error(Error::StsBadArg, "invalid crop parameters or blob sizes");
130
131
crop_ranges[i] = Range(offset_final[i], offset_final[i] + inpSzBlob.size[i]);
132
}
133
}
134
135
void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
136
{
137
CV_TRACE_FUNCTION();
138
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
139
140
std::vector<Mat> inputs, outputs;
141
inputs_arr.getMatVector(inputs);
142
outputs_arr.getMatVector(outputs);
143
144
Mat &input = inputs[0];
145
input(&crop_ranges[0]).copyTo(outputs[0]);
146
}
147
148
virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> >&) CV_OVERRIDE
149
{
150
#ifdef HAVE_INF_ENGINE
151
InferenceEngine::LayerParams lp;
152
lp.name = name;
153
lp.type = "Crop";
154
lp.precision = InferenceEngine::Precision::FP32;
155
std::shared_ptr<InferenceEngine::CropLayer> ieLayer(new InferenceEngine::CropLayer(lp));
156
157
CV_Assert(crop_ranges.size() == 4);
158
159
ieLayer->axis.push_back(0); // batch
160
ieLayer->offset.push_back(crop_ranges[0].start);
161
ieLayer->dim.push_back(crop_ranges[0].end - crop_ranges[0].start);
162
163
ieLayer->axis.push_back(1); // channels
164
ieLayer->offset.push_back(crop_ranges[1].start);
165
ieLayer->dim.push_back(crop_ranges[1].end - crop_ranges[1].start);
166
167
ieLayer->axis.push_back(3); // height
168
ieLayer->offset.push_back(crop_ranges[2].start);
169
ieLayer->dim.push_back(crop_ranges[2].end - crop_ranges[2].start);
170
171
ieLayer->axis.push_back(2); // width
172
ieLayer->offset.push_back(crop_ranges[3].start);
173
ieLayer->dim.push_back(crop_ranges[3].end - crop_ranges[3].start);
174
175
return Ptr<BackendNode>(new InfEngineBackendNode(ieLayer));
176
#endif // HAVE_INF_ENGINE
177
return Ptr<BackendNode>();
178
}
179
180
std::vector<Range> crop_ranges;
181
};
182
183
184
Ptr<CropLayer> CropLayer::create(const LayerParams& params)
185
{
186
return Ptr<CropLayer>(new CropLayerImpl(params));
187
}
188
189
}
190
}
191
192