美文网首页
求IoU代码

求IoU代码

作者: D_Major | 来源:发表于2019-07-22 15:17 被阅读0次

主要用来求RoI和GT的IoU, 也可以用来求预测结果和GT的IoU

def iou(boxes1, boxes2):
    """Computes IoU overlaps between two sets of boxes.
    boxes1, boxes2: [N, (y1, x1, y2, x2)].
    """
    # 1. Tile boxes2 and repeat boxes1. This allows us to compare
    # every boxes1 against every boxes2 without loops.
    # TF doesn't have an equivalent to np.repeat() so simulate it
    # using tf.tile() and tf.reshape().
    b1 = np.repeat(boxes1, np.shape(boxes2)[0], axis=0)
    b2 = np.tile(boxes2, [np.shape(boxes1)[0], 1])
    # 2. Compute intersections
    b1_y1, b1_x1, b1_y2, b1_x2 = np.split(b1, 4, axis=1)
    b2_y1, b2_x1, b2_y2, b2_x2 = np.split(b2, 4, axis=1)
    y1 = np.maximum(b1_y1, b2_y1)
    x1 = np.maximum(b1_x1, b2_x1)
    y2 = np.minimum(b1_y2, b2_y2)
    x2 = np.minimum(b1_x2, b2_x2)
    intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
    # 3. Compute unions
    b1_area = (b1_y2 - b1_y1) * (b1_x2 - b1_x1)
    b2_area = (b2_y2 - b2_y1) * (b2_x2 - b2_x1)
    union = b1_area + b2_area - intersection
    # 4. Compute IoU and reshape to [boxes1, boxes2]
    iou = intersection / union
    overlaps = np.reshape(iou, [np.shape(boxes1)[0], np.shape(boxes2)[0]])
    return overlaps

 roi_iou_max = tf.reduce_max(overlaps, axis=1)

第一步即先把二者形状统一以方便逐个比较:

boxes1 = [[0,0,3,3], [1,1,5,5], [9,9,11,11]]
boxes2 = [[0,0,2,2], [1,1,3,3], [4,4,6,6], [6,6,9,9], [11,11,13,13]]
输出为
[[ 0  0  3  3]
 [ 0  0  3  3]
 [ 0  0  3  3]
 [ 0  0  3  3]
 [ 0  0  3  3]
 [ 1  1  5  5]
 [ 1  1  5  5]
 [ 1  1  5  5]
 [ 1  1  5  5]
 [ 1  1  5  5]
 [ 9  9 11 11]
 [ 9  9 11 11]
 [ 9  9 11 11]
 [ 9  9 11 11]
 [ 9  9 11 11]] 
[[ 0  0  2  2]
 [ 1  1  3  3]
 [ 4  4  6  6]
 [ 6  6  9  9]
 [11 11 13 13]
 [ 0  0  2  2]
 [ 1  1  3  3]
 [ 4  4  6  6]
 [ 6  6  9  9]
 [11 11 13 13]
 [ 0  0  2  2]
 [ 1  1  3  3]
 [ 4  4  6  6]
 [ 6  6  9  9]
 [11 11 13 13]]

之后的交集, 并集, IoU都是15x1的矩阵, 最后reshape成3x5的即[np.shape(boxes1)[0], np.shape(boxes2)[0]]代表把boxes1的每一项逐个和boxes2的每一项比较IoU, boxes1中同一个box得到的结果在同一行.

相关文章

  • 求IoU代码

    主要用来求RoI和GT的IoU, 也可以用来求预测结果和GT的IoU 第一步即先把二者形状统一以方便逐个比较: 之...

  • 目标检测之 IoU计算原理与方法

    IoU 作为目标检测算法性能 mAP 计算的一个非常重要的函数。 但纵观 IoU 计算的介绍知识,都是直接给出代码...

  • IOU入门和代码实现

    IOU的计算方法 假设有下面的两个框Predict和GT。 我们看怎么来计算这两个框的IOU. 下面我们采用左上角...

  • AAAI2020目标检测算法DIoU YOLOv3更加稳定有效的

    背景 让我们回到IoU损失和GioU损失。IoU损失可以表示为:,从IoU的角度看,回归损失是可以解决的,但它的缺...

  • 人脸检测——mtcnn思想,生成negative、positiv

    negative样本:IOU < 0.3,标签为:0 0 0 0 0positive样本:IOU > =0.65,...

  • (17)IOUNet

    IOU_Net是对目前目标检测中IOU区域选择不合理而提出的网络。主要改进在两个方面。 #(1)IoU-guide...

  • 2019-03-03

    图像处理的交并比(IoU) 交并比(Intersection-over-Union,IoU),目标检测中使用的一个...

  • YOLO 目标检测实战项目『原理篇』

    目标检测评价指标 IoU(Intersection-over-Union)指标IoU 简称交并比,顾名思义数学中交...

  • COCO目标检测测评指标

    纪念版的评分图: 在COCO数据中,默认AP就是mAP。 mAP@.5IOU=AP@.5IOU, mAP@.75...

  • 基于pytorch计算IoU

    IoU 是目标检测里面的一个基本的环节,这里看到别人的代码,感觉还是挺高效的,就记录一下: torch.Tenso...

网友评论

      本文标题:求IoU代码

      本文链接:https://www.haomeiwen.com/subject/jchwlctx.html