diff --git a/models.py b/models.py index 01151e25..f254b9c8 100755 --- a/models.py +++ b/models.py @@ -172,7 +172,7 @@ class YOLOLayer(nn.Module): lh = k * MSELoss(h[mask], th[mask]) # lconf = k * BCEWithLogitsLoss(pred_conf[mask], mask[mask].float()) - lconf = (k * 1) * BCEWithLogitsLoss(pred_conf, mask.float()) + lconf = (k * 5) * BCEWithLogitsLoss(pred_conf, mask.float()) lcls = (k / 10) * CrossEntropyLoss(pred_cls[mask], torch.argmax(tcls, 1)) # lcls = (k * 10) * BCEWithLogitsLoss(pred_cls[mask], tcls.float()) diff --git a/train.py b/train.py index 2e862954..cbd78cb7 100644 --- a/train.py +++ b/train.py @@ -125,7 +125,8 @@ def main(opt): g['lr'] = lr # Compute loss, compute gradient, update parameters - loss = model(imgs.to(device), targets, requestPrecision=False) + precision_per_batch = False + loss = model(imgs.to(device), targets, requestPrecision=precision_per_batch) loss.backward() # accumulated_batches = 1 # accumulate gradient for 4 batches before stepping optimizer @@ -133,24 +134,26 @@ def main(opt): optimizer.step() optimizer.zero_grad() - # Compute running epoch-means of tracked metrics + # Running epoch-means of tracked metrics ui += 1 - metrics += model.losses['metrics'] - TP, FP, FN = metrics for key, val in model.losses.items(): rloss[key] = (rloss[key] * ui + val) / (ui + 1) - # Precision - precision = TP / (TP + FP) - k = (TP + FP) > 0 - if k.sum() > 0: - mean_precision = precision[k].mean() + if precision_per_batch: + TP, FP, FN = metrics + metrics += model.losses['metrics'] - # Recall - recall = TP / (TP + FN) - k = (TP + FN) > 0 - if k.sum() > 0: - mean_recall = recall[k].mean() + # Precision + precision = TP / (TP + FP) + k = (TP + FP) > 0 + if k.sum() > 0: + mean_precision = precision[k].mean() + + # Recall + recall = TP / (TP + FN) + k = (TP + FN) > 0 + if k.sum() > 0: + mean_recall = recall[k].mean() s = ('%11s%11s' + '%11.3g' * 14) % ( '%g/%g' % (epoch, opt.epochs - 1), '%g/%g' % (i, len(dataloader) - 1), rloss['x'], diff --git a/utils/utils.py b/utils/utils.py index d7231fa6..b4933d96 100755 --- a/utils/utils.py +++ b/utils/utils.py @@ -438,7 +438,7 @@ def plot_results(): import matplotlib.pyplot as plt plt.figure(figsize=(16, 8)) s = ['X', 'Y', 'Width', 'Height', 'Objectness', 'Classification', 'Total Loss', 'Precision', 'Recall', 'mAP'] - for f in ('results5.txt', 'results_new.txt', 'results3.txt', + for f in ('results_d5.txt', 'results_d10.txt', 'results_new.txt', ): results = np.loadtxt(f, usecols=[2, 3, 4, 5, 6, 7, 8, 9, 10]).T # column 16 is mAP for i in range(9):