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
| Name | Type | Description | Default |
|---|---|---|---|
weight | str | torch.nn.Module | Path to the model weights file or a PyTorch module instance. | required |
device | torch.device | str | Device to run inference on (e.g., 'cpu', 'cuda:0'). | required |
fp16 | bool | Whether to use FP16 half-precision inference. | False |
Attributes
| Name | Type | Description |
|---|---|---|
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. |
Methods
| Name | Description |
|---|---|
apply_metadata | Process and apply model metadata to backend attributes. |
forward | Run inference on the input image tensor. |
load_model | Load the model from a weights file or module instance. |
Source code in ultralytics/nn/backends/base.py
View on GitHubclass 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
| Name | Type | Description | Default |
|---|---|---|---|
metadata | dict | None | Dictionary containing metadata key-value pairs from model export. | required |
Source code in ultralytics/nn/backends/base.py
View on GitHubdef 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
| Name | Type | Description | Default |
|---|---|---|---|
im | torch.Tensor | Input image tensor in BCHW format, normalized to [0, 1]. | required |
Returns
| Type | Description |
|---|---|
torch.Tensor | list[torch.Tensor] | Model output as a single tensor or list of tensors. |
Source code in ultralytics/nn/backends/base.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
weight | str | torch.nn.Module | Path to model weights or a PyTorch module. | required |
Source code in ultralytics/nn/backends/base.py
View 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