model.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. """
  3. SAM model interface.
  4. This module provides an interface to the Segment Anything Model (SAM) from Ultralytics, designed for real-time image
  5. segmentation tasks. The SAM model allows for promptable segmentation with unparalleled versatility in image analysis,
  6. and has been trained on the SA-1B dataset. It features zero-shot performance capabilities, enabling it to adapt to new
  7. image distributions and tasks without prior knowledge.
  8. Key Features:
  9. - Promptable segmentation
  10. - Real-time performance
  11. - Zero-shot transfer capabilities
  12. - Trained on SA-1B dataset
  13. """
  14. from pathlib import Path
  15. from ultralytics.engine.model import Model
  16. from ultralytics.utils.torch_utils import model_info
  17. from .build import build_sam
  18. from .predict import Predictor
  19. class SAM(Model):
  20. """
  21. SAM (Segment Anything Model) interface class.
  22. SAM is designed for promptable real-time image segmentation. It can be used with a variety of prompts such as
  23. bounding boxes, points, or labels. The model has capabilities for zero-shot performance and is trained on the SA-1B
  24. dataset.
  25. """
  26. def __init__(self, model="sam_b.pt") -> None:
  27. """
  28. Initializes the SAM model with a pre-trained model file.
  29. Args:
  30. model (str): Path to the pre-trained SAM model file. File should have a .pt or .pth extension.
  31. Raises:
  32. NotImplementedError: If the model file extension is not .pt or .pth.
  33. """
  34. if model and Path(model).suffix not in {".pt", ".pth"}:
  35. raise NotImplementedError("SAM prediction requires pre-trained *.pt or *.pth model.")
  36. super().__init__(model=model, task="segment")
  37. def _load(self, weights: str, task=None):
  38. """
  39. Loads the specified weights into the SAM model.
  40. Args:
  41. weights (str): Path to the weights file.
  42. task (str, optional): Task name. Defaults to None.
  43. """
  44. self.model = build_sam(weights)
  45. def predict(self, source, stream=False, bboxes=None, points=None, labels=None, **kwargs):
  46. """
  47. Performs segmentation prediction on the given image or video source.
  48. Args:
  49. source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
  50. stream (bool, optional): If True, enables real-time streaming. Defaults to False.
  51. bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
  52. points (list, optional): List of points for prompted segmentation. Defaults to None.
  53. labels (list, optional): List of labels for prompted segmentation. Defaults to None.
  54. Returns:
  55. (list): The model predictions.
  56. """
  57. overrides = dict(conf=0.25, task="segment", mode="predict", imgsz=1024)
  58. kwargs.update(overrides)
  59. prompts = dict(bboxes=bboxes, points=points, labels=labels)
  60. return super().predict(source, stream, prompts=prompts, **kwargs)
  61. def __call__(self, source=None, stream=False, bboxes=None, points=None, labels=None, **kwargs):
  62. """
  63. Alias for the 'predict' method.
  64. Args:
  65. source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
  66. stream (bool, optional): If True, enables real-time streaming. Defaults to False.
  67. bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
  68. points (list, optional): List of points for prompted segmentation. Defaults to None.
  69. labels (list, optional): List of labels for prompted segmentation. Defaults to None.
  70. Returns:
  71. (list): The model predictions.
  72. """
  73. return self.predict(source, stream, bboxes, points, labels, **kwargs)
  74. def info(self, detailed=False, verbose=True):
  75. """
  76. Logs information about the SAM model.
  77. Args:
  78. detailed (bool, optional): If True, displays detailed information about the model. Defaults to False.
  79. verbose (bool, optional): If True, displays information on the console. Defaults to True.
  80. Returns:
  81. (tuple): A tuple containing the model's information.
  82. """
  83. return model_info(self.model, detailed=detailed, verbose=verbose)
  84. @property
  85. def task_map(self):
  86. """
  87. Provides a mapping from the 'segment' task to its corresponding 'Predictor'.
  88. Returns:
  89. (dict): A dictionary mapping the 'segment' task to its corresponding 'Predictor'.
  90. """
  91. return {"segment": {"predictor": Predictor}}