目标检测 IOU(Intersection over Union)

转载自https://blog.csdn.net/u014061630/article/details/82818112

原作者写的挺好的,mark一下

IoU 作为目标检测算法性能 mAP 计算的一个非常重要的函数。

但纵观 IoU 计算的介绍知识,都是直接给出代码,给出计算方法,没有人彻底地分析过其中的逻辑,故本人书写该篇博客来介绍下其中的逻辑。

1. IoU的简介及原理解析
IoU 的全称为交并比(Intersection over Union),通过这个名称我们大概可以猜到 IoU 的计算方法。IoU 计算的是 “预测的边框” 和 “真实的边框” 的交集和并集的比值。

开始计算之前,我们首先进行分析下交集和并集到底应该怎么计算:我们首先需要计算交集,然后并集通过两个边框的面积的和减去交集部分即为并集,因此 IoU 的计算的难点在于交集的计算。

为了计算交集,你脑子里首先想到的方法应该是:考虑两个边框的相对位置,然后按照相对位置(左上,左下,右上,右下,包含,互不相交)分情况讨论,来计算交集。

在这里插入图片描述

上图就是你的直觉,这样想没有错。但计算一个交集,就要分多种情况讨论,要是程序真的按照这逻辑编写就太搞笑了。因此对这个问题进行进一步地研究显得十分有必要。

让我们重新思考一下两个框交集的计算。两个框交集的计算的实质是两个集合交集的计算,因此我们可以将两个框的交集的计算简化为:

在这里插入图片描述

通过简化,我们可以清晰地看到,交集计算的关键是交集上下界点(图中蓝点)的计算。

我们假设集合 A 为 [x1,x2],集合 B 为 [y1,y2]。然后我们来求AB交集的上下界限。

交集计算的逻辑

交集下界 z1 = max(x1,y1)

交集上界 z2 = min(x2,y2)

如果z2 - z1小于0,则说明集合 A 和集合 B 没有交集。
下面使用Python来实现两个一维集合的 IoU 的计算:
 

def iou(set_a, set_b):
    '''
    一维 iou 的计算
    '''
    x1, x2 = set_a # (left, right)
    y1, y2 = set_b # (left, right)
    
    low = max(x1, y1)
    high = min(x2, y2)
    # intersection
    if high-low<0:
        inter = 0
    else:
        inter = high-low
    # union
    union = (x2 - x1) + (y2 - y1) - inter
    # iou
    iou = inter / union
    return iou

上面,我们计算了两个一维集合的 iou,将上面的程序进行扩展,即可得到两个框 IoU 计算的程序。

def iou(box1, box2):
    '''
    两个框(二维)的 iou 计算
    
    注意:边框以左上为原点
    
    box:[top, left, bottom, right]
    '''
    in_h = min(box1[2], box2[2]) - max(box1[0], box2[0])
    in_w = min(box1[3], box2[3]) - max(box1[1], box2[1])
    inter = 0 if in_h<0 or in_w<0 else in_h*in_w
    union = (box1[2] - box1[0]) * (box1[3] - box1[1]) + \
            (box2[2] - box2[0]) * (box2[3] - box2[1]) - inter
    iou = inter / union
    return iou

2. 基于TensorFlow的IoU实现

上节介绍了IoU,及其的计算,下面我们给出其在 TensorFlow 上的实现:

import tensorflow as tf

def IoU_calculator(x, y, w, h, l_x, l_y, l_w, l_h):
    """calaulate IoU
    Args:
      x: net predicted x
      y: net predicted y
      w: net predicted width
      h: net predicted height
      l_x: label x
      l_y: label y
      l_w: label width
      l_h: label height
    
    Returns:
      IoU
    """
    
    # convert to coner
    x_max = x + w/2
    y_max = y + h/2
    x_min = x - w/2
    y_min = y - h/2
 
    l_x_max = l_x + l_w/2
    l_y_max = l_y + l_h/2
    l_x_min = l_x - l_w/2
    l_y_min = l_y - l_h/2
    # calculate the inter
    inter_x_max = tf.minimum(x_max, l_x_max)
    inter_x_min = tf.maximum(x_min, l_x_min)
 
    inter_y_max = tf.minimum(y_max, l_y_max)
    inter_y_min = tf.maximum(y_min, l_y_min)
 
    inter_w = inter_x_max - inter_x_min
    inter_h = inter_y_max - inter_y_min
    
    inter = tf.cond(tf.logical_or(tf.less_equal(inter_w,0), tf.less_equal(inter_h,0)), 
                    lambda:tf.cast(0,tf.float32), 
                    lambda:tf.multiply(inter_w,inter_h))
    # calculate the union
    union = w*h + l_w*l_h - inter
    
    IoU = inter / union
    return IoU

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这里是推荐的20个目标检测 IOU 变形: 1. IOU (Intersection over Union) 2. Jaccard Index 3. Dice Similarity Coefficient 4. Tversky Index 5. Sensitivity 6. Specificity 7. Precision 8. Recall 9. F1 Score 10. Matthew's Correlation Coefficient 11. Informedness 12. Markedness 13. G-Mean 14. AUC-PR (Area Under the Precision-Recall Curve) 15. AUC-ROC (Area Under the Receiver Operating Characteristic Curve) 16. L1 Distance 17. L2 Distance 18. Cosine Similarity 19. Hamming Distance 20. Jaccard Distance ### 回答2: 目标检测IoU变形指的是在计算两个边界框(bounding box)之间的重叠度量时,通过改变IoU的计算公式来达到不同的目的。以下是推荐的20个目标检测IoU变形: 1. IoU (Intersection over Union): 传统的IoU计算方式,定义为两个边界框相交区域面积除以它们的并集面积。 2. GIoU (Generalized Intersection over Union): 在计算IoU时,考虑相交区域相对于并集的冗余面积。 3. DIoU (Distance Intersection over Union): 在计算IoU时,引入两个边界框中心点的欧氏距离,考虑边界框之间的距离。 4. CIoU (Complete Intersection over Union): 在计算IoU时,综合考虑边界框的长宽比、中心点距离和相交区域的冗余面积。 5. IoF (Intersection over Foreground): 只考虑边界框与目标物体的相交区域,忽略背景的贡献。 6. GIoF (Generalized Intersection over Foreground): 在计算IoF时,考虑相交区域相对于目标物体的冗余面积。 7. DIoF (Distance Intersection over Foreground): 在计算IoF时,引入目标物体中心点的欧氏距离,考虑目标物体之间的距离。 8. CIoF (Complete Intersection over Foreground): 在计算IoF时,综合考虑目标物体的长宽比、中心点距离和相交区域的冗余面积。 9. IoB (Intersection over Background): 只考虑边界框与背景的相交区域。 10. GIoB (Generalized Intersection over Background): 在计算IoB时,考虑相交区域相对于背景的冗余面积。 11. DIoB (Distance Intersection over Background): 在计算IoB时,引入背景中心点的欧氏距离,考虑背景之间的距离。 12. CIoB (Complete Intersection over Background): 在计算IoB时,综合考虑背景的长宽比、中心点距离和相交区域的冗余面积。 13. GIoF (Generalized Intersection over Foreground): 同上,但考虑更多的物体信息,如像素点、轮廓等。 14. DIoT (Distance Intersection over Time): 在计算IoU时,将时间作为一维的信息引入,考虑目标物体在时间维度上的变化。 15. SIoU (Symmetric Intersection over Union): 将两个边界框的IoU计算结果互为分子和分母,可以消除计算结果的顺序依赖性。 16. IIoU (Integral Intersection over Union): 在计算IoU时,采用浮点运算的积分值来近似边界框的相交区域面积。 17. MIoU (Modified Intersection over Union): 在计算IoU时,限制边界框之间的重叠面积不能超过边界框自身的一部分。 18. RIoU (Regularized Intersection over Union): 在计算IoU时,对相交区域的像素点进行规则化,消除图像质量不一致的影响。 19. YIoU (Youdens's Intersection over Union): 引入Youdens' index,综合考虑真阳性和假阳性,用于二分类问题的目标检测。 20. CCIoU (Complete Cross Intersection over Union): 综合考虑边界框之间的相交区域、距离和长宽比,采用参数化的方式进行IoU计算。 ### 回答3: IOUIntersection over Union)是一种常用于目标检测中衡量预测框与真实框重叠程度的指标。它计算了两个框的交集面积与并集面积的比值,值介于0和1之间。为了进一步提升目标检测的准确性,人们对IOU进行了多种变形。下面是推荐的20个目标检测IOU变形: 1. GIOU(Generalized Intersection over Union) 2. CIOU(Complete Intersection over Union) 3. DIOU(Distance Intersection over Union) 4. OROU(Online Rectification Intersection over Union) 5. CCIU(Configurable Context Intersection over Union) 6. TIOU(Temporal Intersection over Union) 7. AIOU(Adjusted Intersection over Union) 8. SATIOU(Scale-Aware Temporal Intersection over Union) 9. TGIoU(Temporal Generalized Intersection over Union) 10. AGIOU(Anchor Generalized Intersection over Union) 11. VGIoU(Variable Generalized Intersection over Union) 12. BIoU(Backbone Intersection over Union) 13. FIoU(Future Intersection over Union) 14. PGIoU(Positional Geometric Intersection over Union) 15. GCIoU(Geodesic Intersection over Union) 16. LGIOU(Linear Growth Intersection over Union) 17. PPIoU(Positional Partition Intersection over Union) 18. SJIoU(Set Junction Intersection over Union) 19. CIOU-V2(Complete Intersection over Union Version 2) 20. PIOU(Prior Intersection over Union) 这些变形的提出都是为了解决目标检测中的具体问题或进一步提升检测的准确性。不同的变形方法适用于不同的场景和数据集,需要根据实际需求来选择最适合的IOU变形方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值