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.
60 lines
2.5 KiB
60 lines
2.5 KiB
2 years ago
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||
|
"""
|
||
|
SAM model interface
|
||
|
"""
|
||
2 years ago
|
|
||
1 year ago
|
from ultralytics.cfg import get_cfg
|
||
|
from ultralytics.utils.torch_utils import model_info
|
||
2 years ago
|
|
||
|
from .build import build_sam
|
||
|
from .predict import Predictor
|
||
|
|
||
|
|
||
|
class SAM:
|
||
|
|
||
|
def __init__(self, model='sam_b.pt') -> None:
|
||
2 years ago
|
if model and not model.endswith('.pt') and not model.endswith('.pth'):
|
||
2 years ago
|
# Should raise AssertionError instead?
|
||
|
raise NotImplementedError('Segment anything prediction requires pre-trained checkpoint')
|
||
|
self.model = build_sam(model)
|
||
2 years ago
|
self.task = 'segment' # required
|
||
2 years ago
|
self.predictor = None # reuse predictor
|
||
|
|
||
1 year ago
|
def predict(self, source, stream=False, bboxes=None, points=None, labels=None, **kwargs):
|
||
2 years ago
|
"""Predicts and returns segmentation masks for given image or video source."""
|
||
1 year ago
|
overrides = dict(conf=0.25, task='segment', mode='predict', imgsz=1024)
|
||
2 years ago
|
overrides.update(kwargs) # prefer kwargs
|
||
|
if not self.predictor:
|
||
|
self.predictor = Predictor(overrides=overrides)
|
||
|
self.predictor.setup_model(model=self.model)
|
||
|
else: # only update args if predictor is already setup
|
||
|
self.predictor.args = get_cfg(self.predictor.args, overrides)
|
||
1 year ago
|
return self.predictor(source, stream=stream, bboxes=bboxes, points=points, labels=labels)
|
||
2 years ago
|
|
||
|
def train(self, **kwargs):
|
||
|
"""Function trains models but raises an error as SAM models do not support training."""
|
||
|
raise NotImplementedError("SAM models don't support training")
|
||
|
|
||
|
def val(self, **kwargs):
|
||
|
"""Run validation given dataset."""
|
||
|
raise NotImplementedError("SAM models don't support validation")
|
||
2 years ago
|
|
||
1 year ago
|
def __call__(self, source=None, stream=False, bboxes=None, points=None, labels=None, **kwargs):
|
||
2 years ago
|
"""Calls the 'predict' function with given arguments to perform object detection."""
|
||
1 year ago
|
return self.predict(source, stream, bboxes, points, labels, **kwargs)
|
||
2 years ago
|
|
||
|
def __getattr__(self, attr):
|
||
|
"""Raises error if object has no requested attribute."""
|
||
|
name = self.__class__.__name__
|
||
|
raise AttributeError(f"'{name}' object has no attribute '{attr}'. See valid attributes below.\n{self.__doc__}")
|
||
|
|
||
2 years ago
|
def info(self, detailed=False, verbose=True):
|
||
|
"""
|
||
|
Logs model info.
|
||
|
|
||
|
Args:
|
||
|
detailed (bool): Show detailed information about model.
|
||
|
verbose (bool): Controls verbosity.
|
||
|
"""
|
||
|
return model_info(self.model, detailed=detailed, verbose=verbose)
|