def call(self, y_true, y_pred):
        """Selects the largest bounding box of the predictions and the
           ground truth and calculates the iou_loss between them. And adds the
           MAE to it (to optimize the other coordinates).

        Args:
            y_true (tf.tensor): Ground truth
            y_pred (tf.tensor): Predictions

        Returns:
            [tf.tensor]: IOU loss for largest bounding box
        """
        x_min_pred = tf.reduce_min(y_pred[:, ::2], axis=1)
        x_max_pred = tf.reduce_max(y_pred[:, ::2], axis=1)
        y_min_pred = tf.reduce_min(y_pred[:, 1::2], axis=1)
        y_max_pred = tf.reduce_max(y_pred[:, 1::2], axis=1)

        box_pred = tf.stack([y_min_pred, x_min_pred, y_max_pred, x_max_pred],
                            axis=1)
        # Bounding box for truth
        x_min_true = tf.reduce_min(y_true[:, ::2], axis=1)
        x_max_true = tf.reduce_max(y_true[:, ::2], axis=1)
        y_min_true = tf.reduce_min(y_true[:, 1::2], axis=1)
        y_max_true = tf.reduce_max(y_true[:, 1::2], axis=1)

        box_true = tf.stack([y_min_true, x_min_true, y_max_true, x_max_true],
                            axis=1)

        # Approximating corners of bounding box
        iou = giou_loss(box_true, box_pred, mode='giou')

        return iou
예제 #2
0
 def test_with_integer(self):
     boxes1 = tf.constant([[4, 3, 7, 5], [5, 6, 10, 7]], dtype=tf.int32)
     boxes2 = tf.constant([[3, 4, 6, 8], [14, 14, 15, 15]], dtype=tf.int32)
     expected_result = tf.constant(
         [1.07500000298023224, 1.9333333373069763], dtype=tf.float32)
     loss = giou_loss(boxes1, boxes2)
     self.assertAllCloseAccordingToType(loss, expected_result)
예제 #3
0
def test_with_integer():
    boxes1 = tf.constant([[4, 3, 7, 5], [5, 6, 10, 7]], dtype=tf.int32)
    boxes2 = tf.constant([[3, 4, 6, 8], [14, 14, 15, 15]], dtype=tf.int32)
    expected_result = tf.constant([1.07500000298023224, 1.9333333373069763],
                                  dtype=tf.float32)
    loss = giou_loss(boxes1, boxes2)
    test_utils.assert_allclose_according_to_type(loss, expected_result)
예제 #4
0
 def test_iou(self, dtype):
     boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]],
                          dtype=dtype)
     boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]],
                          dtype=dtype)
     expected_result = tf.constant([0.875, 1.], dtype=dtype)
     loss = giou_loss(boxes1, boxes2, mode='iou')
     self.assertAllCloseAccordingToType(loss, expected_result)
예제 #5
0
def test_iou(dtype):
    boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]],
                         dtype=dtype)
    boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]],
                         dtype=dtype)
    expected_result = tf.constant([0.875, 1.0], dtype=dtype)
    loss = giou_loss(boxes1, boxes2, mode="iou")
    test_utils.assert_allclose_according_to_type(loss, expected_result)
예제 #6
0
 def test_giou_loss(self, dtype):
     boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]],
                          dtype=dtype)
     boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]],
                          dtype=dtype)
     expected_result = tf.constant(
         [1.07500000298023224, 1.9333333373069763], dtype=dtype)
     loss = giou_loss(boxes1, boxes2)
     self.assertAllCloseAccordingToType(loss, expected_result)
예제 #7
0
def test_different_shapes(dtype):
    boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]],
                         dtype=dtype)
    boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0]], dtype=dtype)
    expand_boxes1 = tf.expand_dims(boxes1, -2)
    expand_boxes2 = tf.expand_dims(boxes2, 0)
    expected_result = tf.constant([1.07500000298023224, 1.366071], dtype=dtype)
    loss = giou_loss(expand_boxes1, expand_boxes2)
    test_utils.assert_allclose_according_to_type(loss, expected_result)
예제 #8
0
def test_giou_loss(dtype):
    boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]],
                         dtype=dtype)
    boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]],
                         dtype=dtype)
    expected_result = tf.constant([1.07500000298023224, 1.9333333373069763],
                                  dtype=dtype)
    loss = giou_loss(boxes1, boxes2)
    test_utils.assert_allclose_according_to_type(loss, expected_result)
예제 #9
0
    def test_different_shapes(self, dtype):
        boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]],
                             dtype=dtype)
        boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0]], dtype=dtype)
        tf.expand_dims(boxes1, -2)
        tf.expand_dims(boxes2, 0)
        expected_result = tf.constant([1.07500000298023224, 1.366071],
                                      dtype=dtype)
        loss = giou_loss(boxes1, boxes2)
        self.assertAllCloseAccordingToType(loss, expected_result)

        boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]],
                             dtype=dtype)
        boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0]], dtype=dtype)
        tf.expand_dims(boxes1, 0)
        tf.expand_dims(boxes2, -2)
        expected_result = tf.constant([1.07500000298023224, 1.366071],
                                      dtype=dtype)
        loss = giou_loss(boxes1, boxes2)
        self.assertAllCloseAccordingToType(loss, expected_result)
    def call(self, y_true, y_pred):
        """Calculate sum of the two bounding boxes.

        Args:
            y_true (tf.tensor): Ground truth
            y_pred (tf.tensor): Predictions

        Returns:
            [tf.tensor]: The loss calculated for the two bounding boxes.
        """
        box1_true = tf.gather(y_true, self.box1_index, axis=1)
        box1_pred = tf.gather(y_pred, self.box1_index, axis=1)
        # Get box 2
        box2_true = tf.gather(y_true, self.box2_index, axis=1)
        box2_pred = tf.gather(y_pred, self.box2_index, axis=1)

        # Bounding Boxes
        box1_loss = giou_loss(box1_true, box1_pred, mode='giou')
        box2_loss = giou_loss(box2_true, box2_pred, mode='giou')

        box_loss = (box1_loss + box2_loss) / 2

        return box_loss
예제 #11
0
 def test_one_bbox(self, dtype):
     boxes1 = tf.constant([4.0, 3.0, 7.0, 5.0], dtype=dtype)
     boxes2 = tf.constant([3.0, 4.0, 6.0, 8.0], dtype=dtype)
     expected_result = tf.constant(1.07500000298023224, dtype=dtype)
     loss = giou_loss(boxes1, boxes2)
     self.assertAllCloseAccordingToType(loss, expected_result)
예제 #12
0
def test_one_bbox(dtype):
    boxes1 = tf.constant([4.0, 3.0, 7.0, 5.0], dtype=dtype)
    boxes2 = tf.constant([3.0, 4.0, 6.0, 8.0], dtype=dtype)
    expected_result = tf.constant(1.07500000298023224, dtype=dtype)
    loss = giou_loss(boxes1, boxes2)
    test_utils.assert_allclose_according_to_type(loss, expected_result)