IoU、GIoU、CIoU和DIoU

IoU(Intersection over Union,交并比)、GIoU(Generalized IoU,广义交并比)、CIoU(Complete IoU,完全交并比)和DIoU(Distance IoU,距离交并比)是目标检测中常用于评估预测框和真实框之间重叠程度的指标。

IoU(交并比):

def calculate_iou(box1, box2):
    intersection = max(0, min(box1[2], box2[2]) - max(box1[0], box2[0])) * max(0, min(box1[3], box2[3]) - max(box1[1], box2[1]))
    union = (box1[2] - box1[0]) * (box1[3] - box1[1]) + (box2[2] - box2[0]) * (box2[3] - box2[1]) - intersection
    iou = intersection / union
    return iou

GIoU(广义交并比):

  • 定义: GIoU考虑了边界框的重叠和大小。
  • 用途: 解决了IoU在定位任务中不是一个合适度量的问题。
    def calculate_giou(box1, box2):
        # 具体实现取决于具体要求,包括计算边界框和凸包的面积。以下是一个简化的示例:
        iou = calculate_iou(box1, box2)
        c_area = min(box1[2], box2[2]) * min(box1[3], box2[3])
        giou = iou - ((c_area - union) / c_area)
        return giou
    

    CIoU(完全交并比):

  • 定义: CIoU通过考虑边界框的纵横比进一步改进了GIoU。
  • 用途: 尝试解决GIoU对纵横比差异敏感的问题。
    def calculate_ciou(box1, box2):
        giou = calculate_giou(box1, box2)
        c_x = min(box1[0], box2[0]) + min(box1[2], box2[2]) / 2
        c_y = min(box1[1], box2[1]) + min(box1[3], box2[3]) / 2
        c_distance = ((c_x - box2[0]) ** 2 + (c_y - box2[1]) ** 2) ** 0.5
        diou_term = c_distance ** 2 / ((c_area - union) + 1e-7)
        ciou = giou - diou_term
        return ciou
    

    DIoU(距离交并比):

  • 定义: DIoU引入了考虑边界框中心点距离的项。
  • 用途: 通过惩罚边界框中心点之间的大距离,改进了CIoU。
    def calculate_diou(box1, box2):
        ciou = calculate_ciou(box1, box2)
        c_x = min(box1[0], box2[0]) + min(box1[2], box2[2]) / 2
        c_y = min(box1[1], box2[1]) + min(box1[3], box2[3]) / 2
        c_distance = ((c_x - box2[0]) ** 2 + (c_y - box2[1]) ** 2) ** 0.5
        diou_term = c_distance ** 2 / ((c_area - union) + 1e-7)
        diou = ciou - diou_term
        return diou
    

你可能感兴趣的:(人工智能,计算机视觉,pytorch,python,YOLO,目标检测)