انتقل إلى المحتوى

مرجع ل ultralytics/models/sam/model.py

ملاحظه

هذا الملف متاح في https://github.com/ultralytics/ultralytics/ نقطة / الرئيسية /ultralytics/نماذج/sam/model.py. إذا اكتشفت مشكلة ، فيرجى المساعدة في إصلاحها من خلال المساهمة في طلب 🛠️ سحب. شكرا لك 🙏!



ultralytics.models.sam.model.SAM

قواعد: Model

SAM (نموذج جزء أي شيء) فئة الواجهة.

SAM تم تصميمه لتجزئة الصور في الوقت الفعلي بشكل سريع. يمكن استخدامه مع مجموعة متنوعة من المطالبات مثل المربعات المحيطة أو النقاط أو التسميات. يتمتع النموذج بقدرات لأداء طلقة صفرية ويتم تدريبه على SA-1B مجموعة البيانات.

شفرة المصدر في ultralytics/models/sam/model.py
class SAM(Model):
    """
    SAM (Segment Anything Model) interface class.

    SAM is designed for promptable real-time image segmentation. It can be used with a variety of prompts such as
    bounding boxes, points, or labels. The model has capabilities for zero-shot performance and is trained on the SA-1B
    dataset.
    """

    def __init__(self, model="sam_b.pt") -> None:
        """
        Initializes the SAM model with a pre-trained model file.

        Args:
            model (str): Path to the pre-trained SAM model file. File should have a .pt or .pth extension.

        Raises:
            NotImplementedError: If the model file extension is not .pt or .pth.
        """
        if model and Path(model).suffix not in {".pt", ".pth"}:
            raise NotImplementedError("SAM prediction requires pre-trained *.pt or *.pth model.")
        super().__init__(model=model, task="segment")

    def _load(self, weights: str, task=None):
        """
        Loads the specified weights into the SAM model.

        Args:
            weights (str): Path to the weights file.
            task (str, optional): Task name. Defaults to None.
        """
        self.model = build_sam(weights)

    def predict(self, source, stream=False, bboxes=None, points=None, labels=None, **kwargs):
        """
        Performs segmentation prediction on the given image or video source.

        Args:
            source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
            stream (bool, optional): If True, enables real-time streaming. Defaults to False.
            bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
            points (list, optional): List of points for prompted segmentation. Defaults to None.
            labels (list, optional): List of labels for prompted segmentation. Defaults to None.

        Returns:
            (list): The model predictions.
        """
        overrides = dict(conf=0.25, task="segment", mode="predict", imgsz=1024)
        kwargs.update(overrides)
        prompts = dict(bboxes=bboxes, points=points, labels=labels)
        return super().predict(source, stream, prompts=prompts, **kwargs)

    def __call__(self, source=None, stream=False, bboxes=None, points=None, labels=None, **kwargs):
        """
        Alias for the 'predict' method.

        Args:
            source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
            stream (bool, optional): If True, enables real-time streaming. Defaults to False.
            bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
            points (list, optional): List of points for prompted segmentation. Defaults to None.
            labels (list, optional): List of labels for prompted segmentation. Defaults to None.

        Returns:
            (list): The model predictions.
        """
        return self.predict(source, stream, bboxes, points, labels, **kwargs)

    def info(self, detailed=False, verbose=True):
        """
        Logs information about the SAM model.

        Args:
            detailed (bool, optional): If True, displays detailed information about the model. Defaults to False.
            verbose (bool, optional): If True, displays information on the console. Defaults to True.

        Returns:
            (tuple): A tuple containing the model's information.
        """
        return model_info(self.model, detailed=detailed, verbose=verbose)

    @property
    def task_map(self):
        """
        Provides a mapping from the 'segment' task to its corresponding 'Predictor'.

        Returns:
            (dict): A dictionary mapping the 'segment' task to its corresponding 'Predictor'.
        """
        return {"segment": {"predictor": Predictor}}

task_map property

يوفر تعيينا من مهمة "الجزء" إلى "المتنبئ" المقابل لها.

ارجاع:

نوع وصف
dict

قاموس يعين مهمة "الجزء" إلى "المتنبئ" المقابل لها.

__call__(source=None, stream=False, bboxes=None, points=None, labels=None, **kwargs)

الاسم المستعار لطريقة "التنبؤ".

البارامترات:

اسم نوع وصف افتراضي
source str

المسار إلى ملف الصورة أو الفيديو أو PIL. كائن صورة، أو كائن numpy.ndarray .

None
stream bool

إذا كان صحيحا ، يتيح البث في الوقت الفعلي. الإعدادات الافتراضية إلى خطأ.

False
bboxes list

قائمة إحداثيات المربع المحيط للتجزئة المطلوبة. الإعدادات الافتراضية إلى لا شيء.

None
points list

قائمة النقاط للتجزئة المطلوبة. الإعدادات الافتراضية إلى لا شيء.

None
labels list

قائمة التسميات للتجزئة المطلوبة. الإعدادات الافتراضية إلى لا شيء.

None

ارجاع:

نوع وصف
list

تنبؤات النموذج.

شفرة المصدر في ultralytics/models/sam/model.py
def __call__(self, source=None, stream=False, bboxes=None, points=None, labels=None, **kwargs):
    """
    Alias for the 'predict' method.

    Args:
        source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
        stream (bool, optional): If True, enables real-time streaming. Defaults to False.
        bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
        points (list, optional): List of points for prompted segmentation. Defaults to None.
        labels (list, optional): List of labels for prompted segmentation. Defaults to None.

    Returns:
        (list): The model predictions.
    """
    return self.predict(source, stream, bboxes, points, labels, **kwargs)

__init__(model='sam_b.pt')

تهيئة SAM نموذج مع ملف نموذج مدرب مسبقا.

البارامترات:

اسم نوع وصف افتراضي
model str

الطريق إلى المدربين مسبقا SAM ملف النموذج. يجب أن يكون للملف ملحق .pt أو .pth.

'sam_b.pt'

يثير:

نوع وصف
NotImplementedError

إذا لم يكن ملحق ملف النموذج .pt أو .pth.

شفرة المصدر في ultralytics/models/sam/model.py
def __init__(self, model="sam_b.pt") -> None:
    """
    Initializes the SAM model with a pre-trained model file.

    Args:
        model (str): Path to the pre-trained SAM model file. File should have a .pt or .pth extension.

    Raises:
        NotImplementedError: If the model file extension is not .pt or .pth.
    """
    if model and Path(model).suffix not in {".pt", ".pth"}:
        raise NotImplementedError("SAM prediction requires pre-trained *.pt or *.pth model.")
    super().__init__(model=model, task="segment")

info(detailed=False, verbose=True)

يسجل معلومات حول SAM نموذج.

البارامترات:

اسم نوع وصف افتراضي
detailed bool

إذا كان True ، يعرض معلومات تفصيلية حول النموذج. الإعدادات الافتراضية إلى خطأ.

False
verbose bool

إذا كان صحيحا، يعرض المعلومات على وحدة التحكم. الإعدادات الافتراضية إلى صواب.

True

ارجاع:

نوع وصف
tuple

مجموعة تحتوي على معلومات النموذج.

شفرة المصدر في ultralytics/models/sam/model.py
def info(self, detailed=False, verbose=True):
    """
    Logs information about the SAM model.

    Args:
        detailed (bool, optional): If True, displays detailed information about the model. Defaults to False.
        verbose (bool, optional): If True, displays information on the console. Defaults to True.

    Returns:
        (tuple): A tuple containing the model's information.
    """
    return model_info(self.model, detailed=detailed, verbose=verbose)

predict(source, stream=False, bboxes=None, points=None, labels=None, **kwargs)

يقوم بالتنبؤ بالتجزئة على الصورة أو مصدر الفيديو المحدد.

البارامترات:

اسم نوع وصف افتراضي
source str

المسار إلى ملف الصورة أو الفيديو أو PIL. كائن صورة، أو كائن numpy.ndarray .

مطلوب
stream bool

إذا كان صحيحا ، يتيح البث في الوقت الفعلي. الإعدادات الافتراضية إلى خطأ.

False
bboxes list

قائمة إحداثيات المربع المحيط للتجزئة المطلوبة. الإعدادات الافتراضية إلى لا شيء.

None
points list

قائمة النقاط للتجزئة المطلوبة. الإعدادات الافتراضية إلى لا شيء.

None
labels list

قائمة التسميات للتجزئة المطلوبة. الإعدادات الافتراضية إلى لا شيء.

None

ارجاع:

نوع وصف
list

تنبؤات النموذج.

شفرة المصدر في ultralytics/models/sam/model.py
def predict(self, source, stream=False, bboxes=None, points=None, labels=None, **kwargs):
    """
    Performs segmentation prediction on the given image or video source.

    Args:
        source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
        stream (bool, optional): If True, enables real-time streaming. Defaults to False.
        bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
        points (list, optional): List of points for prompted segmentation. Defaults to None.
        labels (list, optional): List of labels for prompted segmentation. Defaults to None.

    Returns:
        (list): The model predictions.
    """
    overrides = dict(conf=0.25, task="segment", mode="predict", imgsz=1024)
    kwargs.update(overrides)
    prompts = dict(bboxes=bboxes, points=points, labels=labels)
    return super().predict(source, stream, prompts=prompts, **kwargs)





تم الإنشاء 2023-11-12-2023، تم التحديث 2024-05-18
المؤلفون: جلين-جوتشر (4)، برهان-ق (1)