Skip to content

Commit

Permalink
zero-target training bug fix (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher committed Aug 5, 2020
1 parent d0d3dd1 commit 7eaf225
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,7 @@ def compute_loss(p, targets, model): # predictions, targets, model
s = 3 / np # output count scaling
lbox *= h['giou'] * s
lobj *= h['obj'] * s * (1.4 if np == 4 else 1.)
if model.nc > 1:
lcls *= h['cls'] * s
lcls *= h['cls'] * s
bs = tobj.shape[0] # batch size

loss = lbox + lobj + lcls
Expand All @@ -524,7 +523,7 @@ def build_targets(p, targets, model):
gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain

# Match targets to anchors
t, offsets = targets * gain, 0
t = targets * gain
if nt:
# Matches
r = t[:, :, 4:6] / anchors[:, None] # wh ratio
Expand All @@ -540,6 +539,9 @@ def build_targets(p, targets, model):
j = torch.stack((torch.ones_like(j), j, k, l, m))
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
Expand Down

0 comments on commit 7eaf225

Please sign in to comment.