老卫带你学---faster-rcnn源码剖析(generate_anchor.py运算解读2) 【深度学习:目标检测】 faster rcnn RPN之anchor(generate_anchors)源码解析

【深度学习:目标检测】 faster rcnn RPN之anchor(generate_anchors)源码解析

转载:http://blog.csdn.net/xzzppp/article/details/52317863

英文原文:faste rcnn。其中生成RPN(Regional proposal network)的Python代码解析

本代码主要用于:生成尺度为:128,256,512; 宽高比为:1:2,1:1,2:1的anchor

[python]  view plain  copy




  1. <span style=“font-size:24px;”>#功能描述:生成多尺度、多宽高比的anchors。  

  2. #          尺度为:128,256,512; 宽高比为:1:2,1:1,2:1  

  3.   

  4. import numpy as np  #提供矩阵运算功能的库  

  5.   

  6. #生成anchors总函数:ratios为一个列表,表示宽高比为:1:2,1:1,2:1  

  7. #2**x表示:2^x,scales:[2^3 2^4 2^5],即:[8 16 32]  

  8. def generate_anchors(base_size=16, ratios=[0.512],  

  9.                      scales=2**np.arange(36)):  

  10.     ”“” 

  11.     Generate anchor (reference) windows by enumerating aspect ratios X 

  12.     scales wrt a reference (0, 0, 15, 15) window. 

  13.     ”“”  

  14.     base_anchor = np.array([11, base_size, base_size]) - 1  #新建一个数组:base_anchor:[0 0 15 15]  

  15.     ratio_anchors = _ratio_enum(base_anchor, ratios)  #枚举各种宽高比  

  16.     anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)  #枚举各种尺度,vstack:竖向合并数组  

  17.                          for i in xrange(ratio_anchors.shape[0])]) #shape[0]:读取矩阵第一维长度,其值为3  

  18.     return anchors  

  19.   

  20. #用于返回width,height,(x,y)中心坐标(对于一个anchor窗口)  

  21. def _whctrs(anchor):  

  22.     ”“” 

  23.     Return width, height, x center, and y center for an anchor (window). 

  24.     ”“”  

  25.     #anchor:存储了窗口左上角,右下角的坐标  

  26.     w = anchor[2] - anchor[0] + 1  

  27.     h = anchor[3] - anchor[1] + 1  

  28.     x_ctr = anchor[0] + 0.5 * (w - 1)  #anchor中心点坐标  

  29.     y_ctr = anchor[1] + 0.5 * (h - 1)  

  30.     return w, h, x_ctr, y_ctr  

  31.   

  32. #给定一组宽高向量,输出各个anchor,即预测窗口,输出anchor的面积相等,只是宽高比不同  

  33. def _mkanchors(ws, hs, x_ctr, y_ctr):  

  34.     #ws:[23 16 11],hs:[12 16 22],ws和hs一一对应。  

  35.     ”“” 

  36.     Given a vector of widths (ws) and heights (hs) around a center 

  37.     (x_ctr, y_ctr), output a set of anchors (windows). 

  38.     ”“”  

  39.     ws = ws[:, np.newaxis]  #newaxis:将数组转置  

  40.     hs = hs[:, np.newaxis]  

  41.     anchors = np.hstack((x_ctr - 0.5 * (ws - 1),    #hstack、vstack:合并数组  

  42.                          y_ctr - 0.5 * (hs - 1),    #anchor:[[-3.5 2 18.5 13]  

  43.                          x_ctr + 0.5 * (ws - 1),     #        [0  0  15  15]  

  44.                          y_ctr + 0.5 * (hs - 1)))     #       [2.5 -3 12.5 18]]  

  45.     return anchors  

  46.   

  47. #枚举一个anchor的各种宽高比,anchor[0 0 15 15],ratios[0.5,1,2]  

  48. def _ratio_enum(anchor, ratios):  

  49.     ”“”   列举关于一个anchor的三种宽高比 1:2,1:1,2:1 

  50.     Enumerate a set of anchors for each aspect ratio wrt an anchor. 

  51.     ”“”  

  52.   

  53.     w, h, x_ctr, y_ctr = _whctrs(anchor)  #返回宽高和中心坐标,w:16,h:16,x_ctr:7.5,y_ctr:7.5  

  54.     size = w * h   #size:16*16=256  

  55.     size_ratios = size / ratios  #256/ratios[0.5,1,2]=[512,256,128]  

  56.     #round()方法返回x的四舍五入的数字,sqrt()方法返回数字x的平方根  

  57.     ws = np.round(np.sqrt(size_ratios)) #ws:[23 16 11]  

  58.     hs = np.round(ws * ratios)    #hs:[12 16 22],ws和hs一一对应。as:23&12  

  59.     anchors = _mkanchors(ws, hs, x_ctr, y_ctr)  #给定一组宽高向量,输出各个预测窗口  

  60.     return anchors  

  61.   

  62. #枚举一个anchor的各种尺度,以anchor[0 0 15 15]为例,scales[8 16 32]  

  63. def _scale_enum(anchor, scales):  

  64.     ”“”   列举关于一个anchor的三种尺度 128*128,256*256,512*512 

  65.     Enumerate a set of anchors for each scale wrt an anchor. 

  66.     ”“”  

  67.     w, h, x_ctr, y_ctr = _whctrs(anchor) #返回宽高和中心坐标,w:16,h:16,x_ctr:7.5,y_ctr:7.5  

  68.     ws = w * scales   #[128 256 512]  

  69.     hs = h * scales   #[128 256 512]  

  70.     anchors = _mkanchors(ws, hs, x_ctr, y_ctr) #[[-56 -56 71 71] [-120 -120 135 135] [-248 -248 263 263]]  

  71.     return anchors  

  72.   

  73. if name == main:  #主函数  

  74.     import time  

  75.     t = time.time()  

  76.     a = generate_anchors()  #生成anchor(窗口)  

  77.     print time.time() - t   #显示时间  

  78.     print a  

  79.     from IPython import embed; embed()  

  80. </span>  

       其中,_ratio_enum()部分生成三种宽高比 1:2,1:1,2:1的anchor如下图所示:
       其中,_scale_enum()部分,生成三种尺寸的anchor,以_ratio_enum()部分生成的anchor[0 0 15 15]为例,扩展了三种尺度 128*128,256*256,512*512,如下图所示:

转载:http://blog.csdn.net/xzzppp/article/details/52317863

英文原文:faste rcnn。其中生成RPN(Regional proposal network)的Python代码解析

本代码主要用于:生成尺度为:128,256,512; 宽高比为:1:2,1:1,2:1的anchor

[python]  view plain  copy




  1. <span style=“font-size:24px;”>#功能描述:生成多尺度、多宽高比的anchors。  

  2. #          尺度为:128,256,512; 宽高比为:1:2,1:1,2:1  

  3.   

  4. import numpy as np  #提供矩阵运算功能的库  

  5.   

  6. #生成anchors总函数:ratios为一个列表,表示宽高比为:1:2,1:1,2:1  

  7. #2**x表示:2^x,scales:[2^3 2^4 2^5],即:[8 16 32]  

  8. def generate_anchors(base_size=16, ratios=[0.512],  

  9.                      scales=2**np.arange(36)):  

  10.     ”“” 

  11.     Generate anchor (reference) windows by enumerating aspect ratios X 

  12.     scales wrt a reference (0, 0, 15, 15) window. 

  13.     ”“”  

  14.     base_anchor = np.array([11, base_size, base_size]) - 1  #新建一个数组:base_anchor:[0 0 15 15]  

  15.     ratio_anchors = _ratio_enum(base_anchor, ratios)  #枚举各种宽高比  

  16.     anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)  #枚举各种尺度,vstack:竖向合并数组  

  17.                          for i in xrange(ratio_anchors.shape[0])]) #shape[0]:读取矩阵第一维长度,其值为3  

  18.     return anchors  

  19.   

  20. #用于返回width,height,(x,y)中心坐标(对于一个anchor窗口)  

  21. def _whctrs(anchor):  

  22.     ”“” 

  23.     Return width, height, x center, and y center for an anchor (window). 

  24.     ”“”  

  25.     #anchor:存储了窗口左上角,右下角的坐标  

  26.     w = anchor[2] - anchor[0] + 1  

  27.     h = anchor[3] - anchor[1] + 1  

  28.     x_ctr = anchor[0] + 0.5 * (w - 1)  #anchor中心点坐标  

  29.     y_ctr = anchor[1] + 0.5 * (h - 1)  

  30.     return w, h, x_ctr, y_ctr  

  31.   

  32. #给定一组宽高向量,输出各个anchor,即预测窗口,输出anchor的面积相等,只是宽高比不同  

  33. def _mkanchors(ws, hs, x_ctr, y_ctr):  

  34.     #ws:[23 16 11],hs:[12 16 22],ws和hs一一对应。  

  35.     ”“” 

  36.     Given a vector of widths (ws) and heights (hs) around a center 

  37.     (x_ctr, y_ctr), output a set of anchors (windows). 

  38.     ”“”  

  39.     ws = ws[:, np.newaxis]  #newaxis:将数组转置  

  40.     hs = hs[:, np.newaxis]  

  41.     anchors = np.hstack((x_ctr - 0.5 * (ws - 1),    #hstack、vstack:合并数组  

  42.                          y_ctr - 0.5 * (hs - 1),    #anchor:[[-3.5 2 18.5 13]  

  43.                          x_ctr + 0.5 * (ws - 1),     #        [0  0  15  15]  

  44.                          y_ctr + 0.5 * (hs - 1)))     #       [2.5 -3 12.5 18]]  

  45.     return anchors  

  46.   

  47. #枚举一个anchor的各种宽高比,anchor[0 0 15 15],ratios[0.5,1,2]  

  48. def _ratio_enum(anchor, ratios):  

  49.     ”“”   列举关于一个anchor的三种宽高比 1:2,1:1,2:1 

  50.     Enumerate a set of anchors for each aspect ratio wrt an anchor. 

  51.     ”“”  

  52.   

  53.     w, h, x_ctr, y_ctr = _whctrs(anchor)  #返回宽高和中心坐标,w:16,h:16,x_ctr:7.5,y_ctr:7.5  

  54.     size = w * h   #size:16*16=256  

  55.     size_ratios = size / ratios  #256/ratios[0.5,1,2]=[512,256,128]  

  56.     #round()方法返回x的四舍五入的数字,sqrt()方法返回数字x的平方根  

  57.     ws = np.round(np.sqrt(size_ratios)) #ws:[23 16 11]  

  58.     hs = np.round(ws * ratios)    #hs:[12 16 22],ws和hs一一对应。as:23&12  

  59.     anchors = _mkanchors(ws, hs, x_ctr, y_ctr)  #给定一组宽高向量,输出各个预测窗口  

  60.     return anchors  

  61.   

  62. #枚举一个anchor的各种尺度,以anchor[0 0 15 15]为例,scales[8 16 32]  

  63. def _scale_enum(anchor, scales):  

  64.     ”“”   列举关于一个anchor的三种尺度 128*128,256*256,512*512 

  65.     Enumerate a set of anchors for each scale wrt an anchor. 

  66.     ”“”  

  67.     w, h, x_ctr, y_ctr = _whctrs(anchor) #返回宽高和中心坐标,w:16,h:16,x_ctr:7.5,y_ctr:7.5  

  68.     ws = w * scales   #[128 256 512]  

  69.     hs = h * scales   #[128 256 512]  

  70.     anchors = _mkanchors(ws, hs, x_ctr, y_ctr) #[[-56 -56 71 71] [-120 -120 135 135] [-248 -248 263 263]]  

  71.     return anchors  

  72.   

  73. if name == main:  #主函数  

  74.     import time  

  75.     t = time.time()  

  76.     a = generate_anchors()  #生成anchor(窗口)  

  77.     print time.time() - t   #显示时间  

  78.     print a  

  79.     from IPython import embed; embed()  

  80. </span>  

       其中,_ratio_enum()部分生成三种宽高比 1:2,1:1,2:1的anchor如下图所示:
       其中,_scale_enum()部分,生成三种尺寸的anchor,以_ratio_enum()部分生成的anchor[0 0 15 15]为例,扩展了三种尺度 128*128,256*256,512*512,如下图所示:

猜你喜欢

转载自blog.csdn.net/yixieling4397/article/details/82716221