def inference(config, checkpoint, img_path, device='cuda:0', half=False, augment=False, scores_thr=0.3, merge=False, save_json=False, save_file=False, save_path='', show=False): device = select_device(device) model = init_detector(config, checkpoint=checkpoint, device=device) t0 = time.time() if os.path.isdir(img_path): imgs_paths = get_file_realpath( img_path, *[ ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".bmp", ".BMP" ]) for i, img in enumerate(imgs_paths): basename = os.path.basename(img) result, t = inference_detector(model, img=img, scores_thr=scores_thr, augment=augment, half=half, merge=merge) #print(result) # Print time (inference + NMS) print('%s Done , object num : %s .time:(%.3fs)' % (basename, len(result), t)) show_result(img, result, model.CLASSES, show=show, save_json=save_json, save_file=save_file, out_path=save_path, file_name=basename) else: basename = os.path.basename(img_path) t1 = torch_utils.time_synchronized() result = inference_detector(model, img=img_path, scores_thr=scores_thr, augment=augment, half=half, merge=merge) t2 = torch_utils.time_synchronized() # print(result) # Print time (inference + NMS) print('%s Done , object num : %s .time:(%.3fs)' % (basename, len(result), t2 - t1)) show_result(img_path, result, model.CLASSES, show=show, save_json=save_json, save_file=save_file, out_path=save_path, file_name=basename) print('Done. (%.3fs)' % (time.time() - t0))
stds = [0.229, 0.224, 0.225] size = 608 transform = transforms.Compose([ transforms.Resize(size), transforms.CenterCrop(size), transforms.ToTensor(), transforms.Normalize(means, stds) ]) tensor = transform(I).unsqueeze(0).requires_grad_() # model = models.alexnet(pretrained=True) config = '/disk2/project/pytorch-YOLOv4/cfg/yolov4_hand_gpu.py' checkpoint = '/disk2/project/pytorch-YOLOv4/work_dirs/test/epoch_6.pth' model = init_detector(config, checkpoint=checkpoint, device='cuda:0') visualisation = {} def hook_fn(m, i, o): visualisation[m] = o def get_all_layers(model): for name, layer in model._modules.items(): # If it is a sequential, don't register a hook on it # but recursively register hook on all it's module children if isinstance(layer, nn.Sequential): get_all_layers(layer) else: # it's a non sequential. Register a hook
parser.add_argument('--half', action='store_true', help='fp16 half precision') opt = parser.parse_args() print(opt) # config = '/disk2/project/mmdetection/mount/pytorch-YOLOv4/cfg/yolov5_coco_gpu.py' # checkpoint = '/disk2/project/pytorch-YOLOv4/work_dirs/yolov5-l_epoch_24.pth' cfg = Config.fromfile(opt.config) cfg.data.val.train = False val_dataset = build_from_dict(cfg.data.val, DATASET) val_dataloader = build_dataloader(val_dataset, data=cfg.data, shuffle=False) device = select_device(opt.device) # model = init_detector(opt.config, checkpoint=opt.checkpoint, device=device) model = init_detector(opt.config, checkpoint=opt.checkpoint, device=device) result = single_gpu_test(model, val_dataloader, half=opt.half, conf_thres=opt.conf_thres, iou_thres=opt.iou_thres, merge=opt.merge, save_json=opt.save_json, augment=opt.augment, verbose=opt.verbose, coco_val_path=opt.coco_val_path) print(result)