Faster RCNN 源码解析(3.2) -- Anchor 生成(generate_anchors.py)
来源:互联网 发布:php管理系统 编辑:程序博客网 时间:2024/06/07 06:58
基于源码的实现
generate_anchors.py
生成多尺度和多比例的锚点。这里由generate_anthors函数主要完成,可以看到,使用了 3 个尺度( 128, 256, and 512)以及 3 个比例(1:1,1:2,2:1)。一个锚点由w, h, x_ctr, y_ctr固定,也就是宽、高、x center和y center固定。
#功能描述:生成多尺度、多宽高比的anchors。 # 尺度为:128,256,512; 宽高比为:1:2,1:1,2:1 import numpy as np #提供矩阵运算功能的库 #生成anchors总函数:ratios为一个列表,表示宽高比为:1:2,1:1,2:1 #2**x表示:2^x,scales:[2^3 2^4 2^5],即:[8 16 32] def generate_anchors(base_size=16, ratios=[0.5, 1, 2], scales=2**np.arange(3, 6)): """ 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 #新建一个数组:base_anchor:[0 0 15 15] ratio_anchors = _ratio_enum(base_anchor, ratios) #枚举各种宽高比 anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales) #枚举各种尺度,vstack:竖向合并数组 for i in xrange(ratio_anchors.shape[0])]) #shape[0]:读取矩阵第一维长度,其值为3 return anchors #用于返回width,height,(x,y)中心坐标(对于一个anchor窗口) def _whctrs(anchor): """ Return width, height, x center, and y center for an anchor (window). """ #anchor:存储了窗口左上角,右下角的坐标 w = anchor[2] - anchor[0] + 1 h = anchor[3] - anchor[1] + 1 x_ctr = anchor[0] + 0.5 * (w - 1) #anchor中心点坐标 y_ctr = anchor[1] + 0.5 * (h - 1) return w, h, x_ctr, y_ctr #给定一组宽高向量,输出各个anchor,即预测窗口,**输出anchor的面积相等,只是宽高比不同** def _mkanchors(ws, hs, x_ctr, y_ctr): #ws:[23 16 11],hs:[12 16 22],ws和hs一一对应。 """ Given a vector of widths (ws) and heights (hs) around a center (x_ctr, y_ctr), output a set of anchors (windows). """ ws = ws[:, np.newaxis] #newaxis:将数组转置 hs = hs[:, np.newaxis] anchors = np.hstack((x_ctr - 0.5 * (ws - 1), #hstack、vstack:合并数组 y_ctr - 0.5 * (hs - 1), #anchor:[[-3.5 2 18.5 13] x_ctr + 0.5 * (ws - 1), # [0 0 15 15] y_ctr + 0.5 * (hs - 1))) # [2.5 -3 12.5 18]] return anchors #枚举一个anchor的各种宽高比,anchor[0 0 15 15],ratios[0.5,1,2] def _ratio_enum(anchor, ratios): """ 列举关于一个anchor的三种宽高比 1:2,1:1,2:1 Enumerate a set of anchors for each aspect ratio wrt an anchor. """ w, h, x_ctr, y_ctr = _whctrs(anchor) #返回宽高和中心坐标,w:16,h:16,x_ctr:7.5,y_ctr:7.5 size = w * h #size:16*16=256 size_ratios = size / ratios #256/ratios[0.5,1,2]=[512,256,128] #round()方法返回x的四舍五入的数字,sqrt()方法返回数字x的平方根 ws = np.round(np.sqrt(size_ratios)) #ws:[23 16 11] hs = np.round(ws * ratios) #hs:[12 16 22],ws和hs一一对应。as:23&12 anchors = _mkanchors(ws, hs, x_ctr, y_ctr) #给定一组宽高向量,输出各个预测窗口 return anchors #枚举一个anchor的各种尺度,以anchor[0 0 15 15]为例,scales[8 16 32] def _scale_enum(anchor, scales): """ 列举关于一个anchor的三种尺度 128*128,256*256,512*512 Enumerate a set of anchors for each scale wrt an anchor. """ w, h, x_ctr, y_ctr = _whctrs(anchor) #返回宽高和中心坐标,w:16,h:16,x_ctr:7.5,y_ctr:7.5 ws = w * scales #[128 256 512] hs = h * scales #[128 256 512] anchors = _mkanchors(ws, hs, x_ctr, y_ctr) #[[-56 -56 71 71] [-120 -120 135 135] [-248 -248 263 263]] return anchors if __name__ == '__main__': #主函数 import time t = time.time() a = generate_anchors() #生成anchor(窗口) print time.time() - t #显示时间 print a from IPython import embed; embed()
参考资料
generate_anchors.py from fastr-rcnn
generate_anchors.py analyze
阅读全文
1 0
- Faster RCNN 源码解析(3.2) -- Anchor 生成(generate_anchors.py)
- faster rcnn RPN之anchor(generate_anchors)源码解析
- Faster RCNN generate_anchors.py
- Faster RCNN-generate_anchors.py
- 【深度学习:目标检测】 faster rcnn RPN之anchor(generate_anchors)源码解析
- faster rcnn 源码解析之anchor_target_layer.py
- Faster RCNN 源码解析(3.3) -- proposal_layer.py
- Faster-Rcnn demo.py解析
- 【修改anchor】目标检测框架py-faster-rcnn修改anchor_box
- py-faster-rcnn demo.py解析
- py-faster-rcnn demo.py解析
- py-faster-rcnn源码解读系列
- py-faster-rcnn源码解读系列
- [目标检测] faster-rcnn demo.py 解析
- Faster-RCNN_TF代码解读18:generate_anchors.py
- Faster-RCNN Tensorflow版本源码解析(一):网络训练部分train_net.py
- Faster-RCNN Tensorflow版本源码解析(二)train_net.py所用到的函数
- 【深度学习:目标检测】 py-faster-rcnn demo.py解析
- [MYSQL-8]用通配符进行过滤
- Redis数据类型--List(列表)
- 【ORACLE】正常关闭时ORACLE如何根据SCN对数据库进行一致性判断
- 链表的归并排序 特殊优化, merge sort for list,非快慢指针法
- 用composer安装yii框架
- Faster RCNN 源码解析(3.2) -- Anchor 生成(generate_anchors.py)
- Python第二课
- Eclipse+PyDev安装及配置详解
- POJ 1840 Eqs
- Vue+Echarts
- zoj 3817 Chinese Knot(hash+暴力)
- 自己的第一天
- 虚拟机下的Linux不能上网
- 有序链表的重复元素删除