์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ ultralytics/models/yolo/model.py

์ฐธ๊ณ 

์ด ํŒŒ์ผ์€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/models/ yolo/model .py์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ ๐Ÿ› ๏ธ ์— ๊ธฐ์—ฌํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋„๋ก ๋„์™€์ฃผ์„ธ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™!



ultralytics.models.yolo.model.YOLO

๋ฒ ์ด์Šค: Model

YOLO (You Only Look Once) ๊ฐ์ฒด ๊ฐ์ง€ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/yolo/model.py
class YOLO(Model):
    """YOLO (You Only Look Once) object detection model."""

    def __init__(self, model="yolov8n.pt", task=None, verbose=False):
        """Initialize YOLO model, switching to YOLOWorld if model filename contains '-world'."""
        path = Path(model)
        if "-world" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}:  # if YOLOWorld PyTorch model
            new_instance = YOLOWorld(path, verbose=verbose)
            self.__class__ = type(new_instance)
            self.__dict__ = new_instance.__dict__
        else:
            # Continue with default YOLO initialization
            super().__init__(model=model, task=task, verbose=verbose)

    @property
    def task_map(self):
        """Map head to model, trainer, validator, and predictor classes."""
        return {
            "classify": {
                "model": ClassificationModel,
                "trainer": yolo.classify.ClassificationTrainer,
                "validator": yolo.classify.ClassificationValidator,
                "predictor": yolo.classify.ClassificationPredictor,
            },
            "detect": {
                "model": DetectionModel,
                "trainer": yolo.detect.DetectionTrainer,
                "validator": yolo.detect.DetectionValidator,
                "predictor": yolo.detect.DetectionPredictor,
            },
            "segment": {
                "model": SegmentationModel,
                "trainer": yolo.segment.SegmentationTrainer,
                "validator": yolo.segment.SegmentationValidator,
                "predictor": yolo.segment.SegmentationPredictor,
            },
            "pose": {
                "model": PoseModel,
                "trainer": yolo.pose.PoseTrainer,
                "validator": yolo.pose.PoseValidator,
                "predictor": yolo.pose.PosePredictor,
            },
            "obb": {
                "model": OBBModel,
                "trainer": yolo.obb.OBBTrainer,
                "validator": yolo.obb.OBBValidator,
                "predictor": yolo.obb.OBBPredictor,
            },
        }

task_map property

ํ—ค๋“œ๋ฅผ ๋ชจ๋ธ, ํŠธ๋ ˆ์ด๋„ˆ, ๊ฒ€์ฆ์ž ๋ฐ ์˜ˆ์ธก์ž ํด๋ž˜์Šค์— ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.

__init__(model='yolov8n.pt', task=None, verbose=False)

YOLO ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ๋ชจ๋ธ ํŒŒ์ผ๋ช…์— '-world'๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ YOLOWorld๋กœ ์ „ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/yolo/model.py
def __init__(self, model="yolov8n.pt", task=None, verbose=False):
    """Initialize YOLO model, switching to YOLOWorld if model filename contains '-world'."""
    path = Path(model)
    if "-world" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}:  # if YOLOWorld PyTorch model
        new_instance = YOLOWorld(path, verbose=verbose)
        self.__class__ = type(new_instance)
        self.__dict__ = new_instance.__dict__
    else:
        # Continue with default YOLO initialization
        super().__init__(model=model, task=task, verbose=verbose)



ultralytics.models.yolo.model.YOLOWorld

๋ฒ ์ด์Šค: Model

YOLO-์›”๋“œ ์˜ค๋ธŒ์ ํŠธ ๊ฐ์ง€ ๋ชจ๋ธ.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/yolo/model.py
class YOLOWorld(Model):
    """YOLO-World object detection model."""

    def __init__(self, model="yolov8s-world.pt", verbose=False) -> None:
        """
        Initializes the YOLOv8-World model with the given pre-trained model file. Supports *.pt and *.yaml formats.

        Args:
            model (str | Path): Path to the pre-trained model. Defaults to 'yolov8s-world.pt'.
        """
        super().__init__(model=model, task="detect", verbose=verbose)

        # Assign default COCO class names when there are no custom names
        if not hasattr(self.model, "names"):
            self.model.names = yaml_load(ROOT / "cfg/datasets/coco8.yaml").get("names")

    @property
    def task_map(self):
        """Map head to model, validator, and predictor classes."""
        return {
            "detect": {
                "model": WorldModel,
                "validator": yolo.detect.DetectionValidator,
                "predictor": yolo.detect.DetectionPredictor,
                "trainer": yolo.world.WorldTrainer,
            }
        }

    def set_classes(self, classes):
        """
        Set classes.

        Args:
            classes (List(str)): A list of categories i.e. ["person"].
        """
        self.model.set_classes(classes)
        # Remove background if it's given
        background = " "
        if background in classes:
            classes.remove(background)
        self.model.names = classes

        # Reset method class names
        # self.predictor = None  # reset predictor otherwise old names remain
        if self.predictor:
            self.predictor.model.names = classes

task_map property

ํ—ค๋“œ๋ฅผ ๋ชจ๋ธ, ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ธฐ ๋ฐ ์˜ˆ์ธก์ž ํด๋ž˜์Šค์— ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.

__init__(model='yolov8s-world.pt', verbose=False)

์ง€์ •๋œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ ํŒŒ์ผ๋กœ YOLOv8-World ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. .pt ๋ฐ .yaml ํ˜•์‹์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
model str | Path

์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 'yolov8s-world.pt'์ž…๋‹ˆ๋‹ค.

'yolov8s-world.pt'
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/yolo/model.py
def __init__(self, model="yolov8s-world.pt", verbose=False) -> None:
    """
    Initializes the YOLOv8-World model with the given pre-trained model file. Supports *.pt and *.yaml formats.

    Args:
        model (str | Path): Path to the pre-trained model. Defaults to 'yolov8s-world.pt'.
    """
    super().__init__(model=model, task="detect", verbose=verbose)

    # Assign default COCO class names when there are no custom names
    if not hasattr(self.model, "names"):
        self.model.names = yaml_load(ROOT / "cfg/datasets/coco8.yaml").get("names")

set_classes(classes)

ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
classes List(str

A list of categories i.e. ["person"].

ํ•„์ˆ˜
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/yolo/model.py
def set_classes(self, classes):
    """
    Set classes.

    Args:
        classes (List(str)): A list of categories i.e. ["person"].
    """
    self.model.set_classes(classes)
    # Remove background if it's given
    background = " "
    if background in classes:
        classes.remove(background)
    self.model.names = classes

    # Reset method class names
    # self.predictor = None  # reset predictor otherwise old names remain
    if self.predictor:
        self.predictor.model.names = classes





Created 2023-11-12, Updated 2024-06-02
Authors: glenn-jocher (6), sergiuwaxmann (1), Burhan-Q (1)