Faster-RCNN_TF代码解读19:bbox.pyx

# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Sergey Karayev
# --------------------------------------------------------

cimport cython
import numpy as np
cimport numpy as np

DTYPE = np.float
ctypedef np.float_t DTYPE_t
# rpn:region proposal network
# IoU:Intersection-over-Union,交集并集之比
# GT:ground truth,GT boxes (x1, y1, x2, y2, label),左上的坐标 和 右下的坐标 + label
# im:image
# ROI:region of interest
# bbox:bounding-box
# regression:和one hot的classification其实区别不大,只是target是(可能为0和1以及)其他数字
# anchor:一个box,通过从上一个卷积层的结果 滑动得到很多个,每个要和 GT的box 计算bbox_overlap
# bbox_overlap:貌似就是IoU,源码如下

def bbox_overlaps(
        np.ndarray[DTYPE_t, ndim=2] boxes,
        np.ndarray[DTYPE_t, ndim=2] query_boxes):
    """
    Parameters
    ----------
    boxes: (N, 4) ndarray of float
    query_boxes: (K, 4) ndarray of float
    Returns
    -------
    overlaps: (N, K) ndarray of overlap between boxes and query_boxes
    """
    #记录anchor的个数
    cdef unsigned int N = boxes.shape[0]
    #记录ground-truth的个数
    cdef unsigned int K = query_boxes.shape[0]
    #创建一个全零arrayoverlaps
    cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
    cdef DTYPE_t iw, ih, box_area
    cdef DTYPE_t ua
    cdef unsigned int k, n
    for k in range(K):
        box_area = (#query_boxes(GT)的面积
            (query_boxes[k, 2] - query_boxes[k, 0] + 1) *#列数(x)
            (query_boxes[k, 3] - query_boxes[k, 1] + 1)#行数
        )
        #每一个anchor与当前GT比一下
        for n in range(N):
            iw = (#计算重叠部分的宽
                min(boxes[n, 2], query_boxes[k, 2]) -
                max(boxes[n, 0], query_boxes[k, 0]) + 1
            )
            if iw > 0:
                ih = (#计算重叠部分的高
                    min(boxes[n, 3], query_boxes[k, 3]) -
                    max(boxes[n, 1], query_boxes[k, 1]) + 1
                )
                if ih > 0:
                    ua = float(#计算(anchor+GT-重叠)的面积
                        (boxes[n, 2] - boxes[n, 0] + 1) *
                        (boxes[n, 3] - boxes[n, 1] + 1) +
                        box_area - iw * ih
                    )
                    #对应(n,k)元素存入(重叠/(anchor+GT-重叠))面积
                    overlaps[n, k] = iw * ih / ua
    return overlaps
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值