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)
def print_map_summary(mean_ap, results, dataset=None): """Print mAP and results of each class. Args: mean_ap(float): calculated from `eval_map` results(list): calculated from `eval_map` dataset(None or str or list): dataset name. """ num_scales = len(results[0]['ap']) if isinstance(results[0]['ap'], np.ndarray) else 1 num_classes = len(results) recalls = np.zeros((num_scales, num_classes), dtype=np.float32) precisions = np.zeros((num_scales, num_classes), dtype=np.float32) aps = np.zeros((num_scales, num_classes), dtype=np.float32) num_gts = np.zeros((num_scales, num_classes), dtype=int) for i, cls_result in enumerate(results): if cls_result['recall'].size > 0: recalls[:, i] = np.array(cls_result['recall'], ndmin=2)[:, -1] precisions[:, i] = np.array( cls_result['precision'], ndmin=2)[:, -1] aps[:, i] = cls_result['ap'] num_gts[:, i] = cls_result['num_gts'] if dataset is None: label_names = [str(i) for i in range(1, num_classes + 1)] else: label_names = get_classes(dataset) if not isinstance(mean_ap, list): mean_ap = [mean_ap] header = ['class', 'gts', 'dets', 'recall', 'precision', 'ap'] for i in range(num_scales): table_data = [header] for j in range(num_classes): row_data = [ label_names[j], num_gts[i, j], results[j]['num_dets'], '{:.3f}'.format(recalls[i, j]), '{:.3f}'.format( precisions[i, j]), '{:.3f}'.format(aps[i, j]) ] table_data.append(row_data) table_data.append(['mAP', '', '', '', '', '{:.3f}'.format(mean_ap[i])]) table = AsciiTable(table_data) table.inner_footing_row_border = True print(table.table)
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 == 'train': # 模型训练 train_fcos(cfg=cfg, resume_from = None) if task == 'eval': # 数据集评估 parse_log('/home/ubuntu/mytrain/ssd_vgg_voc/20190926_181047.log') eval_dataset_det(cfg_path=cfg_path, load_from = '/home/ubuntu/mytrain/ssd_vgg_voc/epoch_11.pth', 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('../test/4.jpg') predictor = DetPredictor(cfg_path, load_from = '/home/ubuntu/mytrain/ssd_vgg_voc/epoch_11.pth', load_device='cuda') for results in predictor([img]): vis_all_pyplot(*results, class_names=get_classes('voc'), score_thr=0.2) 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/epoch_11.pth', load_device='cpu') vis_cam(src, predictor, class_names=get_classes('voc'), score_thr=0.2)
'/home/ubuntu/MyDatasets/misc/m3.jpg', '/home/ubuntu/MyDatasets/misc/000033.jpg', '/home/ubuntu/MyDatasets/misc/test11.jpg', '/home/ubuntu/MyDatasets/misc/141597.jpg' ] # 分别是cat,dog,bus gt_labels = ['cat', 'dog', 'bus', 'plane'] input_size = (3, 224, 224) DTYPE = trt.float32 if __name__ == '__main__': task = 'cam' 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))
# ## eval_dataset_det(cfg_path=cfg_path, ## load_from = '/home/ubuntu/mytrain/ssd_vgg_voc/epoch_11.pth', ## load_device='cuda') # # if task == 'load': # 已有数据集评估文件,重新载入进行评估 # eval_dataset_det(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/retinaface_widerface/epoch_21.pth', load_device='cpu') for results in predictor([img]): vis_all_opencv(*results, class_names=get_classes('widerface'), score_thr=0.15, show=['img', 'bbox', 'label', 'landmark']) # # 所以靠近摄像头反而检不出来,远离摄像头就能检出来。 # 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) #
]) # # if task == 'eval': # 数据集评估 # eval_dataset_det(cfg_path=cfg_path, # load_from = '/home/ubuntu/mytrain/ssd_vgg_voc/epoch_11.pth', # 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/1.jpg') predictor = DetPredictor( cfg_path, load_from='/home/ubuntu/mytrain/retinanet_resnet50_voc/epoch_1.pth', load_device='cpu') for results in predictor([img]): vis_all_pyplot(*results, class_names=get_classes('voc'), score_thr=0.08) # # 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/epoch_11.pth', # load_device='cpu') # vis_cam(src, predictor, class_names=get_classes('voc'), score_thr=0.2) #