|
| 1 | +# Input tiling |
| 2 | + |
| 3 | +This tutorial will show you how to tile the input to a model, using the {py:class}`Tiler <anomalib.data.utils.tiler.Tiler>`. |
| 4 | + |
| 5 | +```{warning} |
| 6 | +This tutorial assumes that you have already installed anomalib. |
| 7 | +If not, please refer to the [Installation](../../../../index.md#installation) section. |
| 8 | +``` |
| 9 | + |
| 10 | +```{warning} |
| 11 | +Only selected models support tiling. |
| 12 | +In the current version of Anomalib, these are: |
| 13 | +
|
| 14 | +- [Padim](../../reference/models/image/padim.md) |
| 15 | +- [Patchcore](../../reference/models/image/patchcore.md) |
| 16 | +- [Reverse Distillation](../../reference/models/image/reverse_distillation.md) |
| 17 | +- [STFPM](../../reference/models/image/stfpm.md) |
| 18 | +
|
| 19 | +``` |
| 20 | + |
| 21 | +## General tiling information |
| 22 | + |
| 23 | +The general idea of input tiling is that the image is split into a rectangular grid of tiles as a pre-processing step, usually in order to reduce memory usage. |
| 24 | +By passing individual tiles to the model as input instead of full images, tiling reduces the model's input dimensions, while maintaining the effective input resolution of the images content-wise. |
| 25 | + |
| 26 | +```{note} |
| 27 | +Tiler in Anomalib by default stacks the tiles batch-wise, so the memory consumption stays unchanged if the batch size is not reduced. |
| 28 | +``` |
| 29 | + |
| 30 | +The process of tiling is parametrized by four parameters `tile_size`, `stride`, `remove_border_count`, and `mode`. |
| 31 | + |
| 32 | +- `tile_size` - determines the size of our tiles. Can be either a single number (square tiles) or a tuple. |
| 33 | +- `stride` - determines by how much we move in each direction when "cutting" the image into tiles. Can be either a single number (same step in both directions) or a tuple. |
| 34 | +- `remove_border_count` - how many pixels are removed at the border of the image before tiling (defaults to 0). |
| 35 | +- `mode` - what type of upscaling is used when the image isn't exactly divisible into tile-set specified by the parameters `tile_size` and `stride` (defaults to padding). |
| 36 | + |
| 37 | +In most cases, we are only interested in the first two parameters - `tile_size` and `stride`. For the other two, refer to [Tiler implementation](../../reference/data/utils/tiling.md). |
| 38 | + |
| 39 | +## Tiling setup |
| 40 | + |
| 41 | +We can utilize the tiling in two ways. Either with the CLI or by using the API. |
| 42 | +In both cases, we need to use the {py:class}`TilerConfigurationCallback <anomalib.callbacks.TilerConfigurationCallback>`. |
| 43 | +This callback is responsible for assigning the tiler object to the model before the training starts. |
| 44 | + |
| 45 | +```{note} |
| 46 | +Besides the arguments from {py:class}`Tiler <anomalib.data.utils.tiler.Tiler>`, {py:class}`TilerConfigurationCallback <anomalib.callbacks.TilerConfigurationCallback>` also has an additional `enable` argument, which must be set to `True` if we want the tiling to happen. |
| 47 | +``` |
| 48 | + |
| 49 | +::::{tab-set} |
| 50 | + |
| 51 | +:::{tab-item} API |
| 52 | + |
| 53 | +To use tiling from the API, we need to initialize the {py:class}`TilerConfigurationCallback <anomalib.callbacks.TilerConfigurationCallback>` and pass it to the engine: |
| 54 | + |
| 55 | +```{code-block} python |
| 56 | +:lineno-start: 1 |
| 57 | +:emphasize-lines: 12, 15 |
| 58 | +# Import the required modules |
| 59 | +from anomalib.data import MVTec |
| 60 | +from anomalib.engine import Engine |
| 61 | +from anomalib.models import Padim |
| 62 | +from anomalib.callbacks import TilerConfigurationCallback |
| 63 | +
|
| 64 | +# Initialize the datamodule and model |
| 65 | +datamodule = MVTec(num_workers=0, image_size=(128, 128)) |
| 66 | +model = Padim() |
| 67 | +
|
| 68 | +# prepare tiling configuration callback |
| 69 | +tiler_config_callback = TilerConfigurationCallback(enable=True, tile_size=[128, 64], stride=64) |
| 70 | +
|
| 71 | +# pass the tiling configuration callback to the engine |
| 72 | +engine = Engine(image_metrics=["AUROC"], pixel_metrics=["AUROC"], callbacks=[tiler_config_callback]) |
| 73 | +
|
| 74 | +# train the model (tiling is seamlessly utilized in the background) |
| 75 | +engine.fit(datamodule=datamodule, model=model) |
| 76 | +``` |
| 77 | + |
| 78 | +::: |
| 79 | + |
| 80 | +:::{tab-item} CLI |
| 81 | + |
| 82 | +### Using CLI arguments |
| 83 | + |
| 84 | +We can set the {py:class}`TilerConfigurationCallback <anomalib.callbacks.TilerConfigurationCallback>` and its init arguments directly from the CLI. |
| 85 | + |
| 86 | +We pass it as trainer.callback, and then provide the parameters: |
| 87 | + |
| 88 | +```{code-block} bash |
| 89 | +:emphasize-lines: 2, 3, 4, 5 |
| 90 | +anomalib train --model Padim --data anomalib.data.MVTec |
| 91 | + --trainer.callbacks anomalib.callbacks.tiler_configuration.TilerConfigurationCallback |
| 92 | + --trainer.callbacks.enable True |
| 93 | + --trainer.callbacks.tile_size 128 |
| 94 | + --trainer.callbacks.stride 64 |
| 95 | +``` |
| 96 | + |
| 97 | +### Using config |
| 98 | + |
| 99 | +For more advanced configuration, we can prepare the config file: |
| 100 | + |
| 101 | +```{code-block} yaml |
| 102 | +:lineno-start: 1 |
| 103 | +trainer.callbacks: |
| 104 | + class_path: anomalib.callbacks.tiler_configuration.TilerConfigurationCallback |
| 105 | + init_args: |
| 106 | + enable: True |
| 107 | + tile_size: [128, 256] |
| 108 | + stride: 64 |
| 109 | +``` |
| 110 | + |
| 111 | +Then use the config from the CLI: |
| 112 | + |
| 113 | +```{code-block} bash |
| 114 | +anomalib train --model Padim --data anomalib.data.MVTec --config config.yaml |
| 115 | +``` |
| 116 | + |
| 117 | +::: |
| 118 | + |
| 119 | +:::: |
0 commit comments