Skip to content

Reference for ultralytics/models/yolo/model.py

Note

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!


ultralytics.models.yolo.model.YOLO

YOLO(model='yolo11n.pt', task=None, verbose=False)

Bases: Model

YOLO (You Only Look Once) object detection model.

Source code in ultralytics/models/yolo/model.py
def __init__(self, model="yolo11n.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)

task_map property

task_map

Map head to model, trainer, validator, and predictor classes.





ultralytics.models.yolo.model.YOLOWorld

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

Bases: Model

YOLO-World object detection model.

Loads a YOLOv8-World model for object detection. If no custom class names are provided, it assigns default COCO class names.

Parameters:

Name Type Description Default
model str | Path

Path to the pre-trained model file. Supports .pt and .yaml formats.

'yolov8s-world.pt'
verbose bool

If True, prints additional information during initialization.

False
Source code in ultralytics/models/yolo/model.py
def __init__(self, model="yolov8s-world.pt", verbose=False) -> None:
    """
    Initialize YOLOv8-World model with a pre-trained model file.

    Loads a YOLOv8-World model for object detection. If no custom class names are provided, it assigns default
    COCO class names.

    Args:
        model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats.
        verbose (bool): If True, prints additional information during initialization.
    """
    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")

task_map property

task_map

Map head to model, validator, and predictor classes.

set_classes

set_classes(classes)

Set classes.

Parameters:

Name Type Description Default
classes List(str

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

required
Source code in 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 1 year ago ✏️ Updated 2 months ago