zoukankan      html  css  js  c++  java
  • faster-rcnn anchor 介绍

    anchors 值的含义为
    在 feature maps 上进行滑窗操作(sliding window). 滑窗尺寸为 n×n, 如 3×3. 
    对于每个滑窗, 会生成 9 个 anchors, anchors 具有相同的中心 center=xa,center=ya, 但 anchors 具有 3 种不同的长宽比(aspect ratios) 和 3 种不同的尺度(scales)。 最终,9个anchor的四个数值分别代表矩形框的左下角x,y,右上角x,y。计算是相对于原始图片尺寸的.

    anchor的值是由如下步骤计算得来的:
     

    主要包括两步:

    保持 anchor 面积固定不变, 改变长宽比 aspect ratio = [0.5, 1, 2] (函数_ratio_enum(anchor, ratios))

    保持 anchor 长宽比固定不变,缩放尺度 scale = [8,16,32] (函数_scale_enum(anchor, scales))

    1. 先设置基础anchor,base=16,base_anchor为[0,0,15,15],anchor中心(7.5,7.5),以及面积(size=256)。
    2. 计算基础anchor的面积,分别除以ratio[0.5,1,2],得到  [512,256,128]
    3. anchor的宽度w由三个面积的平方根值确定,得到 w[23,16,11]
    4. anchor的高度h由[23,16,11]*[0.5,1,2]确定,得到 h[12,16,22]。
    5. 由anchor的中心以及不同的宽和高可以得到此时的anchors。此处为[-3.5, 2. , 18.5, 13. ], [ 0. , 0. , 15. , 15. ], [ 2.5, -3. , 12.5, 18. ]

    例 xa=7.5, ya=7.5,w=23,h=12,

    x_left = xa-(w-1)/2=-3.5, y_left = ya-(h-1)/2 = 2,

    x_right = xa+(w-1)/2 = 18.5, y_right = ya+(h-1)/2=13

    6. 利用三种不同的scales[8,16,32]分别去扩大anchors,扩大的方法是先计算出来上一步的anchor的中心以及宽高,使宽高分别乘以scale,然后再利用中心和新的宽高计算出最终所要的anchors

    例如:  xa=7.5, ya=7.5, w=23*8, h=12*8, x_min=xa-(w*scale-1)/2 = 7.5-(23*8-1)/2=84 ==>   -83   -39   100    56 
     

    """
    generate_anchors.py
    """
    import numpy as np
     
    # 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.]])
     
     
    def generate_anchors(stride=16, sizes=(32, 64, 128, 256, 512), aspect_ratios=(0.5, 1, 2)):
        """
        生成 anchor boxes 矩阵,其格式为 (x1, y1, x2, y2).
        Anchors 是以 stride / 2 的中心,逼近指定大小的平方根面积(sqrt areas),长宽比
        Anchors are centered on stride / 2, have (approximate) sqrt areas of the specified
        sizes, and aspect ratios as given.
        """
        return _generate_anchors(stride,
                                 np.array(sizes, dtype=np.float) / stride,
                                 np.array(aspect_ratios, dtype=np.float) )
     
     
    def _generate_anchors(base_size, scales, aspect_ratios):
        """
        通过枚举关于参考窗口window (0, 0, base_size - 1, base_size - 1) 的长宽比(aspect ratios) X scales,
        来生成 anchore 窗口(参考窗口 reference windows).
        """
        anchor = np.array([1, 1, base_size, base_size], dtype=np.float) - 1
        anchors = _ratio_enum(anchor, aspect_ratios)
        anchors = np.vstack([_scale_enum(anchors[i, :], scales) for i in range(anchors.shape[0])])
        return anchors
     
     
    def _whctrs(anchor):
        """
        返回 anchor 窗口的 width, height, x center,  y center.
        """
        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):
        """
        给定 center(x_ctr, y_ctr) 及 widths (ws),heights (hs) 向量,输出 anchors窗口window 集合.
        """
        ws = ws[:, np.newaxis]
        hs = hs[:, np.newaxis]
        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):
        """
        对于每个关于一个 anchor 的长宽比aspect ratio,枚举 anchors 集合.
        """
        w, h, x_ctr, y_ctr = _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)
        return anchors
     
     
    def _scale_enum(anchor, scales):
        """
        对于每个关于一个 anchor 的尺度scale,枚举 anchors 集合.
        Enumerate a set of anchors for each scale wrt an anchor."""
        w, h, x_ctr, y_ctr = _whctrs(anchor)
        ws = w * scales
        hs = h * scales
        anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
        return anchors
     
     
    if __name__ == '__main__':
        print 'Anchor Generating ...'
     
        anchors = generate_anchors()
        print anchors
     
        print 'Done.'

    参考:

    https://blog.csdn.net/qq_26974871/article/details/81251851

    https://blog.csdn.net/zziahgf/article/details/79895804

    https://blog.csdn.net/wphkadn/article/details/87915495

  • 相关阅读:
    nginx.conf 配置解析之 http配置
    nginx.conf 配置解析之 events配置
    nginx.conf 配置解析之 全局配置
    nginx.conf 配置解析之文件结构
    centos7 编译安装nginx1.16.0( 完整版 )
    SQL注入是什么?如何防止?
    数据库的脏读、不可重复读、幻读以及不可重复读和幻读的区别
    唯一索引与主键索引的比较
    分布式session的几种实现方式
    反向代理和负载均衡
  • 原文地址:https://www.cnblogs.com/fclbky/p/12925423.html
Copyright © 2011-2022 走看看