Skip to content

Commit

Permalink
Remove transforms from datamodules
Browse files Browse the repository at this point in the history
Signed-off-by: Samet Akcay <samet.akcay@intel.com>
  • Loading branch information
samet-akcay committed Oct 9, 2024
1 parent 180c22f commit 7738e38
Show file tree
Hide file tree
Showing 12 changed files with 7 additions and 266 deletions.
63 changes: 0 additions & 63 deletions src/anomalib/data/datamodules/base/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from lightning.pytorch.trainer.states import TrainerFn
from lightning.pytorch.utilities.types import EVAL_DATALOADERS, TRAIN_DATALOADERS
from torch.utils.data.dataloader import DataLoader
from torchvision.transforms.v2 import Resize, Transform

from anomalib.data.utils import TestSplitMode, ValSplitMode, random_split, split_by_label
from anomalib.data.utils.synthetic import SyntheticAnomalyDataset
Expand Down Expand Up @@ -40,14 +39,6 @@ class AnomalibDataModule(LightningDataModule, ABC):
Defaults to ``None``.
test_split_ratio (float): Fraction of the train images held out for testing.
Defaults to ``None``.
image_size (tuple[int, int], optional): Size to which input images should be resized.
Defaults to ``None``.
transform (Transform, optional): Transforms that should be applied to the input images.
Defaults to ``None``.
train_transform (Transform, optional): Transforms that should be applied to the input images during training.
Defaults to ``None``.
eval_transform (Transform, optional): Transforms that should be applied to the input images during evaluation.
Defaults to ``None``.
seed (int | None, optional): Seed used during random subset splitting.
Defaults to ``None``.
"""
Expand All @@ -61,10 +52,6 @@ def __init__(
val_split_ratio: float,
test_split_mode: TestSplitMode | str | None = None,
test_split_ratio: float | None = None,
image_size: tuple[int, int] | None = None,
transform: Transform | None = None,
train_transform: Transform | None = None,
eval_transform: Transform | None = None,
seed: int | None = None,
) -> None:
super().__init__()
Expand All @@ -75,18 +62,8 @@ def __init__(
self.test_split_ratio = test_split_ratio
self.val_split_mode = ValSplitMode(val_split_mode)
self.val_split_ratio = val_split_ratio
self.image_size = image_size
self.seed = seed

# set transforms
if bool(train_transform) != bool(eval_transform):
msg = "Only one of train_transform and eval_transform was specified. This is not recommended because \
it could lead to unexpected behaviour. Please ensure training and eval transforms have the same \
reshape and normalization characteristics."
logger.warning(msg)
self._train_transform = train_transform or transform
self._eval_transform = eval_transform or transform

self.train_data: AnomalibDataset
self.val_data: AnomalibDataset
self.test_data: AnomalibDataset
Expand Down Expand Up @@ -228,46 +205,6 @@ def predict_dataloader(self) -> EVAL_DATALOADERS:
"""Use the test dataloader for inference unless overridden."""
return self.test_dataloader()

@property
def transform(self) -> Transform:
"""Property that returns the user-specified transform for the datamodule, if any.
This property is accessed by the engine to set the transform for the model. The eval_transform takes precedence
over the train_transform, because the transform that we store in the model is the one that should be used during
inference.
"""
if self._eval_transform:
return self._eval_transform
return None

@property
def train_transform(self) -> Transform:
"""Get the transforms that will be passed to the train dataset.
If the train_transform is not set, the engine will request the transform from the model.
"""
if self._train_transform:
return self._train_transform
if getattr(self, "trainer", None) and self.trainer.lightning_module and self.trainer.lightning_module.transform:
return self.trainer.lightning_module.transform
if self.image_size:
return Resize(self.image_size, antialias=True)
return None

@property
def eval_transform(self) -> Transform:
"""Get the transform that will be passed to the val/test/predict datasets.
If the eval_transform is not set, the engine will request the transform from the model.
"""
if self._eval_transform:
return self._eval_transform
if getattr(self, "trainer", None) and self.trainer.lightning_module and self.trainer.lightning_module.transform:
return self.trainer.lightning_module.transform
if self.image_size:
return Resize(self.image_size, antialias=True)
return None

@classmethod
def from_config(
cls: type["AnomalibDataModule"],
Expand Down
20 changes: 0 additions & 20 deletions src/anomalib/data/datamodules/depth/folder_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from pathlib import Path

from torchvision.transforms.v2 import Transform

from anomalib import TaskType
from anomalib.data.datamodules.base.image import AnomalibDataModule
from anomalib.data.datasets.depth.folder_3d import Folder3DDataset
Expand Down Expand Up @@ -51,14 +49,6 @@ class Folder3D(AnomalibDataModule):
Defaults to ``8``.
task (TaskType, optional): Task type. Could be ``classification``, ``detection`` or ``segmentation``.
Defaults to ``TaskType.SEGMENTATION``.
image_size (tuple[int, int], optional): Size to which input images should be resized.
Defaults to ``None``.
transform (Transform, optional): Transforms that should be applied to the input images.
Defaults to ``None``.
train_transform (Transform, optional): Transforms that should be applied to the input images during training.
Defaults to ``None``.
eval_transform (Transform, optional): Transforms that should be applied to the input images during evaluation.
Defaults to ``None``.
test_split_mode (TestSplitMode): Setting that determines how the testing subset is obtained.
Defaults to ``TestSplitMode.FROM_DIR``.
test_split_ratio (float): Fraction of images from the train set that will be reserved for testing.
Expand Down Expand Up @@ -87,10 +77,6 @@ def __init__(
eval_batch_size: int = 32,
num_workers: int = 8,
task: TaskType | str = TaskType.SEGMENTATION,
image_size: tuple[int, int] | None = None,
transform: Transform | None = None,
train_transform: Transform | None = None,
eval_transform: Transform | None = None,
test_split_mode: TestSplitMode | str = TestSplitMode.FROM_DIR,
test_split_ratio: float = 0.2,
val_split_mode: ValSplitMode | str = ValSplitMode.FROM_TEST,
Expand All @@ -101,10 +87,6 @@ def __init__(
train_batch_size=train_batch_size,
eval_batch_size=eval_batch_size,
num_workers=num_workers,
image_size=image_size,
transform=transform,
train_transform=train_transform,
eval_transform=eval_transform,
test_split_mode=test_split_mode,
test_split_ratio=test_split_ratio,
val_split_mode=val_split_mode,
Expand All @@ -127,7 +109,6 @@ def _setup(self, _stage: str | None = None) -> None:
self.train_data = Folder3DDataset(
name=self.name,
task=self.task,
transform=self.train_transform,
split=Split.TRAIN,
root=self.root,
normal_dir=self.normal_dir,
Expand All @@ -143,7 +124,6 @@ def _setup(self, _stage: str | None = None) -> None:
self.test_data = Folder3DDataset(
name=self.name,
task=self.task,
transform=self.eval_transform,
split=Split.TEST,
root=self.root,
normal_dir=self.normal_dir,
Expand Down
20 changes: 2 additions & 18 deletions src/anomalib/data/datamodules/depth/mvtec_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import logging
from pathlib import Path

from torchvision.transforms.v2 import Transform

from anomalib import TaskType
from anomalib.data.datamodules.base.image import AnomalibDataModule
from anomalib.data.datasets.depth.mvtec_3d import MVTec3DDataset
Expand Down Expand Up @@ -62,13 +60,9 @@ class MVTec3D(AnomalibDataModule):
Defaults to ``8``.
task (TaskType): Task type, 'classification', 'detection' or 'segmentation'
Defaults to ``TaskType.SEGMENTATION``.
image_size (tuple[int, int], optional): Size to which input images should be resized.
Defaults to ``None``.
transform (Transform, optional): Transforms that should be applied to the input images.
Defaults to ``None``.
train_transform (Transform, optional): Transforms that should be applied to the input images during training.
test_split_mode (TestSplitMode): Setting that determines how the testing subset is obtained.
Defaults to ``None``.
eval_transform (Transform, optional): Transforms that should be applied to the input images during evaluation.
test_split_ratio (float): Fraction of images from the train set that will be reserved for testing.
Defaults to ``None``.
test_split_mode (TestSplitMode): Setting that determines how the testing subset is obtained.
Defaults to ``TestSplitMode.FROM_DIR``.
Expand All @@ -90,10 +84,6 @@ def __init__(
eval_batch_size: int = 32,
num_workers: int = 8,
task: TaskType | str = TaskType.SEGMENTATION,
image_size: tuple[int, int] | None = None,
transform: Transform | None = None,
train_transform: Transform | None = None,
eval_transform: Transform | None = None,
test_split_mode: TestSplitMode | str = TestSplitMode.FROM_DIR,
test_split_ratio: float = 0.2,
val_split_mode: ValSplitMode | str = ValSplitMode.SAME_AS_TEST,
Expand All @@ -104,10 +94,6 @@ def __init__(
train_batch_size=train_batch_size,
eval_batch_size=eval_batch_size,
num_workers=num_workers,
image_size=image_size,
transform=transform,
train_transform=train_transform,
eval_transform=eval_transform,
test_split_mode=test_split_mode,
test_split_ratio=test_split_ratio,
val_split_mode=val_split_mode,
Expand All @@ -122,14 +108,12 @@ def __init__(
def _setup(self, _stage: str | None = None) -> None:
self.train_data = MVTec3DDataset(
task=self.task,
transform=self.train_transform,
split=Split.TRAIN,
root=self.root,
category=self.category,
)
self.test_data = MVTec3DDataset(
task=self.task,
transform=self.eval_transform,
split=Split.TEST,
root=self.root,
category=self.category,
Expand Down
22 changes: 0 additions & 22 deletions src/anomalib/data/datamodules/image/btech.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pathlib import Path

import cv2
from torchvision.transforms.v2 import Transform
from tqdm import tqdm

from anomalib import TaskType
Expand Down Expand Up @@ -53,14 +52,6 @@ class BTech(AnomalibDataModule):
Defaults to ``8``.
task (TaskType, optional): Task type.
Defaults to ``TaskType.SEGMENTATION``.
image_size (tuple[int, int], optional): Size to which input images should be resized.
Defaults to ``None``.
transform (Transform, optional): Transforms that should be applied to the input images.
Defaults to ``None``.
train_transform (Transform, optional): Transforms that should be applied to the input images during training.
Defaults to ``None``.
eval_transform (Transform, optional): Transforms that should be applied to the input images during evaluation.
Defaults to ``None``.
test_split_mode (TestSplitMode, optional): Setting that determines how the testing subset is obtained.
Defaults to ``TestSplitMode.FROM_DIR``.
test_split_ratio (float, optional): Fraction of images from the train set that will be reserved for testing.
Expand All @@ -79,12 +70,9 @@ class BTech(AnomalibDataModule):
>>> datamodule = BTech(
... root="./datasets/BTech",
... category="01",
... image_size=256,
... train_batch_size=32,
... eval_batch_size=32,
... num_workers=8,
... transform_config_train=None,
... transform_config_eval=None,
... )
>>> datamodule.setup()
Expand Down Expand Up @@ -121,10 +109,6 @@ def __init__(
eval_batch_size: int = 32,
num_workers: int = 8,
task: TaskType | str = TaskType.SEGMENTATION,
image_size: tuple[int, int] | None = None,
transform: Transform | None = None,
train_transform: Transform | None = None,
eval_transform: Transform | None = None,
test_split_mode: TestSplitMode | str = TestSplitMode.FROM_DIR,
test_split_ratio: float = 0.2,
val_split_mode: ValSplitMode | str = ValSplitMode.SAME_AS_TEST,
Expand All @@ -135,10 +119,6 @@ def __init__(
train_batch_size=train_batch_size,
eval_batch_size=eval_batch_size,
num_workers=num_workers,
image_size=image_size,
transform=transform,
train_transform=train_transform,
eval_transform=eval_transform,
test_split_mode=test_split_mode,
test_split_ratio=test_split_ratio,
val_split_mode=val_split_mode,
Expand All @@ -153,14 +133,12 @@ def __init__(
def _setup(self, _stage: str | None = None) -> None:
self.train_data = BTechDataset(
task=self.task,
transform=self.train_transform,
split=Split.TRAIN,
root=self.root,
category=self.category,
)
self.test_data = BTechDataset(
task=self.task,
transform=self.eval_transform,
split=Split.TEST,
root=self.root,
category=self.category,
Expand Down
22 changes: 0 additions & 22 deletions src/anomalib/data/datamodules/image/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from collections.abc import Sequence
from pathlib import Path

from torchvision.transforms.v2 import Transform

from anomalib import TaskType
from anomalib.data.datamodules.base.image import AnomalibDataModule
from anomalib.data.datasets.image.folder import FolderDataset
Expand Down Expand Up @@ -47,14 +45,6 @@ class Folder(AnomalibDataModule):
Defaults to ``8``.
task (TaskType, optional): Task type. Could be ``classification``, ``detection`` or ``segmentation``.
Defaults to ``segmentation``.
image_size (tuple[int, int], optional): Size to which input images should be resized.
Defaults to ``None``.
transform (Transform, optional): Transforms that should be applied to the input images.
Defaults to ``None``.
train_transform (Transform, optional): Transforms that should be applied to the input images during training.
Defaults to ``None``.
eval_transform (Transform, optional): Transforms that should be applied to the input images during evaluation.
Defaults to ``None``.
test_split_mode (TestSplitMode): Setting that determines how the testing subset is obtained.
Defaults to ``TestSplitMode.FROM_DIR``.
test_split_ratio (float): Fraction of images from the train set that will be reserved for testing.
Expand Down Expand Up @@ -102,8 +92,6 @@ class Folder(AnomalibDataModule):
abnormal_dir="crack",
task=TaskType.SEGMENTATION,
mask_dir=dataset_root / "mask" / "crack",
image_size=256,
normalization=InputNormalizationMethod.NONE,
)
folder_datamodule.setup()
Expand Down Expand Up @@ -136,10 +124,6 @@ def __init__(
eval_batch_size: int = 32,
num_workers: int = 8,
task: TaskType | str = TaskType.SEGMENTATION,
image_size: tuple[int, int] | None = None,
transform: Transform | None = None,
train_transform: Transform | None = None,
eval_transform: Transform | None = None,
test_split_mode: TestSplitMode | str = TestSplitMode.FROM_DIR,
test_split_ratio: float = 0.2,
val_split_mode: ValSplitMode | str = ValSplitMode.FROM_TEST,
Expand All @@ -164,10 +148,6 @@ def __init__(
test_split_ratio=test_split_ratio,
val_split_mode=val_split_mode,
val_split_ratio=val_split_ratio,
image_size=image_size,
transform=transform,
train_transform=train_transform,
eval_transform=eval_transform,
seed=seed,
)

Expand All @@ -186,7 +166,6 @@ def _setup(self, _stage: str | None = None) -> None:
self.train_data = FolderDataset(
name=self.name,
task=self.task,
transform=self.train_transform,
split=Split.TRAIN,
root=self.root,
normal_dir=self.normal_dir,
Expand All @@ -199,7 +178,6 @@ def _setup(self, _stage: str | None = None) -> None:
self.test_data = FolderDataset(
name=self.name,
task=self.task,
transform=self.eval_transform,
split=Split.TEST,
root=self.root,
normal_dir=self.normal_dir,
Expand Down
Loading

0 comments on commit 7738e38

Please sign in to comment.