Skip to content

Reference for ultralytics/nn/backends/base.py

Improvements

This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/nn/backends/base.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏


class ultralytics.nn.backends.base.BaseBackend

BaseBackend(self, weight: str | torch.nn.Module, device: torch.device | str, fp16: bool = False)

Bases: ABC

Base class for all inference backends.

This abstract class defines the interface that all inference backends must implement. It provides common functionality for model loading, metadata processing, and device management.

Args

NameTypeDescriptionDefault
weightstr | torch.nn.ModulePath to the model weights file or a PyTorch module instance.required
devicetorch.device | strDevice to run inference on (e.g., 'cpu', 'cuda:0').required
fp16boolWhether to use FP16 half-precision inference.False

Attributes

NameTypeDescription
modelThe underlying inference model or runtime session.
devicetorch.deviceThe device to run inference on.
fp16boolWhether to use FP16 (half-precision) inference.
nhwcboolWhether the model expects NHWC input format instead of NCHW.
strideintModel stride, typically 32 for YOLO models.
namesdictDictionary mapping class indices to class names.
taskstr | NoneThe task type (detect, segment, classify, pose, obb).
batchintBatch size for inference.
imgsztupleInput image size as (height, width).
channelsintNumber of input channels, typically 3 for RGB.
end2endboolWhether the model includes end-to-end NMS post-processing.
dynamicboolWhether the model supports dynamic input shapes.
metadatadictModel metadata dictionary containing export configuration.

Methods

NameDescription
apply_metadataProcess and apply model metadata to backend attributes.
forwardRun inference on the input image tensor.
load_modelLoad the model from a weights file or module instance.
Source code in ultralytics/nn/backends/base.pyView on GitHub
class BaseBackend(ABC):
    """Base class for all inference backends.

    This abstract class defines the interface that all inference backends must implement. It provides common
    functionality for model loading, metadata processing, and device management.

    Attributes:
        model: The underlying inference model or runtime session.
        device (torch.device): The device to run inference on.
        fp16 (bool): Whether to use FP16 (half-precision) inference.
        nhwc (bool): Whether the model expects NHWC input format instead of NCHW.
        stride (int): Model stride, typically 32 for YOLO models.
        names (dict): Dictionary mapping class indices to class names.
        task (str | None): The task type (detect, segment, classify, pose, obb).
        batch (int): Batch size for inference.
        imgsz (tuple): Input image size as (height, width).
        channels (int): Number of input channels, typically 3 for RGB.
        end2end (bool): Whether the model includes end-to-end NMS post-processing.
        dynamic (bool): Whether the model supports dynamic input shapes.
        metadata (dict): Model metadata dictionary containing export configuration.
    """

    def __init__(self, weight: str | torch.nn.Module, device: torch.device | str, fp16: bool = False):
        """Initialize the base backend with common attributes and load the model.

        Args:
            weight (str | torch.nn.Module): Path to the model weights file or a PyTorch module instance.
            device (torch.device | str): Device to run inference on (e.g., 'cpu', 'cuda:0').
            fp16 (bool): Whether to use FP16 half-precision inference.
        """
        self.device = device
        self.fp16 = fp16
        self.nhwc = False
        self.stride = 32
        self.names = {}
        self.task = None
        self.batch = 1
        self.channels = 3
        self.end2end = False
        self.dynamic = False
        self.metadata = {}
        self.model = None
        self.load_model(weight)


method ultralytics.nn.backends.base.BaseBackend.apply_metadata

def apply_metadata(self, metadata: dict | None) -> None

Process and apply model metadata to backend attributes.

Handles type conversions for common metadata fields (e.g., stride, batch, names) and sets them as instance attributes. Also resolves end-to-end NMS and dynamic shape settings from export args.

Args

NameTypeDescriptionDefault
metadatadict | NoneDictionary containing metadata key-value pairs from model export.required
Source code in ultralytics/nn/backends/base.pyView on GitHub
def apply_metadata(self, metadata: dict | None) -> None:
    """Process and apply model metadata to backend attributes.

    Handles type conversions for common metadata fields (e.g., stride, batch, names) and sets them as
    instance attributes. Also resolves end-to-end NMS and dynamic shape settings from export args.

    Args:
        metadata (dict | None): Dictionary containing metadata key-value pairs from model export.
    """
    if not metadata:
        return

    # Store raw metadata
    self.metadata = metadata

    # Process type conversions for known fields
    for k, v in metadata.items():
        if k in {"stride", "batch", "channels"}:
            metadata[k] = int(v)
        elif k in {"imgsz", "names", "kpt_shape", "kpt_names", "args", "end2end"} and isinstance(v, str):
            metadata[k] = ast.literal_eval(v)

    # Handle models exported with end-to-end NMS
    metadata["end2end"] = metadata.get("end2end", False) or metadata.get("args", {}).get("nms", False)
    metadata["dynamic"] = metadata.get("args", {}).get("dynamic", self.dynamic)

    # Apply all metadata fields as backend attributes
    for k, v in metadata.items():
        setattr(self, k, v)


method ultralytics.nn.backends.base.BaseBackend.forward

def forward(self, im: torch.Tensor) -> torch.Tensor | list[torch.Tensor]

Run inference on the input image tensor.

Args

NameTypeDescriptionDefault
imtorch.TensorInput image tensor in BCHW format, normalized to [0, 1].required

Returns

TypeDescription
torch.Tensor | list[torch.Tensor]Model output as a single tensor or list of tensors.
Source code in ultralytics/nn/backends/base.pyView on GitHub
@abstractmethod
def forward(self, im: torch.Tensor) -> torch.Tensor | list[torch.Tensor]:
    """Run inference on the input image tensor.

    Args:
        im (torch.Tensor): Input image tensor in BCHW format, normalized to [0, 1].

    Returns:
        (torch.Tensor | list[torch.Tensor]): Model output as a single tensor or list of tensors.
    """
    raise NotImplementedError


method ultralytics.nn.backends.base.BaseBackend.load_model

def load_model(self, weight: str | torch.nn.Module) -> None

Load the model from a weights file or module instance.

Args

NameTypeDescriptionDefault
weightstr | torch.nn.ModulePath to model weights or a PyTorch module.required
Source code in ultralytics/nn/backends/base.pyView on GitHub
@abstractmethod
def load_model(self, weight: str | torch.nn.Module) -> None:
    """Load the model from a weights file or module instance.

    Args:
        weight (str | torch.nn.Module): Path to model weights or a PyTorch module.
    """
    raise NotImplementedError





📅 Created 0 days ago ✏️ Updated 0 days ago
Laughing-q