Image Datamodules#
Image datamodules in Anomalib are designed to handle image-based anomaly detection datasets. They provide a standardized interface for loading and processing image data for both training and inference.
Available Datamodules#
Surface defect detection in steel manufacturing.
Dataset format compatible with Intel Geti™.
Custom folder-based dataset organization.
Surface defect detection in electrical commutators.
Industrial anomaly detection benchmark.
Visual inspection of surface anomalies.
API Reference#
Anomalib Datasets.
This module provides datasets and data modules for anomaly detection tasks.
- The module contains:
Data classes for representing different types of data (images, videos, etc.)
Dataset classes for loading and processing data
Data modules for use with PyTorch Lightning
Helper functions for data loading and validation
Example
>>> from anomalib.data import get_datamodule
>>> from omegaconf import DictConfig
>>> config = DictConfig({
... "data": {
... "class_path": "MVTec",
... "init_args": {
... "root": "./datasets/MVTec",
... "category": "bottle",
... "image_size": (256, 256)
... }
... }
... })
>>> datamodule = get_datamodule(config)
- class anomalib.data.BTech(root='./datasets/BTech', category='01', train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, test_split_mode=TestSplitMode.FROM_DIR, test_split_ratio=0.2, val_split_mode=ValSplitMode.SAME_AS_TEST, val_split_ratio=0.5, seed=None)#
Bases:
AnomalibDataModuleBTech Lightning Data Module.
- Parameters:
root (Path | str) – Path to the root of the dataset. Defaults to
"./datasets/BTech".category (str) – Category of the BTech dataset (e.g.
"01","02", or"03"). Defaults to"01".train_batch_size (int, optional) – Training batch size. Defaults to
32.eval_batch_size (int, optional) – Test batch size. Defaults to
32.num_workers (int, optional) – Number of workers. Defaults to
8.train_augmentations (Transform | None) – Augmentations to apply dto the training images Defaults to
None.val_augmentations (Transform | None) – Augmentations to apply to the validation images. Defaults to
None.test_augmentations (Transform | None) – Augmentations to apply to the test images. Defaults to
None.augmentations (Transform | None) – General augmentations to apply if stage-specific augmentations are not provided.
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. Defaults to
0.2.val_split_mode (ValSplitMode) – Setting that determines how the validation subset is obtained. Defaults to
ValSplitMode.SAME_AS_TEST.val_split_ratio (float) – Fraction of train or test images that will be reserved for validation. Defaults to
0.5.seed (int | None, optional) – Seed which may be set to a fixed value for reproducibility. Defaults to
None.
Example
To create the BTech datamodule, instantiate the class and call
setup:>>> from anomalib.data import BTech >>> datamodule = BTech( ... root="./datasets/BTech", ... category="01", ... train_batch_size=32, ... eval_batch_size=32, ... num_workers=8, ... ) >>> datamodule.setup()
Get the train dataloader and first batch:
>>> i, data = next(enumerate(datamodule.train_dataloader())) >>> data.keys() dict_keys(['image']) >>> data["image"].shape torch.Size([32, 3, 256, 256])
Access the validation dataloader and first batch:
>>> i, data = next(enumerate(datamodule.val_dataloader())) >>> data.keys() dict_keys(['image_path', 'label', 'mask_path', 'image', 'mask']) >>> data["image"].shape, data["mask"].shape (torch.Size([32, 3, 256, 256]), torch.Size([32, 256, 256]))
Access the test dataloader and first batch:
>>> i, data = next(enumerate(datamodule.test_dataloader())) >>> data.keys() dict_keys(['image_path', 'label', 'mask_path', 'image', 'mask']) >>> data["image"].shape, data["mask"].shape (torch.Size([32, 3, 256, 256]), torch.Size([32, 256, 256]))
- prepare_data()#
Download the dataset if not available.
This method checks if the specified dataset is available in the file system. If not, it downloads and extracts the dataset into the appropriate directory.
- Return type:
Example
Assume the dataset is not available on the file system. Here’s how the directory structure looks before and after calling
prepare_data:# Before $ tree datasets datasets ├── dataset1 └── dataset2 # Calling prepare_data >>> datamodule = BTech(root="./datasets/BTech", category="01") >>> datamodule.prepare_data() # After $ tree datasets datasets ├── dataset1 ├── dataset2 └── BTech ├── 01 ├── 02 └── 03
- class anomalib.data.Datumaro(root, train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, test_split_mode=TestSplitMode.FROM_DIR, test_split_ratio=0.5, val_split_mode=ValSplitMode.FROM_TEST, val_split_ratio=0.5, seed=None)#
Bases:
AnomalibDataModuleDatumaro datamodule.
- Parameters:
root (Path | str) – Path to the dataset root directory.
train_batch_size (int, optional) – Training batch size. Defaults to
32.eval_batch_size (int, optional) – Test batch size. Defaults to
32.num_workers (int, optional) – Number of workers. Defaults to
8.train_augmentations (Transform | None) – Augmentations to apply dto the training images Defaults to
None.val_augmentations (Transform | None) – Augmentations to apply to the validation images. Defaults to
None.test_augmentations (Transform | None) – Augmentations to apply to the test images. Defaults to
None.augmentations (Transform | None) – General augmentations to apply if stage-specific augmentations are not provided.
image_size (tuple[int, int], optional) – Size to which input images should be resized. 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. Defaults to
0.2.val_split_mode (ValSplitMode) – Setting that determines how the validation subset is obtained. Defaults to
ValSplitMode.SAME_AS_TEST.val_split_ratio (float) – Fraction of train or test images that will be reserved for validation. Defaults to
0.5.seed (int | None, optional) – Seed which may be set to a fixed value for reproducibility. Defaults to
None.
Example
>>> from anomalib.data import Datumaro >>> datamodule = Datumaro( ... root="./datasets/datumaro", ... train_batch_size=32, ... eval_batch_size=32, ... num_workers=8, ... ) >>> datamodule.setup() >>> i, data = next(enumerate(datamodule.train_dataloader())) >>> data.keys() dict_keys(['image_path', 'label', 'image'])
- class anomalib.data.Folder(name, normal_dir, root=None, abnormal_dir=None, normal_test_dir=None, mask_dir=None, normal_split_ratio=0.2, extensions=None, train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, test_split_mode=TestSplitMode.FROM_DIR, test_split_ratio=0.2, val_split_mode=ValSplitMode.FROM_TEST, val_split_ratio=0.5, seed=None)#
Bases:
AnomalibDataModuleFolder DataModule.
- Parameters:
name (str) – Name of the dataset. Used for logging/saving.
normal_dir (str | Path | Sequence) – Directory containing normal images.
root (str | Path | None) – Root folder containing normal and abnormal directories. Defaults to
None.abnormal_dir (str | Path | None | Sequence) – Directory containing abnormal images. Defaults to
None.normal_test_dir (str | Path | Sequence | None) – Directory containing normal test images. Defaults to
None.mask_dir (str | Path | Sequence | None) – Directory containing mask annotations. Defaults to
None.normal_split_ratio (float) – Ratio to split normal training images for test set when no normal test images exist. Defaults to
0.2.extensions (tuple[str, ...] | None) – Image extensions to include. Defaults to
None.train_batch_size (int) – Training batch size. Defaults to
32.eval_batch_size (int) – Validation/test batch size. Defaults to
32.num_workers (int) – Number of workers for data loading. Defaults to
8.train_augmentations (Transform | None) – Augmentations to apply dto the training images Defaults to
None.val_augmentations (Transform | None) – Augmentations to apply to the validation images. Defaults to
None.test_augmentations (Transform | None) – Augmentations to apply to the test images. Defaults to
None.augmentations (Transform | None) – General augmentations to apply if stage-specific augmentations are not provided.
test_split_mode (TestSplitMode) – Method to obtain test subset. Defaults to
TestSplitMode.FROM_DIR.test_split_ratio (float) – Fraction of train images for testing. Defaults to
0.2.val_split_mode (ValSplitMode) – Method to obtain validation subset. Defaults to
ValSplitMode.FROM_TEST.val_split_ratio (float) – Fraction of images for validation. Defaults to
0.5.seed (int | None) – Random seed for splitting. Defaults to
None.
Example
Create and setup a folder datamodule:
>>> from anomalib.data import Folder >>> datamodule = Folder( ... name="custom", ... root="./datasets/custom", ... normal_dir="good", ... abnormal_dir="defect", ... mask_dir="mask" ... ) >>> datamodule.setup()
Get a batch from train dataloader:
>>> batch = next(iter(datamodule.train_dataloader())) >>> batch.keys() dict_keys(['image', 'label', 'mask', 'image_path', 'mask_path'])
Get a batch from test dataloader:
>>> batch = next(iter(datamodule.test_dataloader())) >>> batch.keys() dict_keys(['image', 'label', 'mask', 'image_path', 'mask_path'])
- class anomalib.data.Kolektor(root='./datasets/kolektor', train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, test_split_mode=TestSplitMode.FROM_DIR, test_split_ratio=0.2, val_split_mode=ValSplitMode.SAME_AS_TEST, val_split_ratio=0.5, seed=None)#
Bases:
AnomalibDataModuleKolektor Surface-Defect DataModule.
- Parameters:
root (Path | str) – Path to the root of the dataset. Defaults to
"./datasets/kolektor".train_batch_size (int, optional) – Training batch size. Defaults to
32.eval_batch_size (int, optional) – Test batch size. Defaults to
32.num_workers (int, optional) – Number of workers. Defaults to
8.train_augmentations (Transform | None) – Augmentations to apply dto the training images Defaults to
None.val_augmentations (Transform | None) – Augmentations to apply to the validation images. Defaults to
None.test_augmentations (Transform | None) – Augmentations to apply to the test images. Defaults to
None.augmentations (Transform | None) – General augmentations to apply if stage-specific augmentations are not provided.
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. Defaults to
0.2.val_split_mode (ValSplitMode) – Setting that determines how the validation subset is obtained. Defaults to
ValSplitMode.SAME_AS_TEST.val_split_ratio (float) – Fraction of train or test images that will be reserved for validation. Defaults to
0.5.seed (int | None, optional) – Seed which may be set to a fixed value for reproducibility.
Example
>>> from anomalib.data import Kolektor >>> datamodule = Kolektor( ... root="./datasets/kolektor", ... train_batch_size=32, ... eval_batch_size=32, ... num_workers=8, ... ) >>> datamodule.setup() >>> i, data = next(enumerate(datamodule.train_dataloader())) >>> data.keys() dict_keys(['image', 'label', 'mask', 'image_path', 'mask_path'])
- prepare_data()#
Download the dataset if not available.
This method checks if the specified dataset is available in the file system. If not, it downloads and extracts the dataset into the appropriate directory.
- Return type:
Example
Assume the dataset is not available on the file system. Here’s how the directory structure looks before and after calling the
prepare_datamethod:Before:
$ tree datasets datasets ├── dataset1 └── dataset2
Calling the method:
>>> datamodule = Kolektor(root="./datasets/kolektor") >>> datamodule.prepare_data()
After:
$ tree datasets datasets ├── dataset1 ├── dataset2 └── kolektor ├── kolektorsdd ├── kos01 ├── ... └── kos50 ├── Part0.jpg ├── Part0_label.bmp └── ...
- class anomalib.data.MVTec(root='./datasets/MVTec', category='bottle', train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, test_split_mode=TestSplitMode.FROM_DIR, test_split_ratio=0.2, val_split_mode=ValSplitMode.SAME_AS_TEST, val_split_ratio=0.5, seed=None)#
Bases:
AnomalibDataModuleMVTec Datamodule.
- Parameters:
root (Path | str) – Path to the root of the dataset. Defaults to
"./datasets/MVTec".category (str) – Category of the MVTec dataset (e.g.
"bottle"or"cable"). Defaults to"bottle".train_batch_size (int, optional) – Training batch size. Defaults to
32.eval_batch_size (int, optional) – Test batch size. Defaults to
32.num_workers (int, optional) – Number of workers. Defaults to
8.train_augmentations (Transform | None) – Augmentations to apply dto the training images Defaults to
None.val_augmentations (Transform | None) – Augmentations to apply to the validation images. Defaults to
None.test_augmentations (Transform | None) – Augmentations to apply to the test images. Defaults to
None.augmentations (Transform | None) – General augmentations to apply if stage-specific augmentations are not provided.
test_split_mode (TestSplitMode) – Method to create test set. Defaults to
TestSplitMode.FROM_DIR.test_split_ratio (float) – Fraction of data to use for testing. Defaults to
0.2.val_split_mode (ValSplitMode) – Method to create validation set. Defaults to
ValSplitMode.SAME_AS_TEST.val_split_ratio (float) – Fraction of data to use for validation. Defaults to
0.5.seed (int | None, optional) – Seed for reproducibility. Defaults to
None.
Example
Create MVTec datamodule with default settings:
>>> datamodule = MVTec() >>> datamodule.setup() >>> i, data = next(enumerate(datamodule.train_dataloader())) >>> data.keys() dict_keys(['image_path', 'label', 'image', 'mask_path', 'mask']) >>> data["image"].shape torch.Size([32, 3, 256, 256])
Change the category:
>>> datamodule = MVTec(category="cable")
Create validation set from test data:
>>> datamodule = MVTec( ... val_split_mode=ValSplitMode.FROM_TEST, ... val_split_ratio=0.1 ... )
Create synthetic validation set:
>>> datamodule = MVTec( ... val_split_mode=ValSplitMode.SYNTHETIC, ... val_split_ratio=0.2 ... )
- prepare_data()#
Download the dataset if not available.
This method checks if the specified dataset is available in the file system. If not, it downloads and extracts the dataset into the appropriate directory.
- Return type:
Example
Assume the dataset is not available on the file system:
>>> datamodule = MVTec( ... root="./datasets/MVTec", ... category="bottle" ... ) >>> datamodule.prepare_data()
Directory structure after download:
datasets/ └── MVTec/ ├── bottle/ ├── cable/ └── ...
- class anomalib.data.Visa(root='./datasets/visa', category='capsules', train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, test_split_mode=TestSplitMode.FROM_DIR, test_split_ratio=0.2, val_split_mode=ValSplitMode.SAME_AS_TEST, val_split_ratio=0.5, seed=None)#
Bases:
AnomalibDataModuleVisA Datamodule.
- Parameters:
root (Path | str) – Path to the root of the dataset. Defaults to
"./datasets/visa".category (str) – Category of the VisA dataset (e.g.
"candle"). Defaults to"capsules".train_batch_size (int, optional) – Training batch size. Defaults to
32.eval_batch_size (int, optional) – Test batch size. Defaults to
32.num_workers (int, optional) – Number of workers for data loading. Defaults to
8.train_augmentations (Transform | None) – Augmentations to apply dto the training images Defaults to
None.val_augmentations (Transform | None) – Augmentations to apply to the validation images. Defaults to
None.test_augmentations (Transform | None) – Augmentations to apply to the test images. Defaults to
None.augmentations (Transform | None) – General augmentations to apply if stage-specific augmentations are not provided.
test_split_mode (TestSplitMode | str) – Method to create test set. Defaults to
TestSplitMode.FROM_DIR.test_split_ratio (float) – Fraction of data to use for testing. Defaults to
0.2.val_split_mode (ValSplitMode | str) – Method to create validation set. Defaults to
ValSplitMode.SAME_AS_TEST.val_split_ratio (float) – Fraction of data to use for validation. Defaults to
0.5.seed (int | None, optional) – Random seed for reproducibility. Defaults to
None.
- apply_cls1_split()#
Apply the 1-class subset splitting using the fixed split in the csv file.
Adapted from amazon-science/spot-diff.
- Return type:
- prepare_data()#
Download and prepare the dataset if not available.
This method checks if the dataset exists and is properly formatted. If not, it downloads and prepares the data in the following steps: :rtype:
NoneIf the processed dataset exists (
visa_pytorch/{category}), do nothingIf the raw dataset exists but isn’t processed, apply the train/test split
If the dataset doesn’t exist, download, extract, and process it
The final directory structure will be:
datasets/ └── visa/ ├── visa_pytorch/ │ ├── candle/ │ │ ├── train/ │ │ │ └── good/ │ │ ├── test/ │ │ │ ├── good/ │ │ │ └── bad/ │ │ └── ground_truth/ │ │ └── bad/ │ └── ... └── VisA_20220922.tar