18
18
import sys
19
19
import cv2
20
20
import numpy as np
21
+ from tqdm import tqdm
21
22
22
23
import paddle
23
24
from paddle .inference import Config
@@ -85,12 +86,12 @@ def argsparser():
85
86
type = str ,
86
87
default = 'True' ,
87
88
help = "Whether include nms or not." )
88
- # 是否用来测速
89
89
parser .add_argument (
90
- '--speed' ,
91
- type = str ,
92
- default = 'True' ,
93
- help = "if speed is True, it will print the inference time." )
90
+ "--trt_calib_mode" ,
91
+ type = bool ,
92
+ default = False ,
93
+ help = "If the model is produced by TRT offline quantitative "
94
+ "calibration, trt_calib_mode need to set True." )
94
95
95
96
return parser
96
97
@@ -214,8 +215,9 @@ def load_predictor(
214
215
use_mkldnn = False ,
215
216
batch_size = 1 ,
216
217
device = "CPU" ,
217
- min_subgraph_size = 3 ,
218
+ min_subgraph_size = 4 ,
218
219
use_dynamic_shape = False ,
220
+ trt_calib_mode = False ,
219
221
trt_min_shape = 1 ,
220
222
trt_max_shape = 1280 ,
221
223
trt_opt_shape = 640 ,
@@ -273,7 +275,7 @@ def load_predictor(
273
275
min_subgraph_size = min_subgraph_size ,
274
276
precision_mode = precision_map [precision ],
275
277
use_static = True ,
276
- use_calib_mode = False , )
278
+ use_calib_mode = False )
277
279
278
280
if use_dynamic_shape :
279
281
dynamic_shape_file = os .path .join (FLAGS .model_path ,
@@ -363,12 +365,9 @@ def eval(predictor, val_loader, metric, rerun_flag=False):
363
365
input_names = predictor .get_input_names ()
364
366
output_names = predictor .get_output_names ()
365
367
boxes_tensor = predictor .get_output_handle (output_names [0 ])
366
- print ("output_names:" , output_names )
367
- print ("Number of outputs:" , len (output_names ))
368
- print ("FLAGS.include_nms:" , FLAGS .include_nms )
369
368
if FLAGS .include_nms :
370
369
boxes_num = predictor .get_output_handle (output_names [1 ])
371
- for batch_id , data in enumerate (val_loader ):
370
+ for batch_id , data in tqdm ( enumerate (val_loader ), total = len ( val_loader ), desc = 'Evaluating' ):
372
371
data_all = {k : np .array (v ) for k , v in data .items ()}
373
372
for i , _ in enumerate (input_names ):
374
373
input_tensor = predictor .get_input_handle (input_names [i ])
@@ -386,79 +385,26 @@ def eval(predictor, val_loader, metric, rerun_flag=False):
386
385
time_min = min (time_min , timed )
387
386
time_max = max (time_max , timed )
388
387
predict_time += timed
389
- # print("FLAGS.include_nms:", FLAGS.include_nms)
390
- # print("FLAGS.speed:", FLAGS.speed)
391
- # 如果include_nms为false且flags.speed为True,则走PPYOLOEPostProcess
392
- if not FLAGS .include_nms and FLAGS .speed :
393
- # print("nms为True的时候走了PPYOLOEPostProcess")
388
+ if not FLAGS .include_nms :
394
389
postprocess = PPYOLOEPostProcess (
395
390
score_threshold = 0.3 , nms_threshold = 0.6 )
396
391
res = postprocess (np_boxes , data_all ['scale_factor' ])
397
- #如果include_nms为false且flags.speed为False,则跳过
398
- elif not FLAGS .include_nms and not FLAGS .speed :
399
- continue
400
- #如果include_nms,则直接返回
401
- elif FLAGS .include_nms :
402
- # print("nms为False的时候直接返回")
392
+ else :
403
393
res = {'bbox' : np_boxes , 'bbox_num' : np_boxes_num }
404
394
metric .update (data_all , res )
405
395
if batch_id % 100 == 0 :
406
- print ("Eval iter:" , batch_id )
407
396
sys .stdout .flush ()
408
397
metric .accumulate ()
409
- if not FLAGS .speed :
410
- metric .log ()
398
+ metric .log ()
411
399
map_res = metric .get_results ()
412
400
metric .reset ()
413
401
time_avg = predict_time / sample_nums
414
402
print ("[Benchmark]Inference time(ms): min={}, max={}, avg={}" .format (
415
403
round (time_min * 1000 , 2 ),
416
404
round (time_max * 1000 , 1 ), round (time_avg * 1000 , 1 )))
417
- if not FLAGS .speed :
418
- print ("[Benchmark] COCO mAP: {}" .format (map_res ["bbox" ][0 ]))
405
+ print ("[Benchmark] COCO mAP: {}" .format (map_res ["bbox" ][0 ]))
419
406
sys .stdout .flush ()
420
407
421
- def inference_time (predictor , val_loader , metric , rerun_flag = False ):
422
- cpu_mems , gpu_mems = 0 , 0
423
- predict_time = 0.0
424
- time_min = float ("inf" )
425
- time_max = float ("-inf" )
426
- sample_nums = len (val_loader )
427
- input_names = predictor .get_input_names ()
428
- output_names = predictor .get_output_names ()
429
- boxes_tensor = predictor .get_output_handle (output_names [0 ])
430
- print ("output_names:" , output_names )
431
- print ("Number of outputs:" , len (output_names ))
432
- print ("FLAGS.include_nms:" , FLAGS .include_nms )
433
- if FLAGS .include_nms :
434
- boxes_num = predictor .get_output_handle (output_names [1 ])
435
-
436
- for batch_id , data in enumerate (val_loader ):
437
- data_all = {k : np .array (v ) for k , v in data .items ()}
438
- for i , _ in enumerate (input_names ):
439
- input_tensor = predictor .get_input_handle (input_names [i ])
440
- input_tensor .copy_from_cpu (data_all [input_names [i ]])
441
- paddle .device .cuda .synchronize ()
442
- start_time = time .time ()
443
- predictor .run ()
444
- # np_boxes = boxes_tensor.copy_to_cpu()
445
- if FLAGS .include_nms :
446
- np_boxes_num = boxes_num .copy_to_cpu ()
447
- if rerun_flag :
448
- return
449
- end_time = time .time ()
450
- timed = end_time - start_time
451
- time_min = min (time_min , timed )
452
- time_max = max (time_max , timed )
453
- predict_time += timed
454
- # print("FLAGS.include_nms:", FLAGS.include_nms)
455
- # print("FLAGS.speed:", FLAGS.speed)
456
- # 如果include_nms为false且flags.speed为True,则走PPYOLOEPostProcess
457
- time_avg = predict_time / sample_nums
458
- print ("[Benchmark]Inference time(ms): min={}, max={}, avg={}" .format (
459
- round (time_min * 1000 , 2 ),
460
- round (time_max * 1000 , 1 ), round (time_avg * 1000 , 1 )))
461
- sys .stdout .flush ()
462
408
463
409
def main ():
464
410
"""
@@ -485,7 +431,6 @@ def main():
485
431
repeats = repeats )
486
432
else :
487
433
reader_cfg = load_config (FLAGS .reader_config )
488
-
489
434
dataset = reader_cfg ["EvalDataset" ]
490
435
global val_loader
491
436
val_loader = create ("EvalReader" )(
@@ -496,11 +441,9 @@ def main():
496
441
anno_file = dataset .get_anno ()
497
442
metric = COCOMetric (
498
443
anno_file = anno_file , clsid2catid = clsid2catid , IouType = "bbox" )
499
- if not FLAGS .speed :
500
- eval (predictor , val_loader , metric , rerun_flag = rerun_flag )
501
- else :
502
- inference_time (predictor , val_loader , metric , rerun_flag = rerun_flag )
503
-
444
+
445
+ eval (predictor , val_loader , metric , rerun_flag = rerun_flag )
446
+
504
447
if rerun_flag :
505
448
print (
506
449
"***** Collect dynamic shape done, Please rerun the program to get correct results. *****"
@@ -516,9 +459,6 @@ def main():
516
459
else :
517
460
FLAGS .include_nms = False
518
461
519
- print ('**************main****************' )
520
- print (FLAGS )
521
-
522
462
# DataLoader need run on cpu
523
463
paddle .set_device ("cpu" )
524
464
0 commit comments