align loss to darknet

This commit is contained in:
Glenn Jocher 2018-09-25 01:30:51 +02:00
parent 208fd77fe4
commit c09dc09dba
1 changed files with 6 additions and 4 deletions

View File

@ -159,14 +159,16 @@ class YOLOLayer(nn.Module):
# Mask outputs to ignore non-existing objects (but keep confidence predictions) # Mask outputs to ignore non-existing objects (but keep confidence predictions)
nT = sum([len(x) for x in targets]) # number of targets nT = sum([len(x) for x in targets]) # number of targets
nM = mask.sum().float() # number of anchors (assigned to targets) nM = mask.sum().float() # number of anchors (assigned to targets)
# nB = len(targets) # batch size nB = len(targets) # batch size
k = 1 k = nM / nB
if nM > 0: if nM > 0:
lx = k * MSELoss(x[mask], tx[mask]) lx = k * MSELoss(x[mask], tx[mask])
ly = k * MSELoss(y[mask], ty[mask]) ly = k * MSELoss(y[mask], ty[mask])
lw = k * MSELoss(w[mask], tw[mask]) lw = k * MSELoss(w[mask], tw[mask])
lh = k * MSELoss(h[mask], th[mask]) lh = k * MSELoss(h[mask], th[mask])
lconf = k * BCEWithLogitsLoss(pred_conf[mask], mask[mask].float())
# lconf = k * BCEWithLogitsLoss(pred_conf[mask], mask[mask].float())
lconf = k * BCEWithLogitsLoss(pred_conf, mask.float())
# lcls = k * CrossEntropyLoss(pred_cls[mask], torch.argmax(tcls, 1)) # lcls = k * CrossEntropyLoss(pred_cls[mask], torch.argmax(tcls, 1))
lcls = k * BCEWithLogitsLoss(pred_cls[mask], tcls.float()) lcls = k * BCEWithLogitsLoss(pred_cls[mask], tcls.float())
@ -174,7 +176,7 @@ class YOLOLayer(nn.Module):
lx, ly, lw, lh, lcls, lconf = FT([0]), FT([0]), FT([0]), FT([0]), FT([0]), FT([0]) lx, ly, lw, lh, lcls, lconf = FT([0]), FT([0]), FT([0]), FT([0]), FT([0]), FT([0])
# Add confidence loss for background anchors (noobj) # Add confidence loss for background anchors (noobj)
lconf += k * BCEWithLogitsLoss(pred_conf[~mask], mask[~mask].float()) #lconf += k * BCEWithLogitsLoss(pred_conf[~mask], mask[~mask].float())
# Sum loss components # Sum loss components
loss = lx + ly + lw + lh + lconf + lcls loss = lx + ly + lw + lh + lconf + lcls