Skip to content

ModelImageSize

Bases: ABC

A container for image size in neural networks.

Parameters:

Name Type Description Default
width int

the width of the image

required
height int

the height of the image

required
channel int

the channel of the image

required

Raises:

Type Description
OutOfBoundsError:

if width or height are below 1

ValueError

if an invalid channel is given

Methods:

Name Description
from_image

Create a ModelImageSize of a given image.

from_image_size

Create a ModelImageSize of a given image size.

Attributes:

Name Type Description
channel int

Get the channel of this ImageSize in pixels.

height int

Get the height of this ImageSize in pixels.

width int

Get the width of this ImageSize in pixels.

Source code in src/safeds/ml/nn/typing/_model_image_size.py
class ModelImageSize(ABC):
    """
    A container for image size in neural networks.

    Parameters
    ----------
    width:
        the width of the image
    height:
        the height of the image
    channel:
        the channel of the image

    Raises
    ------
    OutOfBoundsError:
        if width or height are below 1
    ValueError
        if an invalid channel is given
    """

    def __init__(self, width: int, height: int, channel: int, *, _ignore_invalid_channel: bool = False) -> None:
        _check_bounds("width", width, lower_bound=_ClosedBound(1))
        _check_bounds("height", height, lower_bound=_ClosedBound(1))
        if not _ignore_invalid_channel and channel not in (1, 3, 4):
            raise ValueError(f"Channel {channel} is not a valid channel option. Use either 1, 3 or 4")
        _check_bounds("channel", channel, lower_bound=_ClosedBound(1))

        self._width = width
        self._height = height
        self._channel = channel

    @classmethod
    def from_image(cls: type[Self], image: Image) -> Self:
        """
        Create a `ModelImageSize` of a given image.

        Parameters
        ----------
        image:
            the given image for the `ModelImageSize`

        Returns
        -------
        image_size:
            the calculated `ModelImageSize`
        """
        return cls(image.width, image.height, image.channel)

    @classmethod
    def from_image_size(cls: type[Self], image_size: ModelImageSize) -> Self:
        """
        Create a `ModelImageSize` of a given image size.

        Parameters
        ----------
        image_size:
            the given image size for the `ModelImageSize`

        Returns
        -------
        image_size:
            the new `ModelImageSize`
        """
        return cls(image_size.width, image_size.height, image_size.channel)

    @abstractmethod
    def __eq__(self, other: object) -> bool:
        if not isinstance(other, ModelImageSize):
            return NotImplemented
        return (self is other) or (
            self._width == other._width and self._height == other._height and self._channel == other._channel
        )

    @abstractmethod
    def __hash__(self) -> int:
        return _structural_hash(self.__class__.__name__, self._width, self._height, self._channel)

    def __sizeof__(self) -> int:
        return sys.getsizeof(self._width) + sys.getsizeof(self._height) + sys.getsizeof(self._channel)

    def __str__(self) -> str:
        return f"{self._width}x{self._height}x{self._channel} (WxHxC)"

    @property
    def width(self) -> int:
        """
        Get the width of this `ImageSize` in pixels.

        Returns
        -------
        width:
            The width of this `ImageSize`.
        """
        return self._width

    @property
    def height(self) -> int:
        """
        Get the height of this `ImageSize` in pixels.

        Returns
        -------
        height:
            The height of this `ImageSize`.
        """
        return self._height

    @property
    def channel(self) -> int:
        """
        Get the channel of this `ImageSize` in pixels.

        Returns
        -------
        channel:
            The channel of this `ImageSize`.
        """
        return self._channel

channel

Get the channel of this ImageSize in pixels.

Returns:

Name Type Description
channel int

The channel of this ImageSize.

height

Get the height of this ImageSize in pixels.

Returns:

Name Type Description
height int

The height of this ImageSize.

width

Get the width of this ImageSize in pixels.

Returns:

Name Type Description
width int

The width of this ImageSize.

from_image

Create a ModelImageSize of a given image.

Parameters:

Name Type Description Default
image Image

the given image for the ModelImageSize

required

Returns:

Name Type Description
image_size Self

the calculated ModelImageSize

Source code in src/safeds/ml/nn/typing/_model_image_size.py
@classmethod
def from_image(cls: type[Self], image: Image) -> Self:
    """
    Create a `ModelImageSize` of a given image.

    Parameters
    ----------
    image:
        the given image for the `ModelImageSize`

    Returns
    -------
    image_size:
        the calculated `ModelImageSize`
    """
    return cls(image.width, image.height, image.channel)

from_image_size

Create a ModelImageSize of a given image size.

Parameters:

Name Type Description Default
image_size ModelImageSize

the given image size for the ModelImageSize

required

Returns:

Name Type Description
image_size Self

the new ModelImageSize

Source code in src/safeds/ml/nn/typing/_model_image_size.py
@classmethod
def from_image_size(cls: type[Self], image_size: ModelImageSize) -> Self:
    """
    Create a `ModelImageSize` of a given image size.

    Parameters
    ----------
    image_size:
        the given image size for the `ModelImageSize`

    Returns
    -------
    image_size:
        the new `ModelImageSize`
    """
    return cls(image_size.width, image_size.height, image_size.channel)