checks.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 importlib import metadata
  13. from pathlib import Path
  14. from typing import Optional
  15. import cv2
  16. import numpy as np
  17. import requests
  18. import torch
  19. from ultralytics.utils import (
  20. ASSETS,
  21. AUTOINSTALL,
  22. IS_COLAB,
  23. IS_JUPYTER,
  24. IS_KAGGLE,
  25. IS_PIP_PACKAGE,
  26. LINUX,
  27. LOGGER,
  28. ONLINE,
  29. PYTHON_VERSION,
  30. ROOT,
  31. TORCHVISION_VERSION,
  32. USER_CONFIG_DIR,
  33. Retry,
  34. SimpleNamespace,
  35. ThreadingLocked,
  36. TryExcept,
  37. clean_url,
  38. colorstr,
  39. downloads,
  40. emojis,
  41. is_github_action_running,
  42. url2file,
  43. )
  44. def parse_requirements(file_path=ROOT.parent / "requirements.txt", package=""):
  45. """
  46. Parse a requirements.txt file, ignoring lines that start with '#' and any text after '#'.
  47. Args:
  48. file_path (Path): Path to the requirements.txt file.
  49. package (str, optional): Python package to use instead of requirements.txt file, i.e. package='ultralytics'.
  50. Returns:
  51. (List[Dict[str, str]]): List of parsed requirements as dictionaries with `name` and `specifier` keys.
  52. Example:
  53. ```python
  54. from ultralytics.utils.checks import parse_requirements
  55. parse_requirements(package='ultralytics')
  56. ```
  57. """
  58. if package:
  59. requires = [x for x in metadata.distribution(package).requires if "extra == " not in x]
  60. else:
  61. requires = Path(file_path).read_text().splitlines()
  62. requirements = []
  63. for line in requires:
  64. line = line.strip()
  65. if line and not line.startswith("#"):
  66. line = line.split("#")[0].strip() # ignore inline comments
  67. match = re.match(r"([a-zA-Z0-9-_]+)\s*([<>!=~]+.*)?", line)
  68. if match:
  69. requirements.append(SimpleNamespace(name=match[1], specifier=match[2].strip() if match[2] else ""))
  70. return requirements
  71. def parse_version(version="0.0.0") -> tuple:
  72. """
  73. Convert a version string to a tuple of integers, ignoring any extra non-numeric string attached to the version. This
  74. function replaces deprecated 'pkg_resources.parse_version(v)'.
  75. Args:
  76. version (str): Version string, i.e. '2.0.1+cpu'
  77. Returns:
  78. (tuple): Tuple of integers representing the numeric part of the version and the extra string, i.e. (2, 0, 1)
  79. """
  80. try:
  81. return tuple(map(int, re.findall(r"\d+", version)[:3])) # '2.0.1+cpu' -> (2, 0, 1)
  82. except Exception as e:
  83. LOGGER.warning(f"WARNING ⚠️ failure for parse_version({version}), returning (0, 0, 0): {e}")
  84. return 0, 0, 0
  85. def is_ascii(s) -> bool:
  86. """
  87. Check if a string is composed of only ASCII characters.
  88. Args:
  89. s (str): String to be checked.
  90. Returns:
  91. (bool): True if the string is composed only of ASCII characters, False otherwise.
  92. """
  93. # Convert list, tuple, None, etc. to string
  94. s = str(s)
  95. # Check if the string is composed of only ASCII characters
  96. return all(ord(c) < 128 for c in s)
  97. def check_imgsz(imgsz, stride=32, min_dim=1, max_dim=2, floor=0):
  98. """
  99. Verify image size is a multiple of the given stride in each dimension. If the image size is not a multiple of the
  100. stride, update it to the nearest multiple of the stride that is greater than or equal to the given floor value.
  101. Args:
  102. imgsz (int | cList[int]): Image size.
  103. stride (int): Stride value.
  104. min_dim (int): Minimum number of dimensions.
  105. max_dim (int): Maximum number of dimensions.
  106. floor (int): Minimum allowed value for image size.
  107. Returns:
  108. (List[int]): Updated image size.
  109. """
  110. # Convert stride to integer if it is a tensor
  111. stride = int(stride.max() if isinstance(stride, torch.Tensor) else stride)
  112. # Convert image size to list if it is an integer
  113. if isinstance(imgsz, int):
  114. imgsz = [imgsz]
  115. elif isinstance(imgsz, (list, tuple)):
  116. imgsz = list(imgsz)
  117. elif isinstance(imgsz, str): # i.e. '640' or '[640,640]'
  118. imgsz = [int(imgsz)] if imgsz.isnumeric() else eval(imgsz)
  119. else:
  120. raise TypeError(
  121. f"'imgsz={imgsz}' is of invalid type {type(imgsz).__name__}. "
  122. f"Valid imgsz types are int i.e. 'imgsz=640' or list i.e. 'imgsz=[640,640]'"
  123. )
  124. # Apply max_dim
  125. if len(imgsz) > max_dim:
  126. msg = (
  127. "'train' and 'val' imgsz must be an integer, while 'predict' and 'export' imgsz may be a [h, w] list "
  128. "or an integer, i.e. 'yolo export imgsz=640,480' or 'yolo export imgsz=640'"
  129. )
  130. if max_dim != 1:
  131. raise ValueError(f"imgsz={imgsz} is not a valid image size. {msg}")
  132. LOGGER.warning(f"WARNING ⚠️ updating to 'imgsz={max(imgsz)}'. {msg}")
  133. imgsz = [max(imgsz)]
  134. # Make image size a multiple of the stride
  135. sz = [max(math.ceil(x / stride) * stride, floor) for x in imgsz]
  136. # Print warning message if image size was updated
  137. if sz != imgsz:
  138. LOGGER.warning(f"WARNING ⚠️ imgsz={imgsz} must be multiple of max stride {stride}, updating to {sz}")
  139. # Add missing dimensions if necessary
  140. 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
  141. return sz
  142. def check_version(
  143. current: str = "0.0.0",
  144. required: str = "0.0.0",
  145. name: str = "version",
  146. hard: bool = False,
  147. verbose: bool = False,
  148. msg: str = "",
  149. ) -> bool:
  150. """
  151. Check current version against the required version or range.
  152. Args:
  153. current (str): Current version or package name to get version from.
  154. required (str): Required version or range (in pip-style format).
  155. name (str, optional): Name to be used in warning message.
  156. hard (bool, optional): If True, raise an AssertionError if the requirement is not met.
  157. verbose (bool, optional): If True, print warning message if requirement is not met.
  158. msg (str, optional): Extra message to display if verbose.
  159. Returns:
  160. (bool): True if requirement is met, False otherwise.
  161. Example:
  162. ```python
  163. # Check if current version is exactly 22.04
  164. check_version(current='22.04', required='==22.04')
  165. # Check if current version is greater than or equal to 22.04
  166. check_version(current='22.10', required='22.04') # assumes '>=' inequality if none passed
  167. # Check if current version is less than or equal to 22.04
  168. check_version(current='22.04', required='<=22.04')
  169. # Check if current version is between 20.04 (inclusive) and 22.04 (exclusive)
  170. check_version(current='21.10', required='>20.04,<22.04')
  171. ```
  172. """
  173. if not current: # if current is '' or None
  174. LOGGER.warning(f"WARNING ⚠️ invalid check_version({current}, {required}) requested, please check values.")
  175. return True
  176. elif not current[0].isdigit(): # current is package name rather than version string, i.e. current='ultralytics'
  177. try:
  178. name = current # assigned package name to 'name' arg
  179. current = metadata.version(current) # get version string from package name
  180. except metadata.PackageNotFoundError as e:
  181. if hard:
  182. raise ModuleNotFoundError(emojis(f"WARNING ⚠️ {current} package is required but not installed")) from e
  183. else:
  184. return False
  185. if not required: # if required is '' or None
  186. return True
  187. op = ""
  188. version = ""
  189. result = True
  190. c = parse_version(current) # '1.2.3' -> (1, 2, 3)
  191. for r in required.strip(",").split(","):
  192. op, version = re.match(r"([^0-9]*)([\d.]+)", r).groups() # split '>=22.04' -> ('>=', '22.04')
  193. v = parse_version(version) # '1.2.3' -> (1, 2, 3)
  194. if op == "==" and c != v:
  195. result = False
  196. elif op == "!=" and c == v:
  197. result = False
  198. elif op in {">=", ""} and not (c >= v): # if no constraint passed assume '>=required'
  199. result = False
  200. elif op == "<=" and not (c <= v):
  201. result = False
  202. elif op == ">" and not (c > v):
  203. result = False
  204. elif op == "<" and not (c < v):
  205. result = False
  206. if not result:
  207. warning = f"WARNING ⚠️ {name}{op}{version} is required, but {name}=={current} is currently installed {msg}"
  208. if hard:
  209. raise ModuleNotFoundError(emojis(warning)) # assert version requirements met
  210. if verbose:
  211. LOGGER.warning(warning)
  212. return result
  213. def check_latest_pypi_version(package_name="ultralytics"):
  214. """
  215. Returns the latest version of a PyPI package without downloading or installing it.
  216. Parameters:
  217. package_name (str): The name of the package to find the latest version for.
  218. Returns:
  219. (str): The latest version of the package.
  220. """
  221. with contextlib.suppress(Exception):
  222. requests.packages.urllib3.disable_warnings() # Disable the InsecureRequestWarning
  223. response = requests.get(f"https://pypi.org/pypi/{package_name}/json", timeout=3)
  224. if response.status_code == 200:
  225. return response.json()["info"]["version"]
  226. def check_pip_update_available():
  227. """
  228. Checks if a new version of the ultralytics package is available on PyPI.
  229. Returns:
  230. (bool): True if an update is available, False otherwise.
  231. """
  232. if ONLINE and IS_PIP_PACKAGE:
  233. with contextlib.suppress(Exception):
  234. from ultralytics import __version__
  235. latest = check_latest_pypi_version()
  236. if check_version(__version__, f"<{latest}"): # check if current version is < latest version
  237. LOGGER.info(
  238. f"New https://pypi.org/project/ultralytics/{latest} available 😃 "
  239. f"Update with 'pip install -U ultralytics'"
  240. )
  241. return True
  242. return False
  243. @ThreadingLocked()
  244. def check_font(font="Arial.ttf"):
  245. """
  246. Find font locally or download to user's configuration directory if it does not already exist.
  247. Args:
  248. font (str): Path or name of font.
  249. Returns:
  250. file (Path): Resolved font file path.
  251. """
  252. from matplotlib import font_manager
  253. # Check USER_CONFIG_DIR
  254. name = Path(font).name
  255. file = USER_CONFIG_DIR / name
  256. if file.exists():
  257. return file
  258. # Check system fonts
  259. matches = [s for s in font_manager.findSystemFonts() if font in s]
  260. if any(matches):
  261. return matches[0]
  262. # Download to USER_CONFIG_DIR if missing
  263. url = f"https://ultralytics.com/assets/{name}"
  264. if downloads.is_url(url, check=True):
  265. downloads.safe_download(url=url, file=file)
  266. return file
  267. def check_python(minimum: str = "3.8.0", hard: bool = True) -> bool:
  268. """
  269. Check current python version against the required minimum version.
  270. Args:
  271. minimum (str): Required minimum version of python.
  272. hard (bool, optional): If True, raise an AssertionError if the requirement is not met.
  273. Returns:
  274. (bool): Whether the installed Python version meets the minimum constraints.
  275. """
  276. return check_version(PYTHON_VERSION, minimum, name="Python", hard=hard)
  277. @TryExcept()
  278. def check_requirements(requirements=ROOT.parent / "requirements.txt", exclude=(), install=True, cmds=""):
  279. """
  280. Check if installed dependencies meet YOLOv8 requirements and attempt to auto-update if needed.
  281. Args:
  282. requirements (Union[Path, str, List[str]]): Path to a requirements.txt file, a single package requirement as a
  283. string, or a list of package requirements as strings.
  284. exclude (Tuple[str]): Tuple of package names to exclude from checking.
  285. install (bool): If True, attempt to auto-update packages that don't meet requirements.
  286. cmds (str): Additional commands to pass to the pip install command when auto-updating.
  287. Example:
  288. ```python
  289. from ultralytics.utils.checks import check_requirements
  290. # Check a requirements.txt file
  291. check_requirements('path/to/requirements.txt')
  292. # Check a single package
  293. check_requirements('ultralytics>=8.0.0')
  294. # Check multiple packages
  295. check_requirements(['numpy', 'ultralytics>=8.0.0'])
  296. ```
  297. """
  298. prefix = colorstr("red", "bold", "requirements:")
  299. check_python() # check python version
  300. check_torchvision() # check torch-torchvision compatibility
  301. if isinstance(requirements, Path): # requirements.txt file
  302. file = requirements.resolve()
  303. assert file.exists(), f"{prefix} {file} not found, check failed."
  304. requirements = [f"{x.name}{x.specifier}" for x in parse_requirements(file) if x.name not in exclude]
  305. elif isinstance(requirements, str):
  306. requirements = [requirements]
  307. pkgs = []
  308. for r in requirements:
  309. r_stripped = r.split("/")[-1].replace(".git", "") # replace git+https://org/repo.git -> 'repo'
  310. match = re.match(r"([a-zA-Z0-9-_]+)([<>!=~]+.*)?", r_stripped)
  311. name, required = match[1], match[2].strip() if match[2] else ""
  312. try:
  313. assert check_version(metadata.version(name), required) # exception if requirements not met
  314. except (AssertionError, metadata.PackageNotFoundError):
  315. pkgs.append(r)
  316. @Retry(times=2, delay=1)
  317. def attempt_install(packages, commands):
  318. """Attempt pip install command with retries on failure."""
  319. return subprocess.check_output(f"pip install --no-cache-dir {packages} {commands}", shell=True).decode()
  320. s = " ".join(f'"{x}"' for x in pkgs) # console string
  321. if s:
  322. if install and AUTOINSTALL: # check environment variable
  323. n = len(pkgs) # number of packages updates
  324. LOGGER.info(f"{prefix} Ultralytics requirement{'s' * (n > 1)} {pkgs} not found, attempting AutoUpdate...")
  325. try:
  326. t = time.time()
  327. assert ONLINE, "AutoUpdate skipped (offline)"
  328. LOGGER.info(attempt_install(s, cmds))
  329. dt = time.time() - t
  330. LOGGER.info(
  331. f"{prefix} AutoUpdate success ✅ {dt:.1f}s, installed {n} package{'s' * (n > 1)}: {pkgs}\n"
  332. f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n"
  333. )
  334. except Exception as e:
  335. LOGGER.warning(f"{prefix} ❌ {e}")
  336. return False
  337. else:
  338. return False
  339. return True
  340. def check_torchvision():
  341. """
  342. Checks the installed versions of PyTorch and Torchvision to ensure they're compatible.
  343. This function checks the installed versions of PyTorch and Torchvision, and warns if they're incompatible according
  344. to the provided compatibility table based on:
  345. https://github.com/pytorch/vision#installation.
  346. The compatibility table is a dictionary where the keys are PyTorch versions and the values are lists of compatible
  347. Torchvision versions.
  348. """
  349. # Compatibility table
  350. compatibility_table = {
  351. "2.3": ["0.18"],
  352. "2.2": ["0.17"],
  353. "2.1": ["0.16"],
  354. "2.0": ["0.15"],
  355. "1.13": ["0.14"],
  356. "1.12": ["0.13"],
  357. }
  358. # Extract only the major and minor versions
  359. v_torch = ".".join(torch.__version__.split("+")[0].split(".")[:2])
  360. if v_torch in compatibility_table:
  361. compatible_versions = compatibility_table[v_torch]
  362. v_torchvision = ".".join(TORCHVISION_VERSION.split("+")[0].split(".")[:2])
  363. if all(v_torchvision != v for v in compatible_versions):
  364. print(
  365. f"WARNING ⚠️ torchvision=={v_torchvision} is incompatible with torch=={v_torch}.\n"
  366. f"Run 'pip install torchvision=={compatible_versions[0]}' to fix torchvision or "
  367. "'pip install -U torch torchvision' to update both.\n"
  368. "For a full compatibility table see https://github.com/pytorch/vision#installation"
  369. )
  370. def check_suffix(file="yolov8n.pt", suffix=".pt", msg=""):
  371. """Check file(s) for acceptable suffix."""
  372. if file and suffix:
  373. if isinstance(suffix, str):
  374. suffix = (suffix,)
  375. for f in file if isinstance(file, (list, tuple)) else [file]:
  376. s = Path(f).suffix.lower().strip() # file suffix
  377. if len(s):
  378. assert s in suffix, f"{msg}{f} acceptable suffix is {suffix}, not {s}"
  379. def check_yolov5u_filename(file: str, verbose: bool = True):
  380. """Replace legacy YOLOv5 filenames with updated YOLOv5u filenames."""
  381. if "yolov3" in file or "yolov5" in file:
  382. if "u.yaml" in file:
  383. file = file.replace("u.yaml", ".yaml") # i.e. yolov5nu.yaml -> yolov5n.yaml
  384. elif ".pt" in file and "u" not in file:
  385. original_file = file
  386. file = re.sub(r"(.*yolov5([nsmlx]))\.pt", "\\1u.pt", file) # i.e. yolov5n.pt -> yolov5nu.pt
  387. file = re.sub(r"(.*yolov5([nsmlx])6)\.pt", "\\1u.pt", file) # i.e. yolov5n6.pt -> yolov5n6u.pt
  388. file = re.sub(r"(.*yolov3(|-tiny|-spp))\.pt", "\\1u.pt", file) # i.e. yolov3-spp.pt -> yolov3-sppu.pt
  389. if file != original_file and verbose:
  390. LOGGER.info(
  391. f"PRO TIP 💡 Replace 'model={original_file}' with new 'model={file}'.\nYOLOv5 'u' models are "
  392. f"trained with https://github.com/ultralytics/ultralytics and feature improved performance vs "
  393. f"standard YOLOv5 models trained with https://github.com/ultralytics/yolov5.\n"
  394. )
  395. return file
  396. def check_model_file_from_stem(model="yolov8n"):
  397. """Return a model filename from a valid model stem."""
  398. if model and not Path(model).suffix and Path(model).stem in downloads.GITHUB_ASSETS_STEMS:
  399. return Path(model).with_suffix(".pt") # add suffix, i.e. yolov8n -> yolov8n.pt
  400. else:
  401. return model
  402. def check_file(file, suffix="", download=True, hard=True):
  403. """Search/download file (if necessary) and return path."""
  404. check_suffix(file, suffix) # optional
  405. file = str(file).strip() # convert to string and strip spaces
  406. file = check_yolov5u_filename(file) # yolov5n -> yolov5nu
  407. if (
  408. not file
  409. or ("://" not in file and Path(file).exists()) # '://' check required in Windows Python<3.10
  410. or file.lower().startswith("grpc://")
  411. ): # file exists or gRPC Triton images
  412. return file
  413. elif download and file.lower().startswith(("https://", "http://", "rtsp://", "rtmp://", "tcp://")): # download
  414. url = file # warning: Pathlib turns :// -> :/
  415. file = url2file(file) # '%2F' to '/', split https://url.com/file.txt?auth
  416. if Path(file).exists():
  417. LOGGER.info(f"Found {clean_url(url)} locally at {file}") # file already exists
  418. else:
  419. downloads.safe_download(url=url, file=file, unzip=False)
  420. return file
  421. else: # search
  422. files = glob.glob(str(ROOT / "**" / file), recursive=True) or glob.glob(str(ROOT.parent / file)) # find file
  423. if not files and hard:
  424. raise FileNotFoundError(f"'{file}' does not exist")
  425. elif len(files) > 1 and hard:
  426. raise FileNotFoundError(f"Multiple files match '{file}', specify exact path: {files}")
  427. return files[0] if len(files) else [] # return file
  428. def check_yaml(file, suffix=(".yaml", ".yml"), hard=True):
  429. """Search/download YAML file (if necessary) and return path, checking suffix."""
  430. return check_file(file, suffix, hard=hard)
  431. def check_is_path_safe(basedir, path):
  432. """
  433. Check if the resolved path is under the intended directory to prevent path traversal.
  434. Args:
  435. basedir (Path | str): The intended directory.
  436. path (Path | str): The path to check.
  437. Returns:
  438. (bool): True if the path is safe, False otherwise.
  439. """
  440. base_dir_resolved = Path(basedir).resolve()
  441. path_resolved = Path(path).resolve()
  442. return path_resolved.exists() and path_resolved.parts[: len(base_dir_resolved.parts)] == base_dir_resolved.parts
  443. def check_imshow(warn=False):
  444. """Check if environment supports image displays."""
  445. try:
  446. if LINUX:
  447. assert not IS_COLAB and not IS_KAGGLE
  448. assert "DISPLAY" in os.environ, "The DISPLAY environment variable isn't set."
  449. cv2.imshow("test", np.zeros((8, 8, 3), dtype=np.uint8)) # show a small 8-pixel image
  450. cv2.waitKey(1)
  451. cv2.destroyAllWindows()
  452. cv2.waitKey(1)
  453. return True
  454. except Exception as e:
  455. if warn:
  456. LOGGER.warning(f"WARNING ⚠️ Environment does not support cv2.imshow() or PIL Image.show()\n{e}")
  457. return False
  458. def check_yolo(verbose=True, device=""):
  459. """Return a human-readable YOLO software and hardware summary."""
  460. import psutil
  461. from ultralytics.utils.torch_utils import select_device
  462. if IS_JUPYTER:
  463. if check_requirements("wandb", install=False):
  464. os.system("pip uninstall -y wandb") # uninstall wandb: unwanted account creation prompt with infinite hang
  465. if IS_COLAB:
  466. shutil.rmtree("sample_data", ignore_errors=True) # remove colab /sample_data directory
  467. if verbose:
  468. # System info
  469. gib = 1 << 30 # bytes per GiB
  470. ram = psutil.virtual_memory().total
  471. total, used, free = shutil.disk_usage("/")
  472. s = f"({os.cpu_count()} CPUs, {ram / gib:.1f} GB RAM, {(total - free) / gib:.1f}/{total / gib:.1f} GB disk)"
  473. with contextlib.suppress(Exception): # clear display if ipython is installed
  474. from IPython import display
  475. display.clear_output()
  476. else:
  477. s = ""
  478. select_device(device=device, newline=False)
  479. LOGGER.info(f"Setup complete ✅ {s}")
  480. def collect_system_info():
  481. """Collect and print relevant system information including OS, Python, RAM, CPU, and CUDA."""
  482. import psutil
  483. from ultralytics.utils import ENVIRONMENT, IS_GIT_DIR
  484. from ultralytics.utils.torch_utils import get_cpu_info
  485. ram_info = psutil.virtual_memory().total / (1024**3) # Convert bytes to GB
  486. check_yolo()
  487. LOGGER.info(
  488. f"\n{'OS':<20}{platform.platform()}\n"
  489. f"{'Environment':<20}{ENVIRONMENT}\n"
  490. f"{'Python':<20}{PYTHON_VERSION}\n"
  491. f"{'Install':<20}{'git' if IS_GIT_DIR else 'pip' if IS_PIP_PACKAGE else 'other'}\n"
  492. f"{'RAM':<20}{ram_info:.2f} GB\n"
  493. f"{'CPU':<20}{get_cpu_info()}\n"
  494. f"{'CUDA':<20}{torch.version.cuda if torch and torch.cuda.is_available() else None}\n"
  495. )
  496. for r in parse_requirements(package="ultralytics"):
  497. try:
  498. current = metadata.version(r.name)
  499. is_met = "✅ " if check_version(current, str(r.specifier), hard=True) else "❌ "
  500. except metadata.PackageNotFoundError:
  501. current = "(not installed)"
  502. is_met = "❌ "
  503. LOGGER.info(f"{r.name:<20}{is_met}{current}{r.specifier}")
  504. if is_github_action_running():
  505. LOGGER.info(
  506. f"\nRUNNER_OS: {os.getenv('RUNNER_OS')}\n"
  507. f"GITHUB_EVENT_NAME: {os.getenv('GITHUB_EVENT_NAME')}\n"
  508. f"GITHUB_WORKFLOW: {os.getenv('GITHUB_WORKFLOW')}\n"
  509. f"GITHUB_ACTOR: {os.getenv('GITHUB_ACTOR')}\n"
  510. f"GITHUB_REPOSITORY: {os.getenv('GITHUB_REPOSITORY')}\n"
  511. f"GITHUB_REPOSITORY_OWNER: {os.getenv('GITHUB_REPOSITORY_OWNER')}\n"
  512. )
  513. def check_amp(model):
  514. """
  515. This function checks the PyTorch Automatic Mixed Precision (AMP) functionality of a YOLOv8 model. If the checks
  516. fail, it means there are anomalies with AMP on the system that may cause NaN losses or zero-mAP results, so AMP will
  517. be disabled during training.
  518. Args:
  519. model (nn.Module): A YOLOv8 model instance.
  520. Example:
  521. ```python
  522. from ultralytics import YOLO
  523. from ultralytics.utils.checks import check_amp
  524. model = YOLO('yolov8n.pt').model.cuda()
  525. check_amp(model)
  526. ```
  527. Returns:
  528. (bool): Returns True if the AMP functionality works correctly with YOLOv8 model, else False.
  529. """
  530. device = next(model.parameters()).device # get model device
  531. if device.type in {"cpu", "mps"}:
  532. return False # AMP only used on CUDA devices
  533. def amp_allclose(m, im):
  534. """All close FP32 vs AMP results."""
  535. a = m(im, device=device, verbose=False)[0].boxes.data # FP32 inference
  536. with torch.cuda.amp.autocast(True):
  537. b = m(im, device=device, verbose=False)[0].boxes.data # AMP inference
  538. del m
  539. return a.shape == b.shape and torch.allclose(a, b.float(), atol=0.5) # close to 0.5 absolute tolerance
  540. im = ASSETS / "bus.jpg" # image to check
  541. prefix = colorstr("AMP: ")
  542. LOGGER.info(f"{prefix}running Automatic Mixed Precision (AMP) checks with YOLOv8n...")
  543. warning_msg = "Setting 'amp=True'. If you experience zero-mAP or NaN losses you can disable AMP with amp=False."
  544. try:
  545. from ultralytics import YOLO
  546. assert amp_allclose(YOLO("yolov8n.pt"), im)
  547. LOGGER.info(f"{prefix}checks passed ✅")
  548. except ConnectionError:
  549. LOGGER.warning(f"{prefix}checks skipped ⚠️, offline and unable to download YOLOv8n. {warning_msg}")
  550. except (AttributeError, ModuleNotFoundError):
  551. LOGGER.warning(
  552. f"{prefix}checks skipped ⚠️. "
  553. f"Unable to load YOLOv8n due to possible Ultralytics package modifications. {warning_msg}"
  554. )
  555. except AssertionError:
  556. LOGGER.warning(
  557. f"{prefix}checks failed ❌. Anomalies were detected with AMP on your system that may lead to "
  558. f"NaN losses or zero-mAP results, so AMP will be disabled during training."
  559. )
  560. return False
  561. return True
  562. def git_describe(path=ROOT): # path must be a directory
  563. """Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe."""
  564. with contextlib.suppress(Exception):
  565. return subprocess.check_output(f"git -C {path} describe --tags --long --always", shell=True).decode()[:-1]
  566. return ""
  567. def print_args(args: Optional[dict] = None, show_file=True, show_func=False):
  568. """Print function arguments (optional args dict)."""
  569. def strip_auth(v):
  570. """Clean longer Ultralytics HUB URLs by stripping potential authentication information."""
  571. return clean_url(v) if (isinstance(v, str) and v.startswith("http") and len(v) > 100) else v
  572. x = inspect.currentframe().f_back # previous frame
  573. file, _, func, _, _ = inspect.getframeinfo(x)
  574. if args is None: # get args automatically
  575. args, _, _, frm = inspect.getargvalues(x)
  576. args = {k: v for k, v in frm.items() if k in args}
  577. try:
  578. file = Path(file).resolve().relative_to(ROOT).with_suffix("")
  579. except ValueError:
  580. file = Path(file).stem
  581. s = (f"{file}: " if show_file else "") + (f"{func}: " if show_func else "")
  582. LOGGER.info(colorstr(s) + ", ".join(f"{k}={strip_auth(v)}" for k, v in args.items()))
  583. def cuda_device_count() -> int:
  584. """
  585. Get the number of NVIDIA GPUs available in the environment.
  586. Returns:
  587. (int): The number of NVIDIA GPUs available.
  588. """
  589. try:
  590. # Run the nvidia-smi command and capture its output
  591. output = subprocess.check_output(
  592. ["nvidia-smi", "--query-gpu=count", "--format=csv,noheader,nounits"], encoding="utf-8"
  593. )
  594. # Take the first line and strip any leading/trailing white space
  595. first_line = output.strip().split("\n")[0]
  596. return int(first_line)
  597. except (subprocess.CalledProcessError, FileNotFoundError, ValueError):
  598. # If the command fails, nvidia-smi is not found, or output is not an integer, assume no GPUs are available
  599. return 0
  600. def cuda_is_available() -> bool:
  601. """
  602. Check if CUDA is available in the environment.
  603. Returns:
  604. (bool): True if one or more NVIDIA GPUs are available, False otherwise.
  605. """
  606. return cuda_device_count() > 0
  607. # Define constants
  608. IS_PYTHON_MINIMUM_3_10 = check_python("3.10", hard=False)
  609. IS_PYTHON_3_12 = PYTHON_VERSION.startswith("3.12")