深度学习中IU、IoU(Intersection over Union)的概念理解以及python程序实现

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站

有关图像分割评价标准详细信息可以看这里:

IoU(Intersection over Union)

Intersection over Union是一种测量在特定数据集中检测相应物体准确度的一个标准。我们可以在很多物体检测挑战中,例如PASCAL VOC challenge中看多很多使用该标准的做法。

通常我们在 HOG + Linear SVM object detectors 和 Convolutional Neural Network detectors (R-CNN, Faster R-CNN, YOLO, etc.)中使用该方法检测其性能。注意,这个测量方法和你在任务中使用的物体检测算法没有关系。

IoU是一个简单的测量标准,只要是在输出中得出一个预测范围(bounding boxex)的任务都可以用IoU来进行测量。为了可以使IoU用于测量任意大小形状的物体检测,我们需要:
1、 ground-truth bounding boxes(人为在训练集图像中标出要检测物体的大概范围);
2、我们的算法得出的结果范围。

也就是说,这个标准用于测量真实和预测之间的相关度,相关度越高,该值越高。

如下图:

这里写图片描述

下图展示了ground-truth和predicted的结果,绿色标线是人为标记的正确结果,红色标线是算法预测出来的结果,IoU要做的就是在这两个结果中测量算法的准确度。

这里写图片描述

如上图,很简单,IoU相当于两个区域重叠的部分除以两个区域的集合部分得出的结果。
一般来说,这个score > 0.5 就可以被认为一个不错的结果了。

这里写图片描述

python程序实现

具体实现过程请移步:https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/

def bb_intersection_over_union(boxA, boxB):
	# determine the (x, y)-coordinates of the intersection rectangle
	xA = max(boxA[0], boxB[0])
	yA = max(boxA[1], boxB[1])
	xB = min(boxA[2], boxB[2])
	yB = min(boxA[3], boxB[3])
 
	# compute the area of intersection rectangle
	interArea = (xB - xA + 1) * (yB - yA + 1)
 
	# compute the area of both the prediction and ground-truth
	# rectangles
	boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
	boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
 
	# compute the intersection over union by taking the intersection
	# area and dividing it by the sum of prediction + ground-truth
	# areas - the interesection area
	iou = interArea / float(boxAArea + boxBArea - interArea)
 
	# return the intersection over union value
	return iou

后记

IoU在FCN中称为IU,初看Fully Convolutional Networks for Semantic Segmentation论文,其中的IU概念没有能理解,其实那里的IU也就是IoU,检测物体轮廓不一定非得是方框,也可以是沿着物体的边线:

这里写图片描述

在实际的任务中,根据不同的任务要求来写不同具体实现的检测方法,但说白了其实都是IoU或者IU。
另外mean IU指的是不同类别识别准确度的平均值,比如一幅图中要识别三个物体,mean IU就是三个物体分别准确度加起来的平均值。

参考资料:

1、https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
2、https://stackoverflow.com/questions/25349178/calculating-percentage-of-bounding-box-overlap-for-image-detector-evaluation/42874377#42874377
3、https://stackoverflow.com/questions/25349178/calculating-percentage-of-bounding-box-overlap-for-image-detector-evaluation/42874377#42874377

如果你与我志同道合于此,很愿意与你交流
如果你喜欢我的内容,欢迎关注和支持
微信公众号 : oldpan博客
丢给你一个神秘链接: oldpan.me

微信扫码关注我哦

### Facebook Intersection over Union (FB-IoU): 概念与实现 Facebook提出的Intersection over Union (IoU) 的扩展版本通常被称为 **Foreground-background IoU (FB-IoU)** 或者类似的变体形式。这种度量方式主要用于解决传统IoU存在的局限性,尤其是在目标检测和语义分割任务中。 #### FB-IoU的核心概念 传统的IoU仅关注预测框与真实标注框之间的重叠区域比例[^1]。然而,在复杂场景下,尤其是当背景信息占据较大比重时,单纯依赖对象间的交集面积可能无法全面评估模型性能。因此,FB-IoU引入了前景(foreground)和背景(background)两个部分的综合考量: - 前景部分:计算预测掩膜与实际掩膜之间的一致性。 - 背景部分:衡量未被标记为目标的部分是否也被正确分类为背景。 通过这种方式,FB-IoU能够更精确地评价模型对于整个图像的理解能力,而不仅仅是针对特定目标的表现。 #### 数学定义 假设有一个预测掩码 \(P\) 和对应的ground truth 掩码 \(G\) ,则可以分别定义前景和背景得分如下: \[ FB\_IoU_{fg} = \frac{| P \cap G |}{| P \cup G |} \] \[ FB\_IoU_{bg} = \frac{| (\neg P) \cap (\neg G) |}{| (\neg P) \cup (\neg G) |} \] 其中, - \(| P \cap G |\): 预测正类且真实的像素数; - \(| P \cup G |\): 至少属于预测或者真实任一类别的总像素数; - \(\neg X\): 表示取反操作即不属于集合\(X\)的所有元素; 最终的整体分数可以通过加权平均得到: \[ FB\_IoU = w_1 * FB\_IoU_{fg} + w_2 * FB\_IoU_{bg}, where \;w_1+w_2=1. \] 权重的选择可以根据具体应用场景调整,默认情况下两者相等。 #### 实现细节 以下是基于PyTorch的一个简单实现例子: ```python import torch def compute_fb_iou(pred_mask, gt_mask): """ Compute Foreground Background IOU given prediction and groundtruth masks. Args: pred_mask (Tensor): Predicted binary mask tensor with shape [H,W]. gt_mask (Tensor): Ground Truth binary mask tensor with same size as `pred_mask`. Returns: float: The computed FB_IOU score. """ # Ensure tensors have correct dtype & device assert isinstance(pred_mask,torch.Tensor), f'Expected Tensor but got {type(pred_mask)}' assert isinstance(gt_mask,torch.Tensor),f 'Expected Tensor but got {type(gt_mask)}' fg_intersection=(pred_mask*gt_mask).sum().float() fg_union=((pred_mask+gt_mask)>0).sum().float() bg_pred=~(pred_mask.bool()) bg_gt=~(gt_mask.bool()) bg_intersection=(bg_pred&bg_gt).sum().float() bg_union=((~pred_mask)+(~gt_mask)).bool().sum().float() fb_iou_fg=fg_intersection/fg_union if fg_union!=0 else 0.0 fb_iou_bg=bg_intersection/bg_union if bg_union!=0 else 0.0 return 0.5*(fb_iou_fg+fb_iou_bg) # Example Usage if __name__=='__main__': import numpy as np img_size=[100,100] rand_gen=np.random.RandomState(seed=42) test_img=rand_gen.randint(low=0,high=2,size=img_size,dtype=bool) target_img=test_img.copy();target_img[:,-1]=False # Modify last column to create difference result=compute_fb_iu(torch.tensor(test_img),torch.tensor(target_img)) print(f'Resultant FB_IoU Score:{result:.4f}') ``` 上述代码片段展示了如何利用布尔运算快速有效地计算两张二值化图片间FB-IoU指标的方法[^3]。
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老潘的博客

请老潘吃块饼干!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值