Skip to content

pixano_inference.models.segmentation

Segmentation model base class and I/O types.

SegmentationInput(**data)

Bases: BaseModel

Input for image segmentation.

Attributes:

Name Type Description
image str | Path | bytes

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

image_embedding NDArrayFloat | None

Pre-computed image embedding.

high_resolution_features list[NDArrayFloat] | None

High resolution features.

mask_input NDArrayFloat | None

Low-resolution mask logits from a previous refinement round.

reset_predictor bool

True (default) for a new image. If False, keep current predictor.

points list[list[list[int]]] | None

Point prompts [num_prompts, num_points, 2].

labels list[list[int]] | None

Labels for points [num_prompts, num_points].

boxes list[list[int]] | None

Box prompts [num_prompts, 4].

num_multimask_outputs int

Number of masks to generate per prediction.

multimask_output bool

Whether to generate multiple masks per prediction.

return_image_embedding bool

Whether to return the image embeddings.

return_logits bool

Whether to return low-resolution logits for iterative refinement.

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,
        )

SegmentationModel(config)

Bases: InferenceModel

Base class for image segmentation models.

Example
@register_model("my-segmenter")
class MySegmenter(SegmentationModel):
    def load_model(self):
        self.model = load_weights(self.config.model_params["path"])

    def predict(self, input: SegmentationInput) -> SegmentationOutput:
        masks, scores = self.model(input.image, input.points, input.labels, input.boxes)
        return SegmentationOutput(masks=masks, scores=scores)
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 image segmentation.

Parameters:

Name Type Description Default
input SegmentationInput

Segmentation input with image, prompts, and options.

required

Returns:

Type Description
SegmentationOutput

Segmentation output with masks, scores, and optionally embeddings.

Source code in pixano_inference/models/segmentation.py
@abstractmethod
def predict(self, input: SegmentationInput) -> SegmentationOutput:
    """Run image segmentation.

    Args:
        input: Segmentation input with image, prompts, and options.

    Returns:
        Segmentation output with masks, scores, and optionally embeddings.
    """

SegmentationOutput(**data)

Bases: BaseModel

Output for image segmentation.

Attributes:

Name Type Description
masks list[list[CompressedRLE]]

Generated masks [num_predictions, num_masks_per_prediction].

scores NDArrayFloat

Scores of the masks.

image_embedding NDArrayFloat | None

Image embeddings (optional).

high_resolution_features list[NDArrayFloat] | None

High resolution features (optional).

mask_logits NDArrayFloat | None

Low-resolution logits for iterative refinement (optional).

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,
        )