You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 line
3.4KB

  1. """File for accessing YOLOv5 via PyTorch Hub https://pytorch.org/hub/
  2. Usage:
  3. import torch
  4. model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, channels=3, classes=80)
  5. """
  6. dependencies = ['torch', 'yaml']
  7. import os
  8. import torch
  9. from models.common import NMS
  10. from models.yolo import Model
  11. from utils.google_utils import attempt_download
  12. def create(name, pretrained, channels, classes):
  13. """Creates a specified YOLOv5 model
  14. Arguments:
  15. name (str): name of model, i.e. 'yolov5s'
  16. pretrained (bool): load pretrained weights into the model
  17. channels (int): number of input channels
  18. classes (int): number of model classes
  19. Returns:
  20. pytorch model
  21. """
  22. config = os.path.join(os.path.dirname(__file__), 'models', '%s.yaml' % name) # model.yaml path
  23. try:
  24. model = Model(config, channels, classes)
  25. if pretrained:
  26. ckpt = '%s.pt' % name # checkpoint filename
  27. attempt_download(ckpt) # download if not found locally
  28. state_dict = torch.load(ckpt, map_location=torch.device('cpu'))['model'].float().state_dict() # to FP32
  29. state_dict = {k: v for k, v in state_dict.items() if model.state_dict()[k].shape == v.shape} # filter
  30. model.load_state_dict(state_dict, strict=False) # load
  31. model.add_nms() # add NMS module
  32. model.eval()
  33. return model
  34. except Exception as e:
  35. help_url = 'https://github.com/ultralytics/yolov5/issues/36'
  36. s = 'Cache maybe be out of date, deleting cache and retrying may solve this. See %s for help.' % help_url
  37. raise Exception(s) from e
  38. def yolov5s(pretrained=False, channels=3, classes=80):
  39. """YOLOv5-small model from https://github.com/ultralytics/yolov5
  40. Arguments:
  41. pretrained (bool): load pretrained weights into the model, default=False
  42. channels (int): number of input channels, default=3
  43. classes (int): number of model classes, default=80
  44. Returns:
  45. pytorch model
  46. """
  47. return create('yolov5s', pretrained, channels, classes)
  48. def yolov5m(pretrained=False, channels=3, classes=80):
  49. """YOLOv5-medium model from https://github.com/ultralytics/yolov5
  50. Arguments:
  51. pretrained (bool): load pretrained weights into the model, default=False
  52. channels (int): number of input channels, default=3
  53. classes (int): number of model classes, default=80
  54. Returns:
  55. pytorch model
  56. """
  57. return create('yolov5m', pretrained, channels, classes)
  58. def yolov5l(pretrained=False, channels=3, classes=80):
  59. """YOLOv5-large model from https://github.com/ultralytics/yolov5
  60. Arguments:
  61. pretrained (bool): load pretrained weights into the model, default=False
  62. channels (int): number of input channels, default=3
  63. classes (int): number of model classes, default=80
  64. Returns:
  65. pytorch model
  66. """
  67. return create('yolov5l', pretrained, channels, classes)
  68. def yolov5x(pretrained=False, channels=3, classes=80):
  69. """YOLOv5-xlarge model from https://github.com/ultralytics/yolov5
  70. Arguments:
  71. pretrained (bool): load pretrained weights into the model, default=False
  72. channels (int): number of input channels, default=3
  73. classes (int): number of model classes, default=80
  74. Returns:
  75. pytorch model
  76. """
  77. return create('yolov5x', pretrained, channels, classes)