Reference for ultralytics/utils/checks.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/checks.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.checks.parse_requirements(file_path=ROOT.parent / 'requirements.txt')
Parse a requirements.txt file, ignoring lines that start with '#' and any text after '#'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
Path
|
Path to the requirements.txt file. |
parent / 'requirements.txt'
|
Returns:
Type | Description |
---|---|
List[Dict[str, str]]
|
List of parsed requirements as dictionaries with |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.parse_version(version='0.0.0')
Convert a version string to a tuple of integers, ignoring any extra non-numeric string attached to the version. This function replaces deprecated 'pkg_resources.parse_version(v)'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
version |
str
|
Version string, i.e. '2.0.1+cpu' |
'0.0.0'
|
Returns:
Type | Description |
---|---|
tuple
|
Tuple of integers representing the numeric part of the version and the extra string, i.e. (2, 0, 1) |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.is_ascii(s)
Check if a string is composed of only ASCII characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
str
|
String to be checked. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the string is composed only of ASCII characters, False otherwise. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_imgsz(imgsz, stride=32, min_dim=1, max_dim=2, floor=0)
Verify image size is a multiple of the given stride in each dimension. If the image size is not a multiple of the stride, update it to the nearest multiple of the stride that is greater than or equal to the given floor value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
imgsz |
int | cList[int]
|
Image size. |
required |
stride |
int
|
Stride value. |
32
|
min_dim |
int
|
Minimum number of dimensions. |
1
|
max_dim |
int
|
Maximum number of dimensions. |
2
|
floor |
int
|
Minimum allowed value for image size. |
0
|
Returns:
Type | Description |
---|---|
List[int]
|
Updated image size. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_version(current='0.0.0', required='0.0.0', name='version ', hard=False, verbose=False)
Check current version against the required version or range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current |
str
|
Current version. |
'0.0.0'
|
required |
str
|
Required version or range (in pip-style format). |
'0.0.0'
|
name |
str
|
Name to be used in warning message. |
'version '
|
hard |
bool
|
If True, raise an AssertionError if the requirement is not met. |
False
|
verbose |
bool
|
If True, print warning message if requirement is not met. |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if requirement is met, False otherwise. |
Example
check if current version is exactly 22.04
check_version(current='22.04', required='==22.04')
check if current version is greater than or equal to 22.04
check_version(current='22.10', required='22.04') # assumes '>=' inequality if none passed
check if current version is less than or equal to 22.04
check_version(current='22.04', required='<=22.04')
check if current version is between 20.04 (inclusive) and 22.04 (exclusive)
check_version(current='21.10', required='>20.04,<22.04')
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_latest_pypi_version(package_name='ultralytics')
Returns the latest version of a PyPI package without downloading or installing it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package_name |
str
|
The name of the package to find the latest version for. |
'ultralytics'
|
Returns:
Type | Description |
---|---|
str
|
The latest version of the package. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_pip_update_available()
Checks if a new version of the ultralytics package is available on PyPI.
Returns:
Type | Description |
---|---|
bool
|
True if an update is available, False otherwise. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_font(font='Arial.ttf')
Find font locally or download to user's configuration directory if it does not already exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
font |
str
|
Path or name of font. |
'Arial.ttf'
|
Returns:
Name | Type | Description |
---|---|---|
file |
Path
|
Resolved font file path. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_python(minimum='3.8.0')
Check current python version against the required minimum version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
minimum |
str
|
Required minimum version of python. |
'3.8.0'
|
Returns:
Type | Description |
---|---|
bool
|
None |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_requirements(requirements=ROOT.parent / 'requirements.txt', exclude=(), install=True, cmds='')
Check if installed dependencies meet YOLOv8 requirements and attempt to auto-update if needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
requirements |
Union[Path, str, List[str]]
|
Path to a requirements.txt file, a single package requirement as a string, or a list of package requirements as strings. |
parent / 'requirements.txt'
|
exclude |
Tuple[str]
|
Tuple of package names to exclude from checking. |
()
|
install |
bool
|
If True, attempt to auto-update packages that don't meet requirements. |
True
|
cmds |
str
|
Additional commands to pass to the pip install command when auto-updating. |
''
|
Example
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_torchvision()
Checks the installed versions of PyTorch and Torchvision to ensure they're compatible.
This function checks the installed versions of PyTorch and Torchvision, and warns if they're incompatible according to the provided compatibility table based on https://github.com/pytorch/vision#installation. The compatibility table is a dictionary where the keys are PyTorch versions and the values are lists of compatible Torchvision versions.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_suffix(file='yolov8n.pt', suffix='.pt', msg='')
Check file(s) for acceptable suffix.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yolov5u_filename(file, verbose=True)
Replace legacy YOLOv5 filenames with updated YOLOv5u filenames.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_file(file, suffix='', download=True, hard=True)
Search/download file (if necessary) and return path.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yaml(file, suffix=('.yaml', '.yml'), hard=True)
Search/download YAML file (if necessary) and return path, checking suffix.
ultralytics.utils.checks.check_imshow(warn=False)
Check if environment supports image displays.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yolo(verbose=True, device='')
Return a human-readable YOLO software and hardware summary.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.collect_system_info()
Collect and print relevant system information including OS, Python, RAM, CPU, and CUDA.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_amp(model)
This function checks the PyTorch Automatic Mixed Precision (AMP) functionality of a YOLOv8 model. If the checks fail, it means there are anomalies with AMP on the system that may cause NaN losses or zero-mAP results, so AMP will be disabled during training.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
A YOLOv8 model instance. |
required |
Example
Returns:
Type | Description |
---|---|
bool
|
Returns True if the AMP functionality works correctly with YOLOv8 model, else False. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.git_describe(path=ROOT)
Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.print_args(args=None, show_file=True, show_func=False)
Print function arguments (optional args dict).
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.cuda_device_count()
Get the number of NVIDIA GPUs available in the environment.
Returns:
Type | Description |
---|---|
int
|
The number of NVIDIA GPUs available. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.cuda_is_available()
Check if CUDA is available in the environment.
Returns:
Type | Description |
---|---|
bool
|
True if one or more NVIDIA GPUs are available, False otherwise. |