def center_crop(x: torch.Tensor, size: Union[Tuple[int, int], int]) -> torch.Tensor: image_size = extract_image_size(x) size = _parse_size(size) vert_origin = (image_size[0] - size[0]) // 2 horz_origin = (image_size[1] - size[1]) // 2 return cast(torch.Tensor, crop(x, (vert_origin, horz_origin), size))
def bottom_left_crop(x: torch.Tensor, size: Union[Tuple[int, int], int]) -> torch.Tensor: height, _ = extract_image_size(x) origin = (height, 0) return cast( torch.Tensor, crop(x, origin, size, vert_anchor="bottom", horz_anchor="left"))
def test_extract_image_size(self): height = 2 width = 3 image = torch.empty(1, 1, height, width) actual = utils.extract_image_size(image) desired = (height, width) self.assertTupleEqual(actual, desired)
def rescale( image: torch.Tensor, factor: Union[float, Tuple[float, float]], interpolation_mode: str = "bilinear", ) -> torch.Tensor: height, width = extract_image_size(image) height_factor, width_factor = to_2d_arg(factor) height = round(height * height_factor) width = round(width * width_factor) image_size = (height, width) return cast( torch.Tensor, resize(image, image_size, interpolation_mode=interpolation_mode))
def transform_motif_affinely( image: torch.Tensor, shearing_angle: Optional[float] = None, clockwise_shearing: bool = False, shearing_center: Optional[Tuple[float, float]] = None, rotation_angle: Optional[float] = None, clockwise_rotation: bool = False, rotation_center: Optional[Tuple[float, float]] = None, scaling_factor: Optional[Union[float, Tuple[float, float]]] = None, scaling_center: Optional[Tuple[float, float]] = None, translation: Optional[Tuple[float, float]] = None, inverse_translation: bool = False, canvas: str = "same", interpolation_mode: str = "bilinear", padding_mode: str = "zeros", ) -> torch.Tensor: device = image.device image_size = extract_image_size(image) transform_matrix = _create_affine_transform_matrix( image_size, shearing_angle=shearing_angle, clockwise_shearing=clockwise_shearing, shearing_center=shearing_center, rotation_angle=rotation_angle, clockwise_rotation=clockwise_rotation, rotation_center=rotation_center, scaling_factor=scaling_factor, scaling_center=scaling_center, translation=translation, inverse_translation=inverse_translation, ) transform_matrix, resized_image_size = _resize_canvas(transform_matrix, image_size, method=canvas) transform_matrix = _transform_coordinates(transform_matrix, image_size) transform_matrix = transform_matrix.to(device) grid = _calculate_affine_grid(image, transform_matrix, resized_image_size) grid = grid.to(device) return grid_sample( image, grid, mode=interpolation_mode, padding_mode=padding_mode, )
def test_file_starting_point(self, mock_execution_with): image = demo.images()["bird2"] expected = image.read() file = pathlib.Path(pystiche.home()) / image.file mock = self._mock_execution( mock_execution_with, option="starting_point", value=str(file) ) with exits(), pytest.warns(UserWarning): main() (input_image, perceptual_loss), _ = mock.call_args ptu.assert_allclose( input_image, resize(expected, list(extract_image_size(perceptual_loss.content_image))), )
def test_demo_image(self, mock_execution_with, option): name = "bird2" mock = self._mock_execution(mock_execution_with, option=option, value=name) with exits(): cli.main() (input_image, perceptual_loss), _ = mock.call_args if option == "content": image = perceptual_loss.content_image elif option == "style": image = perceptual_loss.style_image else: # option == "starting_point" image = input_image ptu.assert_allclose( image, demo.images()[name].read(size=extract_image_size(image)), )
def bottom_right_crop(x: torch.Tensor, size: Union[Tuple[int, int], int]) -> torch.Tensor: origin = extract_image_size(x) return cast( torch.Tensor, crop(x, origin, size, vert_anchor="bottom", horz_anchor="right"))
def top_right_crop(x: torch.Tensor, size: Union[Tuple[int, int], int]) -> torch.Tensor: _, width = extract_image_size(x) origin = (0, width) return cast(torch.Tensor, crop(x, origin, size, vert_anchor="top", horz_anchor="right"))