Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

100 lines
3.3KB

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