def inference(): config_name = 'config.json' config, _, _, _ = load_config(config_name) if torch.cuda.is_available(): device = 'cuda' net = LDOPC(config['use_bn']).cuda() else: device = 'cpu' net = LDOPC(config['use_bn']).cpu() net.load_state_dict( torch.load(get_model_name(config['name']), map_location=device)) net.set_decode(True) loader, _ = get_data_loader(batch_size=1, use_npy=config['use_npy'], frame_range=config['frame_range']) net.eval() image_id = 25 threshold = config['cls_threshold'] with torch.no_grad(): pc_feature, label_map = loader.dataset[image_id] pc_feature = pc_feature.to(device) label_map = label_map.to(device) label_map_unnorm, label_list = loader.dataset.get_label(image_id) t_start = time.time() pred = net(pc_feature.unsqueeze(0)).squeeze_(0) print("Forward pass time", time.time() - t_start) cls_pred = pred[..., 0] activation = cls_pred > threshold num_boxes = int(activation.sum()) if num_boxes == 0: print("No bounding box found") return corners = torch.zeros((num_boxes, 8)) for i in range(1, 9): corners[:, i - 1] = torch.masked_select(pred[..., i], activation) corners = corners.view(-1, 4, 2).numpy() scores = (torch.masked_select(pred[..., 0], activation)).cpu().numpy() t_start = time.time() selected_ids = non_max_suppression(corners, scores, config['nms_iou_threshold']) corners = corners[selected_ids] scores = scores[selected_ids] print("Non max suppression time:", time.time() - t_start) pc_feature = pc_feature.cpu().numpy() plot_bev(pc_feature, label_list, window_name='GT') plot_bev(pc_feature, corners, window_name='Prediction') plot_label_map(cls_pred.cpu().numpy()) cv2.waitKey(0)
def experiment(config_name, device): config, _, _, _ = load_config(config_name) net, criterion = build_model(config, device, train=False) net.load_state_dict( torch.load(get_model_name(config['name']), map_location=device)) net.set_decode(True) loader, _ = get_data_loader(batch_size=1, use_npy=config['use_npy'], frame_range=config['frame_range']) net.eval() image_id = 25 threshold = config['cls_threshold'] with torch.no_grad(): input, label_map = loader.dataset[image_id] input = input.to(device) label_map = label_map.to(device) label_map_unnorm, label_list = loader.dataset.get_label(image_id) # Forward Pass t_start = time.time() pred = net(input.unsqueeze(0)).squeeze_(0) print("Forward pass time", time.time() - t_start) # Select all the bounding boxes with classification score above threshold cls_pred = pred[..., 0] activation = cls_pred > threshold # Compute (x, y) of the corners of selected bounding box num_boxes = int(activation.sum()) if num_boxes == 0: print("No bounding box found") return corners = torch.zeros((num_boxes, 8)) for i in range(1, 9): corners[:, i - 1] = torch.masked_select(pred[..., i], activation) corners = corners.view(-1, 4, 2).numpy() scores = torch.masked_select(pred[..., 0], activation).numpy() # NMS t_start = time.time() selected_ids = non_max_suppression(corners, scores, config['nms_iou_threshold']) corners = corners[selected_ids] scores = scores[selected_ids] print("Non max suppression time:", time.time() - t_start) # Visualization input_np = input.cpu().numpy() plot_bev(input_np, label_list, window_name='GT') plot_bev(input_np, corners, window_name='Prediction') plot_label_map(cls_pred.numpy())
def eval_one(net, loss_fn, config, loader, image_id, device, plot=False, verbose=False): input, label_map, image_id = loader.dataset[image_id] input = input.to(device) label_map, label_list = loader.dataset.get_label(image_id) loader.dataset.reg_target_transform(label_map) label_map = torch.from_numpy(label_map).permute(2, 0, 1).unsqueeze_(0).to(device) # Forward Pass t_start = time.time() pred = net(input.unsqueeze(0)) t_forward = time.time() - t_start loss, cls_loss, loc_loss = loss_fn(pred, label_map) pred.squeeze_(0) cls_pred = pred[0, ...] if verbose: print("Forward pass time", t_forward) # Filter Predictions t_start = time.time() corners, scores = filter_pred(config, pred) t_post = time.time() - t_start if verbose: print("Non max suppression time:", t_post) gt_boxes = np.array(label_list) gt_match, pred_match, overlaps = compute_matches(gt_boxes, corners, scores, iou_threshold=0.5) num_gt = len(label_list) num_pred = len(scores) input_np = input.cpu().permute(1, 2, 0).numpy() pred_image = get_bev(input_np, corners) if plot == True: # Visualization plot_bev(input_np, label_list, window_name='GT') plot_bev(input_np, corners, window_name='Prediction') plot_label_map(cls_pred.numpy()) return num_gt, num_pred, scores, pred_image, pred_match, loss.item( ), t_forward, t_post
def test0(): k = KITTI() id = 25 k.load_velo() tstart = time.time() scan = k.load_velo_scan(id) processed_v = k.lidar_preprocess(scan) label_map, label_list = k.get_label(id) print('time taken: %gs' % (time.time() - tstart)) plot_bev(processed_v, label_list) plot_label_map(label_map[:, :, 6])