Detection基础模块之(一)IoU

系列博客总结一下目标检测中的基本模块

1.IoU(本文)

2.mAP

3.RPN&&Selective search

4.ROI pooling&&ROI align

5.backbone

Intersection over Union (IoU)

1.IoU的概念

为了用IoU指标来评估目标检测器,我们需要:

ground-truth bounding boxes和我们训练好的模型预测的bounding boxes。

下图是一个示例。图中绿色框为Ground-truth bounding box,红色框为预测框,我们的目标是计算它们的IoU。

 

计算IoU的公式如下图,可以看到IoU是一个比值,即交并比。

在分子中,我们计算预测框和ground-truth之间的重叠区域;

分母是并集区域,或者更简单地说,是预测框和ground-truth所包含的总区域。

重叠区域和并集区域的比值,就是IoU。

 

2.为什么使用IoU来评估目标检测器

与分类任务不同,我们预测的bounding box的坐标需要去匹配ground-truth的坐标,而坐标完全匹配基本是不现实的。因此,我们需要定义一个评估指标,奖励那些与ground-truth匹配较好(重叠较大)的预测框。

 

上图展示了IoU的好坏对比,与ground-truth bounding boxes 重叠比例更大的预测边界框比重叠较少的边界框具有更高的分数,这使得IoU成为评估目标检测器的极佳指标。

我们并不关心(x,y)坐标的精确匹配,但我们确实希望确保我们预测的边界框尽可能匹配——IoU可以做到这一点。

3.IoU的python实现

现在我们已经了解了IoU是什么以及为什么用它来评估目标检测模型,接下来就用Python实现它!

我们的目标是使用IoU来评估目标探测器的性能。 具体来说,给定预测的边界框(红色)与ground-truth(绿色),也就是我们训练好模型的输出与真实label,我们用IoU指标来评估模型的好坏。 

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 = max(0, xB - xA + 1) * max(0, 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

函数需要两个参数:boxA和boxB,假定是我们的ground-truth和预测框。

第3-6行确定两个矩形框的(x,y)坐标,然后我们用它们来计算交集的面积(第9行)。

interArea变量表示IoU公式中的分子。

为了计算分母,我们首先需要计算出预测框和ground-truth的区域(第13和14行)。

然后可以在19行上通过将交集区域除以两个边界框的并集区域与交叉区域的差来计算IoU,(注意从分母中减去交叉区域,否则交叉区域将被双倍计数)。

最后,IoU分数将返回到第22行的调用函数。

现在我们的IoU方法已经完成。

4.IoU的实际使用

下面实现是商汤mmdet中的实现,基本是一样的,只是是batch的矩阵运算。

import numpy as np
 ​
 ​
 def bbox_overlaps(bboxes1, bboxes2, mode='iou'):
     """Calculate the ious between each bbox of bboxes1 and bboxes2.
     Args:
         bboxes1(ndarray): shape (n, 4)
         bboxes2(ndarray): shape (k, 4)
         mode(str): iou (intersection over union) or iof (intersection
             over foreground)
     Returns:
         ious(ndarray): shape (n, k)
     """
 ​
     assert mode in ['iou', 'iof']
 ​
     bboxes1 = bboxes1.astype(np.float32)
     bboxes2 = bboxes2.astype(np.float32)
     rows = bboxes1.shape[0]
     cols = bboxes2.shape[0]
     ious = np.zeros((rows, cols), dtype=np.float32)
     if rows * cols == 0:
         return ious
     exchange = False
     if bboxes1.shape[0] > bboxes2.shape[0]:
         bboxes1, bboxes2 = bboxes2, bboxes1
         ious = np.zeros((cols, rows), dtype=np.float32)
         exchange = True
     area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * (
         bboxes1[:, 3] - bboxes1[:, 1] + 1)
     area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * (
         bboxes2[:, 3] - bboxes2[:, 1] + 1)
     for i in range(bboxes1.shape[0]):
         x_start = np.maximum(bboxes1[i, 0], bboxes2[:, 0])
         y_start = np.maximum(bboxes1[i, 1], bboxes2[:, 1])
         x_end = np.minimum(bboxes1[i, 2], bboxes2[:, 2])
         y_end = np.minimum(bboxes1[i, 3], bboxes2[:, 3])
         overlap = np.maximum(x_end - x_start + 1, 0) * np.maximum(
             y_end - y_start + 1, 0)
         if mode == 'iou':
             union = area1[i] + area2 - overlap
         else:
             union = area1[i] if not exchange else area2
         ious[i, :] = overlap / union
     if exchange:
         ious = ious.T
     return ious

5.参考资料

Intersection over Union (IoU) for object detection

bbox_overlaps.py

 

mask_rcnn_inception_v2_spot.config是TensorFlow的Object Detection API模块中的一个配置文件,用于配置Mask R-CNN模型的训练和推理参数。下面是一些常见参数及其含义: 1. num_classes:指定目标类别的数量,包括背景类。例如,如果要检测20个类别,则num_classes设置为21。 2. image_resizer:设置图像预处理的方式,如通过保持宽高比进行缩放或裁剪。 3. feature_extractor:指定特征提取器的类型,例如InceptionV2、ResNet等。 4. first_stage_anchor_generator:设置第一阶段锚框生成器的参数,包括尺度、宽高比等。 5. first_stage_box_predictor:设置第一阶段边界框预测器的参数,如使用的卷积核大小、激活函数等。 6. first_stage_nms_iou_threshold:指定第一阶段非极大值抑制(NMS)的IoUIntersection over Union)阈值。 7. second_stage_box_predictor:设置第二阶段边界框预测器的参数,如使用的卷积核大小、激活函数等。 8. second_stage_post_processing:设置第二阶段后处理的参数,如使用的非极大值抑制(NMS)的IoU阈值和置信度阈值。 9. second_stage_batch_size:设置第二阶段的批量大小,用于训练和推理。 10. use_dropout:设置是否在特征提取器中使用dropout正则化。 11. detection_score_converter:设置检测分数转换器的类型和参数,用于将检测分数转换为最终的置信度。 以上仅是一些常见的参数,实际上,mask_rcnn_inception_v2_spot.config文件中还包含了其他许多参数,用于配置模型的各个方面,如学习率、优化器、训练和推理的路径等。通过修改这些参数,我们可以根据需求来定制和优化Mask R-CNN模型的训练和推理过程。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值