checks.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. import glob
  3. import inspect
  4. import math
  5. import os
  6. import platform
  7. import re
  8. import shutil
  9. import subprocess
  10. import time
  11. from importlib import metadata
  12. from pathlib import Path
  13. from typing import Optional
  14. import cv2
  15. import numpy as np
  16. import requests
  17. import torch
  18. from ultralytics.utils import (
  19. ASSETS,
  20. AUTOINSTALL,
  21. IS_COLAB,
  22. IS_GIT_DIR,
  23. IS_KAGGLE,
  24. IS_PIP_PACKAGE,
  25. LINUX,
  26. LOGGER,
  27. MACOS,
  28. ONLINE,
  29. PYTHON_VERSION,
  30. ROOT,
  31. TORCHVISION_VERSION,
  32. USER_CONFIG_DIR,
  33. WINDOWS,
  34. Retry,
  35. SimpleNamespace,
  36. ThreadingLocked,
  37. TryExcept,
  38. clean_url,
  39. colorstr,
  40. downloads,
  41. emojis,
  42. is_github_action_running,
  43. url2file,
  44. )
  45. def parse_requirements(file_path=ROOT.parent / "requirements.txt", package=""):
  46. """
  47. Parse a requirements.txt file, ignoring lines that start with '#' and any text after '#'.
  48. Args:
  49. file_path (Path): Path to the requirements.txt file.
  50. package (str, optional): Python package to use instead of requirements.txt file, i.e. package='ultralytics'.
  51. Returns:
  52. (List[Dict[str, str]]): List of parsed requirements as dictionaries with `name` and `specifier` keys.
  53. Example:
  54. ```python
  55. from ultralytics.utils.checks import parse_requirements
  56. parse_requirements(package="ultralytics")
  57. ```
  58. """
  59. if package:
  60. requires = [x for x in metadata.distribution(package).requires if "extra == " not in x]
  61. else:
  62. requires = Path(file_path).read_text().splitlines()
  63. requirements = []
  64. for line in requires:
  65. line = line.strip()
  66. if line and not line.startswith("#"):
  67. line = line.split("#")[0].strip() # ignore inline comments
  68. if match := re.match(r"([a-zA-Z0-9-_]+)\s*([<>!=~]+.*)?", line):
  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. if "sys_platform" in required and ( # i.e. required='<2.4.0,>=1.8.0; sys_platform == "win32"'
  188. (WINDOWS and "win32" not in required)
  189. or (LINUX and "linux" not in required)
  190. or (MACOS and "macos" not in required and "darwin" not in required)
  191. ):
  192. return True
  193. op = ""
  194. version = ""
  195. result = True
  196. c = parse_version(current) # '1.2.3' -> (1, 2, 3)
  197. for r in required.strip(",").split(","):
  198. op, version = re.match(r"([^0-9]*)([\d.]+)", r).groups() # split '>=22.04' -> ('>=', '22.04')
  199. if not op:
  200. op = ">=" # assume >= if no op passed
  201. v = parse_version(version) # '1.2.3' -> (1, 2, 3)
  202. if op == "==" and c != v:
  203. result = False
  204. elif op == "!=" and c == v:
  205. result = False
  206. elif op == ">=" and not (c >= v):
  207. result = False
  208. elif op == "<=" and not (c <= v):
  209. result = False
  210. elif op == ">" and not (c > v):
  211. result = False
  212. elif op == "<" and not (c < v):
  213. result = False
  214. if not result:
  215. warning = f"WARNING ⚠️ {name}{op}{version} is required, but {name}=={current} is currently installed {msg}"
  216. if hard:
  217. raise ModuleNotFoundError(emojis(warning)) # assert version requirements met
  218. if verbose:
  219. LOGGER.warning(warning)
  220. return result
  221. def check_latest_pypi_version(package_name="ultralytics"):
  222. """
  223. Returns the latest version of a PyPI package without downloading or installing it.
  224. Args:
  225. package_name (str): The name of the package to find the latest version for.
  226. Returns:
  227. (str): The latest version of the package.
  228. """
  229. try:
  230. requests.packages.urllib3.disable_warnings() # Disable the InsecureRequestWarning
  231. response = requests.get(f"https://pypi.org/pypi/{package_name}/json", timeout=3)
  232. if response.status_code == 200:
  233. return response.json()["info"]["version"]
  234. except Exception:
  235. return None
  236. def check_pip_update_available():
  237. """
  238. Checks if a new version of the ultralytics package is available on PyPI.
  239. Returns:
  240. (bool): True if an update is available, False otherwise.
  241. """
  242. if ONLINE and IS_PIP_PACKAGE:
  243. try:
  244. from ultralytics import __version__
  245. latest = check_latest_pypi_version()
  246. if check_version(__version__, f"<{latest}"): # check if current version is < latest version
  247. LOGGER.info(
  248. f"New https://pypi.org/project/ultralytics/{latest} available 😃 "
  249. f"Update with 'pip install -U ultralytics'"
  250. )
  251. return True
  252. except Exception:
  253. pass
  254. return False
  255. @ThreadingLocked()
  256. def check_font(font="Arial.ttf"):
  257. """
  258. Find font locally or download to user's configuration directory if it does not already exist.
  259. Args:
  260. font (str): Path or name of font.
  261. Returns:
  262. file (Path): Resolved font file path.
  263. """
  264. from matplotlib import font_manager
  265. # Check USER_CONFIG_DIR
  266. name = Path(font).name
  267. file = USER_CONFIG_DIR / name
  268. if file.exists():
  269. return file
  270. # Check system fonts
  271. matches = [s for s in font_manager.findSystemFonts() if font in s]
  272. if any(matches):
  273. return matches[0]
  274. # Download to USER_CONFIG_DIR if missing
  275. url = f"https://github.com/ultralytics/assets/releases/download/v0.0.0/{name}"
  276. if downloads.is_url(url, check=True):
  277. downloads.safe_download(url=url, file=file)
  278. return file
  279. def check_python(minimum: str = "3.8.0", hard: bool = True, verbose: bool = False) -> bool:
  280. """
  281. Check current python version against the required minimum version.
  282. Args:
  283. minimum (str): Required minimum version of python.
  284. hard (bool, optional): If True, raise an AssertionError if the requirement is not met.
  285. verbose (bool, optional): If True, print warning message if requirement is not met.
  286. Returns:
  287. (bool): Whether the installed Python version meets the minimum constraints.
  288. """
  289. return check_version(PYTHON_VERSION, minimum, name="Python", hard=hard, verbose=verbose)
  290. @TryExcept()
  291. def check_requirements(requirements=ROOT.parent / "requirements.txt", exclude=(), install=True, cmds=""):
  292. """
  293. Check if installed dependencies meet YOLOv8 requirements and attempt to auto-update if needed.
  294. Args:
  295. requirements (Union[Path, str, List[str]]): Path to a requirements.txt file, a single package requirement as a
  296. string, or a list of package requirements as strings.
  297. exclude (Tuple[str]): Tuple of package names to exclude from checking.
  298. install (bool): If True, attempt to auto-update packages that don't meet requirements.
  299. cmds (str): Additional commands to pass to the pip install command when auto-updating.
  300. Example:
  301. ```python
  302. from ultralytics.utils.checks import check_requirements
  303. # Check a requirements.txt file
  304. check_requirements("path/to/requirements.txt")
  305. # Check a single package
  306. check_requirements("ultralytics>=8.0.0")
  307. # Check multiple packages
  308. check_requirements(["numpy", "ultralytics>=8.0.0"])
  309. ```
  310. """
  311. prefix = colorstr("red", "bold", "requirements:")
  312. if isinstance(requirements, Path): # requirements.txt file
  313. file = requirements.resolve()
  314. assert file.exists(), f"{prefix} {file} not found, check failed."
  315. requirements = [f"{x.name}{x.specifier}" for x in parse_requirements(file) if x.name not in exclude]
  316. elif isinstance(requirements, str):
  317. requirements = [requirements]
  318. pkgs = []
  319. for r in requirements:
  320. r_stripped = r.split("/")[-1].replace(".git", "") # replace git+https://org/repo.git -> 'repo'
  321. match = re.match(r"([a-zA-Z0-9-_]+)([<>!=~]+.*)?", r_stripped)
  322. name, required = match[1], match[2].strip() if match[2] else ""
  323. try:
  324. assert check_version(metadata.version(name), required) # exception if requirements not met
  325. except (AssertionError, metadata.PackageNotFoundError):
  326. pkgs.append(r)
  327. @Retry(times=2, delay=1)
  328. def attempt_install(packages, commands):
  329. """Attempt pip install command with retries on failure."""
  330. return subprocess.check_output(f"pip install --no-cache-dir {packages} {commands}", shell=True).decode()
  331. s = " ".join(f'"{x}"' for x in pkgs) # console string
  332. if s:
  333. if install and AUTOINSTALL: # check environment variable
  334. n = len(pkgs) # number of packages updates
  335. LOGGER.info(f"{prefix} Ultralytics requirement{'s' * (n > 1)} {pkgs} not found, attempting AutoUpdate...")
  336. try:
  337. t = time.time()
  338. assert ONLINE, "AutoUpdate skipped (offline)"
  339. LOGGER.info(attempt_install(s, cmds))
  340. dt = time.time() - t
  341. LOGGER.info(
  342. f"{prefix} AutoUpdate success ✅ {dt:.1f}s, installed {n} package{'s' * (n > 1)}: {pkgs}\n"
  343. f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n"
  344. )
  345. except Exception as e:
  346. LOGGER.warning(f"{prefix} ❌ {e}")
  347. return False
  348. else:
  349. return False
  350. return True
  351. def check_torchvision():
  352. """
  353. Checks the installed versions of PyTorch and Torchvision to ensure they're compatible.
  354. This function checks the installed versions of PyTorch and Torchvision, and warns if they're incompatible according
  355. to the provided compatibility table based on:
  356. https://github.com/pytorch/vision#installation.
  357. The compatibility table is a dictionary where the keys are PyTorch versions and the values are lists of compatible
  358. Torchvision versions.
  359. """
  360. # Compatibility table
  361. compatibility_table = {
  362. "2.5": ["0.20"],
  363. "2.4": ["0.19"],
  364. "2.3": ["0.18"],
  365. "2.2": ["0.17"],
  366. "2.1": ["0.16"],
  367. "2.0": ["0.15"],
  368. "1.13": ["0.14"],
  369. "1.12": ["0.13"],
  370. }
  371. # Extract only the major and minor versions
  372. v_torch = ".".join(torch.__version__.split("+")[0].split(".")[:2])
  373. if v_torch in compatibility_table:
  374. compatible_versions = compatibility_table[v_torch]
  375. v_torchvision = ".".join(TORCHVISION_VERSION.split("+")[0].split(".")[:2])
  376. if all(v_torchvision != v for v in compatible_versions):
  377. print(
  378. f"WARNING ⚠️ torchvision=={v_torchvision} is incompatible with torch=={v_torch}.\n"
  379. f"Run 'pip install torchvision=={compatible_versions[0]}' to fix torchvision or "
  380. "'pip install -U torch torchvision' to update both.\n"
  381. "For a full compatibility table see https://github.com/pytorch/vision#installation"
  382. )
  383. def check_suffix(file="yolo11n.pt", suffix=".pt", msg=""):
  384. """Check file(s) for acceptable suffix."""
  385. if file and suffix:
  386. if isinstance(suffix, str):
  387. suffix = (suffix,)
  388. for f in file if isinstance(file, (list, tuple)) else [file]:
  389. s = Path(f).suffix.lower().strip() # file suffix
  390. if len(s):
  391. assert s in suffix, f"{msg}{f} acceptable suffix is {suffix}, not {s}"
  392. def check_yolov5u_filename(file: str, verbose: bool = True):
  393. """Replace legacy YOLOv5 filenames with updated YOLOv5u filenames."""
  394. if "yolov3" in file or "yolov5" in file:
  395. if "u.yaml" in file:
  396. file = file.replace("u.yaml", ".yaml") # i.e. yolov5nu.yaml -> yolov5n.yaml
  397. elif ".pt" in file and "u" not in file:
  398. original_file = file
  399. file = re.sub(r"(.*yolov5([nsmlx]))\.pt", "\\1u.pt", file) # i.e. yolov5n.pt -> yolov5nu.pt
  400. file = re.sub(r"(.*yolov5([nsmlx])6)\.pt", "\\1u.pt", file) # i.e. yolov5n6.pt -> yolov5n6u.pt
  401. file = re.sub(r"(.*yolov3(|-tiny|-spp))\.pt", "\\1u.pt", file) # i.e. yolov3-spp.pt -> yolov3-sppu.pt
  402. if file != original_file and verbose:
  403. LOGGER.info(
  404. f"PRO TIP 💡 Replace 'model={original_file}' with new 'model={file}'.\nYOLOv5 'u' models are "
  405. f"trained with https://github.com/ultralytics/ultralytics and feature improved performance vs "
  406. f"standard YOLOv5 models trained with https://github.com/ultralytics/yolov5.\n"
  407. )
  408. return file
  409. def check_model_file_from_stem(model="yolov8n"):
  410. """Return a model filename from a valid model stem."""
  411. if model and not Path(model).suffix and Path(model).stem in downloads.GITHUB_ASSETS_STEMS:
  412. return Path(model).with_suffix(".pt") # add suffix, i.e. yolov8n -> yolov8n.pt
  413. else:
  414. return model
  415. def check_file(file, suffix="", download=True, download_dir=".", hard=True):
  416. """Search/download file (if necessary) and return path."""
  417. check_suffix(file, suffix) # optional
  418. file = str(file).strip() # convert to string and strip spaces
  419. file = check_yolov5u_filename(file) # yolov5n -> yolov5nu
  420. if (
  421. not file
  422. or ("://" not in file and Path(file).exists()) # '://' check required in Windows Python<3.10
  423. or file.lower().startswith("grpc://")
  424. ): # file exists or gRPC Triton images
  425. return file
  426. elif download and file.lower().startswith(("https://", "http://", "rtsp://", "rtmp://", "tcp://")): # download
  427. url = file # warning: Pathlib turns :// -> :/
  428. file = Path(download_dir) / url2file(file) # '%2F' to '/', split https://url.com/file.txt?auth
  429. if file.exists():
  430. LOGGER.info(f"Found {clean_url(url)} locally at {file}") # file already exists
  431. else:
  432. downloads.safe_download(url=url, file=file, unzip=False)
  433. return str(file)
  434. else: # search
  435. files = glob.glob(str(ROOT / "**" / file), recursive=True) or glob.glob(str(ROOT.parent / file)) # find file
  436. if not files and hard:
  437. raise FileNotFoundError(f"'{file}' does not exist")
  438. elif len(files) > 1 and hard:
  439. raise FileNotFoundError(f"Multiple files match '{file}', specify exact path: {files}")
  440. return files[0] if len(files) else [] # return file
  441. def check_yaml(file, suffix=(".yaml", ".yml"), hard=True):
  442. """Search/download YAML file (if necessary) and return path, checking suffix."""
  443. return check_file(file, suffix, hard=hard)
  444. def check_is_path_safe(basedir, path):
  445. """
  446. Check if the resolved path is under the intended directory to prevent path traversal.
  447. Args:
  448. basedir (Path | str): The intended directory.
  449. path (Path | str): The path to check.
  450. Returns:
  451. (bool): True if the path is safe, False otherwise.
  452. """
  453. base_dir_resolved = Path(basedir).resolve()
  454. path_resolved = Path(path).resolve()
  455. return path_resolved.exists() and path_resolved.parts[: len(base_dir_resolved.parts)] == base_dir_resolved.parts
  456. def check_imshow(warn=False):
  457. """Check if environment supports image displays."""
  458. try:
  459. if LINUX:
  460. assert not IS_COLAB and not IS_KAGGLE
  461. assert "DISPLAY" in os.environ, "The DISPLAY environment variable isn't set."
  462. cv2.imshow("test", np.zeros((8, 8, 3), dtype=np.uint8)) # show a small 8-pixel image
  463. cv2.waitKey(1)
  464. cv2.destroyAllWindows()
  465. cv2.waitKey(1)
  466. return True
  467. except Exception as e:
  468. if warn:
  469. LOGGER.warning(f"WARNING ⚠️ Environment does not support cv2.imshow() or PIL Image.show()\n{e}")
  470. return False
  471. def check_yolo(verbose=True, device=""):
  472. """Return a human-readable YOLO software and hardware summary."""
  473. import psutil
  474. from ultralytics.utils.torch_utils import select_device
  475. if IS_COLAB:
  476. shutil.rmtree("sample_data", ignore_errors=True) # remove colab /sample_data directory
  477. if verbose:
  478. # System info
  479. gib = 1 << 30 # bytes per GiB
  480. ram = psutil.virtual_memory().total
  481. total, used, free = shutil.disk_usage("/")
  482. s = f"({os.cpu_count()} CPUs, {ram / gib:.1f} GB RAM, {(total - free) / gib:.1f}/{total / gib:.1f} GB disk)"
  483. try:
  484. from IPython import display
  485. display.clear_output() # clear display if notebook
  486. except ImportError:
  487. pass
  488. else:
  489. s = ""
  490. select_device(device=device, newline=False)
  491. LOGGER.info(f"Setup complete ✅ {s}")
  492. def collect_system_info():
  493. """Collect and print relevant system information including OS, Python, RAM, CPU, and CUDA."""
  494. import psutil
  495. from ultralytics.utils import ENVIRONMENT # scope to avoid circular import
  496. from ultralytics.utils.torch_utils import get_cpu_info, get_gpu_info
  497. gib = 1 << 30 # bytes per GiB
  498. cuda = torch and torch.cuda.is_available()
  499. check_yolo()
  500. total, used, free = shutil.disk_usage("/")
  501. info_dict = {
  502. "OS": platform.platform(),
  503. "Environment": ENVIRONMENT,
  504. "Python": PYTHON_VERSION,
  505. "Install": "git" if IS_GIT_DIR else "pip" if IS_PIP_PACKAGE else "other",
  506. "RAM": f"{psutil.virtual_memory().total / gib:.2f} GB",
  507. "Disk": f"{(total - free) / gib:.1f}/{total / gib:.1f} GB",
  508. "CPU": get_cpu_info(),
  509. "CPU count": os.cpu_count(),
  510. "GPU": get_gpu_info(index=0) if cuda else None,
  511. "GPU count": torch.cuda.device_count() if cuda else None,
  512. "CUDA": torch.version.cuda if cuda else None,
  513. }
  514. LOGGER.info("\n" + "\n".join(f"{k:<20}{v}" for k, v in info_dict.items()) + "\n")
  515. package_info = {}
  516. for r in parse_requirements(package="ultralytics"):
  517. try:
  518. current = metadata.version(r.name)
  519. is_met = "✅ " if check_version(current, str(r.specifier), name=r.name, hard=True) else "❌ "
  520. except metadata.PackageNotFoundError:
  521. current = "(not installed)"
  522. is_met = "❌ "
  523. package_info[r.name] = f"{is_met}{current}{r.specifier}"
  524. LOGGER.info(f"{r.name:<20}{package_info[r.name]}")
  525. info_dict["Package Info"] = package_info
  526. if is_github_action_running():
  527. github_info = {
  528. "RUNNER_OS": os.getenv("RUNNER_OS"),
  529. "GITHUB_EVENT_NAME": os.getenv("GITHUB_EVENT_NAME"),
  530. "GITHUB_WORKFLOW": os.getenv("GITHUB_WORKFLOW"),
  531. "GITHUB_ACTOR": os.getenv("GITHUB_ACTOR"),
  532. "GITHUB_REPOSITORY": os.getenv("GITHUB_REPOSITORY"),
  533. "GITHUB_REPOSITORY_OWNER": os.getenv("GITHUB_REPOSITORY_OWNER"),
  534. }
  535. LOGGER.info("\n" + "\n".join(f"{k}: {v}" for k, v in github_info.items()))
  536. info_dict["GitHub Info"] = github_info
  537. return info_dict
  538. def check_amp(model):
  539. """
  540. Checks the PyTorch Automatic Mixed Precision (AMP) functionality of a YOLO11 model. If the checks fail, it means
  541. there are anomalies with AMP on the system that may cause NaN losses or zero-mAP results, so AMP will be disabled
  542. during training.
  543. Args:
  544. model (nn.Module): A YOLO11 model instance.
  545. Example:
  546. ```python
  547. from ultralytics import YOLO
  548. from ultralytics.utils.checks import check_amp
  549. model = YOLO("yolo11n.pt").model.cuda()
  550. check_amp(model)
  551. ```
  552. Returns:
  553. (bool): Returns True if the AMP functionality works correctly with YOLO11 model, else False.
  554. """
  555. from ultralytics.utils.torch_utils import autocast
  556. device = next(model.parameters()).device # get model device
  557. prefix = colorstr("AMP: ")
  558. if device.type in {"cpu", "mps"}:
  559. return False # AMP only used on CUDA devices
  560. else:
  561. # GPUs that have issues with AMP
  562. pattern = re.compile(
  563. r"(nvidia|geforce|quadro|tesla).*?(1660|1650|1630|t400|t550|t600|t1000|t1200|t2000|k40m)", re.IGNORECASE
  564. )
  565. gpu = torch.cuda.get_device_name(device)
  566. if bool(pattern.search(gpu)):
  567. LOGGER.warning(
  568. f"{prefix}checks failed ❌. AMP training on {gpu} GPU may cause "
  569. f"NaN losses or zero-mAP results, so AMP will be disabled during training."
  570. )
  571. return False
  572. def amp_allclose(m, im):
  573. """All close FP32 vs AMP results."""
  574. batch = [im] * 8
  575. imgsz = max(256, int(model.stride.max() * 4)) # max stride P5-32 and P6-64
  576. a = m(batch, imgsz=imgsz, device=device, verbose=False)[0].boxes.data # FP32 inference
  577. with autocast(enabled=True):
  578. b = m(batch, imgsz=imgsz, device=device, verbose=False)[0].boxes.data # AMP inference
  579. del m
  580. return a.shape == b.shape and torch.allclose(a, b.float(), atol=0.5) # close to 0.5 absolute tolerance
  581. im = ASSETS / "bus.jpg" # image to check
  582. LOGGER.info(f"{prefix}running Automatic Mixed Precision (AMP) checks...")
  583. warning_msg = "Setting 'amp=True'. If you experience zero-mAP or NaN losses you can disable AMP with amp=False."
  584. try:
  585. from ultralytics import YOLO
  586. # assert amp_allclose(YOLO("yolo11n.pt"), im)
  587. assert amp_allclose(YOLO("yolov13n.pt"), im)
  588. LOGGER.info(f"{prefix}checks passed ✅")
  589. except ConnectionError:
  590. LOGGER.warning(
  591. f"{prefix}checks skipped ⚠️. Offline and unable to download YOLO11n for AMP checks. {warning_msg}"
  592. )
  593. except (AttributeError, ModuleNotFoundError):
  594. LOGGER.warning(
  595. f"{prefix}checks skipped ⚠️. "
  596. f"Unable to load YOLO11n for AMP checks due to possible Ultralytics package modifications. {warning_msg}"
  597. )
  598. except AssertionError:
  599. LOGGER.warning(
  600. f"{prefix}checks failed ❌. Anomalies were detected with AMP on your system that may lead to "
  601. f"NaN losses or zero-mAP results, so AMP will be disabled during training."
  602. )
  603. return False
  604. return True
  605. def git_describe(path=ROOT): # path must be a directory
  606. """Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe."""
  607. try:
  608. return subprocess.check_output(f"git -C {path} describe --tags --long --always", shell=True).decode()[:-1]
  609. except Exception:
  610. return ""
  611. def print_args(args: Optional[dict] = None, show_file=True, show_func=False):
  612. """Print function arguments (optional args dict)."""
  613. def strip_auth(v):
  614. """Clean longer Ultralytics HUB URLs by stripping potential authentication information."""
  615. return clean_url(v) if (isinstance(v, str) and v.startswith("http") and len(v) > 100) else v
  616. x = inspect.currentframe().f_back # previous frame
  617. file, _, func, _, _ = inspect.getframeinfo(x)
  618. if args is None: # get args automatically
  619. args, _, _, frm = inspect.getargvalues(x)
  620. args = {k: v for k, v in frm.items() if k in args}
  621. try:
  622. file = Path(file).resolve().relative_to(ROOT).with_suffix("")
  623. except ValueError:
  624. file = Path(file).stem
  625. s = (f"{file}: " if show_file else "") + (f"{func}: " if show_func else "")
  626. LOGGER.info(colorstr(s) + ", ".join(f"{k}={strip_auth(v)}" for k, v in args.items()))
  627. def cuda_device_count() -> int:
  628. """
  629. Get the number of NVIDIA GPUs available in the environment.
  630. Returns:
  631. (int): The number of NVIDIA GPUs available.
  632. """
  633. try:
  634. # Run the nvidia-smi command and capture its output
  635. output = subprocess.check_output(
  636. ["nvidia-smi", "--query-gpu=count", "--format=csv,noheader,nounits"], encoding="utf-8"
  637. )
  638. # Take the first line and strip any leading/trailing white space
  639. first_line = output.strip().split("\n")[0]
  640. return int(first_line)
  641. except (subprocess.CalledProcessError, FileNotFoundError, ValueError):
  642. # If the command fails, nvidia-smi is not found, or output is not an integer, assume no GPUs are available
  643. return 0
  644. def cuda_is_available() -> bool:
  645. """
  646. Check if CUDA is available in the environment.
  647. Returns:
  648. (bool): True if one or more NVIDIA GPUs are available, False otherwise.
  649. """
  650. return cuda_device_count() > 0
  651. def is_sudo_available() -> bool:
  652. """
  653. Check if the sudo command is available in the environment.
  654. Returns:
  655. (bool): True if the sudo command is available, False otherwise.
  656. """
  657. if WINDOWS:
  658. return False
  659. cmd = "sudo --version"
  660. return subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
  661. # Run checks and define constants
  662. check_python("3.8", hard=False, verbose=True) # check python version
  663. check_torchvision() # check torch-torchvision compatibility
  664. IS_PYTHON_MINIMUM_3_10 = check_python("3.10", hard=False)
  665. IS_PYTHON_3_12 = PYTHON_VERSION.startswith("3.12")