1 anchors
核心代码
def textbox_anchor_one_layer(img_shape,
feat_size,
ratios,
scale,
sizes,
offset = 0.5,
dtype=np.float32):
"""
#这个是用来产生一层的anchor
#当img_shape [300,300]
#feat_size=(30,60) 只是下面的第一层对应的述职
anchor_sizes=[(30., 60.),
(60., 114.),
(114., 168.),
(168., 222.),
(222., 276.),
(276., 330.)],
ratios = (1,2,3,5,7,10)
sizes =[38,38] 第一层映射的map尺寸 总共有[(38,38)(19,19)(10,10)(5,5)(3,3)(1,1)]6层
scale没用到
"""
y, x = np.mgrid[0:feat_size[0], 0:feat_size[1]]
y = (y.astype(dtype) + offset) / feat_size[0]
x = (x.astype(dtype) + offset) / feat_size[1]
y_offset = y + offset/feat_size[0]
x_offset = x
x_out = np.stack((x, x_offset), -1)
y_out = np.stack((y, y_offset), -1)
y_out = np.expand_dims(y_out, axis=-1)
x_out = np.expand_dims(x_out, axis=-1)
numofanchor = len(ratios) + 1
h = np.zeros((numofanchor, ), dtype=dtype)
w = np.zeros((numofanchor, ), dtype=dtype)
for i, r in enumerate(ratios):
h[i] = sizes[0] / img_shape[0] / math.sqrt(r)
w[i] = sizes[0] / img_shape[1] * math.sqrt(r)
h[i] = sizes[0] / img_shape[0] / 3.
w[i] = sizes[0] / img_shape[1] * 1.6
if feat_size[0] == 38:
h[i+1] = sizes[0] / img_shape[0] / 4.
w[i+1] = sizes[0] / img_shape[1] / 2.
else:
h[i+1] = sizes[0] / img_shape[0]
w[i+1] = sizes[0] / img_shape[1] * 2
return y_out, x_out, h, w
"""
y_out [38,38,2,1]表示y轴(0维)上坐标值对应的比值,2表示两部分,一部分没有偏移,一部分+0.5/feat_size[0]
x_out [38,38,2,1]表示x轴(1维)上坐标值对应的比值,2表示两部分的值一样
h = [7] #表示7种高度
w = [7] #表示7种宽度
"""
一共有6层,所以根据下面的代码计算anchor
def textbox_achor_all_layers(img_shape,
layers_shape,
anchor_ratios,
scales,
anchor_sizes,
offset=0.5,
dtype=np.float32):
"""
Compute anchor boxes for all feature layers.
"""
layers_anchors = []
for i, s in enumerate(layers_shape):
anchor_bboxes = textbox_anchor_one_layer(img_shape, s,
anchor_ratios,
scales[i],
anchor_sizes[i],
offset=offset, dtype=dtype)
layers_anchors.append(anchor_bboxes)
return layers_anchors
anchors 的值含有6个值得list
[
([38,38,2,1],[38,38,2,1],[7],[7]) , #其中的数值代表结构
([19,19,2,1],[19,19,2,1],[7],[7])
([10,10,2,1],[10,10,2,1],[7],[7])
([5,5,2,1],[5,5,2,1],[7],[7])
([3,3,2,1],[3,3,2,1],[7],[7])
([1,1,2,1],[1,1,2,1],[7],[7])
]
2 图像处理后的image, labels, bboxes