Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address tiler issues #1411

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion notebooks/100_datamodules/104_tiling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
"metadata": {},
"outputs": [],
"source": [
"ToPILImage()(make_grid(tiled_image, nrow=4, pad=5))"
"ToPILImage()(make_grid(tiled_image, nrow=4, padding=5))"
]
},
{
Expand Down
7 changes: 0 additions & 7 deletions src/anomalib/models/ganomaly/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ dataset:
test_split_ratio: 0.2 # fraction of train images held out testing (usage depends on test_split_mode)
val_split_mode: same_as_test # options: [same_as_test, from_test, synthetic]
val_split_ratio: 0.5 # fraction of train/test images held out for validation (usage depends on val_split_mode)
tiling:
apply: true
tile_size: 64
stride: null
remove_border_count: 0
use_random_tiling: False
random_tile_count: 16

model:
name: ganomaly
Expand Down
7 changes: 3 additions & 4 deletions src/anomalib/pre_processing/tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,9 @@ def __init__(
stride: int | Sequence | None = None,
remove_border_count: int = 0,
mode: ImageUpscaleMode = ImageUpscaleMode.PADDING,
tile_count: int = 4,
) -> None:
self.tile_size_h, self.tile_size_w = self.__validate_size_type(tile_size)
self.tile_count = tile_count
self.random_tile_count = 4

if stride is not None:
self.stride_h, self.stride_w = self.__validate_size_type(stride)
Expand Down Expand Up @@ -218,7 +217,7 @@ def __random_tile(self, image: Tensor) -> Tensor:

Returns: Randomly cropped tiles from the image
"""
return torch.vstack([T.RandomCrop(self.tile_size_h)(image) for i in range(self.tile_count)])
return torch.vstack([T.RandomCrop(self.tile_size_h)(image) for i in range(self.random_tile_count)])

def __unfold(self, tensor: Tensor) -> Tensor:
"""Unfolds tensor into tiles.
Expand Down Expand Up @@ -339,7 +338,7 @@ def __fold(self, tiles: Tensor) -> Tensor:

return img

def tile(self, image: Tensor, use_random_tiling: bool | None = False) -> Tensor:
def tile(self, image: Tensor, use_random_tiling: bool = False) -> Tensor:
"""Tiles an input image to either overlapping, non-overlapping or random patches.

Args:
Expand Down
11 changes: 11 additions & 0 deletions src/anomalib/utils/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def get_callbacks(config: DictConfig | ListConfig) -> list[Callback]:
)
callbacks.append(post_processing_callback)

# Add the tiling configuration callback
if config.dataset.get("tiling") and config.dataset.tiling.apply:
callbacks.append(
TilerConfigurationCallback(
enable=True,
tile_size=config.dataset.tiling.tile_size,
stride=config.dataset.tiling.stride,
remove_border_count=config.dataset.tiling.remove_border_count,
)
)

# Add metric configuration to the model via MetricsConfigurationCallback
metrics_callback = MetricsConfigurationCallback(
config.dataset.task,
Expand Down
3 changes: 0 additions & 3 deletions src/anomalib/utils/callbacks/tiler_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(
stride: int | Sequence | None = None,
remove_border_count: int = 0,
mode: ImageUpscaleMode = ImageUpscaleMode.PADDING,
tile_count: int = 4,
) -> None:
"""Sets tiling configuration from the command line.

Expand All @@ -49,7 +48,6 @@ def __init__(
self.stride = stride
self.remove_border_count = remove_border_count
self.mode = mode
self.tile_count = tile_count

def setup(self, trainer: pl.Trainer, pl_module: pl.LightningModule, stage: str | None = None) -> None:
"""Setup Tiler object within Anomalib Model.
Expand All @@ -72,7 +70,6 @@ def setup(self, trainer: pl.Trainer, pl_module: pl.LightningModule, stage: str |
stride=self.stride,
remove_border_count=self.remove_border_count,
mode=self.mode,
tile_count=self.tile_count,
)
else:
raise ValueError("Model does not support tiling.")
Loading