2023-04-14 20:36:16 +08:00
|
|
|
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
|
2021-08-15 03:17:51 +08:00
|
|
|
"""
|
2022-08-22 07:06:29 +08:00
|
|
|
PyTorch Hub models https://pytorch.org/hub/ultralytics_yolov5
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
import torch
|
2022-10-13 21:27:16 +08:00
|
|
|
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
|
2020-06-11 10:11:11 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
|
2021-05-16 23:41:26 +08:00
|
|
|
def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2024-01-08 08:29:14 +08:00
|
|
|
"""
|
|
|
|
Creates or loads a YOLOv5 model.
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
Arguments:
|
2022-01-26 06:57:27 +08:00
|
|
|
name (str): model name 'yolov5s' or path 'path/to/best.pt'
|
2020-06-11 10:11:11 +08:00
|
|
|
pretrained (bool): load pretrained weights into the model
|
|
|
|
channels (int): number of input channels
|
|
|
|
classes (int): number of model classes
|
2021-04-30 20:59:51 +08:00
|
|
|
autoshape (bool): apply YOLOv5 .autoshape() wrapper to model
|
|
|
|
verbose (bool): print all information to screen
|
2021-05-16 23:41:26 +08:00
|
|
|
device (str, torch.device, None): device to use for model parameters
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
Returns:
|
2022-01-26 06:57:27 +08:00
|
|
|
YOLOv5 model
|
2020-06-11 10:11:11 +08:00
|
|
|
"""
|
2021-07-07 22:08:42 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-12-04 22:00:07 +08:00
|
|
|
from models.common import AutoShape, DetectMultiBackend
|
Fix missing attr model.model when loading custom yolov model (#8830)
* Update hubconf.py
Loading a custom yolov model causes this line to fail. Adding a test to check if the model actually has a model.model field. With this check, I'm able to load the model no prob.
Loading model via
```py
model = torch.hub.load(
'ultralytics/yolov5', 'custom', 'models/frozen_backbone_coco_unlabeled_best.onnx',
autoshape=True, force_reload=False
)
```
Causes traceback:
```
Traceback (most recent call last):
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/Users/jackson/Documents/GitHub/w210-capstone/api/endpoints/predictions.py", line 26, in post_predictions
yolov_predictions = predict_bounding_boxes_for_collection(collection_id)
File "/Users/jackson/Documents/GitHub/w210-capstone/api/predictions/predict_bounding_boxes.py", line 43, in predict_bounding_boxes_for_collection
model = torch.hub.load(
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 404, in load
model = _load_local(repo_or_dir, model, *args, **kwargs)
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 433, in _load_local
model = entry(*args, **kwargs)
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 72, in custom
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 67, in _create
raise Exception(s) from e
Exception: 'DetectMultiBackend' object has no attribute 'model'. Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.
Exception on /api/v1/predictions [POST]
Traceback (most recent call last):
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 58, in _create
model.model.model[-1].inplace = False # Detect.inplace=False for safe multithread inference
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1185, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'DetectMultiBackend' object has no attribute 'model'
```
* Update hubconf.py
* Update common.py
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
2022-08-02 07:46:08 +08:00
|
|
|
from models.experimental import attempt_load
|
2022-09-28 22:43:11 +08:00
|
|
|
from models.yolo import ClassificationModel, DetectionModel, SegmentationModel
|
2021-07-28 08:04:10 +08:00
|
|
|
from utils.downloads import attempt_download
|
2023-05-21 06:18:12 +08:00
|
|
|
from utils.general import LOGGER, ROOT, check_requirements, intersect_dicts, logging
|
2021-05-13 02:18:32 +08:00
|
|
|
from utils.torch_utils import select_device
|
|
|
|
|
2022-01-26 06:57:27 +08:00
|
|
|
if not verbose:
|
|
|
|
LOGGER.setLevel(logging.WARNING)
|
2024-01-08 08:29:14 +08:00
|
|
|
check_requirements(ROOT / "requirements.txt", exclude=("opencv-python", "tensorboard", "thop"))
|
2021-12-04 22:00:07 +08:00
|
|
|
name = Path(name)
|
2024-01-08 08:29:14 +08:00
|
|
|
path = name.with_suffix(".pt") if name.suffix == "" and not name.is_dir() else name # checkpoint path
|
2020-07-12 06:03:12 +08:00
|
|
|
try:
|
2022-06-18 00:28:29 +08:00
|
|
|
device = select_device(device)
|
2021-04-30 20:59:51 +08:00
|
|
|
if pretrained and channels == 3 and classes == 80:
|
Fix missing attr model.model when loading custom yolov model (#8830)
* Update hubconf.py
Loading a custom yolov model causes this line to fail. Adding a test to check if the model actually has a model.model field. With this check, I'm able to load the model no prob.
Loading model via
```py
model = torch.hub.load(
'ultralytics/yolov5', 'custom', 'models/frozen_backbone_coco_unlabeled_best.onnx',
autoshape=True, force_reload=False
)
```
Causes traceback:
```
Traceback (most recent call last):
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/Users/jackson/Documents/GitHub/w210-capstone/api/endpoints/predictions.py", line 26, in post_predictions
yolov_predictions = predict_bounding_boxes_for_collection(collection_id)
File "/Users/jackson/Documents/GitHub/w210-capstone/api/predictions/predict_bounding_boxes.py", line 43, in predict_bounding_boxes_for_collection
model = torch.hub.load(
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 404, in load
model = _load_local(repo_or_dir, model, *args, **kwargs)
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 433, in _load_local
model = entry(*args, **kwargs)
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 72, in custom
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 67, in _create
raise Exception(s) from e
Exception: 'DetectMultiBackend' object has no attribute 'model'. Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.
Exception on /api/v1/predictions [POST]
Traceback (most recent call last):
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 58, in _create
model.model.model[-1].inplace = False # Detect.inplace=False for safe multithread inference
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1185, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'DetectMultiBackend' object has no attribute 'model'
```
* Update hubconf.py
* Update common.py
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
2022-08-02 07:46:08 +08:00
|
|
|
try:
|
|
|
|
model = DetectMultiBackend(path, device=device, fuse=autoshape) # detection model
|
2022-08-22 05:26:07 +08:00
|
|
|
if autoshape:
|
|
|
|
if model.pt and isinstance(model.model, ClassificationModel):
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.warning(
|
|
|
|
"WARNING ⚠️ YOLOv5 ClassificationModel is not yet AutoShape compatible. "
|
|
|
|
"You must pass torch tensors in BCHW to this model, i.e. shape(1,3,224,224)."
|
|
|
|
)
|
2022-09-28 22:43:11 +08:00
|
|
|
elif model.pt and isinstance(model.model, SegmentationModel):
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.warning(
|
|
|
|
"WARNING ⚠️ YOLOv5 SegmentationModel is not yet AutoShape compatible. "
|
|
|
|
"You will not be able to run inference with this model."
|
|
|
|
)
|
2022-08-22 05:26:07 +08:00
|
|
|
else:
|
|
|
|
model = AutoShape(model) # for file/URI/PIL/cv2/np inputs and NMS
|
Fix missing attr model.model when loading custom yolov model (#8830)
* Update hubconf.py
Loading a custom yolov model causes this line to fail. Adding a test to check if the model actually has a model.model field. With this check, I'm able to load the model no prob.
Loading model via
```py
model = torch.hub.load(
'ultralytics/yolov5', 'custom', 'models/frozen_backbone_coco_unlabeled_best.onnx',
autoshape=True, force_reload=False
)
```
Causes traceback:
```
Traceback (most recent call last):
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/Users/jackson/Documents/GitHub/w210-capstone/api/endpoints/predictions.py", line 26, in post_predictions
yolov_predictions = predict_bounding_boxes_for_collection(collection_id)
File "/Users/jackson/Documents/GitHub/w210-capstone/api/predictions/predict_bounding_boxes.py", line 43, in predict_bounding_boxes_for_collection
model = torch.hub.load(
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 404, in load
model = _load_local(repo_or_dir, model, *args, **kwargs)
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 433, in _load_local
model = entry(*args, **kwargs)
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 72, in custom
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 67, in _create
raise Exception(s) from e
Exception: 'DetectMultiBackend' object has no attribute 'model'. Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.
Exception on /api/v1/predictions [POST]
Traceback (most recent call last):
File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 58, in _create
model.model.model[-1].inplace = False # Detect.inplace=False for safe multithread inference
File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1185, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'DetectMultiBackend' object has no attribute 'model'
```
* Update hubconf.py
* Update common.py
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
2022-08-02 07:46:08 +08:00
|
|
|
except Exception:
|
|
|
|
model = attempt_load(path, device=device, fuse=False) # arbitrary model
|
2021-04-30 20:59:51 +08:00
|
|
|
else:
|
2024-01-08 08:29:14 +08:00
|
|
|
cfg = list((Path(__file__).parent / "models").rglob(f"{path.stem}.yaml"))[0] # model.yaml path
|
2022-08-19 03:45:11 +08:00
|
|
|
model = DetectionModel(cfg, channels, classes) # create model
|
2021-04-30 20:59:51 +08:00
|
|
|
if pretrained:
|
2021-07-07 19:41:46 +08:00
|
|
|
ckpt = torch.load(attempt_download(path), map_location=device) # load
|
2024-01-08 08:29:14 +08:00
|
|
|
csd = ckpt["model"].float().state_dict() # checkpoint state_dict as FP32
|
|
|
|
csd = intersect_dicts(csd, model.state_dict(), exclude=["anchors"]) # intersect
|
2021-04-30 20:59:51 +08:00
|
|
|
model.load_state_dict(csd, strict=False) # load
|
2024-01-08 08:29:14 +08:00
|
|
|
if len(ckpt["model"].names) == classes:
|
|
|
|
model.names = ckpt["model"].names # set class names attribute
|
2022-07-23 01:01:16 +08:00
|
|
|
if not verbose:
|
|
|
|
LOGGER.setLevel(logging.INFO) # reset to default
|
2021-03-15 14:16:17 +08:00
|
|
|
return model.to(device)
|
2020-07-17 07:44:00 +08:00
|
|
|
|
2020-07-12 06:03:12 +08:00
|
|
|
except Exception as e:
|
2024-01-08 08:29:14 +08:00
|
|
|
help_url = "https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading"
|
|
|
|
s = f"{e}. Cache may be out of date, try `force_reload=True` or see {help_url} for help."
|
2020-07-17 07:44:00 +08:00
|
|
|
raise Exception(s) from e
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def custom(path="path/to/model.pt", autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Loads a custom or local YOLOv5 model from a given path with optional autoshaping and device specification."""
|
2022-04-24 02:26:25 +08:00
|
|
|
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5n(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Instantiates the YOLOv5-nano model with options for pretraining, input channels, class count, autoshaping,
|
|
|
|
verbosity, and device.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5n", pretrained, channels, classes, autoshape, _verbose, device)
|
2021-10-12 14:47:18 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Creates YOLOv5-small model with options for pretraining, input channels, class count, autoshaping, verbosity, and
|
|
|
|
device.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5s", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-06-11 10:11:11 +08:00
|
|
|
|
2021-04-12 01:23:47 +08:00
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Instantiates the YOLOv5-medium model with customizable pretraining, channel count, class count, autoshaping,
|
|
|
|
verbosity, and device.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5m", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Creates YOLOv5-large model with options for pretraining, channels, classes, autoshaping, verbosity, and device
|
|
|
|
selection.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5l", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Instantiates the YOLOv5-xlarge model with customizable pretraining, channel count, class count, autoshaping,
|
|
|
|
verbosity, and device.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5x", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Creates YOLOv5-nano-P6 model with options for pretraining, channels, classes, autoshaping, verbosity, and
|
|
|
|
device.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5n6", pretrained, channels, classes, autoshape, _verbose, device)
|
2021-10-12 14:47:18 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Instantiate YOLOv5-small-P6 model with options for pretraining, input channels, number of classes, autoshaping,
|
|
|
|
verbosity, and device selection.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5s6", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-10-26 08:06:54 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Creates YOLOv5-medium-P6 model with options for pretraining, channel count, class count, autoshaping, verbosity,
|
|
|
|
and device.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5m6", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-12-13 03:16:57 +08:00
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Instantiates the YOLOv5-large-P6 model with customizable pretraining, channel and class counts, autoshaping,
|
|
|
|
verbosity, and device selection.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5l6", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-12-13 03:16:57 +08:00
|
|
|
|
2021-04-12 01:23:47 +08:00
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
2024-02-25 21:04:01 +08:00
|
|
|
"""Creates YOLOv5-xlarge-P6 model with options for pretraining, channels, classes, autoshaping, verbosity, and
|
|
|
|
device.
|
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
return _create("yolov5x6", pretrained, channels, classes, autoshape, _verbose, device)
|
2020-12-13 03:16:57 +08:00
|
|
|
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
if __name__ == "__main__":
|
2022-07-31 03:00:28 +08:00
|
|
|
import argparse
|
2021-11-05 00:24:25 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-02-20 04:35:38 +08:00
|
|
|
import numpy as np
|
2020-11-17 06:20:17 +08:00
|
|
|
from PIL import Image
|
|
|
|
|
2022-07-31 03:02:26 +08:00
|
|
|
from utils.general import cv2, print_args
|
2022-03-25 20:25:30 +08:00
|
|
|
|
2022-07-31 03:00:28 +08:00
|
|
|
# Argparser
|
|
|
|
parser = argparse.ArgumentParser()
|
2024-01-08 08:29:14 +08:00
|
|
|
parser.add_argument("--model", type=str, default="yolov5s", help="model name")
|
2022-07-31 03:00:28 +08:00
|
|
|
opt = parser.parse_args()
|
2022-07-31 03:02:26 +08:00
|
|
|
print_args(vars(opt))
|
2022-07-31 03:00:28 +08:00
|
|
|
|
|
|
|
# Model
|
|
|
|
model = _create(name=opt.model, pretrained=True, channels=3, classes=80, autoshape=True, verbose=True)
|
|
|
|
# model = custom(path='path/to/model.pt') # custom
|
|
|
|
|
|
|
|
# Images
|
2022-03-31 22:52:34 +08:00
|
|
|
imgs = [
|
2024-01-08 08:29:14 +08:00
|
|
|
"data/images/zidane.jpg", # filename
|
|
|
|
Path("data/images/zidane.jpg"), # Path
|
|
|
|
"https://ultralytics.com/images/zidane.jpg", # URI
|
|
|
|
cv2.imread("data/images/bus.jpg")[:, :, ::-1], # OpenCV
|
|
|
|
Image.open("data/images/bus.jpg"), # PIL
|
|
|
|
np.zeros((320, 640, 3)),
|
|
|
|
] # numpy
|
2021-02-20 04:35:38 +08:00
|
|
|
|
2022-07-31 03:00:28 +08:00
|
|
|
# Inference
|
2021-12-13 20:32:27 +08:00
|
|
|
results = model(imgs, size=320) # batched inference
|
2022-07-31 03:00:28 +08:00
|
|
|
|
|
|
|
# Results
|
2020-11-17 06:09:55 +08:00
|
|
|
results.print()
|
2021-01-24 04:51:04 +08:00
|
|
|
results.save()
|