mirror of
https://github.com/THU-MIG/yolov10.git
synced 2025-05-23 13:34:23 +08:00

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>
19 lines
579 B
Python
19 lines
579 B
Python
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}
|