锚框:以每一个像素点为中心生成多个大小和宽高比不同的边界框
图像 高h , 宽w
大小为s∈(0,1] 且 宽高比为r>0
锚框的高 h1 = hs/√r
锚框的宽 w1 = ws√r
s1,...,sn 与 r1,... ,rm 这样一张图的锚框有 whnm 个
但对于一点来说,我们感兴趣的框有:(s1,r1),(s1,r2)......(s1,rm),(s2,r1),......(sn,r1)
假如 w=416 h=416 s=[0.75,0.5,0.25] r=[1,2,,0.5] 在 (200,200 )处生成5个框:
[44, 44, 356, 356], [96, 96, 304, 304], [148, 148, 252, 252], [0, 90, 416, 310], [90, 0, 310, 416]
accard系数(Jaccard index)可以衡量两个集合的相似度。给定集合A和B,它们的Jaccard系数即二者交集大小除以二者并集大小:
J(A,B)=|A∩B|/|A∪B|
import math
import numpy as np
w = 416
h = 416
s = [0.75, 0.5, 0.25]
r = [1, 2, 0.5]
def create_box(x, y,imgw, imgh, sizes, ratios):
# nn = int(len(sizes) + len(ratios) - 1)
size_1 = sizes[0]
ratio_1 = ratios[0]
ratio_2 = ratios[1:]
x = x / imgw
y = y / imgh
list_1 = [[
x - i * math.sqrt(ratio_1) / 2,
y - i / (2 * math.sqrt(ratio_1)),
x + i * math.sqrt(ratio_1) / 2,
y + i / (2 * math.sqrt(ratio_1))
] for i in sizes]
list_2 = [[
x - size_1 * math.sqrt(i) / 2,
y - size_1 / (2 * math.sqrt(i)),
x + size_1 * math.sqrt(i) / 2,
y + size_1 / (2 * math.sqrt(i))
] for i in ratio_2]
list_1.extend(list_2)
list_all = []
for l in list_1:
if l[0] < 0:
l[0] = 0
else:
l[0] = round(l[0] * imgw)
if l[1] < 0:
l[1] = 0
else:
l[1] = round(l[1] * imgh)
if l[2] > 1:
l[2] = imgw
else:
l[2] = round(l[2] * imgw)
if l[3] > 1:
l[3] = imgh
else:
l[3] = round(l[3] * imgh)
list_all.append(l)
return list_all
if __name__ == '__main__':
print(create_box(200, 200, w, h, s, r))