-
Notifications
You must be signed in to change notification settings - Fork 704
/
ncnn_nanodet.cpp
243 lines (213 loc) · 8.63 KB
/
ncnn_nanodet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// Created by DefTruth on 2021/10/7.
//
#include "ncnn_nanodet.h"
#include "lite/utils.h"
using ncnncv::NCNNNanoDet;
NCNNNanoDet::NCNNNanoDet(const std::string &_param_path,
const std::string &_bin_path,
unsigned int _num_threads,
int _input_height, int _input_width) :
BasicNCNNHandler(_param_path, _bin_path, _num_threads)
{
input_height = _input_height;
input_width = _input_width;
}
void NCNNNanoDet::resize_unscale(const cv::Mat &mat, cv::Mat &mat_rs,
int target_height, int target_width,
NanoScaleParams &scale_params)
{
if (mat.empty()) return;
int img_height = static_cast<int>(mat.rows);
int img_width = static_cast<int>(mat.cols);
mat_rs = cv::Mat(target_height, target_width, CV_8UC3,
cv::Scalar(0, 0, 0));
// scale ratio (new / old) new_shape(h,w)
float w_r = (float) target_width / (float) img_width;
float h_r = (float) target_height / (float) img_height;
float r = std::min(w_r, h_r);
// compute padding
int new_unpad_w = static_cast<int>((float) img_width * r); // floor
int new_unpad_h = static_cast<int>((float) img_height * r); // floor
int pad_w = target_width - new_unpad_w; // >=0
int pad_h = target_height - new_unpad_h; // >=0
int dw = pad_w / 2;
int dh = pad_h / 2;
// resize with unscaling
cv::Mat new_unpad_mat;
// cv::Mat new_unpad_mat = mat.clone(); // may not need clone.
cv::resize(mat, new_unpad_mat, cv::Size(new_unpad_w, new_unpad_h));
new_unpad_mat.copyTo(mat_rs(cv::Rect(dw, dh, new_unpad_w, new_unpad_h)));
// record scale params.
scale_params.ratio = r;
scale_params.dw = dw;
scale_params.dh = dh;
scale_params.flag = true;
}
void NCNNNanoDet::transform(const cv::Mat &mat_rs, ncnn::Mat &in)
{
// BGR NHWC -> BGR NCHW
in = ncnn::Mat::from_pixels(mat_rs.data, ncnn::Mat::PIXEL_BGR, input_width, input_height);
in.substract_mean_normalize(mean_vals, norm_vals);
}
void NCNNNanoDet::detect(const cv::Mat &mat, std::vector<types::Boxf> &detected_boxes,
float score_threshold, float iou_threshold,
unsigned int topk, unsigned int nms_type)
{
if (mat.empty()) return;
auto img_height = static_cast<float>(mat.rows);
auto img_width = static_cast<float>(mat.cols);
// resize & unscale
cv::Mat mat_rs;
NanoScaleParams scale_params;
this->resize_unscale(mat, mat_rs, input_height, input_width, scale_params);
// 1. make input tensor
ncnn::Mat input;
this->transform(mat_rs, input);
// 2. inference & extract
auto extractor = net->create_extractor();
extractor.set_light_mode(false); // default
extractor.set_num_threads(num_threads);
extractor.input("input.1", input);
// 3.rescale & exclude.
std::vector<types::Boxf> bbox_collection;
this->generate_bboxes(scale_params, bbox_collection, extractor, score_threshold, img_height, img_width);
// 4. hard|blend|offset nms with topk.
this->nms(bbox_collection, detected_boxes, iou_threshold, topk, nms_type);
}
void NCNNNanoDet::generate_points(unsigned int target_height, unsigned int target_width)
{
if (center_points_is_update && (!is_dynamic_input)) return;
for (auto stride : strides)
{
unsigned int num_grid_w = target_width / stride;
unsigned int num_grid_h = target_height / stride;
std::vector<NanoCenterPoint> points;
for (unsigned int g1 = 0; g1 < num_grid_h; ++g1)
{
for (unsigned int g0 = 0; g0 < num_grid_w; ++g0)
{
float grid0 = (float) g0 + 0.5f;
float grid1 = (float) g1 + 0.5f;
#ifdef LITE_WIN32
NanoCenterPoint point;
point.grid0 = grid0;
point.grid1 = grid1;
point.stride = (float) stride;
points.push_back(point);
#else
points.push_back((NanoCenterPoint) {grid0, grid1, (float) stride});
#endif
}
}
center_points[stride] = points;
}
center_points_is_update = true;
}
void NCNNNanoDet::generate_bboxes(const NanoScaleParams &scale_params,
std::vector<types::Boxf> &bbox_collection,
ncnn::Extractor &extractor,
float score_threshold,
float img_height, float img_width)
{
ncnn::Mat cls_pred_stride_8;
ncnn::Mat cls_pred_stride_16;
ncnn::Mat cls_pred_stride_32;
ncnn::Mat dis_pred_stride_8;
ncnn::Mat dis_pred_stride_16;
ncnn::Mat dis_pred_stride_32;
extractor.extract("cls_pred_stride_8", cls_pred_stride_8);
extractor.extract("cls_pred_stride_16", cls_pred_stride_16);
extractor.extract("cls_pred_stride_32", cls_pred_stride_32);
extractor.extract("dis_pred_stride_8", dis_pred_stride_8);
extractor.extract("dis_pred_stride_16", dis_pred_stride_16);
extractor.extract("dis_pred_stride_32", dis_pred_stride_32);
this->generate_points(input_height, input_width);
bbox_collection.clear();
// level 8 & 16 & 32
this->generate_bboxes_single_stride(scale_params, cls_pred_stride_8, dis_pred_stride_8, 8,
score_threshold, img_height, img_width, bbox_collection);
this->generate_bboxes_single_stride(scale_params, cls_pred_stride_16, dis_pred_stride_16, 16,
score_threshold, img_height, img_width, bbox_collection);
this->generate_bboxes_single_stride(scale_params, cls_pred_stride_32, dis_pred_stride_32, 32,
score_threshold, img_height, img_width, bbox_collection);
#if LITENCNN_DEBUG
std::cout << "generate_bboxes num: " << bbox_collection.size() << "\n";
#endif
}
void NCNNNanoDet::generate_bboxes_single_stride(const NanoScaleParams &scale_params,
ncnn::Mat &cls_pred, ncnn::Mat &dis_pred,
unsigned int stride, float score_threshold,
float img_height, float img_width,
std::vector<types::Boxf> &bbox_collection)
{
unsigned int nms_pre_ = (stride / 8) * nms_pre; // 1 * 1000,2*1000,...
nms_pre_ = nms_pre_ >= nms_pre ? nms_pre_ : nms_pre;
const unsigned int f_h = (unsigned int) input_height / stride;
const unsigned int f_w = (unsigned int) input_width / stride;
const unsigned int num_points = f_h * f_w;
const unsigned int num_classes = 80;
float ratio = scale_params.ratio;
int dw = scale_params.dw;
int dh = scale_params.dh;
unsigned int count = 0;
auto &stride_points = center_points[stride];
for (unsigned int i = 0; i < num_points; ++i)
{
const float *scores = cls_pred.row(i); // row ptr
float cls_conf = scores[0];
unsigned int label = 0;
for (unsigned int j = 0; j < num_classes; ++j)
{
float tmp_conf = scores[j];
if (tmp_conf > cls_conf)
{
cls_conf = tmp_conf;
label = j;
}
} // argmax
if (cls_conf < score_threshold) continue; // filter
auto &point = stride_points.at(i);
const float cx = point.grid0; // cx
const float cy = point.grid1; // cy
const float s = point.stride; // stride
const float *offsets = dis_pred.row(i);
float l = offsets[0]; // left
float t = offsets[1]; // top
float r = offsets[2]; // right
float b = offsets[3]; // bottom
types::Boxf box;
float x1 = ((cx - l) * s - (float) dw) / ratio; // cx - l x1
float y1 = ((cy - t) * s - (float) dh) / ratio; // cy - t y1
float x2 = ((cx + r) * s - (float) dw) / ratio; // cx + r x2
float y2 = ((cy + b) * s - (float) dh) / ratio; // cy + b y2
box.x1 = std::max(0.f, x1);
box.y1 = std::max(0.f, y1);
box.x2 = std::min(img_width - 1.f, x2);
box.y2 = std::min(img_height - 1.f, y2);
box.score = cls_conf;
box.label = label;
box.label_text = class_names[label];
box.flag = true;
bbox_collection.push_back(box);
count += 1; // limit boxes for nms.
if (count > max_nms)
break;
}
if (bbox_collection.size() > nms_pre_)
{
std::sort(bbox_collection.begin(), bbox_collection.end(),
[](const types::Boxf &a, const types::Boxf &b)
{ return a.score > b.score; }); // sort inplace
// trunc
bbox_collection.resize(nms_pre_);
}
}
void NCNNNanoDet::nms(std::vector<types::Boxf> &input, std::vector<types::Boxf> &output,
float iou_threshold, unsigned int topk,
unsigned int nms_type)
{
if (nms_type == NMS::BLEND) lite::utils::blending_nms(input, output, iou_threshold, topk);
else if (nms_type == NMS::OFFSET) lite::utils::offset_nms(input, output, iou_threshold, topk);
else lite::utils::hard_nms(input, output, iou_threshold, topk);
}