def load_model(self, weights: str, device: str = get_mode_torch()) -> None: self.model = self.exp.get_model() self.model.eval() # load the model state dict ckpt = torch.load(weights, map_location="cpu") self.model.load_state_dict(ckpt["model"]) if device == "gpu": self.model.cuda() if self.half: self.model.half() # to FP16 self.device = device if self.fuse: self.model = fuse_model(self.model)
def main(exp, args): if not args.experiment_name: args.experiment_name = exp.exp_name # set environment variables for distributed training cudnn.benchmark = True rank = 0 file_name = os.path.join(exp.output_dir, args.experiment_name) os.makedirs(file_name, exist_ok=True) if args.save_result: vis_folder = os.path.join(file_name, 'vis_res') os.makedirs(vis_folder, exist_ok=True) setup_logger( file_name, distributed_rank=rank, filename="demo_log.txt", mode="a" ) logger.info("Args: {}".format(args)) if args.conf is not None: exp.test_conf = args.conf if args.nms is not None: exp.nmsthre = args.nms if args.tsize is not None: exp.test_size = (args.tsize, args.tsize) model = exp.get_model() logger.info("Model Summary: {}".format(get_model_info(model, exp.test_size))) torch.cuda.set_device(rank) model.cuda(rank) model.eval() if not args.trt: if args.ckpt is None: ckpt_file = os.path.join(file_name, "best_ckpt.pth.tar") else: ckpt_file = args.ckpt logger.info("loading checkpoint") loc = "cuda:{}".format(rank) ckpt = torch.load(ckpt_file, map_location=loc) # load the model state dict model.load_state_dict(ckpt["model"]) logger.info("loaded checkpoint done.") if args.fuse: logger.info("\tFusing model...") model = fuse_model(model) if args.trt: assert (not args.fuse),\ "TensorRT model is not support model fusing!" trt_file = os.path.join(file_name, "model_trt.pth") assert os.path.exists(trt_file), ( "TensorRT model is not found!\n Run python3 tools/trt.py first!" ) model.head.decode_in_inference = False decoder = model.head.decode_outputs logger.info("Using TensorRT to inference") else: trt_file = None decoder = None predictor = Predictor(model, exp, COCO_CLASSES, trt_file, decoder) current_time = time.localtime() if args.demo == 'image': image_demo(predictor, vis_folder, args.path, current_time, args.save_result) elif args.demo == 'video' or args.demo == 'webcam': imageflow_demo(predictor, vis_folder, current_time, args)
def main(exp, args): if not args.experiment_name: args.experiment_name = exp.exp_name output_dir = osp.join(exp.output_dir, args.experiment_name) os.makedirs(output_dir, exist_ok=True) if args.save_result: vis_folder = osp.join(output_dir, "track_vis") os.makedirs(vis_folder, exist_ok=True) if args.trt: args.device = "gpu" args.device = torch.device("cuda" if args.device == "gpu" else "cpu") logger.info("Args: {}".format(args)) if args.conf is not None: exp.test_conf = args.conf if args.nms is not None: exp.nmsthre = args.nms if args.tsize is not None: exp.test_size = (args.tsize, args.tsize) model = exp.get_model().to(args.device) logger.info("Model Summary: {}".format(get_model_info( model, exp.test_size))) model.eval() if not args.trt: if args.ckpt is None: ckpt_file = osp.join(output_dir, "best_ckpt.pth.tar") else: ckpt_file = args.ckpt logger.info("loading checkpoint") ckpt = torch.load(ckpt_file, map_location="cpu") # load the model state dict model.load_state_dict(ckpt["model"]) logger.info("loaded checkpoint done.") if args.fuse: logger.info("\tFusing model...") model = fuse_model(model) if args.fp16: model = model.half() # to FP16 if args.trt: assert not args.fuse, "TensorRT model is not support model fusing!" trt_file = osp.join(output_dir, "model_trt.pth") assert osp.exists( trt_file ), "TensorRT model is not found!\n Run python3 tools/trt.py first!" model.head.decode_in_inference = False decoder = model.head.decode_outputs logger.info("Using TensorRT to inference") else: trt_file = None decoder = None predictor = Predictor(model, exp, trt_file, decoder, args.device, args.fp16) current_time = time.localtime() if args.demo == "image": image_demo(predictor, vis_folder, current_time, args) elif args.demo == "video" or args.demo == "webcam": imageflow_demo(predictor, vis_folder, current_time, args)
def main(exp, args, num_gpu): if args.seed is not None: random.seed(args.seed) torch.manual_seed(args.seed) cudnn.deterministic = True warnings.warn( "You have chosen to seed testing. This will turn on the CUDNN deterministic setting, " ) is_distributed = num_gpu > 1 # set environment variables for distributed training cudnn.benchmark = True rank = args.local_rank # rank = get_local_rank() file_name = os.path.join(exp.output_dir, args.experiment_name) if rank == 0: os.makedirs(file_name, exist_ok=True) setup_logger(file_name, distributed_rank=rank, filename="val_log.txt", mode="a") logger.info("Args: {}".format(args)) if args.conf is not None: exp.test_conf = args.conf if args.nms is not None: exp.nmsthre = args.nms if args.tsize is not None: exp.test_size = (args.tsize, args.tsize) model = exp.get_model() logger.info("Model Summary: {}".format(get_model_info( model, exp.test_size))) logger.info("Model Structure:\n{}".format(str(model))) evaluator = exp.get_evaluator(args.batch_size, is_distributed, args.test) torch.cuda.set_device(rank) model.cuda(rank) model.eval() if not args.speed and not args.trt: if args.ckpt is None: ckpt_file = os.path.join(file_name, "best_ckpt.pth.tar") else: ckpt_file = args.ckpt logger.info("loading checkpoint") loc = "cuda:{}".format(rank) ckpt = torch.load(ckpt_file, map_location=loc) # load the model state dict model.load_state_dict(ckpt["model"]) logger.info("loaded checkpoint done.") if is_distributed: model = DDP(model, device_ids=[rank]) if args.fuse: logger.info("\tFusing model...") model = fuse_model(model) if args.trt: assert ( not args.fuse and not is_distributed and args.batch_size == 1 ), "TensorRT model is not support model fusing and distributed inferencing!" trt_file = os.path.join(file_name, "model_trt.pth") assert os.path.exists( trt_file), "TensorRT model is not found!\n Run tools/trt.py first!" model.head.decode_in_inference = False decoder = model.head.decode_outputs else: trt_file = None decoder = None # start evaluate *_, summary = evaluator.evaluate(model, is_distributed, args.fp16, trt_file, decoder, exp.test_size) logger.info("\n" + summary)