checks.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import contextlib
  3. import glob
  4. import inspect
  5. import math
  6. import os
  7. import platform
  8. import re
  9. import shutil
  10. import subprocess
  11. import time
  12. from pathlib import Path
  13. from typing import Optional
  14. import cv2
  15. import numpy as np
  16. import pkg_resources as pkg
  17. import psutil
  18. import requests
  19. import torch
  20. from matplotlib import font_manager
  21. from ultralytics.utils import (ASSETS, AUTOINSTALL, LINUX, LOGGER, ONLINE, ROOT, USER_CONFIG_DIR, ThreadingLocked,
  22. TryExcept, clean_url, colorstr, downloads, emojis, is_colab, is_docker, is_jupyter,
  23. is_kaggle, is_online, is_pip_package, url2file)
  24. def is_ascii(s) -> bool:
  25. """
  26. Check if a string is composed of only ASCII characters.
  27. Args:
  28. s (str): String to be checked.
  29. Returns:
  30. bool: True if the string is composed only of ASCII characters, False otherwise.
  31. """
  32. # Convert list, tuple, None, etc. to string
  33. s = str(s)
  34. # Check if the string is composed of only ASCII characters
  35. return all(ord(c) < 128 for c in s)
  36. def check_imgsz(imgsz, stride=32, min_dim=1, max_dim=2, floor=0):
  37. """
  38. Verify image size is a multiple of the given stride in each dimension. If the image size is not a multiple of the
  39. stride, update it to the nearest multiple of the stride that is greater than or equal to the given floor value.
  40. Args:
  41. imgsz (int | cList[int]): Image size.
  42. stride (int): Stride value.
  43. min_dim (int): Minimum number of dimensions.
  44. max_dim (int): Maximum number of dimensions.
  45. floor (int): Minimum allowed value for image size.
  46. Returns:
  47. (List[int]): Updated image size.
  48. """
  49. # Convert stride to integer if it is a tensor
  50. stride = int(stride.max() if isinstance(stride, torch.Tensor) else stride)
  51. # Convert image size to list if it is an integer
  52. if isinstance(imgsz, int):
  53. imgsz = [imgsz]
  54. elif isinstance(imgsz, (list, tuple)):
  55. imgsz = list(imgsz)
  56. else:
  57. raise TypeError(f"'imgsz={imgsz}' is of invalid type {type(imgsz).__name__}. "
  58. f"Valid imgsz types are int i.e. 'imgsz=640' or list i.e. 'imgsz=[640,640]'")
  59. # Apply max_dim
  60. if len(imgsz) > max_dim:
  61. msg = "'train' and 'val' imgsz must be an integer, while 'predict' and 'export' imgsz may be a [h, w] list " \
  62. "or an integer, i.e. 'yolo export imgsz=640,480' or 'yolo export imgsz=640'"
  63. if max_dim != 1:
  64. raise ValueError(f'imgsz={imgsz} is not a valid image size. {msg}')
  65. LOGGER.warning(f"WARNING ⚠️ updating to 'imgsz={max(imgsz)}'. {msg}")
  66. imgsz = [max(imgsz)]
  67. # Make image size a multiple of the stride
  68. sz = [max(math.ceil(x / stride) * stride, floor) for x in imgsz]
  69. # Print warning message if image size was updated
  70. if sz != imgsz:
  71. LOGGER.warning(f'WARNING ⚠️ imgsz={imgsz} must be multiple of max stride {stride}, updating to {sz}')
  72. # Add missing dimensions if necessary
  73. sz = [sz[0], sz[0]] if min_dim == 2 and len(sz) == 1 else sz[0] if min_dim == 1 and len(sz) == 1 else sz
  74. return sz
  75. def check_version(current: str = '0.0.0',
  76. required: str = '0.0.0',
  77. name: str = 'version ',
  78. hard: bool = False,
  79. verbose: bool = False) -> bool:
  80. """
  81. Check current version against the required version or range.
  82. Args:
  83. current (str): Current version.
  84. required (str): Required version or range (in pip-style format).
  85. name (str): Name to be used in warning message.
  86. hard (bool): If True, raise an AssertionError if the requirement is not met.
  87. verbose (bool): If True, print warning message if requirement is not met.
  88. Returns:
  89. (bool): True if requirement is met, False otherwise.
  90. Example:
  91. # check if current version is exactly 22.04
  92. check_version(current='22.04', required='==22.04')
  93. # check if current version is greater than or equal to 22.04
  94. check_version(current='22.10', required='22.04') # assumes '>=' inequality if none passed
  95. # check if current version is less than or equal to 22.04
  96. check_version(current='22.04', required='<=22.04')
  97. # check if current version is between 20.04 (inclusive) and 22.04 (exclusive)
  98. check_version(current='21.10', required='>20.04,<22.04')
  99. """
  100. current = pkg.parse_version(current)
  101. constraints = re.findall(r'([<>!=]{1,2}\s*\d+\.\d+)', required) or [f'>={required}']
  102. result = True
  103. for constraint in constraints:
  104. op, version = re.match(r'([<>!=]{1,2})\s*(\d+\.\d+)', constraint).groups()
  105. version = pkg.parse_version(version)
  106. if op == '==' and current != version:
  107. result = False
  108. elif op == '!=' and current == version:
  109. result = False
  110. elif op == '>=' and not (current >= version):
  111. result = False
  112. elif op == '<=' and not (current <= version):
  113. result = False
  114. elif op == '>' and not (current > version):
  115. result = False
  116. elif op == '<' and not (current < version):
  117. result = False
  118. if not result:
  119. warning_message = f'WARNING ⚠️ {name}{required} is required, but {name}{current} is currently installed'
  120. if hard:
  121. raise ModuleNotFoundError(emojis(warning_message)) # assert version requirements met
  122. if verbose:
  123. LOGGER.warning(warning_message)
  124. return result
  125. def check_latest_pypi_version(package_name='ultralytics'):
  126. """
  127. Returns the latest version of a PyPI package without downloading or installing it.
  128. Parameters:
  129. package_name (str): The name of the package to find the latest version for.
  130. Returns:
  131. (str): The latest version of the package.
  132. """
  133. with contextlib.suppress(Exception):
  134. requests.packages.urllib3.disable_warnings() # Disable the InsecureRequestWarning
  135. response = requests.get(f'https://pypi.org/pypi/{package_name}/json', timeout=3)
  136. if response.status_code == 200:
  137. return response.json()['info']['version']
  138. def check_pip_update_available():
  139. """
  140. Checks if a new version of the ultralytics package is available on PyPI.
  141. Returns:
  142. (bool): True if an update is available, False otherwise.
  143. """
  144. if ONLINE and is_pip_package():
  145. with contextlib.suppress(Exception):
  146. from ultralytics import __version__
  147. latest = check_latest_pypi_version()
  148. if pkg.parse_version(__version__) < pkg.parse_version(latest): # update is available
  149. LOGGER.info(f'New https://pypi.org/project/ultralytics/{latest} available 😃 '
  150. f"Update with 'pip install -U ultralytics'")
  151. return True
  152. return False
  153. @ThreadingLocked()
  154. def check_font(font='Arial.ttf'):
  155. """
  156. Find font locally or download to user's configuration directory if it does not already exist.
  157. Args:
  158. font (str): Path or name of font.
  159. Returns:
  160. file (Path): Resolved font file path.
  161. """
  162. name = Path(font).name
  163. # Check USER_CONFIG_DIR
  164. file = USER_CONFIG_DIR / name
  165. if file.exists():
  166. return file
  167. # Check system fonts
  168. matches = [s for s in font_manager.findSystemFonts() if font in s]
  169. if any(matches):
  170. return matches[0]
  171. # Download to USER_CONFIG_DIR if missing
  172. url = f'https://ultralytics.com/assets/{name}'
  173. if downloads.is_url(url):
  174. downloads.safe_download(url=url, file=file)
  175. return file
  176. def check_python(minimum: str = '3.8.0') -> bool:
  177. """
  178. Check current python version against the required minimum version.
  179. Args:
  180. minimum (str): Required minimum version of python.
  181. Returns:
  182. None
  183. """
  184. return check_version(platform.python_version(), minimum, name='Python ', hard=True)
  185. @TryExcept()
  186. def check_requirements(requirements=ROOT.parent / 'requirements.txt', exclude=(), install=True, cmds=''):
  187. """
  188. Check if installed dependencies meet YOLOv8 requirements and attempt to auto-update if needed.
  189. Args:
  190. requirements (Union[Path, str, List[str]]): Path to a requirements.txt file, a single package requirement as a
  191. string, or a list of package requirements as strings.
  192. exclude (Tuple[str]): Tuple of package names to exclude from checking.
  193. install (bool): If True, attempt to auto-update packages that don't meet requirements.
  194. cmds (str): Additional commands to pass to the pip install command when auto-updating.
  195. Example:
  196. ```python
  197. from ultralytics.utils.checks import check_requirements
  198. # Check a requirements.txt file
  199. check_requirements('path/to/requirements.txt')
  200. # Check a single package
  201. check_requirements('ultralytics>=8.0.0')
  202. # Check multiple packages
  203. check_requirements(['numpy', 'ultralytics>=8.0.0'])
  204. ```
  205. """
  206. prefix = colorstr('red', 'bold', 'requirements:')
  207. check_python() # check python version
  208. check_torchvision() # check torch-torchvision compatibility
  209. if isinstance(requirements, Path): # requirements.txt file
  210. file = requirements.resolve()
  211. assert file.exists(), f'{prefix} {file} not found, check failed.'
  212. with file.open() as f:
  213. requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(f) if x.name not in exclude]
  214. elif isinstance(requirements, str):
  215. requirements = [requirements]
  216. pkgs = []
  217. for r in requirements:
  218. r_stripped = r.split('/')[-1].replace('.git', '') # replace git+https://org/repo.git -> 'repo'
  219. try:
  220. pkg.require(r_stripped) # exception if requirements not met
  221. except pkg.DistributionNotFound:
  222. try: # attempt to import (slower but more accurate)
  223. import importlib
  224. importlib.import_module(next(pkg.parse_requirements(r_stripped)).name)
  225. except ImportError:
  226. pkgs.append(r)
  227. except pkg.VersionConflict:
  228. pkgs.append(r)
  229. s = ' '.join(f'"{x}"' for x in pkgs) # console string
  230. if s:
  231. if install and AUTOINSTALL: # check environment variable
  232. n = len(pkgs) # number of packages updates
  233. LOGGER.info(f"{prefix} Ultralytics requirement{'s' * (n > 1)} {pkgs} not found, attempting AutoUpdate...")
  234. try:
  235. t = time.time()
  236. assert is_online(), 'AutoUpdate skipped (offline)'
  237. LOGGER.info(subprocess.check_output(f'pip install --no-cache {s} {cmds}', shell=True).decode())
  238. dt = time.time() - t
  239. LOGGER.info(
  240. f"{prefix} AutoUpdate success ✅ {dt:.1f}s, installed {n} package{'s' * (n > 1)}: {pkgs}\n"
  241. f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n")
  242. except Exception as e:
  243. LOGGER.warning(f'{prefix} ❌ {e}')
  244. return False
  245. else:
  246. return False
  247. return True
  248. def check_torchvision():
  249. """
  250. Checks the installed versions of PyTorch and Torchvision to ensure they're compatible.
  251. This function checks the installed versions of PyTorch and Torchvision, and warns if they're incompatible according
  252. to the provided compatibility table based on https://github.com/pytorch/vision#installation. The
  253. compatibility table is a dictionary where the keys are PyTorch versions and the values are lists of compatible
  254. Torchvision versions.
  255. """
  256. import torchvision
  257. # Compatibility table
  258. compatibility_table = {'2.0': ['0.15'], '1.13': ['0.14'], '1.12': ['0.13']}
  259. # Extract only the major and minor versions
  260. v_torch = '.'.join(torch.__version__.split('+')[0].split('.')[:2])
  261. v_torchvision = '.'.join(torchvision.__version__.split('+')[0].split('.')[:2])
  262. if v_torch in compatibility_table:
  263. compatible_versions = compatibility_table[v_torch]
  264. if all(pkg.parse_version(v_torchvision) != pkg.parse_version(v) for v in compatible_versions):
  265. print(f'WARNING ⚠️ torchvision=={v_torchvision} is incompatible with torch=={v_torch}.\n'
  266. f"Run 'pip install torchvision=={compatible_versions[0]}' to fix torchvision or "
  267. "'pip install -U torch torchvision' to update both.\n"
  268. 'For a full compatibility table see https://github.com/pytorch/vision#installation')
  269. def check_suffix(file='yolov8n.pt', suffix='.pt', msg=''):
  270. """Check file(s) for acceptable suffix."""
  271. if file and suffix:
  272. if isinstance(suffix, str):
  273. suffix = (suffix, )
  274. for f in file if isinstance(file, (list, tuple)) else [file]:
  275. s = Path(f).suffix.lower().strip() # file suffix
  276. if len(s):
  277. assert s in suffix, f'{msg}{f} acceptable suffix is {suffix}, not {s}'
  278. def check_yolov5u_filename(file: str, verbose: bool = True):
  279. """Replace legacy YOLOv5 filenames with updated YOLOv5u filenames."""
  280. if 'yolov3' in file or 'yolov5' in file:
  281. if 'u.yaml' in file:
  282. file = file.replace('u.yaml', '.yaml') # i.e. yolov5nu.yaml -> yolov5n.yaml
  283. elif '.pt' in file and 'u' not in file:
  284. original_file = file
  285. file = re.sub(r'(.*yolov5([nsmlx]))\.pt', '\\1u.pt', file) # i.e. yolov5n.pt -> yolov5nu.pt
  286. file = re.sub(r'(.*yolov5([nsmlx])6)\.pt', '\\1u.pt', file) # i.e. yolov5n6.pt -> yolov5n6u.pt
  287. file = re.sub(r'(.*yolov3(|-tiny|-spp))\.pt', '\\1u.pt', file) # i.e. yolov3-spp.pt -> yolov3-sppu.pt
  288. if file != original_file and verbose:
  289. LOGGER.info(
  290. f"PRO TIP 💡 Replace 'model={original_file}' with new 'model={file}'.\nYOLOv5 'u' models are "
  291. f'trained with https://github.com/ultralytics/ultralytics and feature improved performance vs '
  292. f'standard YOLOv5 models trained with https://github.com/ultralytics/yolov5.\n')
  293. return file
  294. def check_file(file, suffix='', download=True, hard=True):
  295. """Search/download file (if necessary) and return path."""
  296. check_suffix(file, suffix) # optional
  297. file = str(file).strip() # convert to string and strip spaces
  298. file = check_yolov5u_filename(file) # yolov5n -> yolov5nu
  299. if not file or ('://' not in file and Path(file).exists()): # exists ('://' check required in Windows Python<3.10)
  300. return file
  301. elif download and file.lower().startswith(('https://', 'http://', 'rtsp://', 'rtmp://')): # download
  302. url = file # warning: Pathlib turns :// -> :/
  303. file = url2file(file) # '%2F' to '/', split https://url.com/file.txt?auth
  304. if Path(file).exists():
  305. LOGGER.info(f'Found {clean_url(url)} locally at {file}') # file already exists
  306. else:
  307. downloads.safe_download(url=url, file=file, unzip=False)
  308. return file
  309. else: # search
  310. files = glob.glob(str(ROOT / 'cfg' / '**' / file), recursive=True) # find file
  311. if not files and hard:
  312. raise FileNotFoundError(f"'{file}' does not exist")
  313. elif len(files) > 1 and hard:
  314. raise FileNotFoundError(f"Multiple files match '{file}', specify exact path: {files}")
  315. return files[0] if len(files) else [] # return file
  316. def check_yaml(file, suffix=('.yaml', '.yml'), hard=True):
  317. """Search/download YAML file (if necessary) and return path, checking suffix."""
  318. return check_file(file, suffix, hard=hard)
  319. def check_imshow(warn=False):
  320. """Check if environment supports image displays."""
  321. try:
  322. if LINUX:
  323. assert 'DISPLAY' in os.environ and not is_docker() and not is_colab() and not is_kaggle()
  324. cv2.imshow('test', np.zeros((8, 8, 3), dtype=np.uint8)) # show a small 8-pixel image
  325. cv2.waitKey(1)
  326. cv2.destroyAllWindows()
  327. cv2.waitKey(1)
  328. return True
  329. except Exception as e:
  330. if warn:
  331. LOGGER.warning(f'WARNING ⚠️ Environment does not support cv2.imshow() or PIL Image.show()\n{e}')
  332. return False
  333. def check_yolo(verbose=True, device=''):
  334. """Return a human-readable YOLO software and hardware summary."""
  335. from ultralytics.utils.torch_utils import select_device
  336. if is_jupyter():
  337. if check_requirements('wandb', install=False):
  338. os.system('pip uninstall -y wandb') # uninstall wandb: unwanted account creation prompt with infinite hang
  339. if is_colab():
  340. shutil.rmtree('sample_data', ignore_errors=True) # remove colab /sample_data directory
  341. if verbose:
  342. # System info
  343. gib = 1 << 30 # bytes per GiB
  344. ram = psutil.virtual_memory().total
  345. total, used, free = shutil.disk_usage('/')
  346. s = f'({os.cpu_count()} CPUs, {ram / gib:.1f} GB RAM, {(total - free) / gib:.1f}/{total / gib:.1f} GB disk)'
  347. with contextlib.suppress(Exception): # clear display if ipython is installed
  348. from IPython import display
  349. display.clear_output()
  350. else:
  351. s = ''
  352. select_device(device=device, newline=False)
  353. LOGGER.info(f'Setup complete ✅ {s}')
  354. def check_amp(model):
  355. """
  356. This function checks the PyTorch Automatic Mixed Precision (AMP) functionality of a YOLOv8 model.
  357. If the checks fail, it means there are anomalies with AMP on the system that may cause NaN losses or zero-mAP
  358. results, so AMP will be disabled during training.
  359. Args:
  360. model (nn.Module): A YOLOv8 model instance.
  361. Example:
  362. ```python
  363. from ultralytics import YOLO
  364. from ultralytics.utils.checks import check_amp
  365. model = YOLO('yolov8n.pt').model.cuda()
  366. check_amp(model)
  367. ```
  368. Returns:
  369. (bool): Returns True if the AMP functionality works correctly with YOLOv8 model, else False.
  370. """
  371. device = next(model.parameters()).device # get model device
  372. if device.type in ('cpu', 'mps'):
  373. return False # AMP only used on CUDA devices
  374. def amp_allclose(m, im):
  375. """All close FP32 vs AMP results."""
  376. a = m(im, device=device, verbose=False)[0].boxes.data # FP32 inference
  377. with torch.cuda.amp.autocast(True):
  378. b = m(im, device=device, verbose=False)[0].boxes.data # AMP inference
  379. del m
  380. return a.shape == b.shape and torch.allclose(a, b.float(), atol=0.5) # close to 0.5 absolute tolerance
  381. im = ASSETS / 'bus.jpg' # image to check
  382. prefix = colorstr('AMP: ')
  383. LOGGER.info(f'{prefix}running Automatic Mixed Precision (AMP) checks with YOLOv8n...')
  384. warning_msg = "Setting 'amp=True'. If you experience zero-mAP or NaN losses you can disable AMP with amp=False."
  385. try:
  386. from ultralytics import YOLO
  387. assert amp_allclose(YOLO('yolov8n.pt'), im)
  388. LOGGER.info(f'{prefix}checks passed ✅')
  389. except ConnectionError:
  390. LOGGER.warning(f'{prefix}checks skipped ⚠️, offline and unable to download YOLOv8n. {warning_msg}')
  391. except (AttributeError, ModuleNotFoundError):
  392. LOGGER.warning(
  393. f'{prefix}checks skipped ⚠️. Unable to load YOLOv8n due to possible Ultralytics package modifications. {warning_msg}'
  394. )
  395. except AssertionError:
  396. LOGGER.warning(f'{prefix}checks failed ❌. Anomalies were detected with AMP on your system that may lead to '
  397. f'NaN losses or zero-mAP results, so AMP will be disabled during training.')
  398. return False
  399. return True
  400. def git_describe(path=ROOT): # path must be a directory
  401. """Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe."""
  402. with contextlib.suppress(Exception):
  403. return subprocess.check_output(f'git -C {path} describe --tags --long --always', shell=True).decode()[:-1]
  404. return ''
  405. def print_args(args: Optional[dict] = None, show_file=True, show_func=False):
  406. """Print function arguments (optional args dict)."""
  407. def strip_auth(v):
  408. """Clean longer Ultralytics HUB URLs by stripping potential authentication information."""
  409. return clean_url(v) if (isinstance(v, str) and v.startswith('http') and len(v) > 100) else v
  410. x = inspect.currentframe().f_back # previous frame
  411. file, _, func, _, _ = inspect.getframeinfo(x)
  412. if args is None: # get args automatically
  413. args, _, _, frm = inspect.getargvalues(x)
  414. args = {k: v for k, v in frm.items() if k in args}
  415. try:
  416. file = Path(file).resolve().relative_to(ROOT).with_suffix('')
  417. except ValueError:
  418. file = Path(file).stem
  419. s = (f'{file}: ' if show_file else '') + (f'{func}: ' if show_func else '')
  420. LOGGER.info(colorstr(s) + ', '.join(f'{k}={strip_auth(v)}' for k, v in args.items()))