def test_flip_boxes_3d(self): boxes_3d = np.array([[1, 2, 3, 4, 5, 6, np.pi / 4], [1, 2, 3, 4, 5, 6, -np.pi / 4]]) exp_flipped_boxes_3d = np.array([[-1, 2, 3, 4, 5, 6, 3 * np.pi / 4], [-1, 2, 3, 4, 5, 6, -3 * np.pi / 4]]) flipped_boxes_3d = kitti_aug.flip_boxes_3d(boxes_3d) np.testing.assert_almost_equal(flipped_boxes_3d, exp_flipped_boxes_3d)
def _fill_anchor_pl_inputs(self, anchors_info, ground_plane, image_shape, stereo_calib_p2, sample_name, sample_augs): """ Fills anchor placeholder inputs with corresponding data Args: anchors_info: anchor info from mini_batch_utils ground_plane: ground plane coefficients image_shape: image shape (h, w), used for projecting anchors sample_name: name of the sample, e.g. "000001" sample_augs: list of sample augmentations """ # Lists for merging anchors info all_anchor_boxes_3d = [] anchors_ious = [] anchor_offsets = [] anchor_classes = [] # Create anchors for each class if len(self.dataset.classes) > 1: for class_idx in range(len(self.dataset.classes)): # Generate anchors for all classes grid_anchor_boxes_3d = self._anchor_generator.generate( area_3d=self._area_extents, anchor_3d_sizes=self._cluster_sizes[class_idx], anchor_stride=self._anchor_strides[class_idx], ground_plane=ground_plane) all_anchor_boxes_3d.append(grid_anchor_boxes_3d) all_anchor_boxes_3d = np.concatenate(all_anchor_boxes_3d) else: # Don't loop for a single class class_idx = 0 grid_anchor_boxes_3d = self._anchor_generator.generate( area_3d=self._area_extents, anchor_3d_sizes=self._cluster_sizes[class_idx], anchor_stride=self._anchor_strides[class_idx], ground_plane=ground_plane) all_anchor_boxes_3d = grid_anchor_boxes_3d # Filter empty anchors # Skip if anchors_info is [] sample_has_labels = True if self._train_val_test in ['train', 'val']: # Read in anchor info during training / validation if anchors_info: anchor_indices, anchors_ious, anchor_offsets, \ anchor_classes = anchors_info anchor_boxes_3d_to_use = all_anchor_boxes_3d[anchor_indices] else: train_cond = (self._train_val_test == "train" and self._train_on_all_samples) eval_cond = (self._train_val_test == "val" and self._eval_all_samples) if train_cond or eval_cond: sample_has_labels = False else: sample_has_labels = False if not sample_has_labels: # During testing, or validation with no anchor info, manually # filter empty anchors # TODO: share voxel_grid_2d with BEV generation if possible voxel_grid_2d = \ self.dataset.kitti_utils.create_sliced_voxel_grid_2d( sample_name, self.dataset.bev_source, image_shape=image_shape) # Convert to anchors and filter anchors_to_use = box_3d_encoder.box_3d_to_anchor( all_anchor_boxes_3d) empty_filter = anchor_filter.get_empty_anchor_filter_2d( anchors_to_use, voxel_grid_2d, density_threshold=1) anchor_boxes_3d_to_use = all_anchor_boxes_3d[empty_filter] # Convert lists to ndarrays anchor_boxes_3d_to_use = np.asarray(anchor_boxes_3d_to_use) anchors_ious = np.asarray(anchors_ious) anchor_offsets = np.asarray(anchor_offsets) anchor_classes = np.asarray(anchor_classes) # Flip anchors and centroid x offsets for augmented samples if kitti_aug.AUG_FLIPPING in sample_augs: anchor_boxes_3d_to_use = kitti_aug.flip_boxes_3d( anchor_boxes_3d_to_use, flip_ry=False) if anchors_info: anchor_offsets[:, 0] = -anchor_offsets[:, 0] # Convert to anchors anchors_to_use = box_3d_encoder.box_3d_to_anchor( anchor_boxes_3d_to_use) num_anchors = len(anchors_to_use) # Project anchors into bev bev_anchors, bev_anchors_norm = anchor_projector.project_to_bev( anchors_to_use, self._bev_extents) # Project box_3d anchors into image space img_anchors, img_anchors_norm = \ anchor_projector.project_to_image_space( anchors_to_use, stereo_calib_p2, image_shape) # Reorder into [y1, x1, y2, x2] for tf.crop_and_resize op self._bev_anchors_norm = bev_anchors_norm[:, [1, 0, 3, 2]] self._img_anchors_norm = img_anchors_norm[:, [1, 0, 3, 2]] # Fill in placeholder inputs self._placeholder_inputs[self.PL_ANCHORS] = anchors_to_use # If we are in train/validation mode, and the anchor infos # are not empty, store them. Checking for just anchors_ious # to be non-empty should be enough. if self._train_val_test in ['train', 'val'] and \ len(anchors_ious) > 0: self._placeholder_inputs[self.PL_ANCHOR_IOUS] = anchors_ious self._placeholder_inputs[self.PL_ANCHOR_OFFSETS] = anchor_offsets self._placeholder_inputs[self.PL_ANCHOR_CLASSES] = anchor_classes # During test, or val when there is no anchor info elif self._train_val_test in ['test'] or \ len(anchors_ious) == 0: # During testing, or validation with no gt, fill these in with 0s self._placeholder_inputs[self.PL_ANCHOR_IOUS] = \ np.zeros(num_anchors) self._placeholder_inputs[self.PL_ANCHOR_OFFSETS] = \ np.zeros([num_anchors, 6]) self._placeholder_inputs[self.PL_ANCHOR_CLASSES] = \ np.zeros(num_anchors) else: raise ValueError( 'Got run mode {}, and non-empty anchor info'.format( self._train_val_test)) self._placeholder_inputs[self.PL_BEV_ANCHORS] = bev_anchors self._placeholder_inputs[self.PL_BEV_ANCHORS_NORM] = \ self._bev_anchors_norm self._placeholder_inputs[self.PL_IMG_ANCHORS] = img_anchors self._placeholder_inputs[self.PL_IMG_ANCHORS_NORM] = \ self._img_anchors_norm