AIHGF

目标检测 - IoU 计算
IoU(Intersection over Union) 是用于评价目标检测算法精度的一种常用手段.1. IoU ...
扫描右侧二维码阅读全文
10
2019/05

目标检测 - IoU 计算

IoU(Intersection over Union) 是用于评价目标检测算法精度的一种常用手段.

1. IoU 简述

IoU 常被用于评估 HOG + Linear SVM object detectors 和 Convolutional Neural Network detectors (R-CNN, Faster R-CNN, YOLO, 等) 算法模型的检测性能. 不过,IoU 计算与得到检测结果的具体算法无关.

IoU 是一种简单的评价度量,其可以用于评估任何输出为 bounding box 的模型算法的性能.

IoU 计算的必要项:

[1] - groundtruth bounding boxes,例如,测试集中手工标注的物体边界框.

[2] - predicted bounding boxes, 检测算法模型所预测的输出.

如下图图例示:

即,IoU 的计算如下图,IoU 的值可以认为是两个区域的重叠部分(交集)除以两个区域的集合部分(并集)所得到的比值.

目标检测中,算法的性能好坏,预测的边界框与 groundtruth 边界框间的重叠覆盖成都,如图例示:

2. IoU - Python 实现

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

3. IoU 计算示例

基于 CALTECH-101 dataset 检测数据集,示例图片:

图1 - 检测示例图片,绿色为 groundtruth bounding box;红色为 preditcted bounding box.

HOG + Linear SVM 检测算法 - Histogram of Oriented Gradients and Object Detection - 2014.11.10

IoU 计算:

from collections import namedtuple
import numpy as np
import cv2
 
# define the `Detection` object
Detection = namedtuple("Detection", ["image_path", "gt", "pred"])

# define the list of example detections
examples = [
    Detection("image_0002.jpg", [39, 63, 203, 112], [54, 66, 198, 114]),
    Detection("image_0016.jpg", [49, 75, 203, 125], [42, 78, 186, 126]),
    Detection("image_0075.jpg", [31, 69, 201, 125], [18, 63, 235, 135]),
    Detection("image_0090.jpg", [50, 72, 197, 121], [54, 72, 198, 120]),
    Detection("image_0120.jpg", [35, 51, 196, 110], [36, 60, 180, 108])]

# loop over the example detections
for detection in examples:
    # load the image
    image = cv2.imread(detection.image_path)
 
    # draw the ground-truth bounding box along with the predicted
    # bounding box
    cv2.rectangle(image, tuple(detection.gt[:2]), 
        tuple(detection.gt[2:]), (0, 255, 0), 2)
    cv2.rectangle(image, tuple(detection.pred[:2]), 
        tuple(detection.pred[2:]), (0, 0, 255), 2)
 
    # compute the intersection over union and display it
    iou = bb_intersection_over_union(detection.gt, detection.pred)
    cv2.putText(image, "IoU: {:.4f}".format(iou), (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
    print("{}: {:.4f}".format(detection.image_path, iou))
 
    # show the output image
    cv2.imshow("Image", image)
    cv2.waitKey(0)

计算结果如图:

参考

[1] - Intersection over Union (IoU) for object detection - 2016.11.07

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

Last modification:May 10th, 2019 at 02:42 pm

Leave a Comment