faster-rcnn代码详解(2)

本文介绍并解析了Fast R-CNN中的几个关键工具函数,包括用于去除重复框的独特框索引函数、坐标格式转换函数以及验证和过滤小尺寸框的函数。这些函数在目标检测任务中起到重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

lib/datasets/ds_utils.py

# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

import numpy as np

def unique_boxes(boxes, scale=1.0):
    """Return indices of unique boxes.返回unique boxes 索引"""
    v = np.array([1, 1e3, 1e6, 1e9])
    hashes = np.round(boxes * scale).dot(v).astype(np.int) #将boxes的坐标与缩放比例scale的乘积四舍五入,得到的结果与矩阵V点乘,最后将得到的结果转为整数(即取整数部分)
    _, index = np.unique(hashes, return_index=True)#查找数组中唯一元素,并返回索引值(—,是得到的唯一元素组成的数组,但是我们不需要它)
    return np.sort(index)#对索引值排序(sort函数是list列表中的函数)

def xywh_to_xyxy(boxes):
    """Convert [x y w h] box format to [x1 y1 x2 y2] format.W:宽,h:高 X2=x1+w,y2=y1+h"""
    return np.hstack((boxes[:, 0:2], boxes[:, 0:2] + boxes[:, 2:4] - 1))#VOC2007 的矩形标注坐标是以1为基准的(1-based),而我们在处理图像坐标都是0起始的(0-based)。所以在这里才要对从xml文件中读取的xmin,ymin,xmax,ymax 统统减1将坐标变为我们做数据处理时所需要的0-based坐标

def xyxy_to_xywh(boxes):
    """Convert [x1 y1 x2 y2] box format to [x y w h] format.将[x1y1x2y2]框格式转换为[x y w h ]格式"""
    return np.hstack((boxes[:, 0:2], boxes[:, 2:4] - boxes[:, 0:2] + 1))

def validate_boxes(boxes, width=0, height=0):
    """Check that a set of boxes are valid."""
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    assert (x1 >= 0).all()#所有的X1均大于0,.all返回true
    assert (y1 >= 0).all()
    assert (x2 >= x1).all()
    assert (y2 >= y1).all()
    assert (x2 < width).all()#?
    assert (y2 < height).all()

def filter_small_boxes(boxes, min_size):
    w = boxes[:, 2] - boxes[:, 0]#宽
    h = boxes[:, 3] - boxes[:, 1]#高
    keep = np.where((w >= min_size) & (h > min_size))[0]#返回符合条件的坐标值
    return keep
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值