val.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import torch
  3. from ultralytics.data import ClassificationDataset, build_dataloader
  4. from ultralytics.engine.validator import BaseValidator
  5. from ultralytics.utils import LOGGER
  6. from ultralytics.utils.metrics import ClassifyMetrics, ConfusionMatrix
  7. from ultralytics.utils.plotting import plot_images
  8. class ClassificationValidator(BaseValidator):
  9. """
  10. A class extending the BaseValidator class for validation based on a classification model.
  11. Notes:
  12. - Torchvision classification models can also be passed to the 'model' argument, i.e. model='resnet18'.
  13. Example:
  14. ```python
  15. from ultralytics.models.yolo.classify import ClassificationValidator
  16. args = dict(model='yolov8n-cls.pt', data='imagenet10')
  17. validator = ClassificationValidator(args=args)
  18. validator()
  19. ```
  20. """
  21. def __init__(self, dataloader=None, save_dir=None, pbar=None, args=None, _callbacks=None):
  22. """Initializes ClassificationValidator instance with args, dataloader, save_dir, and progress bar."""
  23. super().__init__(dataloader, save_dir, pbar, args, _callbacks)
  24. self.targets = None
  25. self.pred = None
  26. self.args.task = 'classify'
  27. self.metrics = ClassifyMetrics()
  28. def get_desc(self):
  29. """Returns a formatted string summarizing classification metrics."""
  30. return ('%22s' + '%11s' * 2) % ('classes', 'top1_acc', 'top5_acc')
  31. def init_metrics(self, model):
  32. """Initialize confusion matrix, class names, and top-1 and top-5 accuracy."""
  33. self.names = model.names
  34. self.nc = len(model.names)
  35. self.confusion_matrix = ConfusionMatrix(nc=self.nc, task='classify')
  36. self.pred = []
  37. self.targets = []
  38. def preprocess(self, batch):
  39. """Preprocesses input batch and returns it."""
  40. batch['img'] = batch['img'].to(self.device, non_blocking=True)
  41. batch['img'] = batch['img'].half() if self.args.half else batch['img'].float()
  42. batch['cls'] = batch['cls'].to(self.device)
  43. return batch
  44. def update_metrics(self, preds, batch):
  45. """Updates running metrics with model predictions and batch targets."""
  46. n5 = min(len(self.model.names), 5)
  47. self.pred.append(preds.argsort(1, descending=True)[:, :n5])
  48. self.targets.append(batch['cls'])
  49. def finalize_metrics(self, *args, **kwargs):
  50. """Finalizes metrics of the model such as confusion_matrix and speed."""
  51. self.confusion_matrix.process_cls_preds(self.pred, self.targets)
  52. if self.args.plots:
  53. for normalize in True, False:
  54. self.confusion_matrix.plot(save_dir=self.save_dir,
  55. names=self.names.values(),
  56. normalize=normalize,
  57. on_plot=self.on_plot)
  58. self.metrics.speed = self.speed
  59. self.metrics.confusion_matrix = self.confusion_matrix
  60. def get_stats(self):
  61. """Returns a dictionary of metrics obtained by processing targets and predictions."""
  62. self.metrics.process(self.targets, self.pred)
  63. return self.metrics.results_dict
  64. def build_dataset(self, img_path):
  65. return ClassificationDataset(root=img_path, args=self.args, augment=False, prefix=self.args.split)
  66. def get_dataloader(self, dataset_path, batch_size):
  67. """Builds and returns a data loader for classification tasks with given parameters."""
  68. dataset = self.build_dataset(dataset_path)
  69. return build_dataloader(dataset, batch_size, self.args.workers, rank=-1)
  70. def print_results(self):
  71. """Prints evaluation metrics for YOLO object detection model."""
  72. pf = '%22s' + '%11.3g' * len(self.metrics.keys) # print format
  73. LOGGER.info(pf % ('all', self.metrics.top1, self.metrics.top5))
  74. def plot_val_samples(self, batch, ni):
  75. """Plot validation image samples."""
  76. plot_images(
  77. images=batch['img'],
  78. batch_idx=torch.arange(len(batch['img'])),
  79. cls=batch['cls'].view(-1), # warning: use .view(), not .squeeze() for Classify models
  80. fname=self.save_dir / f'val_batch{ni}_labels.jpg',
  81. names=self.names,
  82. on_plot=self.on_plot)
  83. def plot_predictions(self, batch, preds, ni):
  84. """Plots predicted bounding boxes on input images and saves the result."""
  85. plot_images(batch['img'],
  86. batch_idx=torch.arange(len(batch['img'])),
  87. cls=torch.argmax(preds, dim=1),
  88. fname=self.save_dir / f'val_batch{ni}_pred.jpg',
  89. names=self.names,
  90. on_plot=self.on_plot) # pred