Ejemplo n.º 1
0
def compute_diou(box1, box2):
    """Calculates the distance intersection of union between box1 and box2.
    Args:
        box1: a `Tensor` whose last dimension is 4 representing the coordinates of boxes in
            x_center, y_center, width, height.
        box2: a `Tensor` whose last dimension is 4 representing the coordinates of boxes in
            x_center, y_center, width, height.
    Returns:
        iou: a `Tensor` who represents the distance intersection over union.
    """
    with tf.name_scope("diou"):
        # compute center distance
        dist = _center_distance(box1[..., 0:2], box2[..., 0:2])

        # get box corners
        box1 = _xcycwh_to_xyxy(box1)
        box2 = _xcycwh_to_xyxy(box2)

        # compute IOU
        intersection, union = _intersection_and_union(box1, box2)
        iou = tf.math.divide_no_nan(intersection, union)
        iou = tf.clip_by_value(iou, clip_value_min=0.0, clip_value_max=1.0)

        # compute max diagnal of the smallest enclosing box
        c_mins = tf.math.minimum(box1[..., 0:2], box2[..., 0:2])
        c_maxes = tf.math.maximum(box1[..., 2:4], box2[..., 2:4])
        diag_dist = _center_distance(c_mins, c_maxes)

        regularization = tf.math.divide_no_nan(dist, diag_dist)
        diou = iou + regularization
    return iou, diou
Ejemplo n.º 2
0
def compute_giou(box1, box2):
    """Calculates the generalized intersection of union between box1 and box2.
    Args:
        box1: a `Tensor` whose last dimension is 4 representing the coordinates of boxes in
            x_center, y_center, width, height.
        box2: a `Tensor` whose last dimension is 4 representing the coordinates of boxes in
            x_center, y_center, width, height.
    Returns:
        iou: a `Tensor` who represents the generalized intersection over union.
    """
    with tf.name_scope("giou"):
        # get box corners
        box1 = _xcycwh_to_xyxy(box1)
        box2 = _xcycwh_to_xyxy(box2)

        # compute IOU
        intersection, union = _intersection_and_union(box1, box2)
        iou = tf.math.divide_no_nan(intersection, union)
        iou = tf.clip_by_value(iou, clip_value_min=0.0, clip_value_max=1.0)

        # find the smallest box to encompase both box1 and box2
        c_mins = tf.math.minimum(box1[..., 0:2], box2[..., 0:2])
        c_maxes = tf.math.maximum(box1[..., 2:4], box2[..., 2:4])
        c = _get_area((c_mins, c_maxes), use_tuple=True)

        # compute giou
        giou = iou - tf.math.divide_no_nan((c - union), c)
    return iou, giou
Ejemplo n.º 3
0
def compute_iou(box1, box2):
    # get box corners
    with tf.name_scope("iou"):
        box1 = _xcycwh_to_xyxy(box1)
        box2 = _xcycwh_to_xyxy(box2)
        intersection, union = _intersection_and_union(box1, box2)

        iou = tf.math.divide_no_nan(intersection, union)
        iou = tf.clip_by_value(iou, clip_value_min=0.0, clip_value_max=1.0)
    return iou
Ejemplo n.º 4
0
def compute_iou(box1, box2):
    """Calculates the intersection of union between box1 and box2.
    Args:
        box1: a `Tensor` whose last dimension is 4 representing the coordinates of boxes in
            x_center, y_center, width, height.
        box2: a `Tensor` whose last dimension is 4 representing the coordinates of boxes in
            x_center, y_center, width, height.
    Returns:
        iou: a `Tensor` who represents the intersection over union.
    """
    # get box corners
    with tf.name_scope("iou"):
        box1 = _xcycwh_to_xyxy(box1)
        box2 = _xcycwh_to_xyxy(box2)
        intersection, union = _intersection_and_union(box1, box2)

        iou = tf.math.divide_no_nan(intersection, union)
        iou = tf.clip_by_value(iou, clip_value_min=0.0, clip_value_max=1.0)
    return iou
Ejemplo n.º 5
0
def compute_giou(box1, box2):
    with tf.name_scope("giou"):
        # get box corners
        box1 = _xcycwh_to_xyxy(box1)
        box2 = _xcycwh_to_xyxy(box2)

        # compute IOU
        intersection, union = _intersection_and_union(box1, box2)
        iou = tf.math.divide_no_nan(intersection, union)
        iou = tf.clip_by_value(iou, clip_value_min=0.0, clip_value_max=1.0)

        # find the smallest box to encompase both box1 and box2
        c_mins = K.minimum(box1[..., 0:2], box2[..., 0:2])
        c_maxes = K.maximum(box1[..., 2:4], box2[..., 2:4])
        c = _get_area((c_mins, c_maxes), use_tuple=True)

        # compute giou
        giou = iou - tf.math.divide_no_nan((c - union), c)
    return iou, giou
Ejemplo n.º 6
0
def compute_diou(box1, box2):
    with tf.name_scope("diou"):
        # compute center distance
        dist = _center_distance(box1[..., 0:2], box2[..., 0:2])

        # get box corners
        box1 = _xcycwh_to_xyxy(box1)
        box2 = _xcycwh_to_xyxy(box2)

        # compute IOU
        intersection, union = _intersection_and_union(box1, box2)
        iou = tf.math.divide_no_nan(intersection, union)
        iou = tf.clip_by_value(iou, clip_value_min=0.0, clip_value_max=1.0)

        # compute max diagnal of the smallest enclosing box
        c_mins = K.minimum(box1[..., 0:2], box2[..., 0:2])
        c_maxes = K.maximum(box1[..., 2:4], box2[..., 2:4])
        diag_dist = _center_distance(c_mins, c_maxes)

        regularization = tf.math.divide_no_nan(dist, diag_dist)
        diou = iou + regularization
    return iou, diou