in utils/loss.py [0:0]
def build_patch_targets(self, patch_offsets, targets, imgsz): # for fast-mode, fixed patch division
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
na, nt = self.na, targets.shape[0] # number of anchors, targets
dtype, device = targets.dtype, targets.device
tcls, tbox, indices, anch = [], [], [], []
bs, _, height, width = imgsz
gain = torch.ones(7, device=device) # normalized to gridspace gain
ai = torch.arange(na, device=device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt)
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices, shape(na,nt,7)
bi_ = torch.arange(patch_offsets[0].shape[0], device=device)
g = 0.5 # bias
off = torch.tensor([[0, 0],
[1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
], device=device).float() * g # offsets
for i in range(self.nl):
patch_off = patch_offsets[i]
anchors = self.anchors[i]
r = (2 ** (i - 1)) if self.nl == 4 else 2 ** i
gain[2:6] = torch.tensor([width, height, width, height], dtype=dtype) / (8 * r) # TODO: from 4 to 32
# grid_w, grid_h = patch_off[0, [3, 4]] - patch_off[0, [1, 2]]
grid_wh = patch_off[:1, [3, 4]] - patch_off[:1, [1, 2]]
# Match targets to anchors
t = targets * gain
if nt:
# Matches
r = t[:, :, 4:6] / anchors[:, None] # wh ratio
j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t'] # compare
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
t = t[j] # filter, shape(nt_, 7)
tb, txc, tyc = t[:, [0, 2, 3]].chunk(3, dim=1) # shape(n,1)
pb, px1, py1, px2, py2 = (patch_off.T).chunk(5, dim=0) # shape(1,m)
contained = (tb == pb) & (txc > px1 - g) & (txc < px2 - g) & (tyc > py1 - g) & (tyc < py2 - g) # shape(n,m)
ti, pj = torch.nonzero(contained).T # i-th target is contained within j-th patch
t = t[ti] # shape(n,7)
# Offsets
gxy = t[:, 2:4] # grid xy
gxi = grid_wh - gxy # inverse
j, k = ((gxy - gxy.floor() < g) & (gxy > 0.-g)).T
l, m = ((gxi - gxi.floor() < g) & (gxi > 1.-g)).T
# 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[:, 0] = bi_[pj] # converted batch-indices
t[:, 2:4] -= patch_off[pj, 1:3] # converted xc, yc (minus px1, py1)
t = t.repeat((5, 1, 1))[j]
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
else:
t = targets[0]
offsets = 0
# Define
b, c = t[:, :2].long().T # image, class
gxy = t[:, 2:4] # grid xy
gwh = t[:, 4:6] # grid wh
gij = (gxy - offsets).long()
gi, gj = gij.T # grid xy indices
# Append
a = t[:, 6].long() # anchor indices
# assert ((gj >= 0) & (gj <= grid_wh[0,1] - 1) & (gi >= 0) & (gi <= grid_wh[0,0] - 1)).all()
# indices.append((b, a, gj.clamp_(0, grid_wh[0,1] - 1), gi.clamp_(0, grid_wh[0,0] - 1))) # image, anchor, grid indices
indices.append((b, a, gj, gi)) # image, anchor, grid indices
tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
anch.append(anchors[a]) # anchors
tcls.append(c) # class
return tcls, tbox, indices, anch