Reference for ultralytics/utils/checks.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/checks.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.checks.parse_requirements
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'
|
package
|
str
|
Python package to use instead of requirements.txt file. |
''
|
Returns:
Type | Description |
---|---|
List[SimpleNamespace]
|
List of parsed requirements as SimpleNamespace objects with |
Examples:
>>> from ultralytics.utils.checks import parse_requirements
>>> parse_requirements(package="ultralytics")
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.parse_version
Convert a version string to a tuple of integers, ignoring any extra non-numeric string attached to the version.
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, i.e. (2, 0, 1) |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.is_ascii
Check if a string is composed of only ASCII characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
String to be checked. |
required |
Returns:
Type | Description |
---|---|
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
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 | List[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] | int
|
Updated image size. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_version
check_version(
current: str = "0.0.0",
required: str = "0.0.0",
name: str = "version",
hard: bool = False,
verbose: bool = False,
msg: str = "",
) -> bool
Check current version against the required version or range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current
|
str
|
Current version or package name to get version from. |
'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
|
msg
|
str
|
Extra message to display if verbose. |
''
|
Returns:
Type | Description |
---|---|
bool
|
True if requirement is met, False otherwise. |
Examples:
Check if current version is exactly 22.04
Check if current version is greater than or equal to 22.04
Check if current version is less than or equal to 22.04
Check if current version is between 20.04 (inclusive) and 22.04 (exclusive)
Source code in ultralytics/utils/checks.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
ultralytics.utils.checks.check_latest_pypi_version
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
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:
Type | Description |
---|---|
Path
|
Resolved font file path. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_python
Check current python version against the required minimum version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
minimum
|
str
|
Required minimum version of python. |
'3.8.0'
|
hard
|
bool
|
If True, raise an AssertionError if the requirement is not met. |
True
|
verbose
|
bool
|
If True, print warning message if requirement is not met. |
False
|
Returns:
Type | Description |
---|---|
bool
|
Whether the installed Python version meets the minimum constraints. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_requirements
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. |
''
|
Examples:
Check a requirements.txt file
Check a single package
Check multiple packages
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 compatibility table based on: https://github.com/pytorch/vision#installation.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_suffix
Check file(s) for acceptable suffix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str | List[str]
|
File or list of files to check. |
'yolo11n.pt'
|
suffix
|
str | Tuple[str]
|
Acceptable suffix or tuple of suffixes. |
'.pt'
|
msg
|
str
|
Additional message to display in case of error. |
''
|
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yolov5u_filename
Replace legacy YOLOv5 filenames with updated YOLOv5u filenames.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
Filename to check and potentially update. |
required |
verbose
|
bool
|
Whether to print information about the replacement. |
True
|
Returns:
Type | Description |
---|---|
str
|
Updated filename. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_model_file_from_stem
Return a model filename from a valid model stem.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model stem to check. |
'yolo11n'
|
Returns:
Type | Description |
---|---|
str | Path
|
Model filename with appropriate suffix. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_file
Search/download file (if necessary) and return path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
File name or path. |
required |
suffix
|
str
|
File suffix to check. |
''
|
download
|
bool
|
Whether to download the file if it doesn't exist locally. |
True
|
download_dir
|
str
|
Directory to download the file to. |
'.'
|
hard
|
bool
|
Whether to raise an error if the file is not found. |
True
|
Returns:
Type | Description |
---|---|
str
|
Path to the file. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yaml
Search/download YAML file (if necessary) and return path, checking suffix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
File name or path. |
required |
suffix
|
tuple
|
Acceptable file suffixes. |
('.yaml', '.yml')
|
hard
|
bool
|
Whether to raise an error if the file is not found. |
True
|
Returns:
Type | Description |
---|---|
str
|
Path to the YAML file. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_is_path_safe
Check if the resolved path is under the intended directory to prevent path traversal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
basedir
|
Path | str
|
The intended directory. |
required |
path
|
Path | str
|
The path to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the path is safe, False otherwise. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_imshow
Check if environment supports image displays.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
warn
|
bool
|
Whether to warn if environment doesn't support image displays. |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if environment supports image displays, False otherwise. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yolo
Return a human-readable YOLO software and hardware summary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose
|
bool
|
Whether to print verbose information. |
True
|
device
|
str
|
Device to use for YOLO. |
''
|
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.
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing system information. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_amp
Checks the PyTorch Automatic Mixed Precision (AMP) functionality of a YOLO11 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 YOLO11 model instance. |
required |
Returns:
Type | Description |
---|---|
bool
|
Returns True if the AMP functionality works correctly with YOLO11 model, else False. |
Examples:
>>> from ultralytics import YOLO
>>> from ultralytics.utils.checks import check_amp
>>> model = YOLO("yolo11n.pt").model.cuda()
>>> check_amp(model)
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.git_describe
Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path
|
Path to git repository. |
ROOT
|
Returns:
Type | Description |
---|---|
str
|
Human-readable git description. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.print_args
Print function arguments (optional args dict).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
dict
|
Arguments to print. |
None
|
show_file
|
bool
|
Whether to show the file name. |
True
|
show_func
|
bool
|
Whether to show the function name. |
False
|
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. |
ultralytics.utils.checks.is_rockchip
Check if the current environment is running on a Rockchip SoC.
Returns:
Type | Description |
---|---|
bool
|
True if running on a Rockchip SoC, False otherwise. |
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.is_sudo_available
Check if the sudo command is available in the environment.
Returns:
Type | Description |
---|---|
bool
|
True if the sudo command is available, False otherwise. |