val.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
  2. """
  3. Validate a trained YOLOv5 detection model on a detection dataset
  4. Usage:
  5. $ python val.py --weights yolov5s.pt --data coco128.yaml --img 640
  6. Usage - formats:
  7. $ python val.py --weights yolov5s.pt # PyTorch
  8. yolov5s.torchscript # TorchScript
  9. yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
  10. yolov5s_openvino_model # OpenVINO
  11. yolov5s.engine # TensorRT
  12. yolov5s.mlmodel # CoreML (macOS-only)
  13. yolov5s_saved_model # TensorFlow SavedModel
  14. yolov5s.pb # TensorFlow GraphDef
  15. yolov5s.tflite # TensorFlow Lite
  16. yolov5s_edgetpu.tflite # TensorFlow Edge TPU
  17. yolov5s_paddle_model # PaddlePaddle
  18. """
  19. import argparse
  20. import json
  21. import os
  22. import subprocess
  23. import sys
  24. from pathlib import Path
  25. import numpy as np
  26. import torch
  27. from tqdm import tqdm
  28. FILE = Path(__file__).resolve()
  29. ROOT = FILE.parents[0] # YOLOv5 root directory
  30. if str(ROOT) not in sys.path:
  31. sys.path.append(str(ROOT)) # add ROOT to PATH
  32. ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
  33. from models.common import DetectMultiBackend
  34. from utils.callbacks import Callbacks
  35. from utils.dataloaders import create_dataloader
  36. from utils.general import (LOGGER, TQDM_BAR_FORMAT, Profile, check_dataset, check_img_size, check_requirements,
  37. check_yaml, coco80_to_coco91_class, colorstr, increment_path, non_max_suppression,
  38. print_args, scale_boxes, xywh2xyxy, xyxy2xywh)
  39. from utils.metrics import ConfusionMatrix, ap_per_class, box_iou
  40. from utils.plots import output_to_target, plot_images, plot_val_study
  41. from utils.torch_utils import select_device, smart_inference_mode
  42. def save_one_txt(predn, save_conf, shape, file):
  43. # Save one txt result
  44. gn = torch.tensor(shape)[[1, 0, 1, 0]] # normalization gain whwh
  45. for *xyxy, conf, cls in predn.tolist():
  46. xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
  47. line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
  48. with open(file, 'a') as f:
  49. f.write(('%g ' * len(line)).rstrip() % line + '\n')
  50. def save_one_json(predn, jdict, path, class_map):
  51. # Save one JSON result {"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}
  52. image_id = int(path.stem) if path.stem.isnumeric() else path.stem
  53. box = xyxy2xywh(predn[:, :4]) # xywh
  54. box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
  55. for p, b in zip(predn.tolist(), box.tolist()):
  56. jdict.append({
  57. 'image_id': image_id,
  58. 'category_id': class_map[int(p[5])],
  59. 'bbox': [round(x, 3) for x in b],
  60. 'score': round(p[4], 5)})
  61. def process_batch(detections, labels, iouv):
  62. """
  63. Return correct prediction matrix
  64. Arguments:
  65. detections (array[N, 6]), x1, y1, x2, y2, conf, class
  66. labels (array[M, 5]), class, x1, y1, x2, y2
  67. Returns:
  68. correct (array[N, 10]), for 10 IoU levels
  69. """
  70. correct = np.zeros((detections.shape[0], iouv.shape[0])).astype(bool)
  71. iou = box_iou(labels[:, 1:], detections[:, :4])
  72. correct_class = labels[:, 0:1] == detections[:, 5]
  73. for i in range(len(iouv)):
  74. x = torch.where((iou >= iouv[i]) & correct_class) # IoU > threshold and classes match
  75. if x[0].shape[0]:
  76. matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() # [label, detect, iou]
  77. if x[0].shape[0] > 1:
  78. matches = matches[matches[:, 2].argsort()[::-1]]
  79. matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
  80. # matches = matches[matches[:, 2].argsort()[::-1]]
  81. matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
  82. correct[matches[:, 1].astype(int), i] = True
  83. return torch.tensor(correct, dtype=torch.bool, device=iouv.device)
  84. @smart_inference_mode()
  85. def run(
  86. data,
  87. weights=None, # model.pt path(s)
  88. batch_size=32, # batch size
  89. imgsz=640, # inference size (pixels)
  90. conf_thres=0.001, # confidence threshold
  91. iou_thres=0.6, # NMS IoU threshold
  92. max_det=300, # maximum detections per image
  93. task='val', # train, val, test, speed or study
  94. device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
  95. workers=8, # max dataloader workers (per RANK in DDP mode)
  96. single_cls=False, # treat as single-class dataset
  97. augment=False, # augmented inference
  98. verbose=False, # verbose output
  99. save_txt=False, # save results to *.txt
  100. save_hybrid=False, # save label+prediction hybrid results to *.txt
  101. save_conf=False, # save confidences in --save-txt labels
  102. save_json=False, # save a COCO-JSON results file
  103. project=ROOT / 'runs/val', # save to project/name
  104. name='exp', # save to project/name
  105. exist_ok=False, # existing project/name ok, do not increment
  106. half=True, # use FP16 half-precision inference
  107. dnn=False, # use OpenCV DNN for ONNX inference
  108. model=None,
  109. dataloader=None,
  110. save_dir=Path(''),
  111. plots=True,
  112. callbacks=Callbacks(),
  113. compute_loss=None,
  114. ):
  115. # Initialize/load model and set device
  116. training = model is not None
  117. if training: # called by train.py
  118. device, pt, jit, engine = next(model.parameters()).device, True, False, False # get model device, PyTorch model
  119. half &= device.type != 'cpu' # half precision only supported on CUDA
  120. model.half() if half else model.float()
  121. else: # called directly
  122. device = select_device(device, batch_size=batch_size)
  123. # Directories
  124. save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
  125. (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
  126. # Load model
  127. model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
  128. stride, pt, jit, engine = model.stride, model.pt, model.jit, model.engine
  129. imgsz = check_img_size(imgsz, s=stride) # check image size
  130. half = model.fp16 # FP16 supported on limited backends with CUDA
  131. if engine:
  132. batch_size = model.batch_size
  133. else:
  134. device = model.device
  135. if not (pt or jit):
  136. batch_size = 1 # export.py models default to batch-size 1
  137. LOGGER.info(f'Forcing --batch-size 1 square inference (1,3,{imgsz},{imgsz}) for non-PyTorch models')
  138. # Data
  139. data = check_dataset(data) # check
  140. # Configure
  141. model.eval()
  142. cuda = device.type != 'cpu'
  143. is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'coco{os.sep}val2017.txt') # COCO dataset
  144. nc = 1 if single_cls else int(data['nc']) # number of classes
  145. iouv = torch.linspace(0.5, 0.95, 10, device=device) # iou vector for mAP@0.5:0.95
  146. niou = iouv.numel()
  147. # Dataloader
  148. if not training:
  149. if pt and not single_cls: # check --weights are trained on --data
  150. ncm = model.model.nc
  151. assert ncm == nc, f'{weights} ({ncm} classes) trained on different --data than what you passed ({nc} ' \
  152. f'classes). Pass correct combination of --weights and --data that are trained together.'
  153. model.warmup(imgsz=(1 if pt else batch_size, 3, imgsz, imgsz)) # warmup
  154. pad, rect = (0.0, False) if task == 'speed' else (0.5, pt) # square inference for benchmarks
  155. task = task if task in ('train', 'val', 'test') else 'val' # path to train/val/test images
  156. dataloader = create_dataloader(data[task],
  157. imgsz,
  158. batch_size,
  159. stride,
  160. single_cls,
  161. pad=pad,
  162. rect=rect,
  163. workers=workers,
  164. prefix=colorstr(f'{task}: '))[0]
  165. seen = 0
  166. confusion_matrix = ConfusionMatrix(nc=nc)
  167. names = model.names if hasattr(model, 'names') else model.module.names # get class names
  168. if isinstance(names, (list, tuple)): # old format
  169. names = dict(enumerate(names))
  170. class_map = coco80_to_coco91_class() if is_coco else list(range(1000))
  171. s = ('%22s' + '%11s' * 6) % ('Class', 'Images', 'Instances', 'P', 'R', 'mAP50', 'mAP50-95')
  172. tp, fp, p, r, f1, mp, mr, map50, ap50, map = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
  173. dt = Profile(), Profile(), Profile() # profiling times
  174. loss = torch.zeros(3, device=device)
  175. jdict, stats, ap, ap_class = [], [], [], []
  176. callbacks.run('on_val_start')
  177. pbar = tqdm(dataloader, desc=s, bar_format=TQDM_BAR_FORMAT) # progress bar
  178. for batch_i, (im, targets, paths, shapes) in enumerate(pbar):
  179. callbacks.run('on_val_batch_start')
  180. with dt[0]:
  181. if cuda:
  182. im = im.to(device, non_blocking=True)
  183. targets = targets.to(device)
  184. im = im.half() if half else im.float() # uint8 to fp16/32
  185. im /= 255 # 0 - 255 to 0.0 - 1.0
  186. nb, _, height, width = im.shape # batch size, channels, height, width
  187. # Inference
  188. with dt[1]:
  189. preds, train_out = model(im) if compute_loss else (model(im, augment=augment), None)
  190. # Loss
  191. if compute_loss:
  192. loss += compute_loss(train_out, targets)[1] # box, obj, cls
  193. # NMS
  194. targets[:, 2:] *= torch.tensor((width, height, width, height), device=device) # to pixels
  195. lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
  196. with dt[2]:
  197. preds = non_max_suppression(preds,
  198. conf_thres,
  199. iou_thres,
  200. labels=lb,
  201. multi_label=True,
  202. agnostic=single_cls,
  203. max_det=max_det)
  204. # Metrics
  205. for si, pred in enumerate(preds):
  206. labels = targets[targets[:, 0] == si, 1:]
  207. nl, npr = labels.shape[0], pred.shape[0] # number of labels, predictions
  208. path, shape = Path(paths[si]), shapes[si][0]
  209. correct = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
  210. seen += 1
  211. if npr == 0:
  212. if nl:
  213. stats.append((correct, *torch.zeros((2, 0), device=device), labels[:, 0]))
  214. if plots:
  215. confusion_matrix.process_batch(detections=None, labels=labels[:, 0])
  216. continue
  217. # Predictions
  218. if single_cls:
  219. pred[:, 5] = 0
  220. predn = pred.clone()
  221. scale_boxes(im[si].shape[1:], predn[:, :4], shape, shapes[si][1]) # native-space pred
  222. # Evaluate
  223. if nl:
  224. tbox = xywh2xyxy(labels[:, 1:5]) # target boxes
  225. scale_boxes(im[si].shape[1:], tbox, shape, shapes[si][1]) # native-space labels
  226. labelsn = torch.cat((labels[:, 0:1], tbox), 1) # native-space labels
  227. correct = process_batch(predn, labelsn, iouv)
  228. if plots:
  229. confusion_matrix.process_batch(predn, labelsn)
  230. stats.append((correct, pred[:, 4], pred[:, 5], labels[:, 0])) # (correct, conf, pcls, tcls)
  231. # Save/log
  232. if save_txt:
  233. save_one_txt(predn, save_conf, shape, file=save_dir / 'labels' / f'{path.stem}.txt')
  234. if save_json:
  235. save_one_json(predn, jdict, path, class_map) # append to COCO-JSON dictionary
  236. callbacks.run('on_val_image_end', pred, predn, path, names, im[si])
  237. # Plot images
  238. if plots and batch_i < 3:
  239. plot_images(im, targets, paths, save_dir / f'val_batch{batch_i}_labels.jpg', names) # labels
  240. plot_images(im, output_to_target(preds), paths, save_dir / f'val_batch{batch_i}_pred.jpg', names) # pred
  241. callbacks.run('on_val_batch_end', batch_i, im, targets, paths, shapes, preds)
  242. # Compute metrics
  243. stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)] # to numpy
  244. if len(stats) and stats[0].any():
  245. tp, fp, p, r, f1, ap, ap_class = ap_per_class(*stats, plot=plots, save_dir=save_dir, names=names)
  246. ap50, ap = ap[:, 0], ap.mean(1) # AP@0.5, AP@0.5:0.95
  247. mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean()
  248. nt = np.bincount(stats[3].astype(int), minlength=nc) # number of targets per class
  249. # Print results
  250. pf = '%22s' + '%11i' * 2 + '%11.3g' * 4 # print format
  251. LOGGER.info(pf % ('all', seen, nt.sum(), mp, mr, map50, map))
  252. if nt.sum() == 0:
  253. LOGGER.warning(f'WARNING ⚠️ no labels found in {task} set, can not compute metrics without labels')
  254. # Print results per class
  255. if (verbose or (nc < 50 and not training)) and nc > 1 and len(stats):
  256. for i, c in enumerate(ap_class):
  257. LOGGER.info(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i]))
  258. # Print speeds
  259. t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
  260. if not training:
  261. shape = (batch_size, 3, imgsz, imgsz)
  262. LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)
  263. # Plots
  264. if plots:
  265. confusion_matrix.plot(save_dir=save_dir, names=list(names.values()))
  266. callbacks.run('on_val_end', nt, tp, fp, p, r, f1, ap, ap50, ap_class, confusion_matrix)
  267. # Save JSON
  268. if save_json and len(jdict):
  269. w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
  270. anno_json = str(Path('../datasets/coco/annotations/instances_val2017.json')) # annotations
  271. pred_json = str(save_dir / f'{w}_predictions.json') # predictions
  272. LOGGER.info(f'\nEvaluating pycocotools mAP... saving {pred_json}...')
  273. with open(pred_json, 'w') as f:
  274. json.dump(jdict, f)
  275. try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
  276. check_requirements('pycocotools>=2.0.6')
  277. from pycocotools.coco import COCO
  278. from pycocotools.cocoeval import COCOeval
  279. anno = COCO(anno_json) # init annotations api
  280. pred = anno.loadRes(pred_json) # init predictions api
  281. eval = COCOeval(anno, pred, 'bbox')
  282. if is_coco:
  283. eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.im_files] # image IDs to evaluate
  284. eval.evaluate()
  285. eval.accumulate()
  286. eval.summarize()
  287. map, map50 = eval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5)
  288. except Exception as e:
  289. LOGGER.info(f'pycocotools unable to run: {e}')
  290. # Return results
  291. model.float() # for training
  292. if not training:
  293. s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
  294. LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
  295. maps = np.zeros(nc) + map
  296. for i, c in enumerate(ap_class):
  297. maps[c] = ap[i]
  298. return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t
  299. def parse_opt():
  300. parser = argparse.ArgumentParser()
  301. parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
  302. parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path(s)')
  303. parser.add_argument('--batch-size', type=int, default=32, help='batch size')
  304. parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
  305. parser.add_argument('--conf-thres', type=float, default=0.001, help='confidence threshold')
  306. parser.add_argument('--iou-thres', type=float, default=0.6, help='NMS IoU threshold')
  307. parser.add_argument('--max-det', type=int, default=300, help='maximum detections per image')
  308. parser.add_argument('--task', default='val', help='train, val, test, speed or study')
  309. parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
  310. parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
  311. parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
  312. parser.add_argument('--augment', action='store_true', help='augmented inference')
  313. parser.add_argument('--verbose', action='store_true', help='report mAP by class')
  314. parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
  315. parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
  316. parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
  317. parser.add_argument('--save-json', action='store_true', help='save a COCO-JSON results file')
  318. parser.add_argument('--project', default=ROOT / 'runs/val', help='save to project/name')
  319. parser.add_argument('--name', default='exp', help='save to project/name')
  320. parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
  321. parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
  322. parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
  323. opt = parser.parse_args()
  324. opt.data = check_yaml(opt.data) # check YAML
  325. opt.save_json |= opt.data.endswith('coco.yaml')
  326. opt.save_txt |= opt.save_hybrid
  327. print_args(vars(opt))
  328. return opt
  329. def main(opt):
  330. check_requirements(ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
  331. if opt.task in ('train', 'val', 'test'): # run normally
  332. if opt.conf_thres > 0.001: # https://github.com/ultralytics/yolov5/issues/1466
  333. LOGGER.info(f'WARNING ⚠️ confidence threshold {opt.conf_thres} > 0.001 produces invalid results')
  334. if opt.save_hybrid:
  335. LOGGER.info('WARNING ⚠️ --save-hybrid will return high mAP from hybrid labels, not from predictions alone')
  336. run(**vars(opt))
  337. else:
  338. weights = opt.weights if isinstance(opt.weights, list) else [opt.weights]
  339. opt.half = torch.cuda.is_available() and opt.device != 'cpu' # FP16 for fastest results
  340. if opt.task == 'speed': # speed benchmarks
  341. # python val.py --task speed --data coco.yaml --batch 1 --weights yolov5n.pt yolov5s.pt...
  342. opt.conf_thres, opt.iou_thres, opt.save_json = 0.25, 0.45, False
  343. for opt.weights in weights:
  344. run(**vars(opt), plots=False)
  345. elif opt.task == 'study': # speed vs mAP benchmarks
  346. # python val.py --task study --data coco.yaml --iou 0.7 --weights yolov5n.pt yolov5s.pt...
  347. for opt.weights in weights:
  348. f = f'study_{Path(opt.data).stem}_{Path(opt.weights).stem}.txt' # filename to save to
  349. x, y = list(range(256, 1536 + 128, 128)), [] # x axis (image sizes), y axis
  350. for opt.imgsz in x: # img-size
  351. LOGGER.info(f'\nRunning {f} --imgsz {opt.imgsz}...')
  352. r, _, t = run(**vars(opt), plots=False)
  353. y.append(r + t) # results and times
  354. np.savetxt(f, y, fmt='%10.4g') # save
  355. subprocess.run(['zip', '-r', 'study.zip', 'study_*.txt'])
  356. plot_val_study(x=x) # plot
  357. else:
  358. raise NotImplementedError(f'--task {opt.task} not in ("train", "val", "test", "speed", "study")')
  359. if __name__ == '__main__':
  360. opt = parse_opt()
  361. main(opt)