-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmxnet_mtcnn.cpp
More file actions
709 lines (479 loc) · 16.3 KB
/
Copy pathmxnet_mtcnn.cpp
File metadata and controls
709 lines (479 loc) · 16.3 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
Copyright (C) 2017 Open Intelligent Machines Co.,Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "mxnet/c_predict_api.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include "mtcnn.hpp"
#include "mxnet_mtcnn.hpp"
#include "utils.hpp"
static int LoadFile(const std::string & fname, std::vector<char>& buf)
{
std::ifstream fs(fname, std::ios::binary | std::ios::in);
if(!fs.good())
{
std::cerr<<fname<<" does not exist"<<std::endl;
return -1;
}
fs.seekg(0, std::ios::end);
int fsize=fs.tellg();
fs.seekg(0, std::ios::beg);
buf.resize(fsize);
fs.read(buf.data(),fsize);
fs.close();
return 0;
}
int MxNetMtcnn::LoadModule(const std::string &proto_model_dir)
{
model_dir_=proto_model_dir;
/* Load the network. */
RNet_=LoadRNet(1);
if(RNet_==nullptr)
return -1;
ONet_=LoadONet(1);
if(ONet_==nullptr)
return -1;
return 0;
}
MxNetMtcnn::~MxNetMtcnn(void)
{
MXPredFree(RNet_);
MXPredFree(ONet_);
}
void MxNetMtcnn::LoadPNet(int h, int w)
{
std::string param_file= model_dir_+"/det1-0001.params";
std::string json_file=model_dir_+"/det1-symbol.json";
PNet_=LoadMxNetModule(param_file,json_file,1,3,h,w);
}
void MxNetMtcnn::FreePNet(void)
{
MXPredFree(PNet_);
}
PredictorHandle MxNetMtcnn::LoadMxNetModule(const std::string& param_file, const std::string& json_file,
int batch, int channel, int input_h, int input_w)
{
std::vector<char> param_buffer;
std::vector<char> json_buffer;
PredictorHandle pred_hnd;
if(LoadFile(param_file,param_buffer)<0)
return nullptr;
if(LoadFile(json_file,json_buffer)<0)
return nullptr;
int device_type=1;
int dev_id=0;
mx_uint num_input_nodes=1;
const char * input_keys[1];
const mx_uint input_shape_indptr[] = {0, 4};
const mx_uint input_shape_data[] = {
static_cast<mx_uint>(batch),
static_cast<mx_uint>(channel),
static_cast<mx_uint>(input_h),
static_cast<mx_uint>(input_w)
};
input_keys[0]="data";
MXPredCreate(json_buffer.data(),
param_buffer.data(),
param_buffer.size(),
device_type,
dev_id,
num_input_nodes,
input_keys,
input_shape_indptr,
input_shape_data,
&pred_hnd
);
return pred_hnd;
}
void MxNetMtcnn::Detect(cv::Mat& orig_img, std::vector<face_box>& face_list)
{
cv::Mat img;
orig_img.convertTo(img,CV_32FC3);
img=(img-127.5)*0.0078125;
int img_h=img.rows;
int img_w=img.cols;
std::vector<scale_window> win_list;
std::vector<face_box> total_pnet_boxes;
std::vector<face_box> total_rnet_boxes;
std::vector<face_box> total_onet_boxes;
cal_pyramid_list(img_h,img_w,min_size_,factor_,win_list);
std::cout << "win list size: " << win_list.size() << std::endl;
std::cout << "====Run PNet====" << std::endl;
unsigned long start_time = get_cur_time();
// TODO: can use multiple thread ??
for(int i=0;i<win_list.size();i++)
{
std::cout << "scale window. height: " << win_list[i].h << " width: " << win_list[i].w << std::endl;
std::vector<face_box>boxes;
RunPNet(img,win_list[i],boxes);
total_pnet_boxes.insert(total_pnet_boxes.end(),boxes.begin(),boxes.end());
}
unsigned long end_time = get_cur_time();
std::cout << "====Run PNet time eclipsed: " << end_time - start_time << " us" << std::endl;
std::vector<face_box> pnet_boxes;
start_time = get_cur_time();
process_boxes(total_pnet_boxes,img_h,img_w,pnet_boxes);
end_time = get_cur_time();
std::cout << "====Run Process PNet Boxes time eclipsed: " << end_time - start_time << " us" << std::endl;
if(pnet_boxes.size()==0)
return;
std::cout << "PNet boxes size: " << pnet_boxes.size() << " RNet batch bound: " << rnet_batch_bound_ << std::endl;
start_time = get_cur_time();
if(pnet_boxes.size()>rnet_batch_bound_)
{
//batch mode
std::cout << "===Run RNet====" << std::endl;
RunRNet(img,pnet_boxes,total_rnet_boxes);
}
else
{
for(unsigned int i=0;i<pnet_boxes.size();i++)
{
face_box out_box;
std::cout << "===Run PreLoadRNet====" << std::endl;
if(RunPreLoadRNet(img, pnet_boxes[i],out_box)<0)
continue;
total_rnet_boxes.push_back(out_box);
}
}
end_time = get_cur_time();
std::cout << "====Run RNet time eclipsed: " << end_time - start_time << " us" << std::endl;
std::vector<face_box> rnet_boxes;
start_time = get_cur_time();
process_boxes(total_rnet_boxes,img_h,img_w,rnet_boxes);
end_time = get_cur_time();
std::cout << "====Run Process RNet Boxes time eclipsed: " << end_time - start_time << " us" << std::endl;
std::cout << "RNet boxes size: " << rnet_boxes.size() << " ONet batch bound: " << onet_batch_bound_ << std::endl;
if(rnet_boxes.size()==0)
return;
start_time = get_cur_time();
if(rnet_boxes.size()>onet_batch_bound_)
{
std::cout << "===Run ONet====" << std::endl;
RunONet(img,rnet_boxes, total_onet_boxes);
}
else
{
for(unsigned int i=0;i<rnet_boxes.size();i++)
{
face_box out_box;
std::cout << "===Run PreLoadONet====" << std::endl;
if(RunPreLoadONet(img, rnet_boxes[i],out_box)<0)
continue;
total_onet_boxes.push_back(out_box);
}
}
end_time = get_cur_time();
std::cout << "====Run ONet time eclipsed: " << end_time - start_time << " us" << std::endl;
//calculate the landmark
start_time = get_cur_time();
cal_landmark(total_onet_boxes);
end_time = get_cur_time();
std::cout << "====Run LandMark time eclipsed: " << end_time - start_time << " us" << std::endl;
//Get Final Result
start_time = get_cur_time();
regress_boxes(total_onet_boxes);
nms_boxes(total_onet_boxes, 0.7, NMS_MIN,face_list);
end_time = get_cur_time();
std::cout << "====Run nmsbox time eclipsed: " << end_time - start_time << " us" << std::endl;
}
void MxNetMtcnn::RunPNet(const cv::Mat& img, scale_window& win, std::vector<face_box>&box_list)
{
cv::Mat resized;
int scale_h=win.h;
int scale_w=win.w;
float scale=win.scale;
LoadPNet(scale_h,scale_w);
cv::resize(img, resized, cv::Size(scale_w, scale_h), 0, 0, cv::INTER_LINEAR);
std::vector<float> input(3*scale_h*scale_w);
std::vector<cv::Mat> input_channels;
set_input_buffer(input_channels, input.data(), scale_h, scale_w);
cv::split(resized, input_channels);
MXPredSetInput(PNet_,"data",input.data(),input.size());
MXPredForward(PNet_);
mx_uint *shape = NULL;
mx_uint shape_len = 0;
MXPredGetOutputShape(PNet_,0,&shape,&shape_len);
int reg_size=1;
for(unsigned int i=0;i<shape_len;i++)
reg_size*=shape[i];
MXPredGetOutputShape(PNet_,1,&shape,&shape_len);
int confidence_size=1;
for(unsigned int i=0;i<shape_len;i++)
confidence_size*=shape[i];
std::vector<float> reg(reg_size);
std::vector<float> confidence(confidence_size);
MXPredGetOutput(PNet_,0, reg.data(), reg_size);
MXPredGetOutput(PNet_,1, confidence.data(), confidence_size);
std::vector<face_box> candidate_boxes;
int feature_h=shape[2];
int feature_w=shape[3];
generate_bounding_box(confidence.data(),confidence.size(), reg.data(), scale, pnet_threshold_, feature_h, feature_w, candidate_boxes,false);
nms_boxes(candidate_boxes, 0.5, NMS_UNION,box_list);
FreePNet();
}
void MxNetMtcnn::CopyOnePatch(const cv::Mat& img,face_box&input_box,float * data_to, int height, int width)
{
std::vector<cv::Mat> channels;
set_input_buffer(channels, data_to, height, width);
int pad_top = std::abs(input_box.py0 - input_box.y0);
int pad_left= std::abs(input_box.px0 - input_box.x0);
int pad_bottom = std::abs(input_box.py1 - input_box.y1);
int pad_right = std::abs(input_box.px1 - input_box.x1);
cv::Mat chop_img = img(cv::Range(input_box.py0, input_box.py1),
cv::Range(input_box.px0, input_box.px1));
cv::copyMakeBorder(chop_img, chop_img, pad_top, pad_bottom, pad_left, pad_right, cv::BORDER_CONSTANT, cv::Scalar(0));
cv::resize(chop_img, chop_img, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
cv::split(chop_img, channels);
}
PredictorHandle MxNetMtcnn::LoadRNet(int batch)
{
std::string param_file= model_dir_+"/det2-0001.params";
std::string json_file=model_dir_+"/det2-symbol.json";
return LoadMxNetModule(param_file,json_file,batch,3,24,24);
}
PredictorHandle MxNetMtcnn::LoadONet(int batch)
{
std::string param_file= model_dir_+"/det3-0001.params";
std::string json_file=model_dir_+"/det3-symbol.json";
return LoadMxNetModule(param_file,json_file,batch,3,48,48);
}
void MxNetMtcnn::RunRNet(const cv::Mat& img, std::vector<face_box>& pnet_boxes,std::vector<face_box>& output_boxes)
{
int batch=pnet_boxes.size();
int input_channel = 3;
int input_width = 24;
int input_height = 24;
int input_size=batch*input_channel*input_width*input_height;
PredictorHandle rnet = LoadRNet(batch);
if(rnet == nullptr)
return ;
/* load the data */
std::vector<float> input(input_size);
float * input_data=input.data();
for(int i=0;i<batch;i++)
{
int patch_size=input_width*input_height*input_channel;
CopyOnePatch(img,pnet_boxes[i], input_data,input_height,input_width);
input_data+=patch_size;
}
MXPredSetInput(rnet,"data",input.data(),input_size);
MXPredForward(rnet);
mx_uint *shape = NULL;
mx_uint shape_len = 0;
MXPredGetOutputShape(rnet,0,&shape,&shape_len);
int reg_size=1;
for(unsigned int i=0;i<shape_len;i++)
reg_size*=shape[i];
MXPredGetOutputShape(rnet,1,&shape,&shape_len);
int confidence_size=1;
for(unsigned int i=0;i<shape_len;i++)
confidence_size*=shape[i];
std::vector<float> reg(reg_size);
std::vector<float> confidence(confidence_size);
MXPredGetOutput(rnet,0,reg.data(),reg_size);
MXPredGetOutput(rnet,1,confidence.data(),confidence_size);
const float* confidence_data = confidence.data();
const float* reg_data = reg.data();
/* filter output now */
int conf_page_size=confidence_size/batch;
int reg_page_size=reg_size/batch;
for(int i=0;i<batch;i++)
{
if (*(confidence_data+1) > rnet_threshold_){
face_box output_box;
face_box& input_box=pnet_boxes[i];
output_box.x0=input_box.x0;
output_box.y0=input_box.y0;
output_box.x1=input_box.x1;
output_box.y1=input_box.y1;
output_box.score = *(confidence_data+1);
output_box.regress[0]=reg_data[0];
output_box.regress[1]=reg_data[1];
output_box.regress[2]=reg_data[2];
output_box.regress[3]=reg_data[3];
output_boxes.push_back(output_box);
}
confidence_data+=conf_page_size;
reg_data+=reg_page_size;
}
MXPredFree(rnet);
}
int MxNetMtcnn::RunPreLoadRNet(const cv::Mat& img, face_box& input_box,face_box& output_box )
{
int input_channels = 3;
int input_width = 24;
int input_height = 24;
std::vector<float> input(input_channels*input_width*input_height);
CopyOnePatch(img,input_box,input.data(),input_height,input_width);
MXPredSetInput(RNet_,"data",input.data(),input.size());
MXPredForward(RNet_);
mx_uint *shape = NULL;
mx_uint shape_len = 0;
MXPredGetOutputShape(RNet_, 0, &shape, &shape_len);
int reg_size=1;
for(unsigned int i=0;i<shape_len;i++)
reg_size*=shape[i];
MXPredGetOutputShape(RNet_,1,&shape,&shape_len);
int confidence_size=1;
for(unsigned int i=0;i<shape_len;i++)
confidence_size*=shape[i];
std::vector<float> reg(reg_size);
std::vector<float> confidence(confidence_size);
MXPredGetOutput(RNet_,0,reg.data(),reg_size);
MXPredGetOutput(RNet_,1,confidence.data(),confidence_size);
const float* confidence_data = confidence.data() + confidence.size() / 2;
const float* reg_data = reg.data();
if (*(confidence_data) > rnet_threshold_){
output_box.x0=input_box.x0;
output_box.y0=input_box.y0;
output_box.x1=input_box.x1;
output_box.y1=input_box.y1;
output_box.score = *(confidence_data);
output_box.regress[0]=reg_data[0];
output_box.regress[1]=reg_data[1];
output_box.regress[2]=reg_data[2];
output_box.regress[3]=reg_data[3];
return 0;
}
return -1;
}
int MxNetMtcnn::RunPreLoadONet(const cv::Mat& img, face_box& input_box, face_box& output_box)
{
int input_channels = 3;
int input_width = 48;
int input_height = 48;
std::vector<float> input(input_channels*input_width*input_height);
CopyOnePatch(img,input_box,input.data(),input_height,input_width);
MXPredSetInput(ONet_,"data",input.data(),input.size());
MXPredForward(ONet_);
mx_uint *shape = NULL;
mx_uint shape_len = 0;
MXPredGetOutputShape(ONet_,1,&shape,&shape_len);
int reg_size=1;
for(unsigned int i=0;i<shape_len;i++)
reg_size*=shape[i];
MXPredGetOutputShape(ONet_,0,&shape,&shape_len);
int points_size=1;
for(unsigned int i=0;i<shape_len;i++)
points_size*=shape[i];
MXPredGetOutputShape(ONet_,2,&shape,&shape_len);
int confidence_size=1;
for(unsigned int i=0;i<shape_len;i++)
confidence_size*=shape[i];
std::vector<float> reg(reg_size);
std::vector<float> points(points_size);
std::vector<float> confidence(confidence_size);
MXPredGetOutput(ONet_,0,points.data(),points_size);
MXPredGetOutput(ONet_,1,reg.data(),reg_size);
MXPredGetOutput(ONet_,2,confidence.data(),confidence_size);
const float* confidence_data = confidence.data() + confidence.size() / 2;
const float* reg_data = reg.data();
const float* points_data=points.data();
if (*(confidence_data) > onet_threshold_){
output_box.x0=input_box.x0;
output_box.y0=input_box.y0;
output_box.x1=input_box.x1;
output_box.y1=input_box.y1;
output_box.score=*(confidence_data);
output_box.regress[0]=reg_data[0];
output_box.regress[1]=reg_data[1];
output_box.regress[2]=reg_data[2];
output_box.regress[3]=reg_data[3];
for (int j = 0; j<5; j++){
output_box.landmark.x[j] = *(points_data + j);
output_box.landmark.y[j] = *(points_data + j + 5);
}
return 0;
}
return -1;
}
void MxNetMtcnn::RunONet(const cv::Mat& img,std::vector<face_box>& rnet_boxes, std::vector<face_box>& output_boxes)
{
int batch=rnet_boxes.size();
int input_channel = 3;
int input_width = 48;
int input_height = 48;
int input_size=batch*input_channel*input_width*input_height;
PredictorHandle onet = LoadONet(batch);
if(onet==nullptr)
return;
/* load the data */
std::vector<float> input(input_size);
float * input_data=input.data();
for(int i=0;i<batch;i++)
{
int patch_size=input_width*input_height*input_channel;
CopyOnePatch(img,rnet_boxes[i],input_data,input_height,input_width);
input_data+=patch_size;
}
MXPredSetInput(onet,"data",input.data(),input.size());
MXPredForward(onet);
mx_uint *shape = NULL;
mx_uint shape_len = 0;
MXPredGetOutputShape(onet,1,&shape,&shape_len);
int reg_size=1;
for(unsigned int i=0;i<shape_len;i++)
reg_size*=shape[i];
MXPredGetOutputShape(onet,0,&shape,&shape_len);
int points_size=1;
for(unsigned int i=0;i<shape_len;i++)
points_size*=shape[i];
MXPredGetOutputShape(onet,2,&shape,&shape_len);
int confidence_size=1;
for(unsigned int i=0;i<shape_len;i++)
confidence_size*=shape[i];
std::vector<float> reg(reg_size);
std::vector<float> points(points_size);
std::vector<float> confidence(confidence_size);
MXPredGetOutput(onet,0,points.data(),points_size);
MXPredGetOutput(onet,1,reg.data(),reg_size);
MXPredGetOutput(onet,2,confidence.data(),confidence_size);
const float* confidence_data = confidence.data();
const float* reg_data = reg.data();
const float* points_data=points.data();
int reg_page_size=reg_size/batch;
int confidence_page_size=confidence_size/batch;
int points_page_size=points_size/batch;
for(int i=0;i<batch;i++)
{
if (*(confidence_data+1) > onet_threshold_){
face_box output_box;
face_box & input_box=rnet_boxes[i];
output_box.x0=input_box.x0;
output_box.y0=input_box.y0;
output_box.x1=input_box.x1;
output_box.y1=input_box.y1;
output_box.score=*(confidence_data+1);
output_box.regress[0]=reg_data[0];
output_box.regress[1]=reg_data[1];
output_box.regress[2]=reg_data[2];
output_box.regress[3]=reg_data[3];
for (int j = 0; j<5; j++){
output_box.landmark.x[j] = *(points_data + j);
output_box.landmark.y[j] = *(points_data + j + 5);
}
output_boxes.push_back(output_box);
}
reg_data+=reg_page_size;
confidence_data+=confidence_page_size;
points_data+=points_page_size;
}
}
static Mtcnn * MxNetCreator(void)
{
return new MxNetMtcnn();
}
REGISTER_MTCNN_CREATOR(mxnet, MxNetCreator);