def inference_onnx(src, cfg): predictor = DetPredictorTRT(cfg) if isinstance(src, (str, list)): # 如果是list,则认为是图片预测 for result in predictor(src): vis_all_opencv(*result, class_names=get_classes('coco'), score_thr=0.5) if isinstance(src, int): vis_cam(src, predictor, class_names=get_classes('coco'), score_thr=0.2)
load_device='cuda') if task == 'load': # 已有数据集评估文件,重新载入进行评估 eval_dataset_det( cfg_path=cfg_path, load_from='/home/ubuntu/mytrain/ssd_vgg_voc/epoch_11.pth', load_device='cuda', result_file= '/home/ubuntu/mytrain/ssd_vgg_voc/20190928_084133_eval_result.pkl') if task == 'test': # 测试单张图或多张图的结果: cpu上0.649 sec, gpu上0.388 sec img = cv2.imread('/home/ubuntu/MyDatasets/misc/test13.jpg') predictor = DetPredictor( cfg_path, load_from= '/home/ubuntu/mytrain/ssd_vgg_voc/old weights/epoch_61.pth', load_device='cpu') for results in predictor([img]): vis_all_pyplot(*results, class_names=get_classes('voc'), score_thr=0.5) if task == 'video': # 测试视频预测结果:注意方法稍有不同,vis_cam需要传入一个predictor src = 0 # src可以等于int(cam_id), str(video path), list(img_list) predictor = DetPredictor( cfg_path, load_from= '/home/ubuntu/mytrain/ssd_vgg_voc/old weights/epoch_61.pth', load_device='cpu') vis_cam(src, predictor, class_names=get_classes('voc'), score_thr=0.2)
if task == 'integrate': labels = get_classes('imagenet') model_path = '/home/ubuntu/mytrain/onnx_to_trt/serialized.engine' input_size = (224, 224) # (w, h) predictor = ClsTRTPredictor(model_path, input_size, labels) img = cv2.imread(cfg.imgs_path[0]) # 也可以先打开所有img for result in predictor([img]): cv2.imshow('result', result[0]) if task == 'cam': labels = get_classes('imagenet') model_path = '/home/ubuntu/mytrain/onnx_to_trt/serialized.engine' input_size = (224, 224) # (w, h) predictor = ClsTRTPredictor(model_path, input_size, labels) vis_cam(0, predictor, labels, resolution=(800, 800)) if task == 'infe_pt': # 用pytorch推理,对比时间消耗 from utils.evaluation import ClsPredictor labels = get_classes('imagenet') cfg_path = 'cfg_classifier_resnet50_imagenet.py' img_id = 1 gt_label = cfg.gt_labels[img_id] img = cv2.imread(cfg.imgs_path[img_id]) predictor = ClsPredictor(cfg_path, load_from=None, load_device='cuda') pred, score = list(predictor([img]))[0] pred = labels[pred] print('pred: %s [%.4f], gt: %s' % (pred, score, gt_label)) """ 比对结果:只考虑计算模型计算时间,其他图片预处理等时间都不考虑,即trt的do_inference(),和pytorch的model forward() 1. tensorRT inference时间: 0.061s
cfg_path=cfg_path, load_from='/home/ubuntu/mytrain/ssd_vgg_widerface/epoch_9.pth', load_device='cuda', result_file= '/home/ubuntu/mytrain/ssd_vgg_voc/20190928_084133_eval_result.pkl') if task == 'test': # 测试单张图或多张图的结果 img = cv2.imread('/home/ubuntu/MyDatasets/misc/26.jpg') predictor = DetPredictor( cfg_path, load_from='/home/ubuntu/mytrain/ssd_vgg_widerface/epoch_9.pth', load_device='cpu') for results in predictor([img]): vis_all_opencv(*results, class_names=get_classes('widerface'), score_thr=0.25, show=['img', 'bbox']) # 针对widerface的训练结果模型,对小脸比较有效,但对大脸效果很差,因为数据集里边大部分都是小脸,大脸数据太少。 # 所以靠近摄像头反而检不出来,远离摄像头就能检出来。 if task == 'video': src = 0 # src可以等于int(cam_id), str(video path), list(img_list) predictor = DetPredictor( cfg_path, load_from='/home/ubuntu/mytrain/ssd_vgg_widerface/epoch_9.pth', load_device='cuda') vis_cam(src, predictor, class_names=get_classes('widerface'), score_thr=0.2)