def test_invalid_box_data(self): with self.assertRaises(ValueError): np_box_list.BoxList3d(length=np.array([0]), height=np.array([0]), width=np.array([0]), center_x=np.array([1]), center_y=np.array([1]), center_z=np.array([1]), rotation_z_radians=np.array([0])) with self.assertRaises(ValueError): np_box_list.BoxList3d(length=np.array([1, -1]), height=np.array([1, 1]), width=np.array([1, 1]), center_x=np.array([1, 1]), center_y=np.array([1, 1]), center_z=np.array([1, 1]), rotation_z_radians=np.array([0, 0])) with self.assertRaises(ValueError): np_box_list.BoxList3d(length=np.array([1, 1]), height=np.array([1, 1]), width=np.array([1, 1]), center_x=np.array([1]), center_y=np.array([1, 1]), center_z=np.array([1, 1]), rotation_z_radians=np.array([0, 0]))
def setUp(self): boxes1 = np.array( [[_degree_to_radians(0.0), 1.0, 4.0, 3.0, 2.0, 5.0, 5.0], [_degree_to_radians(0.0), 2.0, 5.0, 6.0, 2.0, 7.5, 7.0]], dtype=float) (rotation_z_radians1, length1, height1, width1, center_x1, center_y1, center_z1) = _decompose_box_tensor(boxes1) boxes2 = np.array( [[_degree_to_radians(0.0), 1.0, 3.0, 4.0, 1.0, 4.5, 8.0], [_degree_to_radians(0.0), 14.0, 14.0, 14.0, 15.0, 8.0, 15.0], [_degree_to_radians(0.0), 1.0, 4.0, 3.0, 2.0, 5.0, 5.0], [_degree_to_radians(0.0), 2.0, 5.0, 6.0, 2.0, 7.5, 7.0]], dtype=float) (rotation_z_radians2, length2, height2, width2, center_x2, center_y2, center_z2) = _decompose_box_tensor(boxes2) rotation_matrix1 = np.tile(np.expand_dims(np.eye(3), axis=0), [2, 1, 1]) rotation_matrix2 = np.tile(np.expand_dims(np.eye(3), axis=0), [4, 1, 1]) self.boxlist1 = np_box_list.BoxList3d( length=length1, height=height1, width=width1, center_x=center_x1, center_y=center_y1, center_z=center_z1, rotation_z_radians=rotation_z_radians1) self.boxlist2 = np_box_list.BoxList3d( length=length2, height=height2, width=width2, center_x=center_x2, center_y=center_y2, center_z=center_z2, rotation_z_radians=rotation_z_radians2) self.boxlist1_matrix = np_box_list.BoxList3d( length=length1, height=height1, width=width1, center_x=center_x1, center_y=center_y1, center_z=center_z1, rotation_matrix=rotation_matrix1) self.boxlist2_matrix = np_box_list.BoxList3d( length=length2, height=height2, width=width2, center_x=center_x2, center_y=center_y2, center_z=center_z2, rotation_matrix=rotation_matrix2)
def setUp(self): boxes = np.array( [[_degree_to_radians(0.1), 1.0, 3.0, 4.0, 1.0, 6.0, 8.0], [_degree_to_radians(0.2), 14.0, 14.0, 14.0, 15.0, 15.0, 15.0], [_degree_to_radians(0.1), 1.0, 4.0, 3.0, 2.0, 7.0, 5.0], [_degree_to_radians(0.2), 2.0, 5.0, 6.0, 2.0, 10.0, 7.0]], dtype=float) (rotation_z_radians, length, height, width, center_x, center_y, center_z) = _decompose_box_tensor(boxes) self.boxlist = np_box_list.BoxList3d( length=length, height=height, width=width, center_x=center_x, center_y=center_y, center_z=center_z, rotation_z_radians=rotation_z_radians) self.boxlist.add_field('scores', np.array([0.5, 0.7, 0.9, 0.4], dtype=float)) self.boxlist.add_field( 'labels', np.array([[0, 0, 0, 1, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 1, 0, 0]], dtype=int))
def test_concatenate_boxes3d(self): boxes1 = np.array([[0.25, 0.25, 0.75, 0.75, 1.0, 2.0, 3.0], [0.0, 0.1, 0.5, 0.75, 0.0, 0.0, 1.0]], dtype=np.float32) (rotation_z_radians1, length1, height1, width1, center_x1, center_y1, center_z1) = _decompose_box_tensor(boxes1) boxlist1 = np_box_list.BoxList3d( length=length1, height=height1, width=width1, center_x=center_x1, center_y=center_y1, center_z=center_z1, rotation_z_radians=rotation_z_radians1) boxes2 = np.array([[0.5, 0.25, 1.0, 1.0, 2.0, 3.0, 4.0], [0.0, 0.1, 1.0, 1.0, 10.0, 20.0, 30.0]], dtype=np.float32) (rotation_z_radians2, length2, height2, width2, center_x2, center_y2, center_z2) = _decompose_box_tensor(boxes2) boxlist2 = np_box_list.BoxList3d( length=length2, height=height2, width=width2, center_x=center_x2, center_y=center_y2, center_z=center_z2, rotation_z_radians=rotation_z_radians2) boxlists = [boxlist1, boxlist2] boxlist_concatenated = np_box_list_ops.concatenate_boxes3d(boxlists) boxes_concatenated_expected = np.array( [[0.25, 0.25, 0.75, 0.75, 1.0, 2.0, 3.0], [0.0, 0.1, 0.5, 0.75, 0.0, 0.0, 1.0], [0.5, 0.25, 1.0, 1.0, 2.0, 3.0, 4.0], [0.0, 0.1, 1.0, 1.0, 10.0, 20.0, 30.0]], dtype=np.float32) self._compare_boxlist_with_boxes(boxlist_concatenated, boxes_concatenated_expected)
def np_nms(np_boxes_length, np_boxes_height, np_boxes_width, np_boxes_center, np_boxes_rotation_matrix, np_boxes_score, score_thresh, iou_thresh, max_output_size): """Non maximum suppression. Args: np_boxes_length: A numpy array of size [N, 1]. np_boxes_height: A numpy array of size [N, 1]. np_boxes_width: A numpy array of size [N, 1]. np_boxes_center: A numpy array of size [N, 3]. np_boxes_rotation_matrix: A numpy array of size [N, 3, 3]. np_boxes_score: A numpy array of size [N, num_classes]. score_thresh: scalar threshold for score (low scoring boxes are removed). iou_thresh: scalar threshold for IOU (boxes that that high IOU overlap with previously selected boxes are removed). max_output_size: maximum number of retained boxes per class. Returns: np_nms_boxes_length: A numpy array of size [N', 1]. np_nms_boxes_height: A numpy array of size [N', 1]. np_nms_boxes_width: A numpy array of size [N', 1]. np_nms_boxes_center: A numpy array of size [N', 3]. np_nms_boxes_rotation_matrix: A numpy array of size [N', 3, 3]. np_nms_boxes_class: A numpy array of size [N', 1]. np_nms_boxes_score: A numpy array of size [N', 1]. """ boxlist = np_box_list.BoxList3d(length=np.squeeze(np_boxes_length, axis=1), height=np.squeeze(np_boxes_height, axis=1), width=np.squeeze(np_boxes_width, axis=1), center_x=np_boxes_center[:, 0], center_y=np_boxes_center[:, 1], center_z=np_boxes_center[:, 2], rotation_matrix=np.reshape( np_boxes_rotation_matrix, [-1, 3, 3])) boxlist.add_field('scores', np_boxes_score) boxlist_nms = np_box_list_ops.multi_class_non_max_suppression3d( boxlist=boxlist, score_thresh=score_thresh, iou_thresh=iou_thresh, max_output_size=max_output_size) return (np.expand_dims(boxlist_nms.get_length(), axis=1), np.expand_dims(boxlist_nms.get_height(), axis=1), np.expand_dims(boxlist_nms.get_width(), axis=1), boxlist_nms.get_center(), np.expand_dims(boxlist_nms.get_rotation_matrix(), axis=1), np.expand_dims(boxlist_nms.get_field('classes'), axis=1).astype(np.int32), np.expand_dims(boxlist_nms.get_field('scores'), axis=1))
def copy_boxlist(boxlist, indices=None): """Copy the boxes of a BoxList3d object into a new BoxList3d object. Args: boxlist: A np_box_list.BoxList3d object. indices: A indices of the boxes to be copied. It is not used if None. Returns: new_boxlist: A new np_box_list.BoxList3d object. """ length = boxlist.get_length() height = boxlist.get_height() width = boxlist.get_width() center_x = boxlist.get_center_x() center_y = boxlist.get_center_y() center_z = boxlist.get_center_z() rotation_matrix = boxlist.get_rotation_matrix() rotation_z_radians = boxlist.get_rotation_z_radians() if indices is not None: length = length[indices] height = height[indices] width = width[indices] center_x = center_x[indices] center_y = center_y[indices] center_z = center_z[indices] if rotation_matrix is not None: rotation_matrix = rotation_matrix[indices, :, :] if rotation_z_radians is not None: rotation_z_radians = rotation_z_radians[indices] new_boxlist = np_box_list.BoxList3d( length=length, height=height, width=width, center_x=center_x, center_y=center_y, center_z=center_z, rotation_matrix=rotation_matrix, rotation_z_radians=rotation_z_radians) return new_boxlist
def setUp(self): boxes = np.array( [[_degree_to_radians(0.0), 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], [_degree_to_radians(10.0), 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], [_degree_to_radians(20.0), 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], [_degree_to_radians(-10.0), 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], [_degree_to_radians(0.0), 1.0, 1.0, 1.0, 10.0, 0.0, 0.0], [_degree_to_radians(10.0), 1.0, 1.0, 1.0, 10.0, 0.0, 0.0], [_degree_to_radians(20.0), 1.0, 1.0, 1.0, 10.0, 0.0, 0.0]], dtype=float) (rotation_z_radians, length, height, width, center_x, center_y, center_z) = _decompose_box_tensor(boxes) self.boxlist = np_box_list.BoxList3d( length=length, height=height, width=width, center_x=center_x, center_y=center_y, center_z=center_z, rotation_z_radians=rotation_z_radians) self.boxlist.add_field( 'scores', np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], dtype=float))
def concatenate_boxes3d(boxlists, fields=None): """Concatenates list of BoxList3d-s. This op concatenates a list of input BoxList3d-s into a larger BoxList. It also handles concatenation of BoxList fields as long as the field tensor shapes are equal except for the first dimension. Args: boxlists: list of BoxList3d objects. fields: optional list of fields to also concatenate. By default, all fields from the first BoxList3d in the list are included in the concatenation. Returns: A BoxList3d with number of boxes equal to sum([boxlist.num_boxes() for boxlist in boxlists]). Raises: ValueError: If boxlists is invalid (i.e., is not a list, is empty, or contains non BoxList3d objects), or if requested fields are not contained in all boxlists. """ if not isinstance(boxlists, list): raise ValueError('boxlists should be a list') if not boxlists: raise ValueError('boxlists should have nonzero length') for boxlist in boxlists: if not isinstance(boxlist, np_box_list.BoxList3d): raise ValueError( 'all elements of boxlists should be BoxList3d objects') length_list = [] height_list = [] width_list = [] center_x_list = [] center_y_list = [] center_z_list = [] rotation_matrix_list = [] rotation_z_radians_list = [] for boxlist in boxlists: length_list.append(boxlist.get_length()) height_list.append(boxlist.get_height()) width_list.append(boxlist.get_width()) center_x_list.append(boxlist.get_center_x()) center_y_list.append(boxlist.get_center_y()) center_z_list.append(boxlist.get_center_z()) rotation_matrix_list.append(boxlist.get_rotation_matrix()) rotation_z_radians_list.append(boxlist.get_rotation_z_radians()) length = np.concatenate(length_list) height = np.concatenate(height_list) width = np.concatenate(width_list) center_x = np.concatenate(center_x_list) center_y = np.concatenate(center_y_list) center_z = np.concatenate(center_z_list) if rotation_matrix_list[0] is None: rotation_matrix = None else: rotation_matrix = np.concatenate(rotation_matrix_list) if rotation_z_radians_list[0] is None: rotation_z_radians = None else: rotation_z_radians = np.concatenate(rotation_z_radians_list) concatenated = np_box_list.BoxList3d(length=length, height=height, width=width, center_x=center_x, center_y=center_y, center_z=center_z, rotation_matrix=rotation_matrix, rotation_z_radians=rotation_z_radians) if fields is None: fields = boxlists[0].get_extra_fields() for field in fields: first_field_shape = boxlists[0].get_field(field).shape first_field_shape = first_field_shape[1:] for boxlist in boxlists: if not boxlist.has_field(field): raise ValueError('boxlist must contain all requested fields') field_shape = boxlist.get_field(field).shape field_shape = field_shape[1:] if field_shape != first_field_shape: raise ValueError( 'field %s must have same shape for all boxlists ' 'except for the 0th dimension.' % field) concatenated_field = np.concatenate( [boxlist.get_field(field) for boxlist in boxlists], axis=0) concatenated.add_field(field, concatenated_field) return concatenated