Skip to content

Segmenter

Segmenter(*args, backend='numpy', **kwargs)

Factory function to create a segmenter instance based on the specified backend.

Parameters:

Name Type Description Default
backend str

The backend to use. Supported options: ["numpy", "torch", "tensorflow"]. Defaults to "numpy".

'numpy'
*args Any

Additional positional arguments to pass to the segmenter.

()
**kwargs Any

Additional keyword arguments to pass to the segmenter.

{}

Returns:

Type Description
Any

An instance of the segmenter corresponding to the chosen backend.

Raises:

Type Description
ValueError

If an unsupported backend or scheme is specified.

NotImplementedError

If the backend or scheme is not implemented.

Source code in src/libsegmenter/Segmenter.py
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
def Segmenter(*args: Any, backend: str = "numpy", **kwargs: Any) -> Any:
    """
    Factory function to create a segmenter instance based on the specified backend.

    Args:
        backend (str, optional): The backend to use. Supported options:
            ["numpy", "torch", "tensorflow"]. Defaults to "numpy".
        *args (Any): Additional positional arguments to pass to the segmenter.
        **kwargs (Any): Additional keyword arguments to pass to the segmenter.

    Returns:
        An instance of the segmenter corresponding to the chosen backend.

    Raises:
        ValueError: If an unsupported backend or scheme is specified.
        NotImplementedError: If the backend or scheme is not implemented.

    """
    if backend not in BACKENDS:
        raise ValueError(f"Unsupported backend {backend}, availible: {BACKENDS}")

    if backend == "numpy":
        from libsegmenter.backends.SegmenterNumpy import SegmenterNumpy

        return SegmenterNumpy(*args, **kwargs)

    if backend == "torch":
        from libsegmenter.backends.SegmenterTorch import SegmenterTorch

        return SegmenterTorch(*args, **kwargs)

    if backend == "tensorflow":
        from libsegmenter.backends.SegmenterTensorFlow import SegmenterTensorFlow

        return SegmenterTensorFlow(*args, **kwargs)

    raise NotImplementedError(f"The '{backend}' backend is not implemented yet.")