2021-08-14 21:17:51 +02:00
|
|
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
|
|
|
"""
|
|
|
|
PyTorch Hub models https://pytorch.org/hub/ultralytics_yolov5/
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
import torch
|
2021-03-24 15:42:00 +01:00
|
|
|
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
|
2021-12-04 15:00:07 +01:00
|
|
|
model = torch.hub.load('ultralytics/yolov5:master', 'custom', 'path/to/yolov5s.onnx') # file from branch
|
2020-06-10 19:11:11 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2022-01-25 14:57:27 -08:00
|
|
|
"""Creates or loads a YOLOv5 model
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
Arguments:
|
2022-01-25 14:57:27 -08:00
|
|
|
name (str): model name 'yolov5s' or path 'path/to/best.pt'
|
2020-06-10 19:11:11 -07:00
|
|
|
pretrained (bool): load pretrained weights into the model
|
|
|
|
channels (int): number of input channels
|
|
|
|
classes (int): number of model classes
|
2021-04-30 14:59:51 +02:00
|
|
|
autoshape (bool): apply YOLOv5 .autoshape() wrapper to model
|
|
|
|
verbose (bool): print all information to screen
|
2021-05-16 17:41:26 +02:00
|
|
|
device (str, torch.device, None): device to use for model parameters
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
Returns:
|
2022-01-25 14:57:27 -08:00
|
|
|
YOLOv5 model
|
2020-06-10 19:11:11 -07:00
|
|
|
"""
|
2021-07-07 16:08:42 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-12-04 15:00:07 +01:00
|
|
|
from models.common import AutoShape, DetectMultiBackend
|
2021-11-04 17:24:25 +01:00
|
|
|
from models.yolo import Model
|
2021-07-28 02:04:10 +02:00
|
|
|
from utils.downloads import attempt_download
|
2022-01-25 14:57:27 -08:00
|
|
|
from utils.general import LOGGER, check_requirements, intersect_dicts, logging
|
2021-05-12 20:18:32 +02:00
|
|
|
from utils.torch_utils import select_device
|
|
|
|
|
2022-01-25 14:57:27 -08:00
|
|
|
if not verbose:
|
|
|
|
LOGGER.setLevel(logging.WARNING)
|
2021-09-18 18:34:30 +02:00
|
|
|
check_requirements(exclude=('tensorboard', 'thop', 'opencv-python'))
|
2021-12-04 15:00:07 +01:00
|
|
|
name = Path(name)
|
|
|
|
path = name.with_suffix('.pt') if name.suffix == '' else name # checkpoint path
|
2020-07-11 15:03:12 -07:00
|
|
|
try:
|
2021-07-05 16:20:46 +02:00
|
|
|
device = select_device(('0' if torch.cuda.is_available() else 'cpu') if device is None else device)
|
|
|
|
|
2021-04-30 14:59:51 +02:00
|
|
|
if pretrained and channels == 3 and classes == 80:
|
2021-12-04 15:00:07 +01:00
|
|
|
model = DetectMultiBackend(path, device=device) # download/load FP32 model
|
|
|
|
# model = models.experimental.attempt_load(path, map_location=device) # download/load FP32 model
|
2021-04-30 14:59:51 +02:00
|
|
|
else:
|
2021-12-13 13:32:27 +01:00
|
|
|
cfg = list((Path(__file__).parent / 'models').rglob(f'{path.stem}.yaml'))[0] # model.yaml path
|
2021-04-30 14:59:51 +02:00
|
|
|
model = Model(cfg, channels, classes) # create model
|
|
|
|
if pretrained:
|
2021-07-07 21:41:46 +10:00
|
|
|
ckpt = torch.load(attempt_download(path), map_location=device) # load
|
2021-04-30 14:59:51 +02:00
|
|
|
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
2021-11-06 15:41:17 +01:00
|
|
|
csd = intersect_dicts(csd, model.state_dict(), exclude=['anchors']) # intersect
|
2021-04-30 14:59:51 +02: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 01:11:36 +01:00
|
|
|
model = AutoShape(model) # for file/URI/PIL/cv2/np inputs and NMS
|
2021-03-14 23:16:17 -07:00
|
|
|
return model.to(device)
|
2020-07-16 16:44:00 -07:00
|
|
|
|
2020-07-11 15:03:12 -07:00
|
|
|
except Exception as e:
|
|
|
|
help_url = 'https://github.com/ultralytics/yolov5/issues/36'
|
2022-01-13 21:23:03 -10:00
|
|
|
s = f'{e}. Cache may be out of date, try `force_reload=True` or see {help_url} for help.'
|
2020-07-16 16:44:00 -07:00
|
|
|
raise Exception(s) from e
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def custom(path='path/to/model.pt', autoshape=True, verbose=True, device=None):
|
2021-05-01 17:35:02 +02:00
|
|
|
# YOLOv5 custom or local model
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create(path, autoshape=autoshape, verbose=verbose, device=device)
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
|
2021-10-11 23:47:18 -07:00
|
|
|
def yolov5n(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
|
|
|
# YOLOv5-nano model https://github.com/ultralytics/yolov5
|
|
|
|
return _create('yolov5n', pretrained, channels, classes, autoshape, verbose, device)
|
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-11 19:23:47 +02:00
|
|
|
# YOLOv5-small model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5s', pretrained, channels, classes, autoshape, verbose, device)
|
2020-06-10 19:11:11 -07:00
|
|
|
|
2021-04-11 19:23:47 +02:00
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-11 19:23:47 +02:00
|
|
|
# YOLOv5-medium model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5m', pretrained, channels, classes, autoshape, verbose, device)
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-11 19:23:47 +02:00
|
|
|
# YOLOv5-large model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5l', pretrained, channels, classes, autoshape, verbose, device)
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-11 19:23:47 +02:00
|
|
|
# YOLOv5-xlarge model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5x', pretrained, channels, classes, autoshape, verbose, device)
|
2020-06-10 19:11:11 -07:00
|
|
|
|
|
|
|
|
2021-10-11 23:47:18 -07:00
|
|
|
def yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
|
|
|
# YOLOv5-nano-P6 model https://github.com/ultralytics/yolov5
|
|
|
|
return _create('yolov5n6', pretrained, channels, classes, autoshape, verbose, device)
|
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-26 03:01:05 +07:00
|
|
|
# YOLOv5-small-P6 model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5s6', pretrained, channels, classes, autoshape, verbose, device)
|
2020-10-26 01:06:54 +01:00
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-26 03:01:05 +07:00
|
|
|
# YOLOv5-medium-P6 model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5m6', pretrained, channels, classes, autoshape, verbose, device)
|
2020-12-12 11:16:57 -08:00
|
|
|
|
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-26 03:01:05 +07:00
|
|
|
# YOLOv5-large-P6 model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5l6', pretrained, channels, classes, autoshape, verbose, device)
|
2020-12-12 11:16:57 -08:00
|
|
|
|
2021-04-11 19:23:47 +02:00
|
|
|
|
2021-05-16 17:41:26 +02:00
|
|
|
def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
|
2021-04-26 03:01:05 +07:00
|
|
|
# YOLOv5-xlarge-P6 model https://github.com/ultralytics/yolov5
|
2021-05-16 17:41:26 +02:00
|
|
|
return _create('yolov5x6', pretrained, channels, classes, autoshape, verbose, device)
|
2020-12-12 11:16:57 -08:00
|
|
|
|
|
|
|
|
2020-10-26 01:06:54 +01:00
|
|
|
if __name__ == '__main__':
|
2021-05-12 20:18:32 +02:00
|
|
|
model = _create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True, verbose=True) # pretrained
|
2021-05-01 17:48:12 +02:00
|
|
|
# model = custom(path='path/to/model.pt') # custom
|
2020-10-28 15:03:50 +01:00
|
|
|
|
|
|
|
# Verify inference
|
2021-11-04 17:24:25 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-02-19 12:35:38 -08:00
|
|
|
import numpy as np
|
2020-11-16 23:20:17 +01:00
|
|
|
from PIL import Image
|
|
|
|
|
2022-03-25 20:25:30 +08:00
|
|
|
from utils.general import cv2
|
|
|
|
|
2022-03-31 23:52:34 +09: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-19 12:35:38 -08:00
|
|
|
|
2021-12-13 13:32:27 +01:00
|
|
|
results = model(imgs, size=320) # batched inference
|
2020-11-16 23:09:55 +01:00
|
|
|
results.print()
|
2021-01-23 12:51:04 -08:00
|
|
|
results.save()
|