Metrics and loss structure (#28)

Co-authored-by: Ayush Chaurasia <ayush.chuararsia@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Ayush Chaurasia
2022-10-15 23:09:05 +05:30
committed by GitHub
parent d0b3c9812b
commit c5cb76b356
12 changed files with 183 additions and 43 deletions

View File

@ -0,0 +1,18 @@
import torch
from ultralytics import yolo
class ClassificationValidator(yolo.BaseValidator):
def init_metrics(self):
self.correct = torch.tensor([])
def update_metrics(self, preds, targets):
correct_in_batch = (targets[:, None] == preds).float()
self.correct = torch.cat((self.correct, correct_in_batch))
def get_stats(self):
acc = torch.stack((self.correct[:, 0], self.correct.max(1).values), dim=1) # (top1, top5) accuracy
top1, top5 = acc.mean(0).tolist()
return {"top1": top1, "top5": top5, "fitness": top5}