update loss components

This commit is contained in:
Glenn Jocher 2018-09-23 22:25:23 +02:00
parent bd3f617129
commit cf9b4cfa52
3 changed files with 33 additions and 31 deletions

View File

@ -137,10 +137,9 @@ class YOLOLayer(nn.Module):
# Training # Training
if targets is not None: if targets is not None:
BCEWithLogitsLoss1 = nn.BCEWithLogitsLoss(size_average=False) MSELoss = nn.MSELoss()
BCEWithLogitsLoss2 = nn.BCEWithLogitsLoss(size_average=True) BCEWithLogitsLoss = nn.BCEWithLogitsLoss()
MSELoss = nn.MSELoss(size_average=False) # version 0.4.0 # CrossEntropyLoss = nn.CrossEntropyLoss()
CrossEntropyLoss = nn.CrossEntropyLoss()
if requestPrecision: if requestPrecision:
gx = self.grid_x[:, :, :nG, :nG] gx = self.grid_x[:, :, :nG, :nG]
@ -157,33 +156,36 @@ class YOLOLayer(nn.Module):
if x.is_cuda: if x.is_cuda:
tx, ty, tw, th, mask, tcls = tx.cuda(), ty.cuda(), tw.cuda(), th.cuda(), mask.cuda(), tcls.cuda() tx, ty, tw, th, mask, tcls = tx.cuda(), ty.cuda(), tw.cuda(), th.cuda(), mask.cuda(), tcls.cuda()
# 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
if nM > 0: k = nM / nB
lx = (5 / nB) * MSELoss(x[mask], tx[mask]) if nM > 0:
ly = (5 / nB) * MSELoss(y[mask], ty[mask]) lx = k * MSELoss(x[mask], tx[mask])
lw = (5 / nB) * MSELoss(w[mask], tw[mask]) ly = k * MSELoss(y[mask], ty[mask])
lh = (5 / nB) * MSELoss(h[mask], th[mask]) lw = k * MSELoss(w[mask], tw[mask])
lconf = (1 / nB) * BCEWithLogitsLoss1(pred_conf[mask], mask[mask].float()) lh = k * MSELoss(h[mask], th[mask])
lconf = k * BCEWithLogitsLoss(pred_conf[mask], mask[mask].float())
lcls = (1 * nM / nB) * CrossEntropyLoss(pred_cls[mask], torch.argmax(tcls, 1)) # lcls = k * CrossEntropyLoss(pred_cls[mask], torch.argmax(tcls, 1))
# lcls = (1 * nM / nB) * BCEWithLogitsLoss2(pred_cls[mask], tcls.float()) lcls = k * BCEWithLogitsLoss(pred_cls[mask], tcls.float())
else: else:
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])
lconf += (0.5 * nM / nB) * BCEWithLogitsLoss2(pred_conf[~mask], mask[~mask].float()) # Add confidence loss for background anchors (noobj)
lconf += k * BCEWithLogitsLoss(pred_conf[~mask], mask[~mask].float())
loss = lx + ly + lw + lh + lconf + lcls # Sum loss components
loss = lx + ly + lw + lh + lconf + lcls
# Sum False Positives from unnasigned anchors # Sum False Positives from unassigned anchors
i = torch.sigmoid(pred_conf[~mask]) > 0.99 i = torch.sigmoid(pred_conf[~mask]) > 0.9
FPe = torch.zeros(self.nC)
if i.sum() > 0: if i.sum() > 0:
FP_classes = torch.argmax(pred_cls[~mask][i], 1) FP_classes = torch.argmax(pred_cls[~mask][i], 1)
for c in FP_classes: FPe = torch.bincount(FP_classes, minlength=self.nC).float().cpu() # extra FPs
FPe[c] += 1 else:
FPe = torch.zeros(self.nC)
return loss, loss.item(), lx.item(), ly.item(), lw.item(), lh.item(), lconf.item(), lcls.item(), \ return loss, loss.item(), lx.item(), ly.item(), lw.item(), lh.item(), lconf.item(), lcls.item(), \
nT, TP, FP, FPe, FN, TC nT, TP, FP, FPe, FN, TC

View File

@ -11,7 +11,7 @@ gsutil cp gs://ultralytics/fresh9_5_e201.pt yolov3/checkpoints
python3 detect.py python3 detect.py
# Test # Test
python3 test.py -img_size 416 -weights_path checkpoints/yolov3.weights python3 test.py -img_size 416 -weights_path checkpoints/latest.pt -conf_thresh 0.5
# Download and Test # Download and Test

View File

@ -282,9 +282,9 @@ def build_targets(pred_boxes, pred_conf, pred_cls, target, anchor_wh, nA, nC, nG
pconf = torch.sigmoid(pred_conf[b, a, gj, gi]).cpu() pconf = torch.sigmoid(pred_conf[b, a, gj, gi]).cpu()
iou_pred = bbox_iou(tb, pred_boxes[b, a, gj, gi].cpu()) iou_pred = bbox_iou(tb, pred_boxes[b, a, gj, gi].cpu())
TP[b, i] = (pconf > 0.99) & (iou_pred > 0.5) & (pcls == tc) TP[b, i] = (pconf > 0.9) & (iou_pred > 0.5) & (pcls == tc)
FP[b, i] = (pconf > 0.99) & (TP[b, i] == 0) # coordinates or class are wrong FP[b, i] = (pconf > 0.9) & (TP[b, i] == 0) # coordinates or class are wrong
FN[b, i] = pconf <= 0.99 # confidence score is too low (set to zero) FN[b, i] = pconf <= 0.9 # confidence score is too low (set to zero)
return tx, ty, tw, th, tconf, tcls, TP, FP, FN, TC return tx, ty, tw, th, tconf, tcls, TP, FP, FN, TC
@ -429,8 +429,8 @@ def plotResults():
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
plt.figure(figsize=(16, 8)) plt.figure(figsize=(16, 8))
s = ['X', 'Y', 'Width', 'Height', 'Objectness', 'Classification', 'Total Loss', 'Precision', 'Recall'] s = ['X', 'Y', 'Width', 'Height', 'Objectness', 'Classification', 'Total Loss', 'Precision', 'Recall']
for f in ('/Users/glennjocher/Downloads/results_CE.txt', for f in ('results.txt',
'/Users/glennjocher/Downloads/results_BCE.txt'): ):
results = np.loadtxt(f, usecols=[2, 3, 4, 5, 6, 7, 8, 9, 10]).T results = np.loadtxt(f, usecols=[2, 3, 4, 5, 6, 7, 8, 9, 10]).T
for i in range(9): for i in range(9):
plt.subplot(2, 5, i + 1) plt.subplot(2, 5, i + 1)