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.

hubconf.py 2.9KB

4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 torch
  8. from models.yolo import Model
  9. from utils import google_utils
  10. def create(name, pretrained, channels, classes):
  11. """Creates a specified YOLOv5 model
  12. Arguments:
  13. name (str): name of model, i.e. 'yolov5s'
  14. pretrained (bool): load pretrained weights into the model
  15. channels (int): number of input channels
  16. classes (int): number of model classes
  17. Returns:
  18. pytorch model
  19. """
  20. model = Model('models/%s.yaml' % name, channels, classes)
  21. if pretrained:
  22. ckpt = '%s.pt' % name # checkpoint filename
  23. google_utils.attempt_download(ckpt) # download if not found locally
  24. state_dict = torch.load(ckpt)['model'].state_dict()
  25. state_dict = {k: v for k, v in state_dict.items() if model.state_dict()[k].numel() == v.numel()} # filter
  26. model.load_state_dict(state_dict, strict=False) # load
  27. return model
  28. def yolov5s(pretrained=False, channels=3, classes=80):
  29. """YOLOv5-small model from https://github.com/ultralytics/yolov5
  30. Arguments:
  31. pretrained (bool): load pretrained weights into the model, default=False
  32. channels (int): number of input channels, default=3
  33. classes (int): number of model classes, default=80
  34. Returns:
  35. pytorch model
  36. """
  37. return create('yolov5s', pretrained, channels, classes)
  38. def yolov5m(pretrained=False, channels=3, classes=80):
  39. """YOLOv5-medium 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('yolov5m', pretrained, channels, classes)
  48. def yolov5l(pretrained=False, channels=3, classes=80):
  49. """YOLOv5-large 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('yolov5l', pretrained, channels, classes)
  58. def yolov5x(pretrained=False, channels=3, classes=80):
  59. """YOLOv5-xlarge 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('yolov5x', pretrained, channels, classes)