validator.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. """
  3. Check a model's accuracy on a test or val split of a dataset.
  4. Usage:
  5. $ yolo mode=val model=yolov8n.pt data=coco8.yaml imgsz=640
  6. Usage - formats:
  7. $ yolo mode=val model=yolov8n.pt # PyTorch
  8. yolov8n.torchscript # TorchScript
  9. yolov8n.onnx # ONNX Runtime or OpenCV DNN with dnn=True
  10. yolov8n_openvino_model # OpenVINO
  11. yolov8n.engine # TensorRT
  12. yolov8n.mlpackage # CoreML (macOS-only)
  13. yolov8n_saved_model # TensorFlow SavedModel
  14. yolov8n.pb # TensorFlow GraphDef
  15. yolov8n.tflite # TensorFlow Lite
  16. yolov8n_edgetpu.tflite # TensorFlow Edge TPU
  17. yolov8n_paddle_model # PaddlePaddle
  18. yolov8n.mnn # MNN
  19. yolov8n_ncnn_model # NCNN
  20. """
  21. import json
  22. import time
  23. from pathlib import Path
  24. import numpy as np
  25. import torch
  26. from ultralytics.cfg import get_cfg, get_save_dir
  27. from ultralytics.data.utils import check_cls_dataset, check_det_dataset
  28. from ultralytics.nn.autobackend import AutoBackend
  29. from ultralytics.utils import LOGGER, TQDM, callbacks, colorstr, emojis
  30. from ultralytics.utils.checks import check_imgsz
  31. from ultralytics.utils.ops import Profile
  32. from ultralytics.utils.torch_utils import de_parallel, select_device, smart_inference_mode
  33. class BaseValidator:
  34. """
  35. BaseValidator.
  36. A base class for creating validators.
  37. Attributes:
  38. args (SimpleNamespace): Configuration for the validator.
  39. dataloader (DataLoader): Dataloader to use for validation.
  40. pbar (tqdm): Progress bar to update during validation.
  41. model (nn.Module): Model to validate.
  42. data (dict): Data dictionary.
  43. device (torch.device): Device to use for validation.
  44. batch_i (int): Current batch index.
  45. training (bool): Whether the model is in training mode.
  46. names (dict): Class names.
  47. seen: Records the number of images seen so far during validation.
  48. stats: Placeholder for statistics during validation.
  49. confusion_matrix: Placeholder for a confusion matrix.
  50. nc: Number of classes.
  51. iouv: (torch.Tensor): IoU thresholds from 0.50 to 0.95 in spaces of 0.05.
  52. jdict (dict): Dictionary to store JSON validation results.
  53. speed (dict): Dictionary with keys 'preprocess', 'inference', 'loss', 'postprocess' and their respective
  54. batch processing times in milliseconds.
  55. save_dir (Path): Directory to save results.
  56. plots (dict): Dictionary to store plots for visualization.
  57. callbacks (dict): Dictionary to store various callback functions.
  58. """
  59. def __init__(self, dataloader=None, save_dir=None, pbar=None, args=None, _callbacks=None):
  60. """
  61. Initializes a BaseValidator instance.
  62. Args:
  63. dataloader (torch.utils.data.DataLoader): Dataloader to be used for validation.
  64. save_dir (Path, optional): Directory to save results.
  65. pbar (tqdm.tqdm): Progress bar for displaying progress.
  66. args (SimpleNamespace): Configuration for the validator.
  67. _callbacks (dict): Dictionary to store various callback functions.
  68. """
  69. self.args = get_cfg(overrides=args)
  70. self.dataloader = dataloader
  71. self.pbar = pbar
  72. self.stride = None
  73. self.data = None
  74. self.device = None
  75. self.batch_i = None
  76. self.training = True
  77. self.names = None
  78. self.seen = None
  79. self.stats = None
  80. self.confusion_matrix = None
  81. self.nc = None
  82. self.iouv = None
  83. self.jdict = None
  84. self.speed = {"preprocess": 0.0, "inference": 0.0, "loss": 0.0, "postprocess": 0.0}
  85. self.save_dir = save_dir or get_save_dir(self.args)
  86. (self.save_dir / "labels" if self.args.save_txt else self.save_dir).mkdir(parents=True, exist_ok=True)
  87. if self.args.conf is None:
  88. self.args.conf = 0.001 # default conf=0.001
  89. self.args.imgsz = check_imgsz(self.args.imgsz, max_dim=1)
  90. self.plots = {}
  91. self.callbacks = _callbacks or callbacks.get_default_callbacks()
  92. @smart_inference_mode()
  93. def __call__(self, trainer=None, model=None):
  94. """Executes validation process, running inference on dataloader and computing performance metrics."""
  95. self.training = trainer is not None
  96. augment = self.args.augment and (not self.training)
  97. if self.training:
  98. self.device = trainer.device
  99. self.data = trainer.data
  100. # force FP16 val during training
  101. self.args.half = self.device.type != "cpu" and trainer.amp
  102. model = trainer.ema.ema or trainer.model
  103. model = model.half() if self.args.half else model.float()
  104. # self.model = model
  105. self.loss = torch.zeros_like(trainer.loss_items, device=trainer.device)
  106. self.args.plots &= trainer.stopper.possible_stop or (trainer.epoch == trainer.epochs - 1)
  107. model.eval()
  108. else:
  109. if str(self.args.model).endswith(".yaml") and model is None:
  110. LOGGER.warning("WARNING ⚠️ validating an untrained model YAML will result in 0 mAP.")
  111. callbacks.add_integration_callbacks(self)
  112. model = AutoBackend(
  113. weights=model or self.args.model,
  114. device=select_device(self.args.device, self.args.batch),
  115. dnn=self.args.dnn,
  116. data=self.args.data,
  117. fp16=self.args.half,
  118. )
  119. # self.model = model
  120. self.device = model.device # update device
  121. self.args.half = model.fp16 # update half
  122. stride, pt, jit, engine = model.stride, model.pt, model.jit, model.engine
  123. imgsz = check_imgsz(self.args.imgsz, stride=stride)
  124. if engine:
  125. self.args.batch = model.batch_size
  126. elif not pt and not jit:
  127. self.args.batch = model.metadata.get("batch", 1) # export.py models default to batch-size 1
  128. LOGGER.info(f"Setting batch={self.args.batch} input of shape ({self.args.batch}, 3, {imgsz}, {imgsz})")
  129. if str(self.args.data).split(".")[-1] in {"yaml", "yml"}:
  130. self.data = check_det_dataset(self.args.data)
  131. elif self.args.task == "classify":
  132. self.data = check_cls_dataset(self.args.data, split=self.args.split)
  133. else:
  134. raise FileNotFoundError(emojis(f"Dataset '{self.args.data}' for task={self.args.task} not found ❌"))
  135. if self.device.type in {"cpu", "mps"}:
  136. self.args.workers = 0 # faster CPU val as time dominated by inference, not dataloading
  137. if not pt:
  138. self.args.rect = False
  139. self.stride = model.stride # used in get_dataloader() for padding
  140. self.dataloader = self.dataloader or self.get_dataloader(self.data.get(self.args.split), self.args.batch)
  141. model.eval()
  142. model.warmup(imgsz=(1 if pt else self.args.batch, 3, imgsz, imgsz)) # warmup
  143. self.run_callbacks("on_val_start")
  144. dt = (
  145. Profile(device=self.device),
  146. Profile(device=self.device),
  147. Profile(device=self.device),
  148. Profile(device=self.device),
  149. )
  150. bar = TQDM(self.dataloader, desc=self.get_desc(), total=len(self.dataloader))
  151. self.init_metrics(de_parallel(model))
  152. self.jdict = [] # empty before each val
  153. for batch_i, batch in enumerate(bar):
  154. self.run_callbacks("on_val_batch_start")
  155. self.batch_i = batch_i
  156. # Preprocess
  157. with dt[0]:
  158. batch = self.preprocess(batch)
  159. # Inference
  160. with dt[1]:
  161. preds = model(batch["img"], augment=augment)
  162. # Loss
  163. with dt[2]:
  164. if self.training:
  165. self.loss += model.loss(batch, preds)[1]
  166. # Postprocess
  167. with dt[3]:
  168. preds = self.postprocess(preds)
  169. self.update_metrics(preds, batch)
  170. if self.args.plots and batch_i < 3:
  171. self.plot_val_samples(batch, batch_i)
  172. self.plot_predictions(batch, preds, batch_i)
  173. self.run_callbacks("on_val_batch_end")
  174. stats = self.get_stats()
  175. self.check_stats(stats)
  176. self.speed = dict(zip(self.speed.keys(), (x.t / len(self.dataloader.dataset) * 1e3 for x in dt)))
  177. self.finalize_metrics()
  178. self.print_results()
  179. self.run_callbacks("on_val_end")
  180. if self.training:
  181. model.float()
  182. results = {**stats, **trainer.label_loss_items(self.loss.cpu() / len(self.dataloader), prefix="val")}
  183. return {k: round(float(v), 5) for k, v in results.items()} # return results as 5 decimal place floats
  184. else:
  185. LOGGER.info(
  186. "Speed: {:.1f}ms preprocess, {:.1f}ms inference, {:.1f}ms loss, {:.1f}ms postprocess per image".format(
  187. *tuple(self.speed.values())
  188. )
  189. )
  190. if self.args.save_json and self.jdict:
  191. with open(str(self.save_dir / "predictions.json"), "w") as f:
  192. LOGGER.info(f"Saving {f.name}...")
  193. json.dump(self.jdict, f) # flatten and save
  194. stats = self.eval_json(stats) # update stats
  195. if self.args.plots or self.args.save_json:
  196. LOGGER.info(f"Results saved to {colorstr('bold', self.save_dir)}")
  197. return stats
  198. def match_predictions(self, pred_classes, true_classes, iou, use_scipy=False):
  199. """
  200. Matches predictions to ground truth objects (pred_classes, true_classes) using IoU.
  201. Args:
  202. pred_classes (torch.Tensor): Predicted class indices of shape(N,).
  203. true_classes (torch.Tensor): Target class indices of shape(M,).
  204. iou (torch.Tensor): An NxM tensor containing the pairwise IoU values for predictions and ground of truth
  205. use_scipy (bool): Whether to use scipy for matching (more precise).
  206. Returns:
  207. (torch.Tensor): Correct tensor of shape(N,10) for 10 IoU thresholds.
  208. """
  209. # Dx10 matrix, where D - detections, 10 - IoU thresholds
  210. correct = np.zeros((pred_classes.shape[0], self.iouv.shape[0])).astype(bool)
  211. # LxD matrix where L - labels (rows), D - detections (columns)
  212. correct_class = true_classes[:, None] == pred_classes
  213. iou = iou * correct_class # zero out the wrong classes
  214. iou = iou.cpu().numpy()
  215. for i, threshold in enumerate(self.iouv.cpu().tolist()):
  216. if use_scipy:
  217. # WARNING: known issue that reduces mAP in https://github.com/ultralytics/ultralytics/pull/4708
  218. import scipy # scope import to avoid importing for all commands
  219. cost_matrix = iou * (iou >= threshold)
  220. if cost_matrix.any():
  221. labels_idx, detections_idx = scipy.optimize.linear_sum_assignment(cost_matrix)
  222. valid = cost_matrix[labels_idx, detections_idx] > 0
  223. if valid.any():
  224. correct[detections_idx[valid], i] = True
  225. else:
  226. matches = np.nonzero(iou >= threshold) # IoU > threshold and classes match
  227. matches = np.array(matches).T
  228. if matches.shape[0]:
  229. if matches.shape[0] > 1:
  230. matches = matches[iou[matches[:, 0], matches[:, 1]].argsort()[::-1]]
  231. matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
  232. # matches = matches[matches[:, 2].argsort()[::-1]]
  233. matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
  234. correct[matches[:, 1].astype(int), i] = True
  235. return torch.tensor(correct, dtype=torch.bool, device=pred_classes.device)
  236. def add_callback(self, event: str, callback):
  237. """Appends the given callback."""
  238. self.callbacks[event].append(callback)
  239. def run_callbacks(self, event: str):
  240. """Runs all callbacks associated with a specified event."""
  241. for callback in self.callbacks.get(event, []):
  242. callback(self)
  243. def get_dataloader(self, dataset_path, batch_size):
  244. """Get data loader from dataset path and batch size."""
  245. raise NotImplementedError("get_dataloader function not implemented for this validator")
  246. def build_dataset(self, img_path):
  247. """Build dataset."""
  248. raise NotImplementedError("build_dataset function not implemented in validator")
  249. def preprocess(self, batch):
  250. """Preprocesses an input batch."""
  251. return batch
  252. def postprocess(self, preds):
  253. """Preprocesses the predictions."""
  254. return preds
  255. def init_metrics(self, model):
  256. """Initialize performance metrics for the YOLO model."""
  257. pass
  258. def update_metrics(self, preds, batch):
  259. """Updates metrics based on predictions and batch."""
  260. pass
  261. def finalize_metrics(self, *args, **kwargs):
  262. """Finalizes and returns all metrics."""
  263. pass
  264. def get_stats(self):
  265. """Returns statistics about the model's performance."""
  266. return {}
  267. def check_stats(self, stats):
  268. """Checks statistics."""
  269. pass
  270. def print_results(self):
  271. """Prints the results of the model's predictions."""
  272. pass
  273. def get_desc(self):
  274. """Get description of the YOLO model."""
  275. pass
  276. @property
  277. def metric_keys(self):
  278. """Returns the metric keys used in YOLO training/validation."""
  279. return []
  280. def on_plot(self, name, data=None):
  281. """Registers plots (e.g. to be consumed in callbacks)."""
  282. self.plots[Path(name)] = {"data": data, "timestamp": time.time()}
  283. # TODO: may need to put these following functions into callback
  284. def plot_val_samples(self, batch, ni):
  285. """Plots validation samples during training."""
  286. pass
  287. def plot_predictions(self, batch, preds, ni):
  288. """Plots YOLO model predictions on batch images."""
  289. pass
  290. def pred_to_json(self, preds, batch):
  291. """Convert predictions to JSON format."""
  292. pass
  293. def eval_json(self, stats):
  294. """Evaluate and return JSON format of prediction statistics."""
  295. pass