Skip to content

rslearn.train.transforms.resize

resize

Resize transform.

Resize

Bases: Transform

Resizes inputs to a target size.

Source code in rslearn/train/transforms/resize.py
class Resize(Transform):
    """Resizes inputs to a target size."""

    def __init__(
        self,
        target_size: tuple[int, int],
        selectors: list[str] = [],
        interpolation: str = "nearest",
        skip_missing: bool = False,
    ):
        """Initialize a resize transform.

        Args:
            target_size: the (height, width) to resize to.
            selectors: items to transform.
            interpolation: the interpolation mode to use for resizing.
                Must be one of "nearest", "nearest_exact", "bilinear", or "bicubic".
            skip_missing: if True, skip selectors that don't exist in the input/target
                dicts. Useful when working with optional inputs.
        """
        super().__init__(skip_missing=skip_missing)
        self.target_size = target_size
        self.selectors = selectors
        self.interpolation = INTERPOLATION_MODES[interpolation]

    def apply_resize(self, image: RasterImage) -> RasterImage:
        """Apply resizing on the specified image.

        Args:
            image: the image to transform.
        """
        image.image = torchvision.transforms.functional.resize(
            image.image, self.target_size, self.interpolation
        )
        return image

    def forward(
        self, input_dict: dict[str, Any], target_dict: dict[str, Any]
    ) -> tuple[dict[str, Any], dict[str, Any]]:
        """Apply transform over the inputs and targets.

        Args:
            input_dict: the input
            target_dict: the target

        Returns:
            transformed (input_dicts, target_dicts) tuple
        """
        self.apply_fn(self.apply_resize, input_dict, target_dict, self.selectors)
        return input_dict, target_dict

apply_resize

apply_resize(image: RasterImage) -> RasterImage

Apply resizing on the specified image.

Parameters:

Name Type Description Default
image RasterImage

the image to transform.

required
Source code in rslearn/train/transforms/resize.py
def apply_resize(self, image: RasterImage) -> RasterImage:
    """Apply resizing on the specified image.

    Args:
        image: the image to transform.
    """
    image.image = torchvision.transforms.functional.resize(
        image.image, self.target_size, self.interpolation
    )
    return image

forward

forward(input_dict: dict[str, Any], target_dict: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]

Apply transform over the inputs and targets.

Parameters:

Name Type Description Default
input_dict dict[str, Any]

the input

required
target_dict dict[str, Any]

the target

required

Returns:

Type Description
tuple[dict[str, Any], dict[str, Any]]

transformed (input_dicts, target_dicts) tuple

Source code in rslearn/train/transforms/resize.py
def forward(
    self, input_dict: dict[str, Any], target_dict: dict[str, Any]
) -> tuple[dict[str, Any], dict[str, Any]]:
    """Apply transform over the inputs and targets.

    Args:
        input_dict: the input
        target_dict: the target

    Returns:
        transformed (input_dicts, target_dicts) tuple
    """
    self.apply_fn(self.apply_resize, input_dict, target_dict, self.selectors)
    return input_dict, target_dict