def read_config(self): config_path = self.config_path cfg_from_yaml_file(self.config_path, cfg) self.logger = common_utils.create_logger() self.demo_dataset = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path( "/home/muzi2045/Documents/project/OpenPCDet/data/kitti/velodyne/000001.bin" ), ext='.bin') self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.net = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=self.demo_dataset) self.net.load_params_from_file(filename=self.model_path, logger=self.logger, to_cpu=True) self.net = self.net.to(self.device).eval() # nuscenes dataset lidar2imu_t = np.array([0.985793, 0.0, 1.84019]) lidar2imu_r = Quaternion( [0.706749235, -0.01530099378, 0.0173974518, -0.7070846]) self.lidar2imu = transform_matrix(lidar2imu_t, lidar2imu_r, inverse=True) self.imu2lidar = transform_matrix(lidar2imu_t, lidar2imu_r, inverse=False)
def main(): args, cfg = parse_config() logger.info( '-----------------Quick Demo 3D of OpenPCDet-------------------------') demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() # print instructions print("To navigate:") print("\tb: back (previous scan)") print("\tn: next (next scan)") print("\tq: quit (exit program)") # run the visualizer vis = VisualizeDets(model, demo_dataset) vis.run() logger.info('Demo done.')
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info('-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger ) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) V.draw_scenes( points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels'] ) if not OPEN3D_FLAG: mlab.show(stop=True) logger.info('Demo done.')
def main(): args, cfg = parse_config() if args.launcher == 'none': dist_test = False else: args.batch_size, cfg.LOCAL_RANK = getattr(common_utils, 'init_dist_%s' % args.launcher)( args.batch_size, args.tcp_port, args.local_rank, backend='nccl' ) dist_test = True output_dir = cfg.ROOT_DIR / 'output' / cfg.EXP_GROUP_PATH / cfg.TAG / args.extra_tag output_dir.mkdir(parents=True, exist_ok=True) eval_output_dir = output_dir / 'eval' if not args.eval_all: num_list = re.findall(r'\d+', args.ckpt) if args.ckpt is not None else [] epoch_id = num_list[-1] if num_list.__len__() > 0 else 'no_number' eval_output_dir = eval_output_dir / ('epoch_%s' % epoch_id) / cfg.DATA_CONFIG.DATA_SPLIT['test'] else: eval_output_dir = eval_output_dir / 'eval_all_default' if args.eval_tag is not None: eval_output_dir = eval_output_dir / args.eval_tag eval_output_dir.mkdir(parents=True, exist_ok=True) log_file = eval_output_dir / ('log_eval_%s.txt' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S')) logger = common_utils.create_logger(log_file, rank=cfg.LOCAL_RANK) # log to file logger.info('**********************Start logging**********************') gpu_list = os.environ['CUDA_VISIBLE_DEVICES'] if 'CUDA_VISIBLE_DEVICES' in os.environ.keys() else 'ALL' logger.info('CUDA_VISIBLE_DEVICES=%s' % gpu_list) if dist_test: total_gpus = dist.get_world_size() logger.info('total_batch_size: %d' % (total_gpus * args.batch_size)) for key, val in vars(args).items(): logger.info('{:16} {}'.format(key, val)) log_config_to_file(cfg, logger=logger) ckpt_dir = args.ckpt_dir if args.ckpt_dir is not None else output_dir / 'ckpt' test_set, test_loader, sampler = build_dataloader( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, batch_size=args.batch_size, dist=dist_test, workers=args.workers, logger=logger, training=False ) model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=test_set) with torch.no_grad(): if args.eval_all: repeat_eval_ckpt(model, test_loader, args, eval_output_dir, logger, ckpt_dir, dist_test=dist_test) else: eval_single_ckpt(model, test_loader, args, eval_output_dir, logger, epoch_id, dist_test=dist_test)
def load_model(self): model = build_network(model_cfg=self.cfg.MODEL, num_class=len(self.cfg.CLASS_NAMES), dataset=self.preprocesiing) model.load_params_from_file(filename=self.args.ckpt, logger=self.logger, to_cpu=True) model.cuda() model.eval() return model
def initialize_model(): """Method that will be called by the evaluation script to load the model and weights. """ global model model = build_network(model_cfg=model_cfg.MODEL, num_class=len(model_cfg.CLASS_NAMES), dataset=dataset_processor) model.load_params_from_file(MODEL_WEIGHTS, logger=logger, to_cpu=False) model.cuda() model.eval()
def main(): args, cfg = parse_config() log_file = 'log_inference_%s.txt' % datetime.datetime.now().strftime( '%Y%m%d-%H%M%S') logger = common_utils.create_logger(log_file, rank=0) logger.info( '-----------------Inference of OpenPCDet-------------------------') test_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(test_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=test_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=False) model.cuda() model.eval() if args.save_video_path is not None: fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(os.path.join(args.save_video_path, 'result.avi'), fourcc, 10.0, (400, 1600)) bev_range = [-5, -20, -2, 155, 20, 5] with torch.no_grad(): for idx, data_dict in tqdm(enumerate(test_dataset)): data_dict = test_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) if args.save_video_path is not None: boxes = pred_dicts[0]['pred_boxes'].cpu().detach().numpy() boxes = boxes[:, np.newaxis, :].repeat(3, axis=1) gt_boxes = None if test_dataset.split == 'val': gt_boxes = test_dataset.val_data_list[idx]['annos'][ 'gt_boxes_lidar'] gt_boxes = gt_boxes[:, np.newaxis, :].repeat(3, axis=1) image = plot_multiframe_boxes( data_dict['points'][:, 1:].cpu().numpy(), boxes, bev_range, gt_boxes=gt_boxes) cv2.imshow('show_result', image) cv2.waitKey(1) out.write(image) out.release()
def inference_with_info(): demo_dataset = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) with torch.no_grad(): model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() for idx, data_dict in tqdm(enumerate(demo_dataset)): data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) det_boxes = pred_dicts[0]['pred_boxes'].cpu().detach().numpy() scores = pred_dicts[0]['pred_scores'].cpu().numpy() labels = pred_dicts[0]['pred_labels'].cpu().numpy() gt_boxes = demo_dataset.val_data_list[idx]['annos']['gt_boxes_lidar'] # Evaluate current frame info = '' for iou_idx in range(len(ious)): for dist_range_idx in range(len(dist_ranges)): tp, num_valid_det, num_valid_gt, dist_err = get_metrics(gt_boxes, det_boxes, dist_ranges[dist_range_idx], ious[iou_idx]) total_num_tp[iou_idx, dist_range_idx] += tp total_num_valid_det[iou_idx, dist_range_idx] += num_valid_det total_num_valid_gt[iou_idx, dist_range_idx] += num_valid_gt total_dist_err[iou_idx, dist_range_idx] += dist_err info += 'tp: {}, dt: {}, gt: {}\n'.format(tp, num_valid_det, num_valid_gt) det_boxes = det_boxes[:, np.newaxis, :].repeat(3, axis=1) gt_boxes = gt_boxes[:, np.newaxis, :].repeat(3, axis=1) image = plot_multiframe_boxes(data_dict['points'][:, 1:].cpu().numpy(), det_boxes, cfg.DATA_CONFIG.POINT_CLOUD_RANGE, gt_boxes=gt_boxes, scores=scores, labels=labels) info = info.split("\n") fontScale = 0.6 thickness = 1 fontFace = cv2.FONT_HERSHEY_SIMPLEX text_size, baseline = cv2.getTextSize(str(info), fontFace, fontScale, thickness) for i, text in enumerate(info): if text: draw_point = (10, 10 + (text_size[1] + 2 + baseline) * i) cv2.putText(image, text, draw_point, fontFace=fontFace, fontScale=fontScale, color=(0, 255, 0), thickness=thickness) [bag_name, _, frame] = demo_dataset.val_data_list[idx]['point_cloud']['lidar_idx'].split('/') image_file = os.path.join(save_path, bag_name + '_' + frame[:-4] + '.png') cv2.imwrite(image_file, image)
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info('-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger ) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) points=data_dict['points'][:, 1:] ref_boxes=pred_dicts[0]['pred_boxes'] ref_scores=pred_dicts[0]['pred_scores'] ref_labels=pred_dicts[0]['pred_labels'] points_cp=points.cpu() points_nu=points_cp.numpy() ref_boxes_cp=ref_boxes.cpu() ref_boxes_nu=ref_boxes_cp.numpy() ref_scores_cp=ref_scores.cpu() ref_scores_nu=ref_scores_cp.numpy() ref_labels_cp=ref_labels.cpu() ref_labels_nu=ref_labels_cp.numpy() print(points.shape,ref_boxes.shape) print(ref_scores.shape,ref_labels.shape) #with open('./save1.npy', 'wb') as f: # np.save(f,points_nu) #with open('./save2.npy', 'wb') as f: # np.save(f,ref_boxes_nu) #with open('./save3.npy', 'wb') as f: # np.save(f,ref_scores_nu) #with open('./save4.npy', 'wb') as f: # np.save(f,ref_labels_nu) #np.array(points.shape).tofile(f) #points.tofile(f) # V.draw_scenes( # points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], # ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels'] # ) # mlab.show(stop=True) logger.info('Demo done.')
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info('-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger ) logger.info(f'Total number of samples: \t{len(demo_dataset)}') if args.saved_pred == "": model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) with open('../saved_pred/curr_pickle.pkl', 'wb+') as f: data_ = { "data_dict": data_dict['points'][:, 1:], "pred_dicts": pred_dicts } pkl.dump(data_, f) else: with open('../saved_pred/curr_pickle.pkl', 'rb') as f: data_ = pkl.load(f) data_dict = data_["data_dict"] pred_dicts = data_["pred_dicts"] vdisplay = Xvfb(width=1920, height=1080) vdisplay.start() V.draw_scenes( points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels'] ) vdisplay.stop() mlab.show(stop=True) mlab.savefig("./test_eg.png") logger.info('Demo done.')
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info( '-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') data_name_list = demo_dataset.sample_file_list # print(data_name_list) print('evaluation data size=', len(data_name_list)) model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): # logger.info(f'Visualized sample index: \t{idx + 1}') logger.info(f'Detecte sample: \t{data_name_list[idx]}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) print(pred_dicts) # print(data_dict) # print(type(pred_dicts[0]['pred_boxes'])) # print(pred_dicts[0]['pred_boxes']) res = pred_dicts[0]['pred_boxes'].cpu().numpy().round(8) save_filename = str(data_name_list[idx]) np.savetxt('evaluation/' + save_filename[save_filename.rfind('/') + 1:].replace( '.bin', '.txt'), res, fmt='%.08f') # test_f.writelines(pred_dicts[0]['pred_boxes']) V.draw_scenes(points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels']) mlab.show(stop=True) logger.info('Demo done.')
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info( '-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): for idx, (data_dict, data_path) in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') logger.info("Process: %s" % data_path) pred_s = dict() pred_s["data_path"] = data_path data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) pred_s['pred_boxes'] = pred_dicts[0]['pred_boxes'].cpu().numpy( ).tolist() pred_s['pred_scores'] = pred_dicts[0]['pred_scores'].cpu().numpy( ).tolist() pred_s['pred_labels'] = pred_dicts[0]['pred_labels'].cpu().numpy( ).tolist() json_path = os.path.splitext(data_path)[0] + ".json" with open(json_path, 'w') as fp: json.dump(pred_s, fp) # exit(1) # V.draw_scenes( # points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], # ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels'] # ) # # mlab.show(stop=True) logger.info('Demo done.')
def setup_model(self): cfg_from_yaml_file(self.detector_config, cfg) self.logger = common_utils.create_logger() self.dataset = DummyDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES) self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.net = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=self.dataset) self.net.load_params_from_file(filename=self.model_path, logger=self.logger, to_cpu=True) self.net = self.net.to(self.device).eval()
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info( '-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') # display = Display(visible=0, size =(1280, 1024)) # display.start() model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) # V.draw_scenes( # points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], # ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels'] # ) boxes_file = open(args.save + '_boxes', 'wb') scores_file = open(args.save + '_scores', 'wb') labels_file = open(args.save + '_labels', 'wb') pickle.dump(pred_dicts[0]['pred_boxes'].cpu().numpy(), boxes_file, protocol=4) pickle.dump(pred_dicts[0]['pred_scores'].cpu().numpy(), scores_file, protocol=4) pickle.dump(pred_dicts[0]['pred_labels'].cpu().numpy(), labels_file, protocol=4) boxes_file.close() scores_file.close() labels_file.close() logger.info('Demo done.')
def main(): for sweep in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: lines = open( 'cfgs/dataset_configs/nuscenes_dataset.yaml').read().splitlines() lines[4] = 'MAX_SWEEPS: ' + str(sweep) open('cfgs/dataset_configs/nuscenes_dataset.yaml', 'w').write('\n'.join(lines)) args, cfg = parse_config() logger = common_utils.create_logger() demo_dataset = NuScenesDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), logger=logger) model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() times = [] for i in range(args.frames): with torch.no_grad(): data_dict = demo_dataset[i] start = time.time() data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) end = time.time() times.append(end - start) print("sweeps", sweep) print("min", 1 / min(times)) print("1st_quantile", 1 / np.quantile(times, .25)) print("median", 1 / statistics.median(times)) print("1st_quantile", 1 / np.quantile(times, .75)) print("max", 1 / max(times)) print()
def __init__(self, input_dict): # pvrcnn cfg cfg_from_yaml_file(input_dict.cfg_file, cfg) # create logger log_dir = Path(str(input_dict.output_dir)) / 'log' log_dir.mkdir(parents=True, exist_ok=True) self.logger = common_utils.create_logger( log_dir / ('log_%s.txt' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S')), rank=cfg.LOCAL_RANK) # build dataset and network self.demo_dataset = DemoDataset( # dummy dataset for preprocess inputdata dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=input_dict.dummy_cloud, ext='.bin', logger=self.logger) self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=self.demo_dataset) self.model.load_params_from_file(filename=input_dict.ckpt_file, logger=self.logger, to_cpu=self.device == "cpu") self.model.to(self.device) self.model.eval() self.score_threshold = input_dict.score_threashold # for ROS self.action_server = actionlib.SimpleActionServer( "excavator/lidar_perception/ros_pvrcnn_action", detector3dAction, execute_cb=self.execute_cb, auto_start=False) self.action_server.start() self.mk_pub = rospy.Publisher("ros_pvrcnn", MarkerArray, queue_size=1) self.cls_list = [String(cls) for cls in cfg.CLASS_NAMES]
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info('-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger ) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() avg_time = 0 avg_fps = 0 with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: {idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) start = time() pred_dicts, _ = model.forward(data_dict) end = time() time_forward = (end - start) * 1000 if idx > 0: avg_time += time_forward avg_fps += 1000 / time_forward logger.info(f'Time in ms for sample index: {idx + 1} is {"{:.2f}".format(time_forward)} ms') logger.info(f'FPS for sample index: {idx + 1} is {"{:.2f}".format(1000 / time_forward)} frame per second') V.draw_scenes( points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels'] ) mlab.show(stop=True) avg_time /= len(demo_dataset) - 1 avg_fps /= len(demo_dataset) - 1 logger.info(f'Average Time in ms for {paper.type}: is {"{:.2f}".format(avg_time)} ms') logger.info(f'Average FPS for {paper.type}: is {"{:.2f}".format(avg_fps)} frame per second') logger.info('Demo done.')
def main(): args, cfg = parse_config() logger = common_utils.create_logger() #logger记录日志 logger.info( '-----------------Quick Demo of OpenPCDet-------------------------') #建立一个DemoDataset类,其中储存关于输入数据的所有信息,包含六个参数 # dataset_cfg=cfg.DATA_CONFIG # 数据参数 # 包含数据集 / 数据路径 / 信息路径 / 数据处理器 / 数据增强器等 # class_names=cfg.CLASS_NAMES # 类别名 # training=False # 是否训练 # root_path=Path(args.data_path) # 数据路径 # ext=args.ext # 扩展 # logger=logger # 日志 demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): #目的是使得其中的数据不需要计算梯度,也不会进行反向传播 for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) V.draw_scenes(points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels']) mlab.show(stop=True) logger.info('Demo done.')
def read_config(self): config_path = self.config_path cfg_from_yaml_file(self.config_path, cfg) self.logger = common_utils.create_logger() self.demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path("/none"), ext='.bin') self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.net = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=self.demo_dataset) print("Model path: ", self.model_path) self.net.load_params_from_file(filename=self.model_path, logger=self.logger, to_cpu=True) self.net = self.net.to(self.device).eval()
def main(): # 1 输入参数 args, cfg = parse_config() # cfg的参数在tools/cfg/kitti_models/pv-rcnn.yaml logger = common_utils.create_logger() logger.info( '-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') # 2 调用的这些包就是pcdet/models/detectors下的各个py文件, model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) # 3 参数加载 model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) # cuda( ) 和 eval( ) 都是数据处理 model.cuda() model.eval() with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') # 样本数 # 4. collate_batch data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) # 传递数据给gpu的 pred_dicts, _ = model.forward( data_dict ) # 在神经网络中向前传递数据data_dict,得到预测数据pred_dicts 定位到forward,因为是PVRCNN类下的函数,先看__init__ /home/hcq/pointcloud/PCDet/pcdet/models/detectors/pv_rcnn.py # 可视化V V.draw_scenes(points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels']) mlab.show(stop=True) logger.info('Demo done.')
def read_config(self): config_path = self.config_path cfg_from_yaml_file(self.config_path, cfg) self.logger = common_utils.create_logger() self.demo_dataset = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path( "/home/muzi2045/Documents/project/OpenPCDet/data/kitti/velodyne/000001.bin" ), ext='.bin') self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.net = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=self.demo_dataset) self.net.load_params_from_file(filename=self.model_path, logger=self.logger, to_cpu=True) self.net = self.net.to(self.device).eval()
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info( '-----------------Quick Inference of OpenPCDet-------------------------' ) demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Run inference of sample index: \t{idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, recall_dicts, batch_dict = model.forward(data_dict) #visualization if args.vis: V.visualize_voxel_prediction(batch_dict) #evaluate point predictions if args.point: E.save_prediction_point_argoverse(batch_dict, save_gt=args.save_gt) logger.info('Quick inference done.')
def read_config(self): print(self.config_path) config_path = self.config_path cfg_from_yaml_file(self.config_path, cfg) self.logger = common_utils.create_logger() self.demo_datasets = DemoDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path( '/home/syang/Data/RS_datasets/datasets/ruby119_longzhudadao_1200423181920/npy/ruby119_longzhudadao_1200423181920_755.npy' ), ext='.npy') self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") print(self.device) self.net = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=self.demo_datasets) self.net.load_params_from_file(filename=self.model_path, logger=self.logger, to_cpu=True) self.net = self.net.to(self.device).eval()
def main(): args, cfg = parse_config() if args.launcher == 'none': dist_test = False total_gpus = 1 else: total_gpus, cfg.LOCAL_RANK = getattr(common_utils, 'init_dist_%s' % args.launcher)(args.tcp_port, args.local_rank, backend='nccl') dist_test = True assert not dist_test and args.batch_size == 1 if args.batch_size is None: args.batch_size = cfg.OPTIMIZATION.BATCH_SIZE_PER_GPU else: assert args.batch_size % total_gpus == 0, 'Batch size should match the number of gpus' args.batch_size = args.batch_size // total_gpus output_dir = cfg.ROOT_DIR / 'output' / cfg.EXP_GROUP_PATH / cfg.TAG / args.extra_tag output_dir.mkdir(parents=True, exist_ok=True) eval_output_dir = Path('deploy/eval') if args.eval_tag is not None: eval_output_dir = eval_output_dir / args.eval_tag eval_output_dir.mkdir(parents=True, exist_ok=True) log_file = eval_output_dir / ( 'log_eval_%s.txt' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S')) logger = common_utils.create_logger(log_file, rank=cfg.LOCAL_RANK) # log to file logger.info('**********************Start logging**********************') gpu_list = os.environ[ 'CUDA_VISIBLE_DEVICES'] if 'CUDA_VISIBLE_DEVICES' in os.environ.keys( ) else 'ALL' logger.info('CUDA_VISIBLE_DEVICES=%s' % gpu_list) if dist_test: logger.info('total_batch_size: %d' % (total_gpus * args.batch_size)) for key, val in vars(args).items(): logger.info('{:16} {}'.format(key, val)) log_config_to_file(cfg, logger=logger) test_set, test_loader, sampler = build_dataloader( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, batch_size=args.batch_size, dist=dist_test, workers=args.workers, logger=logger, training=False) if args.pruned_model is not None: tag = 'pruned_model' model = torch.load(args.pruned_model, map_location=torch.device('cpu')) elif args.pretrained_model is not None: tag = 'large_model' model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=test_set) model.load_params_from_file(filename=args.pretrained_model, logger=logger, to_cpu=dist_test) else: raise RuntimeError('error: please input weights.') model = model.cuda() model.eval() ExportModel = OnnxModelPointPillars(model) ExportModel.eval() ExportModel = ExportModel.cuda() points = np.fromfile(args.pcs_for_export, dtype=np.float32).reshape(-1, 4) points = torch.from_numpy(points).float().cuda() points = torch.autograd.Variable(points.contiguous()) valid = torch.Tensor([len(points)]).int().cuda() dummy_input = torch.zeros((25000, 4)).float().cuda() dummy_input[:len(points)] = points torch.onnx.export( ExportModel, (dummy_input, valid), "pointpillars_%s.onnx" % tag, verbose=True, training=False, operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK, opset_version=10, input_names=['points', 'valid'], output_names=['pointpillars_output1', 'pointpillars_output2']) if args.eval_onnx_model: with torch.no_grad(): eval_single_ckpt_onnx(ExportModel, test_loader, args, eval_output_dir, logger, tag, dist_test=dist_test)
def main(): args, cfg = parse_config() dist_test = False total_gpus = 1 if args.batch_size is None: args.batch_size = cfg.OPTIMIZATION.BATCH_SIZE_PER_GPU else: assert args.batch_size % total_gpus == 0, 'Batch size should match the number of gpus' args.batch_size = args.batch_size // total_gpus output_dir = cfg.ROOT_DIR / 'output' / cfg.EXP_GROUP_PATH / cfg.TAG / args.extra_tag output_dir.mkdir(parents=True, exist_ok=True) eval_output_dir = output_dir / 'eval' epoch_id = '110' eval_output_dir = eval_output_dir / ( 'epoch_%s' % epoch_id) / cfg.DATA_CONFIG.DATA_SPLIT['test'] eval_tag = 'play' eval_output_dir = eval_output_dir / eval_tag eval_output_dir.mkdir(parents=True, exist_ok=True) log_file = eval_output_dir / ( 'log_eval_%s.txt' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S')) logger = common_utils.create_logger(log_file, rank=cfg.LOCAL_RANK) # log to file logger.info('**********************Start logging**********************') gpu_list = os.environ[ 'CUDA_VISIBLE_DEVICES'] if 'CUDA_VISIBLE_DEVICES' in os.environ.keys( ) else 'ALL' logger.info('CUDA_VISIBLE_DEVICES=%s' % gpu_list) if dist_test: logger.info('total_batch_size: %d' % (total_gpus * args.batch_size)) for key, val in vars(args).items(): logger.info('{:16} {}'.format(key, val)) log_config_to_file(cfg, logger=logger) test_set, test_loader, sampler = build_dataloader( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, batch_size=args.batch_size, dist=dist_test, workers=args.workers, logger=logger, training=False) model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=test_set) with torch.no_grad(): eval_single_ckpt(model, test_loader, args, output_dir, eval_output_dir, logger, epoch_id, dist_test=dist_test)
def main(): args, cfg = parge_config() if args.launcher == 'none': dist_train = False else: args.batch_size, cfg.LOCAL_RANK = getattr( common_utils, 'init_dist_%s' % args.launcher)(args.batch_size, args.tcp_port, args.local_rank, backend='nccl') dist_train = True if args.fix_random_seed: common_utils.set_random_seed(666) output_dir = cfg.ROOT_DIR / 'output' / cfg.TAG / args.extra_tag output_dir.mkdir(parents=True, exist_ok=True) ckpt_dir = output_dir / 'ckpt' ckpt_dir.mkdir(parents=True, exist_ok=True) log_file = output_dir / ('log_train_%s.txt' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S')) logger = common_utils.create_logger(log_file, rank=cfg.LOCAL_RANK) # log to file logger.info('**********************Start logging**********************') gpu_list = os.environ[ 'CUDA_VISIBLE_DEVICES'] if 'CUDA_VISIBLE_DEVICES' in os.environ.keys( ) else 'ALL' logger.info('CUDA_VISIBLE_DEVICES=%s' % gpu_list) if dist_train: total_gpus = dist.get_world_size() logger.info('total_batch_size: %d' % (total_gpus * args.batch_size)) for key, val in vars(args).items(): logger.info('{:16} {}'.format(key, val)) log_config_to_file(cfg, logger=logger) tb_log = SummaryWriter( log_dir=str(output_dir / 'tensorboard')) if cfg.LOCAL_RANK == 0 else None if args.local_rank == 0: wandb.init(project='BEVSEG-PCDet', sync_tensorboard=True, name=args.extra_tag, config={ **vars(args), **cfg }) # -----------------------create dataloader & network & optimizer--------------------------- train_set, train_loader, train_sampler = build_dataloader( cfg.DATA_CONFIG.DATA_DIR, args.batch_size, dist_train, workers=args.workers, logger=logger, training=True) model = build_network(train_set) if args.sync_bn: model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model) model.cuda() optimizer = build_optimizer(model, cfg.MODEL.TRAIN.OPTIMIZATION) # load checkpoint if it is possible start_epoch = it = 0 last_epoch = -1 if args.pretrained_model is not None: model.load_params_from_file(filename=args.pretrained_model, to_cpu=dist, logger=logger) if args.ckpt is not None: it, start_epoch = model.load_params_with_optimizer(args.ckpt, to_cpu=dist, optimizer=optimizer, logger=logger) last_epoch = start_epoch + 1 else: ckpt_list = glob.glob(str(ckpt_dir / '*checkpoint_epoch_*.pth')) if len(ckpt_list) > 0: ckpt_list.sort(key=os.path.getmtime) it, start_epoch = model.load_params_with_optimizer( ckpt_list[-1], to_cpu=dist, optimizer=optimizer, logger=logger) last_epoch = start_epoch + 1 model.train( ) # before wrap to DistributedDataParallel to support fixed some parameters if dist_train: model = nn.parallel.DistributedDataParallel( model, device_ids=[cfg.LOCAL_RANK % torch.cuda.device_count()], find_unused_parameters= True # uncomment this line to debug unused params ) logger.info(model) lr_scheduler, lr_warmup_scheduler = build_scheduler( optimizer, total_iters_each_epoch=len(train_loader), total_epochs=args.epochs, last_epoch=last_epoch, optim_cfg=cfg.MODEL.TRAIN.OPTIMIZATION) # -----------------------start training--------------------------- logger.info( '**********************Start training %s(%s)**********************' % (cfg.TAG, args.extra_tag)) train_model(model, optimizer, train_loader, model_func=model_fn_decorator(), lr_scheduler=lr_scheduler, optim_cfg=cfg.MODEL.TRAIN.OPTIMIZATION, start_epoch=start_epoch, total_epochs=args.epochs, start_iter=it, rank=cfg.LOCAL_RANK, tb_log=tb_log, ckpt_save_dir=ckpt_dir, train_sampler=train_sampler, lr_warmup_scheduler=lr_warmup_scheduler, ckpt_save_interval=args.ckpt_save_interval, max_ckpt_save_num=args.max_ckpt_save_num) logger.info('**********************End training**********************') '''
def main(): args, cfg = parse_config() curr_seq = args.seq_path.split("/")[-1] logger = common_utils.create_logger() logger.info( '-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = DemoDataset(dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.seq_path), ext=args.ext, logger=logger) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() # Removing existing csv file csv_file_path = '%s/%s.csv' % (args.output_dir, curr_seq) if os.path.exists(csv_file_path): os.remove(csv_file_path) # Loading the ground truth from kitti-odometry gt_path = "/".join( args.seq_path.split("/")[0:-2]) + "/label_02/" + curr_seq + ".txt" gt_data = np.genfromtxt(gt_path, dtype=str)[:, [0, 2, 13, 14, 15, 10, 11, 12, 16]] gt_data = gt_data[np.logical_or( gt_data[:, 1] == 'Car', np.logical_or(gt_data[:, 1] == 'Cyclist', gt_data[:, 1] == 'Pedestrian')), :] gt_data = gt_data[:, [0, 2, 3, 4, 5, 6, 7, 8]] # Converting to floats gt_data = gt_data.astype(np.float) with torch.no_grad(): for idx, data_dict in enumerate(demo_dataset): logger.info(f'Visualized sample index: \t{idx + 1}') data_dict = demo_dataset.collate_batch([data_dict]) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) # Creating output dir if it does not already exist Path(args.output_dir).mkdir(parents=True, exist_ok=True) relevant_gt_boxes = gt_data[gt_data[:, 0] == idx][:, 1:] data_ = { "data_dict": data_dict['points'][:, 1:].cpu().detach().numpy(), "pred_boxes": pred_dicts[0]["pred_boxes"].cpu().detach().numpy(), "pred_labels": pred_dicts[0]["pred_labels"].cpu().detach().numpy(), "pred_scores": pred_dicts[0]["pred_scores"].cpu().detach().numpy(), "gt_boxes": relevant_gt_boxes } with open('%s/curr_pickle_%s.pkl' % (args.output_dir, str(idx)), 'wb+') as f: pkl.dump(data_, f) # Writing to text file in kitti format for tracking step frame_data = np.zeros((data_["pred_labels"].shape[0], 15)) frame_data[:, 0] = idx # Frame ID frame_data[:, 1] = data_["pred_labels"] # Labels frame_data[:, 2:6] = 0 # 2d bounding boxes frame_data[:, 6] = data_["pred_scores"] # 2d bounding boxes frame_data[:, 7:10] = data_["pred_boxes"][:, 3:6] frame_data[:, 10:13] = data_["pred_boxes"][:, 0:3] frame_data[:, 13] = data_["pred_boxes"][:, -1] frame_data[:, 14] = 0 # Alpha with open('%s/%s.csv' % (args.output_dir, curr_seq), 'a') as f: np.savetxt(f, frame_data, delimiter=",")
def main(): args, cfg = parse_config() logger = common_utils.create_logger() logger.info('-----------------Quick Demo of OpenPCDet-------------------------') demo_dataset = NuScenesDataset( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, training=False, root_path=Path(args.data_path), logger=logger ) logger.info(f'Total number of samples: \t{len(demo_dataset)}') model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=demo_dataset) model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=True) model.cuda() model.eval() start = time.time() with torch.no_grad(): data_dict = demo_dataset[args.idx] print(type(data_dict)) print(data_dict) logger.info(f'Visualized sample index: \t{args.idx}') data_dict = demo_dataset.collate_batch([data_dict]) # print(type(data_dict)) # print(data_dict) load_data_to_gpu(data_dict) pred_dicts, _ = model.forward(data_dict) # for bb in pred_dicts[0]['pred_boxes']: # for x in bb: # print(float(x), end=" ") # print() # pred_dicts[0]['pred_boxes'][:,0] = 1 # pred_dicts[0]['pred_boxes'][:,1] = 0 # pred_dicts[0]['pred_boxes'][:,2] = 1 # pred_dicts[0]['pred_boxes'][:,3] = 1 # pred_dicts[0]['pred_boxes'][:,4] = 1 # pred_dicts[0]['pred_boxes'][:,5] = 1 # pred_dicts[0]['pred_boxes'][:,6] = 0 # pred_dicts[0]['pred_boxes'] = pred_dicts[0]['pred_boxes'].cpu().numpy() # foo = np.zeros((len(pred_dicts[0]['pred_boxes']), 2), dtype=pred_dicts[0]['pred_boxes'].dtype) # pred_dicts[0]['pred_boxes'] = np.concatenate((pred_dicts[0]['pred_boxes'], foo), axis=1) # pred_dicts[0]['pred_boxes'][:,7] = 0 # pred_dicts[0]['pred_boxes'][:,8] = 0 mask = (pred_dicts[0]['pred_scores']>0.3).float() indices = torch.nonzero(mask) V.draw_scenes( points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'][indices].reshape(-1, 9), ref_scores=pred_dicts[0]['pred_scores'][indices].reshape(-1), ref_labels=pred_dicts[0]['pred_labels'][indices].reshape(-1) ) # mask = (pred_dicts[0]['pred_scores']>0.5).float() # indices = torch.nonzero(mask) # V.draw_scenes( # points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'][indices].reshape(-1, 7), # ref_scores=pred_dicts[0]['pred_scores'][indices].reshape(-1), ref_labels=pred_dicts[0]['pred_labels'][indices].reshape(-1) # ) # print(pred_dicts[0]['pred_boxes'][indices]) # V.draw_scenes( # points=data_dict['points'][:, 1:], ref_boxes=pred_dicts[0]['pred_boxes'], # ref_scores=pred_dicts[0]['pred_scores'], ref_labels=pred_dicts[0]['pred_labels'] # ) mlab.show(stop=True) end = time.time() print(end-start) logger.info('Demo done.')
def main(): args, cfg = parse_config() if args.launcher == 'none': dist_train = False else: args.batch_size, cfg.LOCAL_RANK = getattr( common_utils, 'init_dist_%s' % args.launcher)(args.batch_size, args.tcp_port, args.local_rank, backend='nccl') dist_train = True if args.fix_random_seed: common_utils.set_random_seed(666) output_dir = cfg.ROOT_DIR / 'output' / cfg.EXP_GROUP_PATH / cfg.TAG / args.extra_tag ckpt_dir = output_dir / 'ckpt' output_dir.mkdir(parents=True, exist_ok=True) ckpt_dir.mkdir(parents=True, exist_ok=True) log_file = output_dir / ('log_train_%s.txt' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S')) logger = common_utils.create_logger(log_file, rank=cfg.LOCAL_RANK) # log to file logger.info('**********************Start logging**********************') gpu_list = os.environ[ 'CUDA_VISIBLE_DEVICES'] if 'CUDA_VISIBLE_DEVICES' in os.environ.keys( ) else 'ALL' logger.info('CUDA_VISIBLE_DEVICES=%s' % gpu_list) if dist_train: total_gpus = dist.get_world_size() logger.info('total_batch_size: %d' % (total_gpus * args.batch_size)) for key, val in vars(args).items(): logger.info('{:16} {}'.format(key, val)) log_config_to_file(cfg, logger=logger) if cfg.LOCAL_RANK == 0: os.system('cp %s %s' % (args.cfg_file, output_dir)) tb_log = SummaryWriter( log_dir=str(output_dir / 'tensorboard')) if cfg.LOCAL_RANK == 0 else None # -----------------------create dataloader & network & optimizer--------------------------- train_set, train_loader, train_sampler = build_dataloader( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, batch_size=args.batch_size, dist=dist_train, workers=args.workers, logger=logger, training=True, merge_all_iters_to_one_epoch=args.merge_all_iters_to_one_epoch, total_epochs=args.epochs) model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=train_set) if args.sync_bn: model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model) model.cuda() optimizer = build_optimizer(model, cfg.OPTIMIZATION) # load checkpoint if it is possible start_epoch = it = 0 last_epoch = -1 if args.pretrained_model is not None: model.load_params_from_file(filename=args.pretrained_model, to_cpu=dist, logger=logger) if args.ckpt is not None: it, start_epoch = model.load_params_with_optimizer(args.ckpt, to_cpu=dist, optimizer=optimizer, logger=logger) last_epoch = start_epoch + 1 else: ckpt_list = glob.glob(str(ckpt_dir / '*checkpoint_epoch_*.pth')) if len(ckpt_list) > 0: ckpt_list.sort(key=os.path.getmtime) it, start_epoch = model.load_params_with_optimizer( ckpt_list[-1], to_cpu=dist, optimizer=optimizer, logger=logger) last_epoch = start_epoch + 1 model.train( ) # before wrap to DistributedDataParallel to support fixed some parameters if dist_train: model = nn.parallel.DistributedDataParallel( model, device_ids=[cfg.LOCAL_RANK % torch.cuda.device_count()]) logger.info(model) lr_scheduler, lr_warmup_scheduler = build_scheduler( optimizer, total_iters_each_epoch=len(train_loader), total_epochs=args.epochs, last_epoch=last_epoch, optim_cfg=cfg.OPTIMIZATION) # -----------------------start training--------------------------- logger.info( '**********************Start training %s/%s(%s)**********************' % (cfg.EXP_GROUP_PATH, cfg.TAG, args.extra_tag)) train_model(model, optimizer, train_loader, model_func=model_fn_decorator(), lr_scheduler=lr_scheduler, optim_cfg=cfg.OPTIMIZATION, start_epoch=start_epoch, total_epochs=args.epochs, start_iter=it, rank=cfg.LOCAL_RANK, tb_log=tb_log, ckpt_save_dir=ckpt_dir, train_sampler=train_sampler, lr_warmup_scheduler=lr_warmup_scheduler, ckpt_save_interval=args.ckpt_save_interval, max_ckpt_save_num=args.max_ckpt_save_num, merge_all_iters_to_one_epoch=args.merge_all_iters_to_one_epoch) logger.info( '**********************End training %s/%s(%s)**********************\n\n\n' % (cfg.EXP_GROUP_PATH, cfg.TAG, args.extra_tag)) logger.info( '**********************Start evaluation %s/%s(%s)**********************' % (cfg.EXP_GROUP_PATH, cfg.TAG, args.extra_tag)) test_set, test_loader, sampler = build_dataloader( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, batch_size=args.batch_size, dist=dist_train, workers=args.workers, logger=logger, training=False) eval_output_dir = output_dir / 'eval' / 'eval_with_train' eval_output_dir.mkdir(parents=True, exist_ok=True) args.start_epoch = max(args.epochs - 10, 0) # Only evaluate the last 10 epochs repeat_eval_ckpt(model.module if dist_train else model, test_loader, args, eval_output_dir, logger, ckpt_dir, dist_test=dist_train) logger.info( '**********************End evaluation %s/%s(%s)**********************' % (cfg.EXP_GROUP_PATH, cfg.TAG, args.extra_tag))
def inference_with_scene(): total_gpus = 1 assert args.batch_size % total_gpus == 0, 'Batch size should match the number of gpus' args.batch_size = args.batch_size // total_gpus test_set, _, _ = build_dataloader( dataset_cfg=cfg.DATA_CONFIG, class_names=cfg.CLASS_NAMES, batch_size=args.batch_size, dist=False, workers=args.workers, training=False ) # Load the data mot_dataset_path = Path("/home/yao.xu/datasets/mot_dataset") test_scene_list = os.listdir(mot_dataset_path) test_scene_list.sort() test_scene_list = test_scene_list[0:1] model = build_network(model_cfg=cfg.MODEL, num_class=len(cfg.CLASS_NAMES), dataset=test_set) with torch.no_grad(): # load checkpoint model.load_params_from_file(filename=args.ckpt, logger=logger, to_cpu=False) model.cuda() model.eval() # start inference class_names = cfg.CLASS_NAMES point_cloud_range = np.array(cfg.DATA_CONFIG.POINT_CLOUD_RANGE) processor = DataProcessor(cfg.DATA_CONFIG.DATA_PROCESSOR, point_cloud_range, training=False) frame_idx = 0 for test_scene in tqdm(test_scene_list): test_scene_path = mot_dataset_path / test_scene test_frame_list = os.listdir(test_scene_path / 'pointcloud') for lidar_file in tqdm(test_frame_list): with open(test_scene_path / 'pointcloud' / lidar_file, 'rb') as f: points = np.fromfile(f, dtype=np.float32) points = np.reshape(points, (-1, 4))[:, :3] points = np.concatenate((points, np.zeros((points.shape[0], 1))), axis=1) batch_dict = processor.forward({'points': points, 'use_lead_xyz': True, 'batch_size': 1}) batch_dict['points'] = np.concatenate( (np.zeros((batch_dict['points'].shape[0], 1)), batch_dict['points']), axis=1) batch_dict['voxel_coords'] = np.concatenate( (np.zeros((batch_dict['voxel_coords'].shape[0], 1)), batch_dict['voxel_coords']), axis=1) load_data_to_gpu(batch_dict) pred_dicts, _ = model(batch_dict) det_boxes = pred_dicts[0]['pred_boxes'].cpu().detach().numpy() # Load annotation from pickle file gt_boxes = [] label_file = lidar_file.replace('bin', 'pkl') try: assert (test_scene_path / 'label' / label_file).exists() except AssertionError: continue with open(test_scene_path / 'label' / label_file, 'rb') as f: anno = pickle.load(f, encoding='iso-8859-1') for obj in anno['obstacle_list']: loc = np.array([obj['position']['x'], obj['position']['y'], obj['position']['z']]) dims = obj['size'] rotz = np.array([math.atan(obj['direction']['y'] / obj['direction']['x'])]) if loc[0] < point_cloud_range[0] or loc[0] > point_cloud_range[3] \ or loc[1] < point_cloud_range[1] or loc[1] > point_cloud_range[4]: continue gt_boxes.append(np.concatenate((loc, dims, rotz), axis=0)) gt_boxes = np.array(gt_boxes) ###########################Plot DET results############################### PLOT_BOX = False if PLOT_BOX: points = batch_dict['points'][:, 1:4].cpu().detach().numpy() bev_range = cfg.DATA_CONFIG.POINT_CLOUD_RANGE # plot_gt_boxes(points, det_boxes, bev_range, name="mot_bench_%04d" % idx) plot_gt_det_cmp(points, gt_boxes, det_boxes, bev_range, name="mot_bench_%04d" % frame_idx) ########################################################################## # Evaluate current frame for iou_idx in range(len(ious)): for dist_range_idx in range(len(dist_ranges)): tp, num_valid_det, num_valid_gt, dist_err = get_metrics(gt_boxes, det_boxes, dist_ranges[dist_range_idx], ious[iou_idx]) total_num_tp[iou_idx, dist_range_idx] += tp total_num_valid_det[iou_idx, dist_range_idx] += num_valid_det total_num_valid_gt[iou_idx, dist_range_idx] += num_valid_gt total_dist_err[iou_idx, dist_range_idx] += dist_err frame_idx += 1