1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
import numpy as np
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.
:param base_size:输入的anchors的基准大小,所有的anchors都给予这个基准大小产生。 :param ratios:表示需要产生的anchors的纵横比,通常是含有多个纵横比数值的list。 :param scales:表示需要产生的anchors的尺寸,通常是含有多个数值的list,这些数值会将生成的anchors按照一定的比例饿缩放(保持anchors的中心位置不变)。 :return:返回产生的anchors,是一个二维的numpy数组,每一行表示一个anchor。 """
base_anchor = np.array([1, 1, base_size, base_size]) - 1
ratio_anchors = _ratio_enum(base_anchor, ratios)
anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales) for i in xrange(ratio_anchors.shape[0])])
return anchors
def _whctrs(anchor): """ Return width, height, x center, and y center for an anchor (window). :param anchor: 通常是一个长度为4的list或者numpy数组,表示需要获取宽度,高度和中心坐标的anchor。 :return: 该函数返回四个数值,分别表示anchor宽度 w,高度 h,中心的x坐标 x_ctr, 中心的y坐标 y_ctr。 """
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). :param ws: 宽度的集合,通常是一个list,里面的每一个元素表示一个anchor的宽度。 :param hs: 高度的集合,通常是一个list,里面的每一个元素表示一个anchor的高度。hs的长度需和ws的长度保持一致。 :param x_ctr: 中心点的x坐标 :param y_ctr: 中心点的y坐标 :return 根据以上的信息生成的anchors的集合,一个二维的numpy数组。 """
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): """ Enumerate a set of anchors for each aspect ratio wrt an anchor. :param anchor: 基准的anchor,是一个长度为4的list或者numpy数组。 :param ratios: 需要变换的纵横比,是一个由若干个浮点数组成的numpy数组。 :return: 返回经过变化之后的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): """ Enumerate a set of anchors for each scale wrt an anchor. :param anchor: 一个anchor,通常是一个长度为4的numpy数组。 :param scales: 需要缩放的尺寸,即比例系数,是一个由多个浮点数组成的list。 :return : 缩放之后的anchors的集合 """
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__': import time
t = time.time() a = generate_anchors() print time.time() - t print a
|