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, i.e. package='ultralytics'. |
''
|
Returns:
Type | Description |
---|---|
List[Dict[str, str]]
|
List of parsed requirements as dictionaries with |
Example
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. 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
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 | 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
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. |
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
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 257 258 259 260 261 |
|
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:
Name | Type | Description |
---|---|---|
file |
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
|
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. |
''
|
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
Check file(s) for acceptable suffix.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yolov5u_filename
Replace legacy YOLOv5 filenames with updated YOLOv5u filenames.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_model_file_from_stem
Return a model filename from a valid model stem.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_file
Search/download file (if necessary) and return path.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yaml
Search/download YAML file (if necessary) and return path, checking suffix.
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.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.check_yolo
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
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 |
Example
Returns:
Type | Description |
---|---|
bool
|
Returns True if the AMP functionality works correctly with YOLO11 model, else False. |
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.
Source code in ultralytics/utils/checks.py
ultralytics.utils.checks.print_args
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. |