na, nt = self.na, targets.shape[0]
tcls, tbox, indices, anch = [], [], [], []
gain = torch.ones(7, device=targets.device)
ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt)
Out[3]:
tensor([[0., 0., 0.],
[1., 1., 1.],
[2., 2., 2.]], device='cuda:0')
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2)
Out[4]:
tensor([[[0.00000, 0.00000, 0.58192, 0.16796, 0.26108, 0.08724, 0.00000],
[1.00000, 0.00000, 0.54517, 0.33744, 0.06395, 0.02632, 0.00000],
[1.00000, 0.00000, 0.96964, 0.42483, 0.06071, 0.05264, 0.00000]],
[[0.00000, 0.00000, 0.58192, 0.16796, 0.26108, 0.08724, 1.00000],
[1.00000, 0.00000, 0.54517, 0.33744, 0.06395, 0.02632, 1.00000],
[1.00000, 0.00000, 0.96964, 0.42483, 0.06071, 0.05264, 1.00000]],
[[0.00000, 0.00000, 0.58192, 0.16796, 0.26108, 0.08724, 2.00000],
[1.00000, 0.00000, 0.54517, 0.33744, 0.06395, 0.02632, 2.00000],
[1.00000, 0.00000, 0.96964, 0.42483, 0.06071, 0.05264, 2.00000]]], device='cuda:0')
g = 0.5
off = torch.tensor([[0, 0],
[1, 0], [0, 1], [-1, 0], [0, -1],
], device=targets.device).float() * g
Out[5]:
tensor([[ 0.00000, 0.00000],
[ 0.50000, 0.00000],
[ 0.00000, 0.50000],
[-0.50000, 0.00000],
[ 0.00000, -0.50000]], device='cuda:0')
for i in range(self.nl):
"""
p[i].shape = (b, 3, h, w,nc+5)
gain = [1, 1, w, h, w, h, 1]
"""
anchors = self.anchors[i]
Out[7]:
tensor([[1.25000, 1.62500],
[2.00000, 3.75000],
[4.12500, 2.87500]], device='cuda:0')
gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]]
Out[8]: tensor([ 1., 1., 64., 64., 64., 64., 1.], device='cuda:0')
t = targets * gain
Out[9]:
tensor([[[ 0.00000, 0.00000, 37.24281, 10.74930, 16.70916, 5.58366, 0.00000],
[ 1.00000, 0.00000, 34.89063, 21.59622, 4.09269, 1.68436, 0.00000],
[ 1.00000, 0.00000, 62.05726, 27.18916, 3.88548, 3.36872, 0.00000]],
[[ 0.00000, 0.00000, 37.24281, 10.74930, 16.70916, 5.58366, 1.00000],
[ 1.00000, 0.00000, 34.89063, 21.59622, 4.09269, 1.68436, 1.00000],
[ 1.00000, 0.00000, 62.05726, 27.18916, 3.88548, 3.36872, 1.00000]],
[[ 0.00000, 0.00000, 37.24281, 10.74930, 16.70916, 5.58366, 2.00000],
[ 1.00000, 0.00000, 34.89063, 21.59622, 4.09269, 1.68436, 2.00000],
[ 1.00000, 0.00000, 62.05726, 27.18916, 3.88548, 3.36872, 2.00000]]], device='cuda:0')
if nt:
r = t[:, :, 4:6] / anchors[:, None]
Out[10]:
tensor([[[13.36733, 3.43610],
[ 3.27415, 1.03653],
[ 3.10838, 2.07306]],
[[ 8.35458, 1.48897],
[ 2.04635, 0.44916],
[ 1.94274, 0.89833]],
[[ 4.05071, 1.94214],
[ 0.99217, 0.58587],
[ 0.94193, 1.17173]]], device='cuda:0')
j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t']
Out[12]:
tensor([[False, True, True],
[False, True, True],
[False, True, True]], device='cuda:0')
t = t[j]
Out[14]:
tensor([[ 1.00000, 0.00000, 34.89063, 21.59622, 4.09269, 1.68436, 0.00000],
[ 1.00000, 0.00000, 62.05726, 27.18916, 3.88548, 3.36872, 0.00000],
[ 1.00000, 0.00000, 34.89063, 21.59622, 4.09269, 1.68436, 1.00000],
[ 1.00000, 0.00000, 62.05726, 27.18916, 3.88548, 3.36872, 1.00000],
[ 1.00000, 0.00000, 34.89063, 21.59622, 4.09269, 1.68436, 2.00000],
[ 1.00000, 0.00000, 62.05726, 27.18916, 3.88548, 3.36872, 2.00000]], device='cuda:0')
gxy = t[:, 2:4]
gxi = gain[[2, 3]] - gxy
j, k = ((gxy % 1. < g) & (gxy > 1.)).T
l, m = ((gxi % 1. < g) & (gxi > 1.)).T
j = torch.stack((torch.ones_like(j), j, k, l, m))
t = t.repeat((5, 1, 1))[j]
原文