Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/hubconf.py
Views: 475
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license1"""2PyTorch Hub models https://pytorch.org/hub/ultralytics_yolov5/34Usage:5import torch6model = torch.hub.load('ultralytics/yolov5', 'yolov5s')7model = torch.hub.load('ultralytics/yolov5:master', 'custom', 'path/to/yolov5s.onnx') # file from branch8"""910import torch111213def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):14"""Creates a specified YOLOv5 model1516Arguments:17name (str): name of model, i.e. 'yolov5s'18pretrained (bool): load pretrained weights into the model19channels (int): number of input channels20classes (int): number of model classes21autoshape (bool): apply YOLOv5 .autoshape() wrapper to model22verbose (bool): print all information to screen23device (str, torch.device, None): device to use for model parameters2425Returns:26YOLOv5 pytorch model27"""28from pathlib import Path2930from models.common import AutoShape, DetectMultiBackend31from models.yolo import Model32from utils.downloads import attempt_download33from utils.general import check_requirements, intersect_dicts, set_logging34from utils.torch_utils import select_device3536check_requirements(exclude=('tensorboard', 'thop', 'opencv-python'))37set_logging(verbose=verbose)3839name = Path(name)40path = name.with_suffix('.pt') if name.suffix == '' else name # checkpoint path41try:42device = select_device(('0' if torch.cuda.is_available() else 'cpu') if device is None else device)4344if pretrained and channels == 3 and classes == 80:45model = DetectMultiBackend(path, device=device) # download/load FP32 model46# model = models.experimental.attempt_load(path, map_location=device) # download/load FP32 model47else:48cfg = list((Path(__file__).parent / 'models').rglob(f'{path.stem}.yaml'))[0] # model.yaml path49model = Model(cfg, channels, classes) # create model50if pretrained:51ckpt = torch.load(attempt_download(path), map_location=device) # load52csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP3253csd = intersect_dicts(csd, model.state_dict(), exclude=['anchors']) # intersect54model.load_state_dict(csd, strict=False) # load55if len(ckpt['model'].names) == classes:56model.names = ckpt['model'].names # set class names attribute57if autoshape:58model = AutoShape(model) # for file/URI/PIL/cv2/np inputs and NMS59return model.to(device)6061except Exception as e:62help_url = 'https://github.com/ultralytics/yolov5/issues/36'63s = 'Cache may be out of date, try `force_reload=True`. See %s for help.' % help_url64raise Exception(s) from e656667def custom(path='path/to/model.pt', autoshape=True, verbose=True, device=None):68# YOLOv5 custom or local model69return _create(path, autoshape=autoshape, verbose=verbose, device=device)707172def yolov5n(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):73# YOLOv5-nano model https://github.com/ultralytics/yolov574return _create('yolov5n', pretrained, channels, classes, autoshape, verbose, device)757677def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):78# YOLOv5-small model https://github.com/ultralytics/yolov579return _create('yolov5s', pretrained, channels, classes, autoshape, verbose, device)808182def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):83# YOLOv5-medium model https://github.com/ultralytics/yolov584return _create('yolov5m', pretrained, channels, classes, autoshape, verbose, device)858687def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):88# YOLOv5-large model https://github.com/ultralytics/yolov589return _create('yolov5l', pretrained, channels, classes, autoshape, verbose, device)909192def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):93# YOLOv5-xlarge model https://github.com/ultralytics/yolov594return _create('yolov5x', pretrained, channels, classes, autoshape, verbose, device)959697def yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):98# YOLOv5-nano-P6 model https://github.com/ultralytics/yolov599return _create('yolov5n6', pretrained, channels, classes, autoshape, verbose, device)100101102def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):103# YOLOv5-small-P6 model https://github.com/ultralytics/yolov5104return _create('yolov5s6', pretrained, channels, classes, autoshape, verbose, device)105106107def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):108# YOLOv5-medium-P6 model https://github.com/ultralytics/yolov5109return _create('yolov5m6', pretrained, channels, classes, autoshape, verbose, device)110111112def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):113# YOLOv5-large-P6 model https://github.com/ultralytics/yolov5114return _create('yolov5l6', pretrained, channels, classes, autoshape, verbose, device)115116117def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):118# YOLOv5-xlarge-P6 model https://github.com/ultralytics/yolov5119return _create('yolov5x6', pretrained, channels, classes, autoshape, verbose, device)120121122if __name__ == '__main__':123model = _create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True, verbose=True) # pretrained124# model = custom(path='path/to/model.pt') # custom125126# Verify inference127from pathlib import Path128129import cv2130import numpy as np131from PIL import Image132133imgs = ['data/images/zidane.jpg', # filename134Path('data/images/zidane.jpg'), # Path135'https://ultralytics.com/images/zidane.jpg', # URI136cv2.imread('data/images/bus.jpg')[:, :, ::-1], # OpenCV137Image.open('data/images/bus.jpg'), # PIL138np.zeros((320, 640, 3))] # numpy139140results = model(imgs, size=320) # batched inference141results.print()142results.save()143144145