예제 #1
0
def resize(
    image: torch.Tensor,
    size: Union[int, Tuple[int, int]],
    edge: str = "short",
    aspect_ratio: Optional[float] = None,
    interpolation_mode: str = "bilinear",
) -> torch.Tensor:
    r"""Resize an image as specified. See :class:`~pystiche.image.transforms.Resize`
    for details.
    """
    if aspect_ratio is None:
        aspect_ratio = extract_aspect_ratio(image)

    if is_image_size(size):
        image_size = size
    elif is_edge_size(size):
        image_size = edge_to_image_size(cast(int, size), aspect_ratio, edge)
    else:
        raise RuntimeError

    return interpolate(
        image,
        size=image_size,
        scale_factor=None,
        mode=interpolation_mode,
    )
예제 #2
0
def _parse_size(size: Union[Tuple[int, int], int]) -> Tuple[int, int]:
    if is_image_size(size):
        return cast(Tuple[int, int], size)
    elif is_edge_size(size):
        edge_size = cast(int, size)
        return edge_size, edge_size
    else:
        raise RuntimeError
예제 #3
0
def _parse_size(size: Union[Tuple[int, int], int]) -> Tuple[int, int]:
    if is_image_size(size):
        return cast(Tuple[int, int], size)
    elif is_edge_size(size):
        edge_size = cast(int, size)
        return edge_size, edge_size
    else:
        msg = (f"size can either be an edge size (int) or an image size "
               f"(Tuple[int, int]), but got {type(size)}.")
        raise TypeError(msg)
예제 #4
0
 def _properties(self) -> Dict[str, Any]:
     dct = super()._properties()
     if is_image_size(self.size):
         dct["image_size"] = self.size
     else:
         key = "edge_size" if is_edge_size(self.size) else "size"
         dct[key] = self.size
         dct["edge"] = self.edge
         if self.aspect_ratio is not None:
             dct["aspect_ratio"] = self.aspect_ratio
     if self.interpolation_mode != "bilinear":
         dct["interpolation_mode"] = self.interpolation_mode
     return dct
예제 #5
0
def resize(
    image: torch.Tensor,
    size: Union[int, Tuple[int, int]],
    edge: str = "short",
    aspect_ratio: Optional[float] = None,
    interpolation_mode: str = "bilinear",
) -> torch.Tensor:
    if aspect_ratio is None:
        aspect_ratio = extract_aspect_ratio(image)

    if is_image_size(size):
        image_size = size
    elif is_edge_size(size):
        image_size = edge_to_image_size(cast(int, size), aspect_ratio, edge)
    else:
        raise RuntimeError

    return interpolate(
        image,
        size=image_size,
        scale_factor=None,
        mode=interpolation_mode,
    )
예제 #6
0
 def test_is_edge_size(self):
     edge_size = 1
     self.assertTrue(utils.is_edge_size(edge_size))
     self.assertFalse(utils.is_edge_size(float(edge_size)))
     self.assertFalse(utils.is_edge_size((edge_size, edge_size)))