在Faster RCNN中有使用一些额外的程序,类等等,为了使文章整体内容不过于分散,这里作为篇外卷学习。
文件地址:\py-faster-rcnn\lib\datasets
本文章解析代码为ds_utils.py。
代码包含5个子函数,逐一进行解析。
第一个子函数unique_boxes,作者命名为独一无二的盒子们。
def unique_boxes(boxes, scale=1.0):
"""Return indices of unique boxes."""
v = np.array([1, 1e3, 1e6, 1e9])
hashes = np.round(boxes * scale).dot(v)
_, index = np.unique(hashes, return_index=True)
return np.sort(index)
首先是生存一个1*4的向量V(np.array),然后是一个矩阵的乘(np.round 和dot()),输出不一样的数的位置索引(_, index = np.unique(hashes, return_index=True) ),然后对索引进行排序(np.sort(),默认为升序)
重点说明:np.unique的输出,根据一个简单的例子可知
A = [1, 2, 2, 3, 4, 5]
a, b, c = np.unique(A, return_index=True, return_inverse=True)
print a, b, c # 输出为 [1 2 3 4 5], [0 1 3 4 5], [0 1 1 2 3 4]
可以看出第一个输出是不重复的值,第二个是每个不重复在A中的位置索引,第三个是每个值在A中的位置。
简单来说,该子函数unique_boxes输入了盒子的尺寸,乘以一个放缩尺度大小(估计是后面三个尺度太小需要放大便于比较。)后删除了重复的盒子。
然后的格式数据的转化xywh_to_xyxy()和xyxy_to_xywh()。
def xywh_to_xyxy(boxes):
"""Convert [x y w h] box format to [x1 y1 x2 y2] format."""
return np.hstack((boxes[:, 0:2], boxes[:, 0:2] + boxes[:, 2:4] - 1))
def xyxy_to_xywh(boxes):
"""Convert [x1 y1 x2 y2] box format to [x y w h] format."""
return np.hstack((boxes[:, 0:2], boxes[:, 2:4] - boxes[:, 0:2] + 1))
根据一个简单的例子来了解下np.hstack():
import numpy as np
a = np.array((1,2))
b = np.array((2,3))
print(np.hstack((a,b)))
[1 2 2 3]
a = np.array([[1],[2]])
b = np.array([[2],[3]])
print(np.hstack((a,b)))
[[1 2]
[2 3]]
下列函数validate_boxes()为验证一个盒子是否有效:
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()
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
解释np.where(),该函数防护的是一个布尔值的列表,后面判断语句中为真的地方返回True,假为False。