Elffuss BraidHub

kikocis/ncnn public

master · 9.5 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 "proposal.h"
16
17namespace ncnn {
18
19Proposal::Proposal()
20{
21 one_blob_only = false;
22 support_inplace = false;
23
24 // TODO load from param
25 ratios.create(3);
26 ratios[0] = 0.5f;
27 ratios[1] = 1.f;
28 ratios[2] = 2.f;
29
30 scales.create(3);
31 scales[0] = 8.f;
32 scales[1] = 16.f;
33 scales[2] = 32.f;
34}
35
36static Mat generate_anchors(int base_size, const Mat& ratios, const Mat& scales)
37{
38 int num_ratio = ratios.w;
39 int num_scale = scales.w;
40
41 Mat anchors;
42 anchors.create(4, num_ratio * num_scale);
43
44 const float cx = base_size * 0.5f;
45 const float cy = base_size * 0.5f;
46
47 for (int i = 0; i < num_ratio; i++)
48 {
49 float ar = ratios[i];
50
51 int r_w = static_cast<int>(round(base_size / sqrtf(ar)));
52 int r_h = static_cast<int>(round(r_w * ar)); //round(base_size * sqrt(ar));
53
54 for (int j = 0; j < num_scale; j++)
55 {
56 float scale = scales[j];
57
58 float rs_w = r_w * scale;
59 float rs_h = r_h * scale;
60
61 float* anchor = anchors.row(i * num_scale + j);
62
63 anchor[0] = cx - rs_w * 0.5f;
64 anchor[1] = cy - rs_h * 0.5f;
65 anchor[2] = cx + rs_w * 0.5f;
66 anchor[3] = cy + rs_h * 0.5f;
67 }
68 }
69
70 return anchors;
71}
72
73int Proposal::load_param(const ParamDict& pd)
74{
75 feat_stride = pd.get(0, 16);
76 base_size = pd.get(1, 16);
77 pre_nms_topN = pd.get(2, 6000);
78 after_nms_topN = pd.get(3, 300);
79 nms_thresh = pd.get(4, 0.7f);
80 min_size = pd.get(5, 16);
81
82 // Mat ratio;
83 // Mat scale;
84
85 anchors = generate_anchors(base_size, ratios, scales);
86
87 return 0;
88}
89
90struct Rect
91{
92 float x1;
93 float y1;
94 float x2;
95 float y2;
96};
97
98static inline float intersection_area(const Rect& a, const Rect& b)
99{
100 if (a.x1 > b.x2 || a.x2 < b.x1 || a.y1 > b.y2 || a.y2 < b.y1)
101 {
102 // no intersection
103 return 0.f;
104 }
105
106 float inter_width = std::min(a.x2, b.x2) - std::max(a.x1, b.x1);
107 float inter_height = std::min(a.y2, b.y2) - std::max(a.y1, b.y1);
108
109 return inter_width * inter_height;
110}
111
112template<typename T>
113static void qsort_descent_inplace(std::vector<T>& datas, std::vector<float>& scores, int left, int right)
114{
115 int i = left;
116 int j = right;
117 float p = scores[(left + right) / 2];
118
119 while (i <= j)
120 {
121 while (scores[i] > p)
122 i++;
123
124 while (scores[j] < p)
125 j--;
126
127 if (i <= j)
128 {
129 // swap
130 std::swap(datas[i], datas[j]);
131 std::swap(scores[i], scores[j]);
132
133 i++;
134 j--;
135 }
136 }
137
138 if (left < j)
139 qsort_descent_inplace(datas, scores, left, j);
140
141 if (i < right)
142 qsort_descent_inplace(datas, scores, i, right);
143}
144
145template<typename T>
146static void qsort_descent_inplace(std::vector<T>& datas, std::vector<float>& scores)
147{
148 if (datas.empty() || scores.empty())
149 return;
150
151 qsort_descent_inplace(datas, scores, 0, static_cast<int>(scores.size() - 1));
152}
153
154static void nms_sorted_bboxes(const std::vector<Rect>& bboxes, std::vector<size_t>& picked, float nms_threshold)
155{
156 picked.clear();
157
158 const size_t n = bboxes.size();
159
160 std::vector<float> areas(n);
161 for (size_t i = 0; i < n; i++)
162 {
163 const Rect& r = bboxes[i];
164
165 float width = r.x2 - r.x1;
166 float height = r.y2 - r.y1;
167
168 areas[i] = width * height;
169 }
170
171 for (size_t i = 0; i < n; i++)
172 {
173 const Rect& a = bboxes[i];
174
175 int keep = 1;
176 for (size_t j = 0; j < picked.size(); j++)
177 {
178 const Rect& b = bboxes[picked[j]];
179
180 // intersection over union
181 float inter_area = intersection_area(a, b);
182 float union_area = areas[i] + areas[picked[j]] - inter_area;
183 // float IoU = inter_area / union_area
184 if (inter_area / union_area > nms_threshold)
185 keep = 0;
186 }
187
188 if (keep)
189 picked.push_back(i);
190 }
191}
192
193int Proposal::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
194{
195 const Mat& score_blob = bottom_blobs[0];
196 const Mat& bbox_blob = bottom_blobs[1];
197 const Mat& im_info_blob = bottom_blobs[2];
198
199 int w = score_blob.w;
200 int h = score_blob.h;
201
202 // generate proposals from bbox deltas and shifted anchors
203 const int num_anchors = anchors.h;
204
205 Mat proposals;
206 proposals.create(4, w * h, num_anchors);
207
208 #pragma omp parallel for num_threads(opt.num_threads)
209 for (int q = 0; q < num_anchors; q++)
210 {
211 const float* bbox_xptr = bbox_blob.channel(q * 4);
212 const float* bbox_yptr = bbox_blob.channel(q * 4 + 1);
213 const float* bbox_wptr = bbox_blob.channel(q * 4 + 2);
214 const float* bbox_hptr = bbox_blob.channel(q * 4 + 3);
215
216 Mat pbs = proposals.channel(q);
217
218 const float* anchor = anchors.row(q);
219
220 // shifted anchor
221 float anchor_y = anchor[1];
222
223 float anchor_w = anchor[2] - anchor[0];
224 float anchor_h = anchor[3] - anchor[1];
225
226 for (int i = 0; i < h; i++)
227 {
228 float anchor_x = anchor[0];
229
230 for (int j = 0; j < w; j++)
231 {
232 float* pb = pbs.row(i * w + j);
233
234 // apply center size
235 float dx = bbox_xptr[j];
236 float dy = bbox_yptr[j];
237 float dw = bbox_wptr[j];
238 float dh = bbox_hptr[j];
239
240 float cx = anchor_x + anchor_w * 0.5f;
241 float cy = anchor_y + anchor_h * 0.5f;
242
243 float pb_cx = cx + anchor_w * dx;
244 float pb_cy = cy + anchor_h * dy;
245
246 float pb_w = anchor_w * expf(dw);
247 float pb_h = anchor_h * expf(dh);
248
249 pb[0] = pb_cx - pb_w * 0.5f;
250 pb[1] = pb_cy - pb_h * 0.5f;
251 pb[2] = pb_cx + pb_w * 0.5f;
252 pb[3] = pb_cy + pb_h * 0.5f;
253
254 anchor_x += feat_stride;
255 }
256
257 bbox_xptr += w;
258 bbox_yptr += w;
259 bbox_wptr += w;
260 bbox_hptr += w;
261
262 anchor_y += feat_stride;
263 }
264 }
265
266 // clip predicted boxes to image
267 float im_w = im_info_blob[1];
268 float im_h = im_info_blob[0];
269
270 #pragma omp parallel for num_threads(opt.num_threads)
271 for (int q = 0; q < num_anchors; q++)
272 {
273 Mat pbs = proposals.channel(q);
274
275 for (int i = 0; i < w * h; i++)
276 {
277 float* pb = pbs.row(i);
278
279 // clip box
280 pb[0] = std::max(std::min(pb[0], im_w - 1), 0.f);
281 pb[1] = std::max(std::min(pb[1], im_h - 1), 0.f);
282 pb[2] = std::max(std::min(pb[2], im_w - 1), 0.f);
283 pb[3] = std::max(std::min(pb[3], im_h - 1), 0.f);
284 }
285 }
286
287 // remove predicted boxes with either height or width < threshold
288 std::vector<Rect> proposal_boxes;
289 std::vector<float> scores;
290
291 float im_scale = im_info_blob[2];
292 float min_boxsize = min_size * im_scale;
293
294 for (int q = 0; q < num_anchors; q++)
295 {
296 Mat pbs = proposals.channel(q);
297 const float* scoreptr = score_blob.channel(q + num_anchors);
298
299 for (int i = 0; i < w * h; i++)
300 {
301 float* pb = pbs.row(i);
302
303 float pb_w = pb[2] - pb[0] + 1;
304 float pb_h = pb[3] - pb[1] + 1;
305
306 if (pb_w >= min_boxsize && pb_h >= min_boxsize)
307 {
308 Rect r = {pb[0], pb[1], pb[2], pb[3]};
309 proposal_boxes.push_back(r);
310 scores.push_back(scoreptr[i]);
311 }
312 }
313 }
314
315 // sort all (proposal, score) pairs by score from highest to lowest
316 qsort_descent_inplace(proposal_boxes, scores);
317
318 // take top pre_nms_topN
319 if (pre_nms_topN > 0 && pre_nms_topN < (int)proposal_boxes.size())
320 {
321 proposal_boxes.resize(pre_nms_topN);
322 scores.resize(pre_nms_topN);
323 }
324
325 // apply nms with nms_thresh
326 std::vector<size_t> picked;
327 nms_sorted_bboxes(proposal_boxes, picked, nms_thresh);
328
329 // take after_nms_topN
330 int picked_count = std::min((int)picked.size(), after_nms_topN);
331
332 // return the top proposals
333 Mat& roi_blob = top_blobs[0];
334 roi_blob.create(4, 1, picked_count);
335 if (roi_blob.empty())
336 return -100;
337
338 for (int i = 0; i < picked_count; i++)
339 {
340 float* outptr = roi_blob.channel(i);
341
342 outptr[0] = proposal_boxes[picked[i]].x1;
343 outptr[1] = proposal_boxes[picked[i]].y1;
344 outptr[2] = proposal_boxes[picked[i]].x2;
345 outptr[3] = proposal_boxes[picked[i]].y2;
346 }
347
348 if (top_blobs.size() > 1)
349 {
350 Mat& roi_score_blob = top_blobs[1];
351 roi_score_blob.create(1, 1, picked_count);
352 if (roi_score_blob.empty())
353 return -100;
354
355 for (int i = 0; i < picked_count; i++)
356 {
357 float* outptr = roi_score_blob.channel(i);
358 outptr[0] = scores[picked[i]];
359 }
360 }
361
362 return 0;
363}
364
365} // namespace ncnn