def test_corners_3d_coder(): # import ipdb # ipdb.set_trace() coder_config = {'type': constants.KEY_CORNERS_3D} bbox_coder = bbox_coders.build(coder_config) dataset = build_dataset() sample = dataset[0] label_boxes_3d = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_3D]) label_boxes_2d = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_2D]) p2 = torch.from_numpy(sample[constants.KEY_STEREO_CALIB_P2]) proposals = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_2D]) num_instances = torch.from_numpy(sample[constants.KEY_NUM_INSTANCES]) # ry = compute_ray_angle(label_boxes_3d[:, :3]) # label_boxes_3d[:, -1] += ry label_boxes_3d = torch.stack(1 * [label_boxes_3d[:num_instances]], dim=0) label_boxes_2d = torch.stack(1 * [label_boxes_2d[:num_instances]], dim=0) proposals = torch.stack(1 * [proposals[:num_instances]], dim=0) p2 = torch.stack(1 * [p2], dim=0) # import ipdb # ipdb.set_trace() # label_boxes_3d[:, :, -1] = 0 encoded_corners_3d = bbox_coder.encode_batch(label_boxes_3d, label_boxes_2d, p2) # torch.cat([encoded_corners_2d, ]) num_boxes = encoded_corners_3d.shape[1] batch_size = encoded_corners_3d.shape[0] decoded_corners_3d = bbox_coder.decode_batch( encoded_corners_3d.view(batch_size, num_boxes, -1), proposals, p2) decoded_corners_2d = geometry_utils.torch_points_3d_to_points_2d( decoded_corners_3d[0].view(-1, 3), p2[0]).view(-1, 8, 2) decoded_corners_2d = decoded_corners_2d.cpu().detach().numpy() image_path = sample[constants.KEY_IMAGE_PATH] image_dir = '/data/object/training/image_2' result_dir = './results/data' save_dir = 'results/images' calib_dir = '/data/object/training/calib' label_dir = None calib_file = None visualizer = ImageVisualizer(image_dir, result_dir, label_dir=label_dir, calib_dir=calib_dir, calib_file=calib_file, online=False, save_dir=save_dir) visualizer.render_image_corners_2d(image_path, decoded_corners_2d)
def main(): normal_mean = np.asarray([0.485, 0.456, 0.406]) normal_van = np.asarray([0.229, 0.224, 0.225]) dataset = build_dataset() image_dir = '/data/object/training/image_2' result_dir = './results/data' save_dir = 'results/images' calib_dir = '/data/object/training/calib' label_dir = None calib_file = None visualizer = ImageVisualizer(image_dir, result_dir, label_dir=label_dir, calib_dir=calib_dir, calib_file=calib_file, online=True, save_dir=save_dir) for sample in dataset: label_boxes_3d = sample['gt_boxes_3d'] label_boxes_2d = sample['gt_boxes'] label_classes = sample['gt_labels'] p2 = torch.from_numpy(sample['p2']) image_path = sample['img_name'] label_boxes_3d = torch.cat([ label_boxes_3d[:, 3:6], label_boxes_3d[:, :3], label_boxes_3d[:, 6:] ], dim=-1) corners_3d = geometry_utils.torch_boxes_3d_to_corners_3d( label_boxes_3d) image = sample['img'].permute(1, 2, 0).cpu().detach().numpy() image = image.copy() image = image * normal_van + normal_mean # import ipdb # ipdb.set_trace() corners_3d = corners_3d.cpu().detach().numpy() visualizer.render_image_corners_2d(image_path, image, corners_3d=corners_3d, p2=p2)
def test_mobileye_coder(): coder_config = {'type': constants.KEY_MOBILEYE} bbox_coder = bbox_coders.build(coder_config) dataset = build_dataset('kitti') image_dir = '/data/object/training/image_2' result_dir = './results/data' save_dir = 'results/images' calib_dir = '/data/object/training/calib' label_dir = None calib_file = None visualizer = ImageVisualizer(image_dir, result_dir, label_dir=label_dir, calib_dir=calib_dir, calib_file=calib_file, online=False, save_dir=save_dir) for sample in dataset: label_boxes_3d = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_3D]) label_boxes_2d = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_2D]) p2 = torch.from_numpy(sample[constants.KEY_STEREO_CALIB_P2]) proposals = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_2D]) num_instances = torch.from_numpy(sample[constants.KEY_NUM_INSTANCES]) image_info = torch.from_numpy(sample[constants.KEY_IMAGE_INFO]) label_boxes_3d = torch.stack(1 * [label_boxes_3d[:num_instances]], dim=0) label_boxes_2d = torch.stack(1 * [label_boxes_2d[:num_instances]], dim=0) proposals = torch.stack(1 * [proposals[:num_instances]], dim=0) image_info = torch.stack(1 * [image_info], dim=0) p2 = torch.stack(1 * [p2], dim=0) encoded_corners_2d = bbox_coder.encode_batch(label_boxes_3d, label_boxes_2d, p2, image_info, label_boxes_2d) # torch.cat([encoded_corners_2d, ]) # num_boxes = encoded_corners_2d.shape[1] # batch_size = encoded_corners_2d.shape[0] # center_depth = encoded_corners_2d[:, :, -1:] # encoded_corners_2d = encoded_corners_2d[:, :, :-1].view( # batch_size, num_boxes, 8, 4) # encoded_visibility = torch.zeros_like(encoded_corners_2d[:, :, :, :2]) # visibility = encoded_corners_2d[:, :, :, -1:].long() # row = torch.arange(0, visibility.numel()).type_as(visibility) # encoded_visibility.view(-1, 2)[row, visibility.view(-1)] = 1 # encoded_corners_2d = torch.cat( # [encoded_corners_2d[:, :, :, :3], encoded_visibility], dim=-1) # encoded_corners_2d = torch.cat( # [encoded_corners_2d.view(batch_size, num_boxes, -1), center_depth], # dim=-1) decoded_corners_2d = bbox_coder.decode_batch(encoded_corners_2d, proposals) decoded_corners_2d = decoded_corners_2d.cpu().detach().numpy() # import ipdb # ipdb.set_trace() image_path = sample[constants.KEY_IMAGE_PATH] visualizer.render_image_corners_2d(image_path, corners_2d=decoded_corners_2d[0], p2=p2[0])
def test_keypoint_hm_coder(): coder_config = {'type': constants.KEY_KEYPOINTS_HEATMAP} bbox_coder = bbox_coders.build(coder_config) dataset = build_dataset(dataset_type='keypoint_kitti') sample = dataset[0] label_boxes_3d = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_3D]) label_boxes_2d = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_2D]) p2 = torch.from_numpy(sample[constants.KEY_STEREO_CALIB_P2]) proposals = torch.from_numpy(sample[constants.KEY_LABEL_BOXES_2D]) num_instances = torch.from_numpy(sample[constants.KEY_NUM_INSTANCES]) keypoints = sample[constants.KEY_KEYPOINTS] # ry = compute_ray_angle(label_boxes_3d[:, :3]) # label_boxes_3d[:, -1] += ry label_boxes_3d = torch.stack(1 * [label_boxes_3d[:num_instances]], dim=0) label_boxes_2d = torch.stack(1 * [label_boxes_2d[:num_instances]], dim=0) proposals = torch.stack(1 * [proposals[:num_instances]], dim=0) keypoints = torch.stack(1 * [keypoints[:num_instances]], dim=0) p2 = torch.stack(1 * [p2], dim=0) # label_boxes_3d[:, :, -1] = 0 # import ipdb # ipdb.set_trace() encoded_corners_3d = bbox_coder.encode_batch(proposals, keypoints) # torch.cat([encoded_corners_2d, ]) num_boxes = encoded_corners_3d.shape[1] batch_size = encoded_corners_3d.shape[0] keypoint_heatmap = encoded_corners_3d.view(batch_size, num_boxes, 8, -1)[..., :-1] # resolution = bbox_coder.resolution # keypoint_heatmap = torch.zeros((batch_size * num_boxes * 8, resolution * resolution)) # row = torch.arange(keypoint.numel()).type_as(keypoint) # keypoint_heatmap[row, keypoint.view(-1)] = 1 # keypoint_heatmap = torch.stack([keypoint_heatmap] * 3, dim=1) # reshape before decode keypoint_heatmap = keypoint_heatmap.contiguous().view( batch_size, num_boxes, -1) decoded_corners_2d = bbox_coder.decode_batch(proposals, keypoint_heatmap) decoded_corners_2d = decoded_corners_2d.cpu().detach().numpy() image_path = sample[constants.KEY_IMAGE_PATH] image_dir = '/data/object/training/image_2' result_dir = './results/data' save_dir = 'results/images' calib_dir = '/data/object/training/calib' label_dir = None calib_file = None visualizer = ImageVisualizer(image_dir, result_dir, label_dir=label_dir, calib_dir=calib_dir, calib_file=calib_file, online=False, save_dir=save_dir) # import ipdb # ipdb.set_trace() visualizer.render_image_corners_2d(image_path, corners_2d=decoded_corners_2d[0])
class Tester(object): def __init__(self, eval_config, logger=None): if logger is None: logger = logging.getLogger(__name__) self.logger = logger self.feat_vis = eval_config['feat_vis'] self.thresh = eval_config['thresh'] self.nms = eval_config['nms'] self.class_agnostic = eval_config['class_agnostic'] self.classes = ['bg'] + eval_config['classes'] self.n_classes = len(self.classes) # self.batch_size = eval_config['batch_size'] self.eval_out = eval_config['eval_out'] self.test_type = eval_config['test_type'] # image visualizer for any dataset image_dir = '/data/object/training/image_2' result_dir = './results/data' save_dir = 'results/images' calib_dir = '/data/object/training/calib' label_dir = None calib_file = None self.visualizer = ImageVisualizer(image_dir, result_dir, label_dir=label_dir, calib_dir=calib_dir, calib_file=calib_file, online=False, save_dir=save_dir) def _generate_label_path(self, image_path): image_name = os.path.basename(image_path) sample_name = os.path.splitext(image_name)[0] label_name = sample_name + '.txt' return os.path.join(self.eval_out, label_name) def save_mono_3d_dets(self, dets, label_path): res_str = [] kitti_template = '{} -1 -1 -10 {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.8f}' # kitti_template = '{} -1 -1 -10 {:.3f} {:.3f} {:.3f} {:.3f} -1 -1 -1 -1000 -1000 -1000 -10 {:.8f}' with open(label_path, 'w') as f: for cls_ind, dets_per_classes in enumerate(dets): if self.classes[cls_ind] == 'Tram': continue for det in dets_per_classes: # xmin, ymin, xmax, ymax, cf, l, h, w, ry, x, y, z = det xmin, ymin, xmax, ymax, cf, h, w, l, x, y, z, ry = det res_str.append( kitti_template.format(self.classes[cls_ind], xmin, ymin, xmax, ymax, h, w, l, x, y, z, ry, cf)) # xmin, ymin, xmax, ymax, cf, h, w, l, x, y, z, alpha = det # res_str.append( # kitti_template.format(self.classes[cls_ind], xmin, # ymin, xmax, ymax, h, w, l, x, y, # z, alpha, cf)) f.write('\n'.join(res_str)) def save_dets(self, dets, label_path, image_path): res_str = [] # kitti_template = '{} -1 -1 -10 {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.8f}' kitti_template = '{} -1 -1 -10 {:.3f} {:.3f} {:.3f} {:.3f} -1 -1 -1 -1000 -1000 -1000 -10 {:.8f}' with open(label_path, 'w') as f: for cls_ind, dets_per_classes in enumerate(dets): for det in dets_per_classes: xmin, ymin, xmax, ymax, cf = det res_str.append( kitti_template.format(self.classes[cls_ind], xmin, ymin, xmax, ymax, cf)) # xmin, ymin, xmax, ymax, cf, h, w, l, x, y, z, alpha = det # res_str.append( # kitti_template.format(self.classes[cls_ind], xmin, # ymin, xmax, ymax, h, w, l, x, y, # z, alpha, cf)) f.write('\n'.join(res_str)) # image = self.visualizer.parse_image(image_path) # self.visualizer.render_image_2d(image, boxes_2d, label_classes) def test_corners_3d(self, dataloader, model, logger): self.logger.info('Start testing') num_samples = len(dataloader) if self.feat_vis: # enable it before forward pass model.enable_feat_vis() end_time = 0 for step, data in enumerate(dataloader): # start_time = time.time() data = common.to_cuda(data) image_path = data[constants.KEY_IMAGE_PATH] with torch.no_grad(): prediction, _, _ = model(data) # duration_time = time.time() - start_time if self.feat_vis: featmaps_dict = model.get_feat() from utils.visualizer import FeatVisualizer feat_visualizer = FeatVisualizer() feat_visualizer.visualize_maps(featmaps_dict) # initialize dets for each classes # dets = [[] for class_ind in range(self.n_classes)] scores = prediction[constants.KEY_CLASSES] boxes_2d = prediction[constants.KEY_BOXES_2D] # dims = prediction[constants.KEY_DIMS] corners_2d = prediction[constants.KEY_CORNERS_2D] # import ipdb # ipdb.set_trace() p2 = data[constants.KEY_STEREO_CALIB_P2_ORIG] # rcnn_3d = prediction['rcnn_3d'] batch_size = scores.shape[0] scores = scores.view(-1, self.n_classes) new_scores = torch.zeros_like(scores) _, scores_argmax = scores.max(dim=-1) row = torch.arange(0, scores_argmax.numel()).type_as(scores_argmax) new_scores[row, scores_argmax] = scores[row, scores_argmax] scores = new_scores.view(batch_size, -1, self.n_classes) # if step == 6: # import ipdb # ipdb.set_trace() for batch_ind in range(batch_size): boxes_2d_per_img = boxes_2d[batch_ind] scores_per_img = scores[batch_ind] # dims_per_img = dims[batch_ind] corners_2d_per_img = corners_2d[batch_ind] p2_per_img = p2[batch_ind] num_cols = corners_2d.shape[-1] dets = [np.zeros((0, 8, num_cols), dtype=np.float32)] dets_2d = [np.zeros((0, 4), dtype=np.float32)] for class_ind in range(1, self.n_classes): # cls thresh inds = torch.nonzero( scores_per_img[:, class_ind] > self.thresh).view(-1) threshed_scores_per_img = scores_per_img[inds, class_ind] if inds.numel() > 0: # if self.class_agnostic: threshed_boxes_2d_per_img = boxes_2d_per_img[inds] # threshed_dims_per_img = dims_per_img[inds] threshed_corners_2d_per_img = corners_2d_per_img[inds] # threshed_rcnn_3d_per_img = rcnn_3d_per_img[inds] # else: # threshed_boxes_2d_per_img = boxes_2d_per_img[ # inds, class_ind * 4:class_ind * 4 + 4] # concat boxes and scores threshed_dets_per_img = torch.cat( [ threshed_boxes_2d_per_img, threshed_scores_per_img.unsqueeze(-1), # threshed_dims_per_img, ], dim=-1) # sort by scores _, order = torch.sort(threshed_scores_per_img, 0, True) threshed_dets_per_img = threshed_dets_per_img[order] threshed_corners_2d_per_img = threshed_corners_2d_per_img[ order] # nms keep = nms(threshed_dets_per_img[:, :4], threshed_dets_per_img[:, 4], self.nms).view(-1).long() nms_dets_per_img = threshed_dets_per_img[keep].detach( ).cpu().numpy() nms_corners_2d_per_img = threshed_corners_2d_per_img[ keep].detach().cpu().numpy() dets.append(nms_corners_2d_per_img) dets_2d.append(nms_dets_per_img[:, :4]) else: dets.append( np.zeros((0, 8, num_cols), dtype=np.float32)) dets_2d.append(np.zeros((0, 4))) # import ipdb # ipdb.set_trace() corners = np.concatenate(dets, axis=0) dets_2d = np.concatenate(dets_2d, axis=0) corners_2d = None corners_3d = None if num_cols == 3: corners_3d = corners else: corners_2d = corners self.visualizer.render_image_corners_2d( image_path[0], boxes_2d=dets_2d, corners_2d=corners_2d, corners_3d=corners_3d, p2=p2_per_img.cpu().numpy()) duration_time = time.time() - end_time # label_path = self._generate_label_path(image_path[batch_ind]) # self.save_mono_3d_dets(dets, label_path) sys.stdout.write('\r{}/{},duration: {}'.format( step + 1, num_samples, duration_time)) sys.stdout.flush() end_time = time.time() def test_3d(self, dataloader, model, logger): self.logger.info('Start testing') num_samples = len(dataloader) if self.feat_vis: # enable it before forward pass model.enable_feat_vis() end_time = 0 for step, data in enumerate(dataloader): # start_time = time.time() data = common.to_cuda(data) image_path = data[constants.KEY_IMAGE_PATH] with torch.no_grad(): prediction, _, _ = model(data) # duration_time = time.time() - start_time if self.feat_vis: featmaps_dict = model.get_feat() from utils.visualizer import FeatVisualizer feat_visualizer = FeatVisualizer() feat_visualizer.visualize_maps(featmaps_dict) # initialize dets for each classes # dets = [[] for class_ind in range(self.n_classes)] dets = [[]] scores = prediction[constants.KEY_CLASSES] boxes_2d = prediction[constants.KEY_BOXES_2D] dims = prediction[constants.KEY_DIMS] orients = prediction[constants.KEY_ORIENTS_V2] p2 = data[constants.KEY_STEREO_CALIB_P2_ORIG] # rcnn_3d = prediction['rcnn_3d'] batch_size = scores.shape[0] scores = scores.view(-1, self.n_classes) new_scores = torch.zeros_like(scores) _, scores_argmax = scores.max(dim=-1) row = torch.arange(0, scores_argmax.numel()).type_as(scores_argmax) new_scores[row, scores_argmax] = scores[row, scores_argmax] scores = new_scores.view(batch_size, -1, self.n_classes) # if step == 6: # import ipdb # ipdb.set_trace() for batch_ind in range(batch_size): boxes_2d_per_img = boxes_2d[batch_ind] scores_per_img = scores[batch_ind] dims_per_img = dims[batch_ind] orients_per_img = orients[batch_ind] p2_per_img = p2[batch_ind] # rcnn_3d_per_img = rcnn_3d[batch_ind] for class_ind in range(1, self.n_classes): # cls thresh inds = torch.nonzero( scores_per_img[:, class_ind] > self.thresh).view(-1) threshed_scores_per_img = scores_per_img[inds, class_ind] if inds.numel() > 0: # if self.class_agnostic: threshed_boxes_2d_per_img = boxes_2d_per_img[inds] threshed_dims_per_img = dims_per_img[inds] threshed_orients_per_img = orients_per_img[inds] # threshed_rcnn_3d_per_img = rcnn_3d_per_img[inds] # else: # threshed_boxes_2d_per_img = boxes_2d_per_img[ # inds, class_ind * 4:class_ind * 4 + 4] # concat boxes and scores threshed_dets_per_img = torch.cat([ threshed_boxes_2d_per_img, threshed_scores_per_img.unsqueeze(-1), threshed_dims_per_img, threshed_orients_per_img.unsqueeze(-1) ], dim=-1) # sort by scores _, order = torch.sort(threshed_scores_per_img, 0, True) threshed_dets_per_img = threshed_dets_per_img[order] # threshed_rcnn_3d_per_img = threshed_rcnn_3d_per_img[order] # nms keep = nms(threshed_dets_per_img[:, :4], threshed_dets_per_img[:, 4], self.nms).view(-1).long() nms_dets_per_img = threshed_dets_per_img[keep].detach( ).cpu().numpy() # nms_rcnn_3d_per_img = threshed_rcnn_3d_per_img[keep].detach().cpu().numpy() # calculate location location = geometry_utils.calc_location( nms_dets_per_img[:, 5:8], nms_dets_per_img[:, :5], nms_dets_per_img[:, 8], p2_per_img.cpu().numpy()) # import ipdb # ipdb.set_trace() # location, _ = mono_3d_postprocess_bbox( # nms_rcnn_3d_per_img, nms_dets_per_img[:, :5], # p2_per_img.cpu().numpy()) nms_dets_per_img = np.concatenate([ nms_dets_per_img[:, :5], nms_dets_per_img[:, 5:8], location, nms_dets_per_img[:, -1:] ], axis=-1) # nms_dets_per_img = np.concatenate( # [nms_dets_per_img[:, :5], location], axis=-1) dets.append(nms_dets_per_img) else: dets.append([]) duration_time = time.time() - end_time label_path = self._generate_label_path(image_path[batch_ind]) self.save_mono_3d_dets(dets, label_path) sys.stdout.write('\r{}/{},duration: {}'.format( step + 1, num_samples, duration_time)) sys.stdout.flush() end_time = time.time() def test_2d(self, dataloader, model, logger): self.logger.info('Start testing') num_samples = len(dataloader) if self.feat_vis: # enable it before forward pass model.enable_feat_vis() end_time = 0 for step, data in enumerate(dataloader): # start_time = time.time() data = common.to_cuda(data) image_path = data[constants.KEY_IMAGE_PATH] with torch.no_grad(): prediction, _, _ = model(data) # duration_time = time.time() - start_time if self.feat_vis: featmaps_dict = model.get_feat() from utils.visualizer import FeatVisualizer feat_visualizer = FeatVisualizer() feat_visualizer.visualize_maps(featmaps_dict) # initialize dets for each classes # dets = [[] for class_ind in range(self.n_classes)] dets = [[]] scores = prediction[constants.KEY_CLASSES] boxes_2d = prediction[constants.KEY_BOXES_2D] batch_size = scores.shape[0] scores = scores.view(-1, self.n_classes) new_scores = torch.zeros_like(scores) _, scores_argmax = scores.max(dim=-1) row = torch.arange(0, scores_argmax.numel()).type_as(scores_argmax) new_scores[row, scores_argmax] = scores[row, scores_argmax] scores = new_scores.view(batch_size, -1, self.n_classes) # if step == 6: # import ipdb # ipdb.set_trace() for batch_ind in range(batch_size): boxes_2d_per_img = boxes_2d[batch_ind] scores_per_img = scores[batch_ind] for class_ind in range(1, self.n_classes): # cls thresh inds = torch.nonzero( scores_per_img[:, class_ind] > self.thresh).view(-1) threshed_scores_per_img = scores_per_img[inds, class_ind] if inds.numel() > 0: # if self.class_agnostic: threshed_boxes_2d_per_img = boxes_2d_per_img[inds] # else: # threshed_boxes_2d_per_img = boxes_2d_per_img[ # inds, class_ind * 4:class_ind * 4 + 4] # concat boxes and scores threshed_dets_per_img = torch.cat([ threshed_boxes_2d_per_img, threshed_scores_per_img.unsqueeze(-1), ], dim=-1) # sort by scores _, order = torch.sort(threshed_scores_per_img, 0, True) threshed_dets_per_img = threshed_dets_per_img[order] # nms keep = nms(threshed_dets_per_img[:, :4], threshed_dets_per_img[:, 4], self.nms).view(-1).long() nms_dets_per_img = threshed_dets_per_img[keep].detach( ).cpu().numpy() dets.append(nms_dets_per_img) else: dets.append([]) duration_time = time.time() - end_time label_path = self._generate_label_path(image_path[batch_ind]) self.save_dets(dets, label_path, image_path[batch_ind]) sys.stdout.write('\r{}/{},duration: {}'.format( step + 1, num_samples, duration_time)) sys.stdout.flush() end_time = time.time() def test_super_nms(self, dataloader, model, logger): self.logger.info('Start testing') num_samples = len(dataloader) if self.feat_vis: # enable it before forward pass model.enable_feat_vis() end_time = 0 for step, data in enumerate(dataloader): # start_time = time.time() data = common.to_cuda(data) image_path = data[constants.KEY_IMAGE_PATH] with torch.no_grad(): prediction = model(data) # duration_time = time.time() - start_time if self.feat_vis: featmaps_dict = model.get_feat() from utils.visualizer import FeatVisualizer feat_visualizer = FeatVisualizer() feat_visualizer.visualize_maps(featmaps_dict) # initialize dets for each classes # dets = [[] for class_ind in range(self.n_classes)] dets = [[]] scores = prediction[constants.KEY_CLASSES] boxes_2d = prediction[constants.KEY_BOXES_2D] batch_size = scores.shape[0] # scores = scores.view(-1, self.n_classes) # new_scores = torch.zeros_like(scores) # _, scores_argmax = scores.max(dim=-1) # row = torch.arange(0, scores_argmax.numel()).type_as(scores_argmax) # new_scores[row, scores_argmax] = scores[row, scores_argmax] # scores = new_scores.view(batch_size, -1, self.n_classes) # if step == 6: # import ipdb # ipdb.set_trace() for batch_ind in range(batch_size): boxes_2d_per_img = boxes_2d[batch_ind] scores_per_img = scores[batch_ind] for class_ind in range(1, self.n_classes): # cls thresh # import ipdb # ipdb.set_trace() inds = torch.nonzero( scores_per_img[:, class_ind] > 0.01).view(-1) threshed_scores_per_img = scores_per_img[inds, class_ind] if inds.numel() > 0: # if self.class_agnostic: threshed_boxes_2d_per_img = boxes_2d_per_img[inds] # else: # threshed_boxes_2d_per_img = boxes_2d_per_img[ # inds, class_ind * 4:class_ind * 4 + 4] # concat boxes and scores threshed_dets_per_img = torch.cat([ threshed_boxes_2d_per_img, threshed_scores_per_img.unsqueeze(-1), ], dim=-1) # sort by scores _, order = torch.sort(threshed_scores_per_img, 0, True) threshed_dets_per_img = threshed_dets_per_img[order] # nms # keep = nms(threshed_dets_per_img[:, :4], # threshed_dets_per_img[:, 4], # self.nms).view(-1).long() keep = box_ops.super_nms(threshed_dets_per_img[:, :4], 0.8, nms_num=3, loop_time=2) nms_dets_per_img = threshed_dets_per_img[keep].detach( ).cpu().numpy() dets.append(nms_dets_per_img) else: dets.append([]) duration_time = time.time() - end_time label_path = self._generate_label_path(image_path[batch_ind]) self.save_dets(dets, label_path) sys.stdout.write('\r{}/{},duration: {}'.format( step + 1, num_samples, duration_time)) sys.stdout.flush() end_time = time.time() def test(self, dataloader, model, logger): test_fn = getattr(self, self.test_type) test_fn(dataloader, model, logger)
def test_bbox_coder(): bbox_coder = BBox3DCoder({}) dataset = build_dataset() image_dir = '/data/object/training/image_2' result_dir = './results/data' save_dir = 'results/images' calib_dir = '/data/object/training/calib' label_dir = None calib_file = None visualizer = ImageVisualizer( image_dir, result_dir, label_dir=label_dir, calib_dir=calib_dir, calib_file=calib_file, online=True, save_dir=save_dir) for sample in dataset: mean_dims = torch.from_numpy(sample['mean_dims'][None]) label_boxes_3d = sample['gt_boxes_3d'] label_boxes_2d = sample['gt_boxes'] label_classes = sample['gt_labels'] p2 = torch.from_numpy(sample['p2']) bbox_coder.mean_dims = mean_dims encoded_corners_2d = bbox_coder.encode_batch_bbox( label_boxes_3d, label_boxes_2d, label_classes, p2) # side_lines = encoded_corners_2d[:, 16:20] # encoded_corners_2d = torch.cat( # [ # encoded_corners_2d[:, :6], encoded_corners_2d[:, 6:11], # encoded_corners_2d[:, 10:11], encoded_corners_2d[:, 11:16], # encoded_corners_2d[:, 15:16] # ], # dim=-1) decoded_corners_2d = bbox_coder.decode_batch_bbox( encoded_corners_2d, label_boxes_2d, p2) boxes_3d = torch.cat( [ decoded_corners_2d[:, 6:9], decoded_corners_2d[:, 3:6], decoded_corners_2d[:, -1:] ], dim=-1) # boxes_3d = decoded_corners_2d corners_3d = geometry_utils.torch_boxes_3d_to_corners_3d(boxes_3d) corners_3d = corners_3d.cpu().detach().numpy() # import ipdb # ipdb.set_trace() # image_path = sample[] image_path = sample['img_name'] image = sample['img'].permute(1, 2, 0).cpu().detach().numpy() image = image.copy() image = image * normal_van + normal_mean # image = None # corners_2d = torch.cat([side_lines] * 4, dim=-1).view(-1, 8, 2) # corners_2d = corners_2d.cpu().detach().numpy() visualizer.render_image_corners_2d( image_path, image, corners_3d=corners_3d, p2=p2)