Refactor code for speed and clarity
This commit is contained in:
Glenn Jocher authored and GitHub committed 2024-07-08 21:19:04 +02:00
1 parent 900a1262cd
commit 8257e0a68c
6 files changed
+1446 -75

No files matched your search

+322 -24
View File
@@ -15,19 +15,38 @@ import torch
def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
"""
Creates or loads a YOLOv5 model.
Creates or loads a YOLOv5 model, with options for pretrained weights and model customization.
Arguments:
name (str): model name 'yolov5s' or path 'path/to/best.pt'
pretrained (bool): load pretrained weights into the model
channels (int): number of input channels
classes (int): number of model classes
autoshape (bool): apply YOLOv5 .autoshape() wrapper to model
verbose (bool): print all information to screen
device (str, torch.device, None): device to use for model parameters
Args:
name (str): Model name (e.g., 'yolov5s') or path to the model checkpoint (e.g., 'path/to/best.pt').
pretrained (bool, optional): If True, loads pretrained weights into the model. Defaults to True.
channels (int, optional): Number of input channels the model expects. Defaults to 3.
classes (int, optional): Number of classes the model is expected to detect. Defaults to 80.
autoshape (bool, optional): If True, applies the YOLOv5 .autoshape() wrapper for various input formats. Defaults to True.
verbose (bool, optional): If True, prints detailed information during the model creation/loading process. Defaults to True.
device (str | torch.device | None, optional): Device to use for model parameters (e.g., 'cpu', 'cuda'). If None, selects the best available device. Defaults to None.
Returns:
YOLOv5 model
DetectMultiBackend | AutoShape: The loaded YOLOv5 model, potentially wrapped with AutoShape if specified.
Examples:
```python
import torch
from ultralytics import _create
# Load an official YOLOv5s model with pretrained weights
model = _create('yolov5s')
# Load a custom model from a local checkpoint
model = _create('path/to/custom_model.pt', pretrained=False)
# Load a model with specific input channels and classes
model = _create('yolov5s', channels=1, classes=10)
```
Notes:
For more information on model loading and customization, visit the
[YOLOv5 PyTorch Hub Documentation](https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading).
"""
from pathlib import Path
@@ -84,76 +103,355 @@ def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbo
def custom(path="path/to/model.pt", autoshape=True, _verbose=True, device=None):
"""Loads a custom or local YOLOv5 model from a given path with optional autoshaping and device specification."""
"""
Loads a custom or local YOLOv5 model from a given path with optional autoshaping and device specification.
Args:
path (str): Path to the custom model file (e.g., 'path/to/model.pt').
autoshape (bool): Apply YOLOv5 .autoshape() wrapper to model if True, enabling compatibility with various input types
(default is True).
_verbose (bool): If True, prints all informational messages to the screen; otherwise, operates silently
(default is True).
device (str | torch.device | None): Device to load the model on, e.g., 'cpu', 'cuda', torch.device('cuda:0'), etc.
(default is None, which automatically selects the best available device).
Returns:
torch.nn.Module: A YOLOv5 model loaded with the specified parameters.
Notes:
For more details on loading models from PyTorch Hub:
https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading
Examples:
```python
# Load model from a given path with autoshape enabled on the best available device
model = torch.hub.load('ultralytics/yolov5', 'custom', 'yolov5s.pt')
# Load model from a local path without autoshape on the CPU device
model = torch.hub.load('.', 'custom', 'yolov5s.pt', source='local', autoshape=False, device='cpu')
```
"""
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
def yolov5n(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Instantiates the YOLOv5-nano model with options for pretraining, input channels, class count, autoshaping,
"""
Instantiates the YOLOv5-nano model with options for pretraining, input channels, class count, autoshaping,
verbosity, and device.
Args:
pretrained (bool): If True, loads pretrained weights into the model. Defaults to True.
channels (int): Number of input channels for the model. Defaults to 3.
classes (int): Number of classes for object detection. Defaults to 80.
autoshape (bool): If True, applies the YOLOv5 .autoshape() wrapper to the model for various formats (file/URI/PIL/
cv2/np) and non-maximum suppression (NMS) during inference. Defaults to True.
_verbose (bool): If True, prints detailed information to the screen. Defaults to True.
device (str | torch.device | None): Specifies the device to use for model computation. If None, uses the best device
available (i.e., GPU if available, otherwise CPU). Defaults to None.
Returns:
DetectionModel | ClassificationModel | SegmentationModel: The instantiated YOLOv5-nano model, potentially with
pretrained weights and autoshaping applied.
Notes:
For further details on loading models from PyTorch Hub, refer to [PyTorch Hub models](https://pytorch.org/hub/
ultralytics_yolov5).
Examples:
```python
import torch
from ultralytics import yolov5n
# Load the YOLOv5-nano model with defaults
model = yolov5n()
# Load the YOLOv5-nano model with a specific device
model = yolov5n(device='cuda')
```
"""
return _create("yolov5n", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Creates YOLOv5-small model with options for pretraining, input channels, class count, autoshaping, verbosity, and
"""
Creates YOLOv5-small model with options for pretraining, input channels, class count, autoshaping, verbosity, and
device.
Args:
pretrained (bool, optional): Flag to load pretrained weights into the model. Defaults to True.
channels (int, optional): Number of input channels. Defaults to 3.
classes (int, optional): Number of model classes. Defaults to 80.
autoshape (bool, optional): Whether to apply YOLOv5 .autoshape() wrapper to the model for preprocessed inputs.
Defaults to True.
_verbose (bool, optional): Flag to print detailed information on model loading. Defaults to True.
device (str | torch.device | None, optional): Device to use for model parameters, e.g., 'cpu', 'cuda'. If None,
auto-select the best available device. Defaults to None.
Returns:
YOLOv5 model (torch.nn.Module): The YOLOv5-small model loaded with specified configurations and optionally
pretrained weights.
Usage:
```python
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # official model
model = torch.hub.load('ultralytics/yolov5:master', 'yolov5s') # from branch
model = torch.hub.load('ultralytics/yolov5', 'custom', 'yolov5s.pt') # custom/local model
model = torch.hub.load('.', 'custom', 'yolov5s.pt', source='local') # local repo
```
For more information, visit https://pytorch.org/hub/ultralytics_yolov5.
"""
return _create("yolov5s", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Instantiates the YOLOv5-medium model with customizable pretraining, channel count, class count, autoshaping,
"""
Instantiates the YOLOv5-medium model with customizable pretraining, channel count, class count, autoshaping,
verbosity, and device.
Args:
pretrained (bool, optional): Whether to load pretrained weights into the model. Default is True.
channels (int, optional): Number of input channels. Default is 3.
classes (int, optional): Number of model classes. Default is 80.
autoshape (bool, optional): Apply YOLOv5 .autoshape() wrapper to the model for handling various input formats. Default is True.
_verbose (bool, optional): Whether to print detailed information to the screen. Default is True.
device (str | torch.device | None, optional): Device specification to use for model parameters (e.g., 'cpu', 'cuda'). Default is None.
Returns:
torch.nn.Module: The instantiated YOLOv5-medium model.
Usage Example:
```python
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5m') # Load YOLOv5-medium from Ultralytics repository
model = torch.hub.load('ultralytics/yolov5:master', 'yolov5m') # Load from the master branch
model = torch.hub.load('ultralytics/yolov5', 'custom', 'yolov5m.pt') # Load a custom/local YOLOv5-medium model
model = torch.hub.load('.', 'custom', 'yolov5m.pt', source='local') # Load from a local repository
```
"""
return _create("yolov5m", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Creates YOLOv5-large model with options for pretraining, channels, classes, autoshaping, verbosity, and device
"""
Creates YOLOv5-large model with options for pretraining, channels, classes, autoshaping, verbosity, and device
selection.
Args:
pretrained (bool): Load pretrained weights into the model. Default is True.
channels (int): Number of input channels. Default is 3.
classes (int): Number of model classes. Default is 80.
autoshape (bool): Apply YOLOv5 .autoshape() wrapper to model. Default is True.
_verbose (bool): Print all information to screen. Default is True.
device (str | torch.device | None): Device to use for model parameters, e.g., 'cpu', 'cuda', or a torch.device instance. Default is None.
Returns:
YOLOv5 model (torch.nn.Module).
Example:
```python
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5l')
```
Notes:
For additional details, refer to the PyTorch Hub models documentation:
https://pytorch.org/hub/ultralytics_yolov5
"""
return _create("yolov5l", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Instantiates the YOLOv5-xlarge model with customizable pretraining, channel count, class count, autoshaping,
"""
Instantiates the YOLOv5-xlarge model with customizable pretraining, channel count, class count, autoshaping,
verbosity, and device.
Args:
pretrained (bool): If True, loads pretrained weights into the model. Defaults to True.
channels (int): Number of input channels. Defaults to 3.
classes (int): Number of model classes. Defaults to 80.
autoshape (bool): If True, applies YOLOv5 .autoshape() wrapper to the model for easier image handling. Defaults to
True.
_verbose (bool): If True, prints detailed information to the screen. Defaults to True.
device (str | torch.device | None): Device for model parameters, e.g., 'cpu', 'cuda:0', or a torch.device object.
Defaults to None.
Returns:
torch.nn.Module: The instantiated YOLOv5-xlarge model.
Example:
```python
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5x')
```
For more details and usage, refer to the official YOLOv5 PyTorch Hub models documentation:
https://pytorch.org/hub/ultralytics_yolov5
"""
return _create("yolov5x", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Creates YOLOv5-nano-P6 model with options for pretraining, channels, classes, autoshaping, verbosity, and
device.
"""
Creates YOLOv5-nano-P6 model with options for pretraining, channels, classes, autoshaping, verbosity, and device.
Args:
pretrained (bool, optional): If True, loads pretrained weights into the model. Default is True.
channels (int, optional): Number of input channels. Default is 3.
classes (int, optional): Number of model classes. Default is 80.
autoshape (bool, optional): If True, applies the YOLOv5 .autoshape() wrapper to the model. Default is True.
_verbose (bool, optional): If True, prints all information to screen. Default is True.
device (str | torch.device | None, optional): Device to use for model parameters. Can be 'cpu', 'cuda', or None.
Default is None.
Returns:
torch.nn.Module: YOLOv5 model loaded with the specified configurations.
Example:
```python
import torch
model = yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device='cuda')
```
Notes:
For more information on PyTorch Hub models, visit: https://pytorch.org/hub/ultralytics_yolov5
"""
return _create("yolov5n6", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Instantiate YOLOv5-small-P6 model with options for pretraining, input channels, number of classes, autoshaping,
"""
Instantiate the YOLOv5-small-P6 model with options for pretraining, input channels, number of classes, autoshaping,
verbosity, and device selection.
Args:
pretrained (bool): If True, loads pretrained weights. Default is True.
channels (int): Number of input channels. Default is 3.
classes (int): Number of object detection classes. Default is 80.
autoshape (bool): If True, applies YOLOv5 .autoshape() wrapper to the model, allowing for varied input formats.
Default is True.
_verbose (bool): If True, prints detailed information during model loading. Default is True.
device (str | torch.device | None): Device specification for model parameters (e.g., 'cpu', 'cuda', or torch.device).
Default is None, which selects an available device automatically.
Returns:
torch.nn.Module: The YOLOv5-small-P6 model instance.
Usage:
```python
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5s6')
model = torch.hub.load('ultralytics/yolov5:master', 'yolov5s6') # load from a specific branch
model = torch.hub.load('ultralytics/yolov5', 'custom', 'path/to/yolov5s6.pt') # custom/local model
model = torch.hub.load('.', 'custom', 'path/to/yolov5s6.pt', source='local') # local repo model
```
Notes:
- For more information, refer to the PyTorch Hub models documentation at https://pytorch.org/hub/ultralytics_yolov5
Raises:
Exception: If there is an error during model creation or loading, with a suggestion to visit the YOLOv5
tutorials for help.
"""
return _create("yolov5s6", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Creates YOLOv5-medium-P6 model with options for pretraining, channel count, class count, autoshaping, verbosity,
and device.
"""
Creates YOLOv5-medium-P6 model with options for pretraining, channel count, class count, autoshaping, verbosity, and
device.
Args:
pretrained (bool): If True, loads pretrained weights. Default is True.
channels (int): Number of input channels. Default is 3.
classes (int): Number of model classes. Default is 80.
autoshape (bool): Apply YOLOv5 .autoshape() wrapper to the model for file/URI/PIL/cv2/np inputs and NMS. Default is True.
_verbose (bool): If True, prints detailed information to the screen. Default is True.
device (str | torch.device | None): Device to use for model parameters. Default is None, which uses the best available device.
Returns:
torch.nn.Module: The YOLOv5-medium-P6 model.
Refer to the PyTorch Hub models documentation: https://pytorch.org/hub/ultralytics_yolov5 for additional details.
Example:
```python
import torch
# Load YOLOv5-medium-P6 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5m6')
```
Notes:
- The model can be loaded with pre-trained weights for better performance on specific tasks.
- The autoshape feature simplifies input handling by allowing various popular data formats.
"""
return _create("yolov5m6", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Instantiates the YOLOv5-large-P6 model with customizable pretraining, channel and class counts, autoshaping,
"""
Instantiates the YOLOv5-large-P6 model with customizable pretraining, channel and class counts, autoshaping,
verbosity, and device selection.
Args:
pretrained (bool, optional): If True, load pretrained weights into the model. Default is True.
channels (int, optional): Number of input channels. Default is 3.
classes (int, optional): Number of model classes. Default is 80.
autoshape (bool, optional): If True, apply YOLOv5 .autoshape() wrapper to the model for input flexibility.
Default is True.
_verbose (bool, optional): If True, print all information to the screen. Default is True.
device (str | torch.device | None, optional): Device to use for model parameters, e.g., 'cpu', 'cuda', or
torch.device. If None, automatically selects the best available
device. Default is None.
Returns:
torch.nn.Module: The instantiated YOLOv5-large-P6 model.
Usage:
```python
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5l6') # official model
model = torch.hub.load('ultralytics/yolov5:master', 'yolov5l6') # from specific branch
model = torch.hub.load('ultralytics/yolov5', 'custom', 'path/to/yolov5l6.pt') # custom/local model
model = torch.hub.load('.', 'custom', 'path/to/yolov5l6.pt', source='local') # local repository
```
Note:
Refer to [PyTorch Hub Documentation](https://pytorch.org/hub/ultralytics_yolov5) for additional usage instructions.
"""
return _create("yolov5l6", pretrained, channels, classes, autoshape, _verbose, device)
def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
"""Creates YOLOv5-xlarge-P6 model with options for pretraining, channels, classes, autoshaping, verbosity, and
device.
"""
Creates the YOLOv5-xlarge-P6 model with options for pretraining, number of input channels, class count, autoshaping,
verbosity, and device selection.
Args:
pretrained (bool): If True, loads pretrained weights into the model. Default is True.
channels (int): Number of input channels. Default is 3.
classes (int): Number of model classes. Default is 80.
autoshape (bool): If True, applies YOLOv5 .autoshape() wrapper to the model. Default is True.
_verbose (bool): If True, prints all information to the screen. Default is True.
device (str | torch.device | None): Device to use for model parameters, can be a string, torch.device object, or
None for default device selection. Default is None.
Returns:
torch.nn.Module: The instantiated YOLOv5-xlarge-P6 model.
Example:
```python
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5x6') # load the YOLOv5-xlarge-P6 model
```
Note:
For more information on YOLOv5 models, visit the official documentation:
https://docs.ultralytics.com/yolov5
"""
return _create("yolov5x6", pretrained, channels, classes, autoshape, _verbose, device)