Reference for ultralytics/utils/patches.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/patches.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.patches.imread
Read an image from a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename | str | Path to the file to read. | required |
flags | int | Flag that can take values of cv2.IMREAD_*. Defaults to cv2.IMREAD_COLOR. | IMREAD_COLOR |
Returns:
Type | Description |
---|---|
ndarray | The read image. |
Source code in ultralytics/utils/patches.py
ultralytics.utils.patches.imwrite
Write an image to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename | str | Path to the file to write. | required |
img | ndarray | Image to write. | required |
params | list of ints | Additional parameters. See OpenCV documentation. | None |
Returns:
Type | Description |
---|---|
bool | True if the file was written, False otherwise. |
Source code in ultralytics/utils/patches.py
ultralytics.utils.patches.imshow
Displays an image in the specified window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
winname | str | Name of the window. | required |
mat | ndarray | Image to be shown. | required |
ultralytics.utils.patches.torch_load
Load a PyTorch model with updated arguments to avoid warnings.
This function wraps torch.load and adds the 'weights_only' argument for PyTorch 1.13.0+ to prevent warnings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args | Any | Variable length argument list to pass to torch.load. | () |
**kwargs | Any | Arbitrary keyword arguments to pass to torch.load. | {} |
Returns:
Type | Description |
---|---|
Any | The loaded PyTorch object. |
Note
For PyTorch versions 2.0 and above, this function automatically sets 'weights_only=False' if the argument is not provided, to avoid deprecation warnings.
Source code in ultralytics/utils/patches.py
ultralytics.utils.patches.torch_save
Optionally use dill to serialize lambda functions where pickle does not, adding robustness with 3 retries and exponential standoff in case of save failure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args | tuple | Positional arguments to pass to torch.save. | () |
**kwargs | Any | Keyword arguments to pass to torch.save. | {} |