Skip to content

xwhy.models.BaseSegmentation

Bases: ABC

Base class for all segmentation implementations.

Source code in src/xwhy/models/segmentation/base.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class BaseSegmentation(ABC):
    """Base class for all segmentation implementations."""

    @property
    @abstractmethod
    def model(self) -> Any:  # noqa: ANN401
        """Read-only property to access the underlying raw segmentation model.

        Raises:
            RuntimeError: If the model has not been loaded into memory yet.

        Returns:
            The loaded raw segmentation model object.

        """
        pass

    @property
    @abstractmethod
    def preprocess_fn(self) -> Callable[..., Any] | None:
        """Read-only property to access the preprocessing transform function.

        Returns:
            The callable preprocessing function, or None if not applicable.

        """
        pass

    @abstractmethod
    def __call__(self, inputs: Any) -> Any:  # noqa: ANN401
        """Execute the forward pass of the segmentation model.

        Args:
            inputs: The preprocessed inputs (e.g., a PyTorch tensor).

        Returns:
            The segmentation masks or logits output by the model.

        """
        pass

    @property
    @abstractmethod
    def class_names(self) -> list[str]:
        """Return the list of class names (categories) supported by the model."""
        raise NotImplementedError

    @abstractmethod
    def load(self) -> Any:  # noqa: ANN401
        """Load segmentation model into memory."""
        raise NotImplementedError

    @abstractmethod
    def predict(self, inputs: Any) -> Any:  # noqa: ANN401
        """Run inference on the given inputs and return predictions/masks."""
        raise NotImplementedError

model abstractmethod property

Read-only property to access the underlying raw segmentation model.

Raises:

Type Description
RuntimeError

If the model has not been loaded into memory yet.

Returns:

Type Description
Any

The loaded raw segmentation model object.

preprocess_fn abstractmethod property

Read-only property to access the preprocessing transform function.

Returns:

Type Description
Callable[..., Any] | None

The callable preprocessing function, or None if not applicable.

class_names abstractmethod property

Return the list of class names (categories) supported by the model.

__call__(inputs) abstractmethod

Execute the forward pass of the segmentation model.

Parameters:

Name Type Description Default
inputs Any

The preprocessed inputs (e.g., a PyTorch tensor).

required

Returns:

Type Description
Any

The segmentation masks or logits output by the model.

Source code in src/xwhy/models/segmentation/base.py
38
39
40
41
42
43
44
45
46
47
48
49
@abstractmethod
def __call__(self, inputs: Any) -> Any:  # noqa: ANN401
    """Execute the forward pass of the segmentation model.

    Args:
        inputs: The preprocessed inputs (e.g., a PyTorch tensor).

    Returns:
        The segmentation masks or logits output by the model.

    """
    pass

load() abstractmethod

Load segmentation model into memory.

Source code in src/xwhy/models/segmentation/base.py
57
58
59
60
@abstractmethod
def load(self) -> Any:  # noqa: ANN401
    """Load segmentation model into memory."""
    raise NotImplementedError

predict(inputs) abstractmethod

Run inference on the given inputs and return predictions/masks.

Source code in src/xwhy/models/segmentation/base.py
62
63
64
65
@abstractmethod
def predict(self, inputs: Any) -> Any:  # noqa: ANN401
    """Run inference on the given inputs and return predictions/masks."""
    raise NotImplementedError