-
Notifications
You must be signed in to change notification settings - Fork 0
/
LFFD_ncnn.cpp
263 lines (215 loc) · 7.99 KB
/
LFFD_ncnn.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "LFFD_ncnn.h"
LFFD::LFFD(int scale_num, int num_thread_)
{
num_output_scales = scale_num;
num_thread = num_thread_;
if (num_output_scales == 5) {
param_file_name = "symbol_10_320_20L_5scales_v2_deploy.param";
bin_file_name = "train_10_320_20L_5scales_v2_iter_1000000.bin";
receptive_field_list = { 20, 40, 80, 160, 320 };
receptive_field_stride = { 4, 8, 16, 32, 64 };
bbox_small_list = { 10, 20, 40, 80, 160 };
bbox_large_list = { 20, 40, 80, 160, 320 };
receptive_field_center_start = { 3, 7, 15, 31, 63 };
for (size_t i = 0; i < receptive_field_list.size(); i++) {
constant.push_back(receptive_field_list[i] / 2);
}
output_blob_names = { "softmax0","conv8_3_bbox",
"softmax1","conv11_3_bbox",
"softmax2","conv14_3_bbox",
"softmax3","conv17_3_bbox",
"softmax4","conv20_3_bbox" };
}
else if (num_output_scales == 8) {
param_file_name = "symbol_10_560_25L_8scales_v1_deploy.param";
bin_file_name = "train_10_560_25L_8scales_v1_iter_1400000.bin";
receptive_field_list = { 15, 20, 40, 70, 110, 250, 400, 560 };
receptive_field_stride = { 4, 4, 8, 8, 16, 32, 32, 32 };
bbox_small_list = { 10, 15, 20, 40, 70, 110, 250, 400 };
bbox_large_list = { 15, 20, 40, 70, 110, 250, 400, 560 };
receptive_field_center_start = { 3, 3, 7, 7, 15, 31, 31, 31 };
for (size_t i = 0; i < receptive_field_list.size(); i++) {
constant.push_back(receptive_field_list[i] / 2);
}
output_blob_names={ "softmax0","conv8_3_bbox",
"softmax1","conv10_3_bbox",
"softmax2","conv13_3_bbox",
"softmax3","conv15_3_bbox",
"softmax4","conv18_3_bbox",
"softmax5","conv21_3_bbox",
"softmax6","conv23_3_bbox",
"softmax7","conv25_3_bbox" };
}
lffd.load_param(param_file_name.data());
lffd.load_model(bin_file_name.data());
}
LFFD::~LFFD()
{
lffd.clear();
}
int LFFD::detect(ncnn::Mat& img, std::vector<FaceInfo>& face_list, int resize_h, int resize_w,
float score_threshold, float nms_threshold, int top_k, std::vector<int> skip_scale_branch_list)
{
if (img.empty()) {
std::cout << "image is empty ,please check!" << std::endl;
return -1;
}
image_h = img.h;
image_w = img.w;
ncnn::Mat in;
ncnn::resize_bilinear(img,in,resize_w,resize_h);
float ratio_w=(float)image_w/in.w;
float ratio_h=(float)image_h/in.h;
ncnn::Mat ncnn_img = in;
ncnn_img.substract_mean_normalize(mean_vals, norm_vals);
std::vector<FaceInfo> bbox_collection;
ncnn::Extractor ex = lffd.create_extractor();
ex.set_num_threads(num_thread);
ex.input("data", ncnn_img);
for (int i = 0; i <num_output_scales; i++) {
ncnn::Mat conf;
ncnn::Mat reg;
ex.extract(output_blob_names[2*i].c_str(), conf);
ex.extract(output_blob_names[2 * i+1].c_str(), reg);
generateBBox(bbox_collection, conf, reg, score_threshold, conf.w, conf.h, in.w, in.h, i);
}
std::vector<FaceInfo> valid_input;
get_topk_bbox(bbox_collection, valid_input, top_k);
nms(valid_input, face_list, nms_threshold);
for(size_t i=0;i<face_list.size();i++){
face_list[i].x1*=ratio_w;
face_list[i].y1*=ratio_h;
face_list[i].x2*=ratio_w;
face_list[i].y2*=ratio_h;
float w,h,maxSize;
float cenx,ceny;
w=face_list[i].x2-face_list[i].x1;
h=face_list[i].y2-face_list[i].y1;
maxSize = w > h ? w : h;
cenx=face_list[i].x1+w/2;
ceny=face_list[i].y1+h/2;
face_list[i].x1=cenx-maxSize/2>0? cenx - maxSize / 2:0;
face_list[i].y1=ceny-maxSize/2>0? ceny - maxSize / 2:0;
face_list[i].x2=cenx+maxSize/2>image_w? image_w-1: cenx + maxSize / 2;
face_list[i].y2=ceny+maxSize/2> image_h? image_h-1: ceny + maxSize / 2;
}
return 0;
}
void LFFD::generateBBox(std::vector<FaceInfo>& bbox_collection, ncnn::Mat score_map, ncnn::Mat box_map, float score_threshold, int fea_w, int fea_h, int cols, int rows, int scale_id)
{
float* RF_center_Xs = new float[fea_w];
float* RF_center_Xs_mat = new float[fea_w * fea_h];
float* RF_center_Ys = new float[fea_h];
float* RF_center_Ys_mat = new float[fea_h * fea_w];
for (int x = 0; x < fea_w; x++) {
RF_center_Xs[x] = receptive_field_center_start[scale_id] + receptive_field_stride[scale_id] * x;
}
for (int x = 0; x < fea_h; x++) {
for (int y = 0; y < fea_w; y++) {
RF_center_Xs_mat[x * fea_w + y] = RF_center_Xs[y];
}
}
for (int x = 0; x < fea_h; x++) {
RF_center_Ys[x] = receptive_field_center_start[scale_id] + receptive_field_stride[scale_id] * x;
for (int y = 0; y < fea_w; y++) {
RF_center_Ys_mat[x * fea_w + y] = RF_center_Ys[x];
}
}
float* x_lt_mat = new float[fea_h * fea_w];
float* y_lt_mat = new float[fea_h * fea_w];
float* x_rb_mat = new float[fea_h * fea_w];
float* y_rb_mat = new float[fea_h * fea_w];
//x-left-top
float mid_value = 0;
for (int j = 0; j < fea_h * fea_w; j++) {
mid_value = RF_center_Xs_mat[j] - box_map.channel(0)[j] * constant[scale_id];
x_lt_mat[j] = mid_value < 0 ? 0 : mid_value;
}
//y-left-top
for (int j = 0; j < fea_h * fea_w; j++) {
mid_value = RF_center_Ys_mat[j] - box_map.channel(1)[j] * constant[scale_id];
y_lt_mat[j] = mid_value < 0 ? 0 : mid_value;
}
//x-right-bottom
for (int j = 0; j < fea_h * fea_w; j++) {
mid_value = RF_center_Xs_mat[j] - box_map.channel(2)[j] * constant[scale_id];
x_rb_mat[j] = mid_value > cols - 1 ? cols - 1 : mid_value;
}
//y-right-bottom
for (int j = 0; j < fea_h * fea_w; j++) {
mid_value = RF_center_Ys_mat[j] - box_map.channel(3)[j] * constant[scale_id];
y_rb_mat[j] = mid_value > rows - 1 ? rows - 1 : mid_value;
}
for (int k = 0; k < fea_h * fea_w; k++) {
if (score_map.channel(0)[k] > score_threshold) {
FaceInfo faceinfo;
faceinfo.x1 = x_lt_mat[k];
faceinfo.y1 = y_lt_mat[k];
faceinfo.x2 = x_rb_mat[k];
faceinfo.y2 = y_rb_mat[k];
faceinfo.score = score_map[k];
faceinfo.area = (faceinfo.x2 - faceinfo.x1) * (faceinfo.y2 - faceinfo.y1);
bbox_collection.push_back(faceinfo);
}
}
delete[] RF_center_Xs; RF_center_Xs = NULL;
delete[] RF_center_Ys; RF_center_Ys = NULL;
delete[] RF_center_Xs_mat; RF_center_Xs_mat = NULL;
delete[] RF_center_Ys_mat; RF_center_Ys_mat = NULL;
delete[] x_lt_mat; x_lt_mat = NULL;
delete[] y_lt_mat; y_lt_mat = NULL;
delete[] x_rb_mat; x_rb_mat = NULL;
delete[] y_rb_mat; y_rb_mat = NULL;
}
void LFFD::get_topk_bbox(std::vector<FaceInfo>& input, std::vector<FaceInfo>& output, int top_k)
{
std::sort(input.begin(), input.end(),
[](const FaceInfo& a, const FaceInfo& b)
{
return a.score > b.score;
});
if (input.size() > size_t(top_k)) {
for (int k = 0; k < top_k; k++) {
output.push_back(input[k]);
}
}
else {
output = input;
}
}
void LFFD::nms(std::vector<FaceInfo>& input, std::vector<FaceInfo>& output, float threshold, int type)
{
if(input.empty())
return;
std::sort(input.begin(), input.end(),
[](const FaceInfo& a, const FaceInfo& b)
{
return a.score > b.score;
});
int box_num = input.size();
std::vector<int> merged(box_num, 0);
for (int i = 0; i < box_num; i++)
{
if (merged[i]) continue;
output.push_back(input[i]);
for (int j = i + 1; j < box_num; j++)
{
if (merged[j])
continue;
float inner_x0 = input[i].x1 > input[j].x1 ? input[i].x1 : input[j].x1;//std::max(input[i].x1, input[j].x1);
float inner_y0 = input[i].y1 > input[j].y1 ? input[i].y1 : input[j].y1;
float inner_x1 = input[i].x2 < input[j].x2 ? input[i].x2 : input[j].x2; //bug fixed ,sorry
float inner_y1 = input[i].y2 < input[j].y2 ? input[i].y2 : input[j].y2;
float inner_h = inner_y1 - inner_y0 + 1;
float inner_w = inner_x1 - inner_x0 + 1;
if (inner_h <= 0 || inner_w <= 0)
continue;
float inner_area = inner_h * inner_w;
float h1 = input[j].y2 - input[j].y1 + 1;
float w1 = input[j].x2 - input[j].x1 + 1;
float area1 = h1 * w1;
float score= inner_area/area1;
if (score > threshold) merged[j] = 1;
}
}
}