2021-08-15 03:17:51 +08:00
|
|
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
|
|
|
"""
|
|
|
|
PyTorch Hub models https://pytorch.org/hub/ultralytics_yolov5/
|
2020-06-11 10:11:11 +08:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
import torch
|
2021-03-24 22:42:00 +08:00
|
|
|
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
|
2021-12-04 22:00:07 +08:00
|
|
|
model = torch.hub.load('ultralytics/yolov5:master', 'custom', 'path/to/yolov5s.onnx') # file from branch
|
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):
|
2022-01-26 06:57:27 +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
|
2021-11-05 00:24:25 +08:00
|
|
|
from models.yolo import Model
|
2021-07-28 08:04:10 +08:00
|
|
|
from utils.downloads import attempt_download
|
2022-01-26 06:57:27 +08:00
|
|
|
from utils.general import LOGGER, 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)
|
2021-09-19 00:34:30 +08:00
|
|
|
check_requirements(exclude=('tensorboard', 'thop', 'opencv-python'))
|
2021-12-04 22:00:07 +08:00
|
|
|
name = Path(name)
|
2022-05-16 06:56:44 +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-07-05 22:20:46 +08:00
|
|
|
|
2021-04-30 20:59:51 +08:00
|
|
|
if pretrained and channels == 3 and classes == 80:
|
2022-07-17 05:46:23 +08:00
|
|
|
model = DetectMultiBackend(path, device=device, fuse=autoshape) # download/load FP32 model
|
2021-12-04 22:00:07 +08:00
|
|
|
# model = models.experimental.attempt_load(path, map_location=device) # download/load FP32 model
|
2021-04-30 20:59:51 +08:00
|
|
|
else:
|
2021-12-13 20:32:27 +08:00
|
|
|
cfg = list((Path(__file__).parent / 'models').rglob(f'{path.stem}.yaml'))[0] # model.yaml path
|
2021-04-30 20:59:51 +08:00
|
|
|
model = Model(cfg, channels, classes) # create model
|
|
|
|
if pretrained:
|
2021-07-07 19:41:46 +08:00
|
|
|
ckpt = torch.load(attempt_download(path), map_location=device) # load
|
2021-04-30 20:59:51 +08:00
|
|
|
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
2021-11-06 22:41:17 +08:00
|
|
|
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
|
|
|
|
if len(ckpt['model'].names) == classes:
|
|
|
|
model.names = ckpt['model'].names # set class names attribute
|
|
|
|
if autoshape:
|
2021-11-20 08:11:36 +08:00
|
|
|
model = AutoShape(model) # for file/URI/PIL/cv2/np inputs and NMS
|
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:
|
|
|
|
help_url = 'https://github.com/ultralytics/yolov5/issues/36'
|
2022-01-14 15:23:03 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2022-04-24 02:26:25 +08:00
|
|
|
def custom(path='path/to/model.pt', autoshape=True, _verbose=True, device=None):
|
2021-05-01 23:35:02 +08:00
|
|
|
# YOLOv5 custom or local model
|
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):
|
2021-10-12 14:47:18 +08:00
|
|
|
# YOLOv5-nano model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-12 01:23:47 +08:00
|
|
|
# YOLOv5-small model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-12 01:23:47 +08:00
|
|
|
# YOLOv5-medium model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-12 01:23:47 +08:00
|
|
|
# YOLOv5-large model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-12 01:23:47 +08:00
|
|
|
# YOLOv5-xlarge model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-10-12 14:47:18 +08:00
|
|
|
# YOLOv5-nano-P6 model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-26 04:01:05 +08:00
|
|
|
# YOLOv5-small-P6 model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-26 04:01:05 +08:00
|
|
|
# YOLOv5-medium-P6 model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-26 04:01:05 +08:00
|
|
|
# YOLOv5-large-P6 model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +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):
|
2021-04-26 04:01:05 +08:00
|
|
|
# YOLOv5-xlarge-P6 model https://github.com/ultralytics/yolov5
|
2022-04-24 02:26:25 +08:00
|
|
|
return _create('yolov5x6', pretrained, channels, classes, autoshape, _verbose, device)
|
2020-12-13 03:16:57 +08:00
|
|
|
|
|
|
|
|
2020-10-26 08:06:54 +08:00
|
|
|
if __name__ == '__main__':
|
2022-04-24 02:26:25 +08:00
|
|
|
model = _create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True, verbose=True)
|
2021-05-01 23:48:12 +08:00
|
|
|
# model = custom(path='path/to/model.pt') # custom
|
2020-10-28 22:03:50 +08:00
|
|
|
|
|
|
|
# Verify inference
|
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-03-25 20:25:30 +08:00
|
|
|
from utils.general import cv2
|
|
|
|
|
2022-03-31 22:52:34 +08:00
|
|
|
imgs = [
|
|
|
|
'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
|
|
|
|
2021-12-13 20:32:27 +08:00
|
|
|
results = model(imgs, size=320) # batched inference
|
2020-11-17 06:09:55 +08:00
|
|
|
results.print()
|
2021-01-24 04:51:04 +08:00
|
|
|
results.save()
|