Skip to content

pixano_inference.models.detection

Detection model base class and I/O types.

DetectionInput(**data)

Bases: BaseModel

Input for detection.

When classes is provided the model runs in open-vocabulary mode (e.g. GroundingDINO, YOLOE with text prompt). When classes is None the model uses its built-in class set (closed-vocabulary mode, e.g. YOLO, prompt-free YOLOE).

Attributes:

Name Type Description
image str | Path

Image for detection (path, URL, or base64).

classes list[str] | str | None

Class names to detect. None means closed-vocabulary.

box_threshold float

Confidence threshold for boxes.

text_threshold float

Confidence threshold for text matching (open-vocab only).

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            stacklevel=2,
        )

DetectionModel(config)

Bases: InferenceModel

Base class for detection and instance-segmentation models.

Subclasses implement predict which receives a :class:DetectionInput and returns a :class:DetectionOutput. The same pair of types covers both pure detection (no masks) and instance segmentation (with masks).

Example
@register_model("my-detector")
class MyDetector(DetectionModel):
    def load_model(self):
        self.model = load_weights(self.config.model_params["path"])

    def predict(self, input: DetectionInput) -> DetectionOutput:
        boxes, scores, cls = self.model(input.image, input.classes)
        return DetectionOutput(boxes=boxes, scores=scores, classes=cls)
Source code in pixano_inference/models/base.py
def __init__(self, config: ModelDeploymentConfig) -> None:
    """Initialize the model with deployment config.

    Args:
        config: Model deployment configuration.
    """
    self._config = config

predict(input) abstractmethod

Run detection or instance segmentation.

Parameters:

Name Type Description Default
input DetectionInput

Detection input with image, optional classes, and thresholds.

required

Returns:

Type Description
DetectionOutput

Detection output with boxes, scores, classes, and optional masks.

Source code in pixano_inference/models/detection.py
@abstractmethod
def predict(self, input: DetectionInput) -> DetectionOutput:
    """Run detection or instance segmentation.

    Args:
        input: Detection input with image, optional classes, and thresholds.

    Returns:
        Detection output with boxes, scores, classes, and optional masks.
    """

DetectionOutput(**data)

Bases: BaseModel

Output for detection.

masks is populated when the model performs instance segmentation (boxes + masks). For detection-only models it is None.

Attributes:

Name Type Description
boxes list[list[int]]

List of detected boxes.

scores list[float]

List of confidence scores.

classes list[str]

List of class names associated with each box.

masks list[CompressedRLE] | None

Optional instance masks in compressed-RLE format.

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            stacklevel=2,
        )