时间2022/8/11
代码:https://github.com/jwyang/faster-rcnn.pytorch
作者只是一个刚刚开始学代码/的小白,本文更多作为个人学习笔记,错误之处欢迎大家批评指正。
参考的部分文章:
文件名:lib - model - rpn - generate_anchors
from __future__ import print_function
# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick and Sean Bell
# --------------------------------------------------------
import numpy as np
import pdb
# Verify that we compute the same anchors as Shaoqing's matlab implementation:
#
# >> load output/rpn_cachedir/faster_rcnn_VOC2007_ZF_stage1_rpn/anchors.mat
# >> anchors
#
# anchors =
#
# -83 -39 100 56
# -175 -87 192 104
# -359 -183 376 200
# -55 -55 72 72
# -119 -119 136 136
# -247 -247 264 264
# -35 -79 52 96
# -79 -167 96 184
# -167 -343 184 360
#array([[ -83., -39., 100., 56.],
# [-175., -87., 192., 104.],
# [-359., -183., 376., 200.],
# [ -55., -55., 72., 72.],
# [-119., -119., 136., 136.],
# [-247., -247., 264., 264.],
# [ -35., -79., 52., 96.],
# [ -79., -167., 96., 184.],
# [-167., -343., 184., 360.]])
try:
xrange # Python 2
except NameError: #由于fasterrcnn比较久了这里做了下兼容
xrange = range # Python 3
#这个文件整体的作用就是根据一个基础的anchor就是1:1的然后根据高宽比和尺度生成9个anchor
#如果是小白读代码之前一定要完全读懂论文,我其实都没咋读论文直接上代码,这样会有很多问题
def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
scales=2**np.arange(3, 6)):
#定义总函数,通过调用下面定义的一些函数生成最终anchors
"""
Generate anchor (reference) windows by enumerating aspect ratios X
scales wrt a reference (0, 0, 15, 15) window.
"""
base_anchor = np.array([1, 1, base_size, base_size]) - 1
#这里的结果是 0,0,15,15
ratio_anchors = _ratio_enum(base_anchor, ratios)
#这里调用了下面定义的ratio_enum函数得到不同高宽比的anchor
#print(ratio_anchors)打印出来会出现三组数据
#[[-3.5 2. 18.5 13.]
#[0. 0. 15. 15.]
#[2.5 -3. 12.5 18.]]
anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
for i in xrange(ratio_anchors.shape[0])])
#这里再通过调用_scale_enum函数得到不同尺度的anchor一共输出9个
return anchors
def _whctrs(anchor):#定义这个函数是为了通过左上角和右下角坐标得到中心坐标和宽高
#因为本身给的基础anchor数据是左上角和右下角坐标
"""
Return width, height, x center, and y center for an anchor (window).
"""
#例如:我们输入0,0,15,15
#得到 16,16,7.5,7.5 这里大小发生了变化,但是我觉得这不是问题后面还
#有四舍五入
#我也没有太明白这里 但是感觉这点对结果不会有太大影响
#前面的base_anchor那里也加了1不知道为什么
#不太恰当的知识:我读过其他新的目标检测论文有那个双线性插值法去解决一些和这种类似的问题
#我了解也不多,大家有兴趣可以看看
w = anchor[2] - anchor[0] + 1
h = anchor[3] - anchor[1] + 1
x_ctr = anchor[0] + 0.5 * (w - 1)
y_ctr = anchor[1] + 0.5 * (h - 1)
return w, h, x_ctr, y_ctr
def _mkanchors(ws, hs, x_ctr, y_ctr):
"""
Given a vector of widths (ws) and heights (hs) around a center
(x_ctr, y_ctr), output a set of anchors (windows).
"""
#这个函数是通过中心坐标和宽高得到左上角和右下角坐标和上面那个刚好反过来
#例如输入16,16,7.5,7.5
#得到 0,0,15,15
ws = ws[:, np.newaxis]
hs = hs[:, np.newaxis]
#np.newaxis的功能:增加一个新维度在np.newaxis所在的位置
#np.hstack:在水平方向平铺
anchors = np.hstack((x_ctr - 0.5 * (ws - 1),
y_ctr - 0.5 * (hs - 1),
x_ctr + 0.5 * (ws - 1),
y_ctr + 0.5 * (hs - 1)))
return anchors
def _ratio_enum(anchor, ratios):
"""
Enumerate a set of anchors for each aspect ratio wrt an anchor.
"""
#这个函数是通过一个基础anchor就是一个1:1的得到三个不同高宽比的anchors
w, h, x_ctr, y_ctr = _whctrs(anchor)
#先用_whctrs函数把用左上角和右下角坐标表示的anchor转换为用中点和高宽表示
size = w * h
size_ratios = size / ratios
ws = np.round(np.sqrt(size_ratios))
hs = np.round(ws * ratios)
#这里用了取整,我觉得这个可能影响不大,因为后面还有一系列操作,回归啥的
anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
#这里再通过调用_mkanchor函数把表示方法转换回来
#最终得到一组不同高宽比的anchor但注意这里的大小是一样的,也就是scales是一样的
#所以这里输入一个anchor输出应该是三个anchor
return anchors
def _scale_enum(anchor, scales):
"""
Enumerate a set of anchors for each scale wrt an anchor.
"""
#这个函数是通过一个基础anchor得到三个不同尺度的anchors
w, h, x_ctr, y_ctr = _whctrs(anchor)
#这里同样先转换下表示方法
ws = w * scales#中心位置不变高和宽变一下就行
hs = h * scales
anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
#这里再通过调用_mkanchor函数把表示方法转换回来
#最终得到一组不同尺度的anchor
return anchors
if __name__ == '__main__':
import time
t = time.time()#这个节点的时间
a = generate_anchors()
print(time.time() - t)#用现在这个节点时间减去上面那个得到运行时间
print(a)
from IPython import embed; embed()#这里如果没有装ipython会报错可以用conda装一下