1 /*M ///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "../precomp.hpp"
44 #include <opencv2/dnn/shape_utils.hpp>
45 #include <opencv2/dnn/all_layers.hpp>
46 #include "../nms.inl.hpp"
49 #include "opencl_kernels_dnn.hpp"
57 class RegionLayerImpl CV_FINAL : public RegionLayer
60 int coords, classes, anchors, classfix;
61 float thresh, nmsThreshold;
62 bool useSoftmax, useLogistic;
67 RegionLayerImpl(const LayerParams& params)
69 setParamsFrom(params);
70 CV_Assert(blobs.size() == 1);
72 thresh = params.get<float>("thresh", 0.2);
73 coords = params.get<int>("coords", 4);
74 classes = params.get<int>("classes", 0);
75 anchors = params.get<int>("anchors", 5);
76 classfix = params.get<int>("classfix", 0);
77 useSoftmax = params.get<bool>("softmax", false);
78 useLogistic = params.get<bool>("logistic", false);
79 nmsThreshold = params.get<float>("nms_threshold", 0.4);
81 CV_Assert(nmsThreshold >= 0.);
82 CV_Assert(coords == 4);
83 CV_Assert(classes >= 1);
84 CV_Assert(anchors >= 1);
85 CV_Assert(useLogistic || useSoftmax);
86 if (params.get<bool>("softmax_tree", false))
87 CV_Error(cv::Error::StsNotImplemented, "Yolo9000 is not implemented");
90 bool getMemoryShapes(const std::vector<MatShape> &inputs,
91 const int requiredOutputs,
92 std::vector<MatShape> &outputs,
93 std::vector<MatShape> &internals) const CV_OVERRIDE
95 CV_Assert(inputs.size() > 0);
96 // channels == cell_size*anchors
97 CV_Assert(inputs[0][3] == (1 + coords + classes)*anchors);
98 int batch_size = inputs[0][0];
100 outputs = std::vector<MatShape>(1, shape(batch_size, inputs[0][1] * inputs[0][2] * anchors, inputs[0][3] / anchors));
102 outputs = std::vector<MatShape>(1, shape(inputs[0][1] * inputs[0][2] * anchors, inputs[0][3] / anchors));
106 float logistic_activate(float x) { return 1.F / (1.F + exp(-x)); }
108 void softmax_activate(const float* input, const int n, const float temp, float* output)
112 float largest = -FLT_MAX;
113 for (i = 0; i < n; ++i) {
114 if (input[i] > largest) largest = input[i];
116 for (i = 0; i < n; ++i) {
117 float e = exp((input[i] - largest) / temp);
121 for (i = 0; i < n; ++i) {
127 bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
129 if (blob_umat.empty())
130 blobs[0].copyTo(blob_umat);
132 std::vector<UMat> inputs;
133 std::vector<UMat> outputs;
135 // TODO: implement a logistic activation to classification scores.
136 if (useLogistic || inps.depth() == CV_16S)
139 inps.getUMatVector(inputs);
140 outs.getUMatVector(outputs);
142 CV_Assert(inputs.size() >= 1);
143 int const cell_size = classes + coords + 1;
145 for (size_t ii = 0; ii < outputs.size(); ii++)
147 UMat& inpBlob = inputs[ii];
148 UMat& outBlob = outputs[ii];
150 int batch_size = inpBlob.size[0];
151 int rows = inpBlob.size[1];
152 int cols = inpBlob.size[2];
154 // channels == cell_size*anchors, see l. 94
155 int sample_size = cell_size*rows*cols*anchors;
157 ocl::Kernel logistic_kernel("logistic_activ", ocl::dnn::region_oclsrc);
158 size_t nanchors = rows*cols*anchors*batch_size;
159 logistic_kernel.set(0, (int)nanchors);
160 logistic_kernel.set(1, ocl::KernelArg::PtrReadOnly(inpBlob));
161 logistic_kernel.set(2, (int)cell_size);
162 logistic_kernel.set(3, ocl::KernelArg::PtrWriteOnly(outBlob));
163 logistic_kernel.run(1, &nanchors, NULL, false);
168 // softmax activation for Probability, for each grid cell (X x Y x Anchor-index)
169 ocl::Kernel softmax_kernel("softmax_activ", ocl::dnn::region_oclsrc);
170 size_t nanchors = rows*cols*anchors*batch_size;
171 softmax_kernel.set(0, (int)nanchors);
172 softmax_kernel.set(1, ocl::KernelArg::PtrReadOnly(inpBlob));
173 softmax_kernel.set(2, ocl::KernelArg::PtrReadOnly(blob_umat));
174 softmax_kernel.set(3, (int)cell_size);
175 softmax_kernel.set(4, (int)classes);
176 softmax_kernel.set(5, (int)classfix);
177 softmax_kernel.set(6, (int)rows);
178 softmax_kernel.set(7, (int)cols);
179 softmax_kernel.set(8, (int)anchors);
180 softmax_kernel.set(9, (float)thresh);
181 softmax_kernel.set(10, ocl::KernelArg::PtrWriteOnly(outBlob));
182 if (!softmax_kernel.run(1, &nanchors, NULL, false))
186 if (nmsThreshold > 0) {
187 Mat mat = outBlob.getMat(ACCESS_WRITE);
188 float *dstData = mat.ptr<float>();
189 for (int b = 0; b < batch_size; ++b)
190 do_nms_sort(dstData + b*sample_size, rows*cols*anchors, thresh, nmsThreshold);
199 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
202 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
204 CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
205 forward_ocl(inputs_arr, outputs_arr, internals_arr))
207 if (inputs_arr.depth() == CV_16S)
209 forward_fallback(inputs_arr, outputs_arr, internals_arr);
213 std::vector<Mat> inputs, outputs, internals;
214 inputs_arr.getMatVector(inputs);
215 outputs_arr.getMatVector(outputs);
216 internals_arr.getMatVector(internals);
218 CV_Assert(inputs.size() >= 1);
219 CV_Assert(outputs.size() == 1);
220 int const cell_size = classes + coords + 1;
222 const float* biasData = blobs[0].ptr<float>();
224 for (size_t ii = 0; ii < outputs.size(); ii++)
226 Mat &inpBlob = inputs[ii];
227 Mat &outBlob = outputs[ii];
229 int batch_size = inpBlob.size[0];
230 int rows = inpBlob.size[1];
231 int cols = inpBlob.size[2];
233 // address length for one image in batch, both for input and output
234 int sample_size = cell_size*rows*cols*anchors;
236 // assert that the comment above is true
237 CV_Assert(sample_size*batch_size == inpBlob.total());
238 CV_Assert(sample_size*batch_size == outBlob.total());
240 CV_Assert(inputs.size() < 2 || inputs[1].dims == 4);
241 int hNorm = inputs.size() > 1 ? inputs[1].size[2] : rows;
242 int wNorm = inputs.size() > 1 ? inputs[1].size[3] : cols;
244 const float *srcData = inpBlob.ptr<float>();
245 float *dstData = outBlob.ptr<float>();
247 // logistic activation for t0, for each grid cell (X x Y x Anchor-index)
248 for (int i = 0; i < batch_size*rows*cols*anchors; ++i) {
249 int index = cell_size*i;
250 float x = srcData[index + 4];
251 dstData[index + 4] = logistic_activate(x); // logistic activation
254 if (useSoftmax) { // Yolo v2
255 for (int i = 0; i < batch_size*rows*cols*anchors; ++i) {
256 int index = cell_size*i;
257 softmax_activate(srcData + index + 5, classes, 1, dstData + index + 5);
260 else if (useLogistic) { // Yolo v3
261 for (int i = 0; i < batch_size*rows*cols*anchors; ++i){
262 int index = cell_size*i;
263 const float* input = srcData + index + 5;
264 float* output = dstData + index + 5;
265 for (int c = 0; c < classes; ++c)
266 output[c] = logistic_activate(input[c]);
269 for (int b = 0; b < batch_size; ++b)
270 for (int x = 0; x < cols; ++x)
271 for(int y = 0; y < rows; ++y)
272 for (int a = 0; a < anchors; ++a) {
273 // relative start address for image b within the batch data
274 int index_sample_offset = sample_size*b;
275 int index = (y*cols + x)*anchors + a; // index for each grid-cell & anchor
276 int p_index = index_sample_offset + index * cell_size + 4;
277 float scale = dstData[p_index];
278 if (classfix == -1 && scale < .5) scale = 0; // if(t0 < 0.5) t0 = 0;
279 int box_index = index_sample_offset + index * cell_size;
281 dstData[box_index + 0] = (x + logistic_activate(srcData[box_index + 0])) / cols;
282 dstData[box_index + 1] = (y + logistic_activate(srcData[box_index + 1])) / rows;
283 dstData[box_index + 2] = exp(srcData[box_index + 2]) * biasData[2 * a] / wNorm;
284 dstData[box_index + 3] = exp(srcData[box_index + 3]) * biasData[2 * a + 1] / hNorm;
286 int class_index = index_sample_offset + index * cell_size + 5;
287 for (int j = 0; j < classes; ++j) {
288 float prob = scale*dstData[class_index + j]; // prob = IoU(box, object) = t0 * class-probability
289 dstData[class_index + j] = (prob > thresh) ? prob : 0; // if (IoU < threshold) IoU = 0;
292 if (nmsThreshold > 0) {
293 for (int b = 0; b < batch_size; ++b){
294 do_nms_sort(dstData+b*sample_size, rows*cols*anchors, thresh, nmsThreshold);
300 void do_nms_sort(float *detections, int total, float score_thresh, float nms_thresh)
302 std::vector<Rect2d> boxes(total);
303 std::vector<float> scores(total);
305 for (int i = 0; i < total; ++i)
307 Rect2d &b = boxes[i];
308 int box_index = i * (classes + coords + 1);
309 b.width = detections[box_index + 2];
310 b.height = detections[box_index + 3];
311 b.x = detections[box_index + 0] - b.width / 2;
312 b.y = detections[box_index + 1] - b.height / 2;
315 std::vector<int> indices;
316 for (int k = 0; k < classes; ++k)
318 for (int i = 0; i < total; ++i)
320 int box_index = i * (classes + coords + 1);
321 int class_index = box_index + 5;
322 scores[i] = detections[class_index + k];
323 detections[class_index + k] = 0;
325 NMSBoxes(boxes, scores, score_thresh, nms_thresh, indices);
326 for (int i = 0, n = indices.size(); i < n; ++i)
328 int box_index = indices[i] * (classes + coords + 1);
329 int class_index = box_index + 5;
330 detections[class_index + k] = scores[indices[i]];
335 virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
336 const std::vector<MatShape> &outputs) const CV_OVERRIDE
338 CV_UNUSED(outputs); // suppress unused variable warning
341 for(int i = 0; i < inputs.size(); i++)
343 flops += 60*total(inputs[i]);
349 Ptr<RegionLayer> RegionLayer::create(const LayerParams& params)
351 return Ptr<RegionLayer>(new RegionLayerImpl(params));