def _have_collision(self, label, new_obj): # TODO: Bottleneck in speed # (Specifically: KittiAugmentor check_overlap) from det3.dataloader.augmentor import KittiAugmentor _label = label.copy() _label.add_obj(new_obj) return not KittiAugmentor.check_overlap(label=_label)
def augmenting(pc, calib, label, training: bool, augment_dict: dict): ''' data augmentation @pc: np.array @calib: [KittiCalib] @label: KittiLabel @single_lidar: (None, str) @multi_lidar: (None, str) @training: bool @augment_dict: dict -> label: KittiLabel -> pc: np.array ''' if not training or augment_dict is None: return label, pc augmentor = KittiAugmentor(**augment_dict) label, pc = augmentor.apply(label, pc, calib) return label, pc
def test_flip_pc(self): pc = read_pc_from_bin("./unit-test/data/test_KittiObj_000016.bin") calib = KittiCalib("./unit-test/data/test_KittiObj_000016.txt").read_calib_file() label = KittiLabel("./unit-test/data/test_KittiObj_000016_label.txt").read_label_file() bevimg = BEVImage(x_range=(0, 70), y_range=(-40, 40), grid_size=(0.05, 0.05)) bevimg.from_lidar(pc, scale=1) for obj in label.data: bevimg.draw_box(obj, calib, bool_gt=True) bevimg_img = Image.fromarray(bevimg.data) bevimg_img.save(os.path.join('./unit-test/result/', 'test_KittiAugmentor_flippc_origin.png')) kitti_agmtor = KittiAugmentor() label, pc = kitti_agmtor.flip_pc(label, pc, calib) bevimg = BEVImage(x_range=(0, 70), y_range=(-40, 40), grid_size=(0.05, 0.05)) bevimg.from_lidar(pc, scale=1) for obj in label.data: bevimg.draw_box(obj, calib, bool_gt=True) bevimg_img = Image.fromarray(bevimg.data) bevimg_img.save(os.path.join('./unit-test/result/', 'test_KittiAugmentor_flippc_result.png')) print(bcolors.WARNING + "Warning: TestKittiAugmentor:test_flip_pc: You should check the function manully.:)"+ bcolors.ENDC) print(bcolors.WARNING + os.path.join('Warning: TestKittiAugmentor:test_flip_pc: ./unit-test/result/', 'test_KittiAugmentor_flippc_origin.png') + bcolors.ENDC) print(bcolors.WARNING + os.path.join('Warning: TestKittiAugmentor:test_flip_pc: ./unit-test/result/', 'test_KittiAugmentor_flippc_result.png') + bcolors.ENDC)
def prep_pointcloud(input_dict, training, db_sampler, augment_dict, voxelizer, max_voxels, anchor_cache, target_assigner): pc_reduced = input_dict["lidar"]["points"] gt_calib = input_dict["calib"] tag = input_dict["metadata"]["tag"] if training: gt_label = input_dict["cam"]["label"] # db sampling if db_sampler is not None: sample_res = db_sampler.sample(gt_label=gt_label, gt_calib=gt_calib) for i in range(sample_res["num_obj"]): obj = sample_res["res_label"].data[i] obj_calib = sample_res["calib_list"][i] objpc = sample_res["objpc_list"][i] if objpc is not None: # del origin pts in obj mask = obj.get_pts_idx(pc_reduced[:, :3], obj_calib) mask = np.logical_not(mask) pc_reduced = pc_reduced[mask, :] # add objpc pc_reduced = np.concatenate([pc_reduced, objpc], axis=0) else: pass # original obj np.random.shuffle(pc_reduced) calib_list = [ itm if itm is not None else gt_calib for itm in sample_res["calib_list"] ] label = sample_res["res_label"] else: label = gt_label calib_list = [gt_calib for itm in label.data] # augmentation augmentor = KittiAugmentor(**augment_dict) label, pc_reduced = augmentor.apply(label, pc_reduced, calib_list) # label cleaning label = filt_label_by_cls(label, keep_cls=target_assigner.classes) else: label = input_dict["cam"]["label"] pc_reduced = pc_reduced calib_list = [gt_calib] * len(label.data) # Voxelization vox_res = voxelizer.generate(pc_reduced, max_voxels) voxels = vox_res["voxels"] coordinates = vox_res["coordinates"] num_points = vox_res["num_points_per_voxel"] num_voxels = np.array([voxels.shape[0]], dtype=np.int64) example = { 'tag': tag, 'voxels': voxels, 'num_points': num_points, 'coordinates': coordinates, "num_voxels": num_voxels, } # Create Anchors if anchor_cache is not None: anchors = anchor_cache["anchors"] anchors_bv = anchor_cache["anchors_bv"] anchors_dict = anchor_cache["anchors_dict"] matched_thresholds = anchor_cache["matched_thresholds"] unmatched_thresholds = anchor_cache["unmatched_thresholds"] else: from det3.methods.second.ops.ops import rbbox2d_to_near_bbox grid_size = voxelizer.grid_size out_size_factor = 2 feature_map_size = grid_size[:2] // out_size_factor feature_map_size = [*feature_map_size, 1][::-1] ret = target_assigner.generate_anchors(feature_map_size) anchors = ret["anchors"] anchors = anchors.reshape([-1, target_assigner.box_ndim]) anchors_dict = target_assigner.generate_anchors_dict(feature_map_size) anchors_bv = rbbox2d_to_near_bbox(anchors[:, [0, 1, 3, 4, 6]]) matched_thresholds = ret["matched_thresholds"] unmatched_thresholds = ret["unmatched_thresholds"] example["anchors"] = anchors anchors_mask = None if not training: return example # Target Assignment class_names = target_assigner.classes gt_dict = kittilabel2gt_dict(label, class_names, calib_list) targets_dict = target_assigner.assign( anchors, anchors_dict, gt_dict["gt_boxes"], anchors_mask, gt_classes=gt_dict["gt_classes"], gt_names=gt_dict["gt_names"], matched_thresholds=matched_thresholds, unmatched_thresholds=unmatched_thresholds, importance=gt_dict["gt_importance"]) example.update({ 'labels': targets_dict['labels'], 'reg_targets': targets_dict['bbox_targets'], # 'reg_weights': targets_dict['bbox_outside_weights'], 'importance': targets_dict['importance'], }) return example
def test_init(self): kitti_agmtor = KittiAugmentor() self.assertEqual(kitti_agmtor.dataset, "Kitti")