Skip to content

Commit 26a6d91

Browse files
committed
最终代码
1 parent ca74d46 commit 26a6d91

File tree

4 files changed

+21
-81
lines changed

4 files changed

+21
-81
lines changed

example/auto_compression/detection/configs/ppyoloe_l_qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Global:
33
reader_config: configs/yolo_reader.yml
44
arch: PPYOLOE
5-
include_nms: True
5+
include_nms: False
66
Evaluation: True
77
model_dir: ./ppyoloe_crn_l_300e_coco
88
model_filename: model.pdmodel

example/auto_compression/detection/configs/yolo_reader.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ TrainDataset:
66
!COCODataSet
77
image_dir: train2017
88
anno_path: annotations/instances_train2017.json
9-
dataset_dir: dataset/coco/
9+
dataset_dir: /work/GETR-Lite-paddle-new/inference/datasets/coco/
1010

1111
EvalDataset:
1212
!COCODataSet
1313
image_dir: val2017
1414
anno_path: annotations/instances_val2017.json
15-
dataset_dir: dataset/coco/
15+
dataset_dir: /work/GETR-Lite-paddle-new/inference/datasets/coco/
1616

1717
worker_num: 0
1818

example/auto_compression/detection/paddle_inference_eval.py

+17-77
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import sys
1919
import cv2
2020
import numpy as np
21+
from tqdm import tqdm
2122

2223
import paddle
2324
from paddle.inference import Config
@@ -85,12 +86,12 @@ def argsparser():
8586
type=str,
8687
default='True',
8788
help="Whether include nms or not.")
88-
# 是否用来测速
8989
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.")
9495

9596
return parser
9697

@@ -214,8 +215,9 @@ def load_predictor(
214215
use_mkldnn=False,
215216
batch_size=1,
216217
device="CPU",
217-
min_subgraph_size=3,
218+
min_subgraph_size=4,
218219
use_dynamic_shape=False,
220+
trt_calib_mode=False,
219221
trt_min_shape=1,
220222
trt_max_shape=1280,
221223
trt_opt_shape=640,
@@ -273,7 +275,7 @@ def load_predictor(
273275
min_subgraph_size=min_subgraph_size,
274276
precision_mode=precision_map[precision],
275277
use_static=True,
276-
use_calib_mode=False, )
278+
use_calib_mode=False)
277279

278280
if use_dynamic_shape:
279281
dynamic_shape_file = os.path.join(FLAGS.model_path,
@@ -363,12 +365,9 @@ def eval(predictor, val_loader, metric, rerun_flag=False):
363365
input_names = predictor.get_input_names()
364366
output_names = predictor.get_output_names()
365367
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)
369368
if FLAGS.include_nms:
370369
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'):
372371
data_all = {k: np.array(v) for k, v in data.items()}
373372
for i, _ in enumerate(input_names):
374373
input_tensor = predictor.get_input_handle(input_names[i])
@@ -386,79 +385,26 @@ def eval(predictor, val_loader, metric, rerun_flag=False):
386385
time_min = min(time_min, timed)
387386
time_max = max(time_max, timed)
388387
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:
394389
postprocess = PPYOLOEPostProcess(
395390
score_threshold=0.3, nms_threshold=0.6)
396391
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:
403393
res = {'bbox': np_boxes, 'bbox_num': np_boxes_num}
404394
metric.update(data_all, res)
405395
if batch_id % 100 == 0:
406-
print("Eval iter:", batch_id)
407396
sys.stdout.flush()
408397
metric.accumulate()
409-
if not FLAGS.speed:
410-
metric.log()
398+
metric.log()
411399
map_res = metric.get_results()
412400
metric.reset()
413401
time_avg = predict_time / sample_nums
414402
print("[Benchmark]Inference time(ms): min={}, max={}, avg={}".format(
415403
round(time_min * 1000, 2),
416404
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]))
419406
sys.stdout.flush()
420407

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()
462408

463409
def main():
464410
"""
@@ -485,7 +431,6 @@ def main():
485431
repeats=repeats)
486432
else:
487433
reader_cfg = load_config(FLAGS.reader_config)
488-
489434
dataset = reader_cfg["EvalDataset"]
490435
global val_loader
491436
val_loader = create("EvalReader")(
@@ -496,11 +441,9 @@ def main():
496441
anno_file = dataset.get_anno()
497442
metric = COCOMetric(
498443
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+
504447
if rerun_flag:
505448
print(
506449
"***** Collect dynamic shape done, Please rerun the program to get correct results. *****"
@@ -516,9 +459,6 @@ def main():
516459
else:
517460
FLAGS.include_nms = False
518461

519-
print('**************main****************')
520-
print(FLAGS)
521-
522462
# DataLoader need run on cpu
523463
paddle.set_device("cpu")
524464

example/auto_compression/detection/post_process.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _non_max_suppression(self, prediction, scale_factor):
122122
picked_labels.extend([class_index] * box_probs.shape[0])
123123

124124
if len(picked_box_probs) == 0:
125-
out_boxes_list.append(np.empty((0, 4)))
125+
out_boxes_list.append(np.empty((0, 6)))
126126

127127
else:
128128
picked_box_probs = np.concatenate(picked_box_probs)

0 commit comments

Comments
 (0)