model.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. """
  3. YOLO-NAS model interface.
  4. Example:
  5. ```python
  6. from ultralytics import NAS
  7. model = NAS('yolo_nas_s')
  8. results = model.predict('ultralytics/assets/bus.jpg')
  9. ```
  10. """
  11. from pathlib import Path
  12. import torch
  13. from ultralytics.engine.model import Model
  14. from ultralytics.utils.torch_utils import model_info, smart_inference_mode
  15. from .predict import NASPredictor
  16. from .val import NASValidator
  17. class NAS(Model):
  18. """
  19. YOLO NAS model for object detection.
  20. This class provides an interface for the YOLO-NAS models and extends the `Model` class from Ultralytics engine.
  21. It is designed to facilitate the task of object detection using pre-trained or custom-trained YOLO-NAS models.
  22. Example:
  23. ```python
  24. from ultralytics import NAS
  25. model = NAS('yolo_nas_s')
  26. results = model.predict('ultralytics/assets/bus.jpg')
  27. ```
  28. Attributes:
  29. model (str): Path to the pre-trained model or model name. Defaults to 'yolo_nas_s.pt'.
  30. Note:
  31. YOLO-NAS models only support pre-trained models. Do not provide YAML configuration files.
  32. """
  33. def __init__(self, model="yolo_nas_s.pt") -> None:
  34. """Initializes the NAS model with the provided or default 'yolo_nas_s.pt' model."""
  35. assert Path(model).suffix not in {".yaml", ".yml"}, "YOLO-NAS models only support pre-trained models."
  36. super().__init__(model, task="detect")
  37. @smart_inference_mode()
  38. def _load(self, weights: str, task: str):
  39. """Loads an existing NAS model weights or creates a new NAS model with pretrained weights if not provided."""
  40. import super_gradients
  41. suffix = Path(weights).suffix
  42. if suffix == ".pt":
  43. self.model = torch.load(weights)
  44. elif suffix == "":
  45. self.model = super_gradients.training.models.get(weights, pretrained_weights="coco")
  46. # Standardize model
  47. self.model.fuse = lambda verbose=True: self.model
  48. self.model.stride = torch.tensor([32])
  49. self.model.names = dict(enumerate(self.model._class_names))
  50. self.model.is_fused = lambda: False # for info()
  51. self.model.yaml = {} # for info()
  52. self.model.pt_path = weights # for export()
  53. self.model.task = "detect" # for export()
  54. def info(self, detailed=False, verbose=True):
  55. """
  56. Logs model info.
  57. Args:
  58. detailed (bool): Show detailed information about model.
  59. verbose (bool): Controls verbosity.
  60. """
  61. return model_info(self.model, detailed=detailed, verbose=verbose, imgsz=640)
  62. @property
  63. def task_map(self):
  64. """Returns a dictionary mapping tasks to respective predictor and validator classes."""
  65. return {"detect": {"predictor": NASPredictor, "validator": NASValidator}}