anomalib.pre_processing.tiler¶
Image Tiler.
Module Contents¶
Classes¶
Tile Image into (non)overlapping Patches. Images are tiled in order to efficiently process large images. |
Functions¶
|
This function checks if image size is divisible by tile size and stride. |
|
Upscale image to the desired size via either padding or interpolation. |
|
Opposite of upscaling. This image downscales image to a desired size. |
- exception anomalib.pre_processing.tiler.StrideSizeError[source]¶
Bases:
ExceptionStrideSizeError to raise exception when stride size is greater than the tile size.
- anomalib.pre_processing.tiler.compute_new_image_size(image_size: Tuple, tile_size: Tuple, stride: Tuple) Tuple[source]¶
This function checks if image size is divisible by tile size and stride.
If not divisible, it resizes the image size to make it divisible.
- Parameters
image_size (Tuple) – Original image size
tile_size (Tuple) – Tile size
stride (Tuple) – Stride
Examples
>>> compute_new_image_size(image_size=(512, 512), tile_size=(256, 256), stride=(128, 128)) (512, 512)
>>> compute_new_image_size(image_size=(512, 512), tile_size=(222, 222), stride=(111, 111)) (555, 555)
- Returns
Updated image size that is divisible by tile size and stride.
- Return type
Tuple
- anomalib.pre_processing.tiler.upscale_image(image: torch.Tensor, size: Tuple, mode: str = 'padding') torch.Tensor[source]¶
Upscale image to the desired size via either padding or interpolation.
- Parameters
image (Tensor) – Image
size (Tuple) – Tuple to which image is upscaled.
mode (str, optional) – Upscaling mode. Defaults to “padding”.
Examples
>>> image = torch.rand(1, 3, 512, 512) >>> image = upscale_image(image, size=(555, 555), mode="padding") >>> image.shape torch.Size([1, 3, 555, 555])
>>> image = torch.rand(1, 3, 512, 512) >>> image = upscale_image(image, size=(555, 555), mode="interpolation") >>> image.shape torch.Size([1, 3, 555, 555])
- Returns
Upscaled image.
- Return type
Tensor
- anomalib.pre_processing.tiler.downscale_image(image: torch.Tensor, size: Tuple, mode: str = 'padding') torch.Tensor[source]¶
Opposite of upscaling. This image downscales image to a desired size.
- Parameters
image (Tensor) – Input image
size (Tuple) – Size to which image is down scaled.
mode (str, optional) – Downscaling mode. Defaults to “padding”.
Examples
>>> x = torch.rand(1, 3, 512, 512) >>> y = upscale_image(image, upscale_size=(555, 555), mode="padding") >>> y = downscale_image(y, size=(512, 512), mode='padding') >>> torch.allclose(x, y) True
- Returns
Downscaled image
- Return type
Tensor
- class anomalib.pre_processing.tiler.Tiler(tile_size: Union[int, Sequence], stride: Optional[Union[int, Sequence]] = None, remove_border_count: int = 0, mode: str = 'padding', tile_count: int = 4)[source]¶
Tile Image into (non)overlapping Patches. Images are tiled in order to efficiently process large images.
- Parameters
tile_size – Tile dimension for each patch
stride – Stride length between patches
remove_border_count – Number of border pixels to be removed from tile before untiling
mode – Upscaling mode for image resize.Supported formats: padding, interpolation
Examples
>>> import torch >>> from torchvision import transforms >>> from skimage.data import camera >>> tiler = Tiler(tile_size=256,stride=128) >>> image = transforms.ToTensor()(camera()) >>> tiles = tiler.tile(image) >>> image.shape, tiles.shape (torch.Size([3, 512, 512]), torch.Size([9, 3, 256, 256]))
>>> # Perform your operations on the tiles.
>>> # Untile the patches to reconstruct the image >>> reconstructed_image = tiler.untile(tiles) >>> reconstructed_image.shape torch.Size([1, 3, 512, 512])
- __random_tile(self, image: torch.Tensor) torch.Tensor[source]¶
Randomly crop tiles from the given image.
- Parameters
image – input image to be cropped
Returns: Randomly cropped tiles from the image
- __unfold(self, tensor: torch.Tensor) torch.Tensor[source]¶
Unfolds tensor into tiles.
This is the core function to perform tiling operation.
- Parameters
tensor – Input tensor from which tiles are generated.
Returns: Generated tiles
- __fold(self, tiles: torch.Tensor) torch.Tensor[source]¶
Fold the tiles back into the original tensor.
This is the core method to reconstruct the original image from its tiled version.
- Parameters
tiles – Tiles from the input image, generated via __unfold method.
- Returns
Output that is the reconstructed version of the input tensor.
- tile(self, image: torch.Tensor, use_random_tiling: Optional[bool] = False) torch.Tensor[source]¶
Tiles an input image to either overlapping, non-overlapping or random patches.
- Parameters
image – Input image to tile.
Examples
>>> from anomalib.data.tiler import Tiler >>> tiler = Tiler(tile_size=512,stride=256) >>> image = torch.rand(size=(2, 3, 1024, 1024)) >>> image.shape torch.Size([2, 3, 1024, 1024]) >>> tiles = tiler.tile(image) >>> tiles.shape torch.Size([18, 3, 512, 512])
- Returns
Tiles generated from the image.
- untile(self, tiles: torch.Tensor) torch.Tensor[source]¶
Untiles patches to reconstruct the original input image.
If patches, are overlapping patches, the function averages the overlapping pixels, and return the reconstructed image.
- Parameters
tiles – Tiles from the input image, generated via tile()..
Examples
>>> from anomalib.datasets.tiler import Tiler >>> tiler = Tiler(tile_size=512,stride=256) >>> image = torch.rand(size=(2, 3, 1024, 1024)) >>> image.shape torch.Size([2, 3, 1024, 1024]) >>> tiles = tiler.tile(image) >>> tiles.shape torch.Size([18, 3, 512, 512]) >>> reconstructed_image = tiler.untile(tiles) >>> reconstructed_image.shape torch.Size([2, 3, 1024, 1024]) >>> torch.equal(image, reconstructed_image) True
- Returns
Output that is the reconstructed version of the input tensor.