def filter_bad_area(boxes: tf.Tensor, labels: tf.Tensor) -> tf.Tensor: """Remove all the boxes that have an area less or equal to 0. Arguments: - *boxes*: A tensor of shape [num_boxes, (y1, x1, y2, x2)] - *labels*: A tensor of shape [num_boxes, ] Returns: - *boxes*: A tensor of shape [N <= num_boxes, (y1, x1, y2, x2)] - *labels*: A tensor of shape [N <= num_boxes, ] """ area = compute_area(boxes) return tf.gather_nd(boxes, tf.where(area > 0)), tf.gather_nd(labels, tf.where(area > 0))
def filter_bad_area( groundtruths: Dict[str, tf.Tensor]) -> Dict[str, tf.Tensor]: """Remove all the boxes that have an area less or equal to 0. Arguments: - *groundtruths*: A dict with the following keys: 1. bbox: A tensor of shape [num_boxes, (y1, x1, y2, x2)] 2. label: A tensor of shape [num_boxes, ] Returns: - *groundtruths*: All the groundtruths which match have not been filtered. """ area = compute_area(groundtruths[BoxField.BOXES]) filter_area = tf.where(area > 0) return _filter(groundtruths, filter_area)
def assign_pyramid_level_to_boxes(boxes, num_level, level_target=2): """Compute the pyramid level of an RoI Arguments: - *boxes*: A tensor of shape [nb_batches * nb_boxes, 4] - *num_level*: Assign all the boxes mapped to a superior level to the num_level. level_target: Will affect all the boxes of area 224^2 to the level_target of the pyramid. Returns: A 2-D tensor of type int32 and shape [nb_batches * nb_boxes] corresponding to the target level of the pyramid. """ denominator = tf.constant(224, dtype=boxes.dtype) area = compute_area(boxes) k = level_target + tf.math.log(tf.sqrt(area) / denominator + K.epsilon( )) * tf.cast(1. / tf.math.log(2.0), dtype=boxes.dtype) k = tf.cast(k, tf.int32) k = tf.clip_by_value(k, 0, num_level - 1) k = tf.reshape(k, [-1]) return k
def test_compute_area(): boxes = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]]) exp_output = [200.0, 4.0] areas_output = box_ops.compute_area(boxes) np.testing.assert_allclose(areas_output, exp_output)