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_*. Controls how the image is read. |
IMREAD_COLOR
|
Returns:
Type | Description |
---|---|
ndarray
|
The read image. |
Examples:
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[int]
|
Additional parameters for image encoding. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if the file was written successfully, False otherwise. |
Examples:
>>> import numpy as np
>>> img = np.zeros((100, 100, 3), dtype=np.uint8) # Create a black image
>>> success = imwrite("output.jpg", img) # Write image to file
>>> print(success)
True
Source code in ultralytics/utils/patches.py
ultralytics.utils.patches.imshow
Display an image in the specified window.
This function is a wrapper around OpenCV's imshow function that displays an image in a named window. It is particularly useful for visualizing images during development and debugging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
winname
|
str
|
Name of the window where the image will be displayed. If a window with this name already exists, the image will be displayed in that window. |
required |
mat
|
ndarray
|
Image to be shown. Should be a valid numpy array representing an image. |
required |
Examples:
>>> import numpy as np
>>> img = np.zeros((300, 300, 3), dtype=np.uint8) # Create a black image
>>> img[:100, :100] = [255, 0, 0] # Add a blue square
>>> imshow("Example Window", img) # Display the image
Source code in ultralytics/utils/patches.py
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. |
Notes
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
Save PyTorch objects with retry mechanism for robustness.
This function wraps torch.save with 3 retries and exponential backoff in case of save failures, which can occur due to device flushing delays or antivirus scanning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Positional arguments to pass to torch.save. |
()
|
**kwargs
|
Any
|
Keyword arguments to pass to torch.save. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
Result of torch.save operation if successful, None otherwise. |
Examples: