Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 8.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "detectionoutput.h"
16
17namespace ncnn {
18
19DetectionOutput::DetectionOutput()
20{
21 one_blob_only = false;
22 support_inplace = false;
23}
24
25int DetectionOutput::load_param(const ParamDict& pd)
26{
27 num_class = pd.get(0, 0);
28 nms_threshold = pd.get(1, 0.05f);
29 nms_top_k = pd.get(2, 300);
30 keep_top_k = pd.get(3, 100);
31 confidence_threshold = pd.get(4, 0.5f);
32 variances[0] = pd.get(5, 0.1f);
33 variances[1] = pd.get(6, 0.1f);
34 variances[2] = pd.get(7, 0.2f);
35 variances[3] = pd.get(8, 0.2f);
36
37 return 0;
38}
39
40struct BBoxRect
41{
42 float xmin;
43 float ymin;
44 float xmax;
45 float ymax;
46 int label;
47};
48
49static inline float intersection_area(const BBoxRect& a, const BBoxRect& b)
50{
51 if (a.xmin > b.xmax || a.xmax < b.xmin || a.ymin > b.ymax || a.ymax < b.ymin)
52 {
53 // no intersection
54 return 0.f;
55 }
56
57 float inter_width = std::min(a.xmax, b.xmax) - std::max(a.xmin, b.xmin);
58 float inter_height = std::min(a.ymax, b.ymax) - std::max(a.ymin, b.ymin);
59
60 return inter_width * inter_height;
61}
62
63template<typename T>
64static void qsort_descent_inplace(std::vector<T>& datas, std::vector<float>& scores, int left, int right)
65{
66 int i = left;
67 int j = right;
68 float p = scores[(left + right) / 2];
69
70 while (i <= j)
71 {
72 while (scores[i] > p)
73 i++;
74
75 while (scores[j] < p)
76 j--;
77
78 if (i <= j)
79 {
80 // swap
81 std::swap(datas[i], datas[j]);
82 std::swap(scores[i], scores[j]);
83
84 i++;
85 j--;
86 }
87 }
88
89 if (left < j)
90 qsort_descent_inplace(datas, scores, left, j);
91
92 if (i < right)
93 qsort_descent_inplace(datas, scores, i, right);
94}
95
96template<typename T>
97static void qsort_descent_inplace(std::vector<T>& datas, std::vector<float>& scores)
98{
99 if (datas.empty() || scores.empty())
100 return;
101
102 qsort_descent_inplace(datas, scores, 0, static_cast<int>(scores.size() - 1));
103}
104
105static void nms_sorted_bboxes(const std::vector<BBoxRect>& bboxes, std::vector<size_t>& picked, float nms_threshold)
106{
107 picked.clear();
108
109 const size_t n = bboxes.size();
110
111 std::vector<float> areas(n);
112 for (size_t i = 0; i < n; i++)
113 {
114 const BBoxRect& r = bboxes[i];
115
116 float width = r.xmax - r.xmin;
117 float height = r.ymax - r.ymin;
118
119 areas[i] = width * height;
120 }
121
122 for (size_t i = 0; i < n; i++)
123 {
124 const BBoxRect& a = bboxes[i];
125
126 int keep = 1;
127 for (int j = 0; j < (int)picked.size(); j++)
128 {
129 const BBoxRect& b = bboxes[picked[j]];
130
131 // intersection over union
132 float inter_area = intersection_area(a, b);
133 float union_area = areas[i] + areas[picked[j]] - inter_area;
134 // float IoU = inter_area / union_area
135 if (inter_area / union_area > nms_threshold)
136 keep = 0;
137 }
138
139 if (keep)
140 picked.push_back(i);
141 }
142}
143
144int DetectionOutput::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
145{
146 const Mat& location = bottom_blobs[0];
147 const Mat& confidence = bottom_blobs[1];
148 const Mat& priorbox = bottom_blobs[2];
149
150 bool mxnet_ssd_style = num_class == -233;
151
152 // mxnet-ssd _contrib_MultiBoxDetection
153 const int num_prior = mxnet_ssd_style ? priorbox.h : priorbox.w / 4;
154
155 int num_class_copy = mxnet_ssd_style ? confidence.h : num_class;
156
157 // apply location with priorbox
158 Mat bboxes;
159 bboxes.create(4, num_prior, 4u, opt.workspace_allocator);
160 if (bboxes.empty())
161 return -100;
162
163 const float* location_ptr = location;
164 const float* priorbox_ptr = priorbox.row(0);
165 const float* variance_ptr = mxnet_ssd_style ? 0 : priorbox.row(1);
166
167 #pragma omp parallel for num_threads(opt.num_threads)
168 for (int i = 0; i < num_prior; i++)
169 {
170 // if score of background class is larger than confidence threshold
171 float score = mxnet_ssd_style ? confidence[i] : confidence[static_cast<size_t>(i) * static_cast<size_t>(num_class_copy)];
172 if (score >= (1.0 - confidence_threshold))
173 {
174 continue;
175 }
176 const float* loc = location_ptr + i * 4;
177 const float* pb = priorbox_ptr + i * 4;
178 const float* var = variance_ptr ? variance_ptr + i * 4 : variances;
179
180 float* bbox = bboxes.row(i);
181
182 // CENTER_SIZE
183 float pb_w = pb[2] - pb[0];
184 float pb_h = pb[3] - pb[1];
185 float pb_cx = (pb[0] + pb[2]) * 0.5f;
186 float pb_cy = (pb[1] + pb[3]) * 0.5f;
187
188 float bbox_cx = var[0] * loc[0] * pb_w + pb_cx;
189 float bbox_cy = var[1] * loc[1] * pb_h + pb_cy;
190 float bbox_w = expf(var[2] * loc[2]) * pb_w;
191 float bbox_h = expf(var[3] * loc[3]) * pb_h;
192
193 bbox[0] = bbox_cx - bbox_w * 0.5f;
194 bbox[1] = bbox_cy - bbox_h * 0.5f;
195 bbox[2] = bbox_cx + bbox_w * 0.5f;
196 bbox[3] = bbox_cy + bbox_h * 0.5f;
197 }
198
199 // sort and nms for each class
200 std::vector<std::vector<BBoxRect> > all_class_bbox_rects;
201 std::vector<std::vector<float> > all_class_bbox_scores;
202 all_class_bbox_rects.resize(num_class_copy);
203 all_class_bbox_scores.resize(num_class_copy);
204
205 // start from 1 to ignore background class
206 #pragma omp parallel for num_threads(opt.num_threads)
207 for (int i = 1; i < num_class_copy; i++)
208 {
209 // filter by confidence_threshold
210 std::vector<BBoxRect> class_bbox_rects;
211 std::vector<float> class_bbox_scores;
212
213 for (int j = 0; j < num_prior; j++)
214 {
215 // prob data layout
216 // caffe-ssd = num_class x num_prior
217 // mxnet-ssd = num_prior x num_class
218 float score = mxnet_ssd_style ? confidence[i * num_prior + j] : confidence[j * num_class_copy + i];
219
220 if (score > confidence_threshold)
221 {
222 const float* bbox = bboxes.row(j);
223 BBoxRect c = {bbox[0], bbox[1], bbox[2], bbox[3], i};
224 class_bbox_rects.push_back(c);
225 class_bbox_scores.push_back(score);
226 }
227 }
228
229 // sort inplace
230 qsort_descent_inplace(class_bbox_rects, class_bbox_scores);
231
232 // keep nms_top_k
233 if (nms_top_k < (int)class_bbox_rects.size())
234 {
235 class_bbox_rects.resize(nms_top_k);
236 class_bbox_scores.resize(nms_top_k);
237 }
238
239 // apply nms
240 std::vector<size_t> picked;
241 nms_sorted_bboxes(class_bbox_rects, picked, nms_threshold);
242
243 // select
244 for (size_t j = 0; j < picked.size(); j++)
245 {
246 size_t z = picked[j];
247 all_class_bbox_rects[i].push_back(class_bbox_rects[z]);
248 all_class_bbox_scores[i].push_back(class_bbox_scores[z]);
249 }
250 }
251
252 // gather all class
253 std::vector<BBoxRect> bbox_rects;
254 std::vector<float> bbox_scores;
255
256 for (int i = 1; i < num_class_copy; i++)
257 {
258 const std::vector<BBoxRect>& class_bbox_rects = all_class_bbox_rects[i];
259 const std::vector<float>& class_bbox_scores = all_class_bbox_scores[i];
260
261 bbox_rects.insert(bbox_rects.end(), class_bbox_rects.begin(), class_bbox_rects.end());
262 bbox_scores.insert(bbox_scores.end(), class_bbox_scores.begin(), class_bbox_scores.end());
263 }
264
265 // global sort inplace
266 qsort_descent_inplace(bbox_rects, bbox_scores);
267
268 // keep_top_k
269 if (keep_top_k < (int)bbox_rects.size())
270 {
271 bbox_rects.resize(keep_top_k);
272 bbox_scores.resize(keep_top_k);
273 }
274
275 // fill result
276 int num_detected = static_cast<int>(bbox_rects.size());
277 if (num_detected == 0)
278 return 0;
279
280 Mat& top_blob = top_blobs[0];
281 top_blob.create(6, num_detected, 4u, opt.blob_allocator);
282 if (top_blob.empty())
283 return -100;
284
285 for (int i = 0; i < num_detected; i++)
286 {
287 const BBoxRect& r = bbox_rects[i];
288 float score = bbox_scores[i];
289 float* outptr = top_blob.row(i);
290
291 outptr[0] = static_cast<float>(r.label);
292 outptr[1] = score;
293 outptr[2] = r.xmin;
294 outptr[3] = r.ymin;
295 outptr[4] = r.xmax;
296 outptr[5] = r.ymax;
297 }
298
299 return 0;
300}
301
302} // namespace ncnn