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#

BMAD

BMAD dataset datamodule for medical anomaly detection.

anomalib.data.datamodules.image.bmad
BTech

Surface defect detection in steel manufacturing.

anomalib.data.datamodules.image.BTech
Datumaro

Dataset format compatible with Intel Geti™.

anomalib.data.datamodules.image.Datumaro
Folder

Custom folder-based dataset organization.

anomalib.data.datamodules.image.Folder
Kolektor

Surface defect detection in electrical commutators.

anomalib.data.datamodules.image.Kolektor
MVTecAD

Industrial anomaly detection benchmark.

anomalib.data.datamodules.image.MVTecAD
MVTecAD2

MVTec AD 2 dataset for anomaly detection with natural images.

anomalib.data.datamodules.image.MVTecAD2
MVTecLOCO

MVTec LOCO dataset for logical and structural anomaly detection.

anomalib.data.datamodules.image.MVTecLOCO
RealIAD

Real-IAD dataset for industrial anomaly detection scenarios.

anomalib.data.datamodules.image.RealIAD
Tabular

Custom tabular dataset.

anomalib.data.datamodules.image.Tabular
VAD

Valeo Anomaly Detection dataset for automotive applications.

anomalib.data.datamodules.image.VAD
Visa

Visual Anomaly dataset.

anomalib.data.datamodules.image.Visa

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 MVTecAD
>>> datamodule = MVTecAD(
...     root="./datasets/MVTecAD",
...     category="bottle",
...     image_size=(256, 256)
... )
class anomalib.data.BMAD(root='./datasets/BMAD', category='Brain', 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_DIR, val_split_ratio=None, seed=None)#

Bases: AnomalibDataModule

BMAD Datamodule.

Parameters:
  • root (Path | str) – Path to the root of the dataset. Defaults to "./datasets/BMAD".

  • category (str) – Category of the BMAD dataset (e.g. "Brain", "Liver", "Retina_OCT2017", "Retina_RESC", "Chest", or "Histopathology"). Defaults to "Brain".

  • 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 to 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) – Seed for reproducibility. Defaults to None.

Example

Create BMAD datamodule with default settings:

>>> datamodule = BMAD()
>>> datamodule.setup()
>>> i, data = next(enumerate(datamodule.train_dataloader()))

>>> data.image.shape
torch.Size([32, 3, 240, 240])

Change the category:

>>> datamodule = BMAD(category="Liver")

Use Retina_RESC:

>>> datamodule = BMAD(category="Retina_RESC")

Create validation set from test data:

>>> datamodule = BMAD(
...     val_split_mode=ValSplitMode.FROM_TEST,
...     val_split_ratio=0.1
... )
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:

None

Example

Assume the dataset is not available on the file system:

>>> datamodule = BMAD(
...     root="./datasets/BMAD",
...     category="Brain"
... )
>>> datamodule.prepare_data()

Directory structure after download:

datasets/
└── BMAD/
    ├── Brain/
    ├── Liver/
    └── ...
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: AnomalibDataModule

BTech 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:

None

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: AnomalibDataModule

Datumaro 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: AnomalibDataModule

Folder 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'])
property name: str#

Get name of the datamodule.

Returns:

Name of the datamodule.

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: AnomalibDataModule

Kolektor 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:

None

Example

Assume the dataset is not available on the file system. Here’s how the directory structure looks before and after calling the prepare_data method:

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.MVTecAD(root='./datasets/MVTecAD', 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: AnomalibDataModule

MVTec AD Datamodule.

Parameters:
  • root (Path | str) – Path to the root of the dataset. Defaults to "./datasets/MVTecAD".

  • category (str) – Category of the MVTec AD 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 AD datamodule with default settings:

>>> datamodule = MVTecAD()
>>> 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 = MVTecAD(category="cable")

Create validation set from test data:

>>> datamodule = MVTecAD(
...     val_split_mode=ValSplitMode.FROM_TEST,
...     val_split_ratio=0.1
... )

Create synthetic validation set:

>>> datamodule = MVTecAD(
...     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:

None

Example

Assume the dataset is not available on the file system:

>>> datamodule = MVTecAD(
...     root="./datasets/MVTecAD",
...     category="bottle"
... )
>>> datamodule.prepare_data()

Directory structure after download:

datasets/
└── MVTecAD/
    ├── bottle/
    ├── cable/
    └── ...
class anomalib.data.MVTecAD2(root='./datasets/MVTec_AD_2', category='sheet_metal', train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, test_type=TestType.PUBLIC, seed=None)#

Bases: AnomalibDataModule

MVTec AD 2 Lightning Data Module.

Parameters:
  • root (str | Path) – Path to the dataset root directory. Defaults to "./datasets/MVTec_AD_2".

  • category (str) – Name of the MVTec AD 2 category to load. Defaults to "sheet_metal".

  • train_batch_size (int, optional) – Training batch size. Defaults to 32.

  • eval_batch_size (int, optional) – Validation and 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 to 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_type (str | TestType) –

    Type of test set to use: - "public": Test set with ground truth for local evaluation and initial

    performance estimation

    • "private": Official test set without ground truth for leaderboard submission

    • "private_mixed": Official test set with mixed lighting conditions (seen and unseen, randomly mixed) for leaderboard submission

    Defaults to TestType.PUBLIC.

  • seed (int | None, optional) – Random seed for reproducibility. Defaults to None.

Example

>>> from anomalib.data import MVTecAD2
>>> datamodule = MVTecAD2(
...     root="./datasets/MVTec_AD_2",
...     category="sheet_metal",
...     train_batch_size=32,
...     eval_batch_size=32,
...     num_workers=8,
... )

To use private test set: >>> datamodule = MVTecAD2( … root=”./datasets/MVTec_AD_2”, … category=”sheet_metal”, … test_type=”private”, … )

Access different test sets: >>> datamodule.setup() >>> public_loader = datamodule.test_dataloader() # returns loader based on test_type >>> private_loader = datamodule.test_dataloader(test_type=”private”) >>> mixed_loader = datamodule.test_dataloader(test_type=”private_mixed”)

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:

None

Example

Assume the dataset is not available on the file system:

>>> datamodule = MVTecAD2(
...     root="./datasets/MVTecAD2",
...     category="can"
... )
>>> datamodule.prepare_data()

Directory structure after download:

datasets/
└── MVTecAD2/
    ├── can/
    ├── fabric/
    └── ...
test_dataloader(test_type=None)#

Get test dataloader for the specified test type.

Parameters:

test_type (str | TestType | None, optional) – Type of test set to use: - "public": Test set with ground truth for local evaluation - "private": Official test set without ground truth for leaderboard - "private_mixed": Official test set with mixed lighting conditions If None, uses the test_type specified in __init__. Defaults to None.

Example

>>> datamodule.setup()
>>> public_loader = datamodule.test_dataloader()  # returns loader based on test_type
>>> private_loader = datamodule.test_dataloader(test_type="private")
>>> mixed_loader = datamodule.test_dataloader(test_type="private_mixed")
Returns:

Test dataloader for the specified test type.

Return type:

EVAL_DATALOADERS

class anomalib.data.MVTecLOCO(root='./datasets/MVTec_LOCO', category='breakfast_box', 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, val_split_mode=ValSplitMode.FROM_DIR, test_split_ratio=None, val_split_ratio=None, seed=None)#

Bases: AnomalibDataModule

MVTec LOCO Datamodule.

Parameters:
  • root (Path | str) – Path to the root of the dataset. Defaults to "./datasets/MVTec_LOCO".

  • category (str) – Category of the MVTec LOCO dataset (e.g. "breakfast_box" or "juice_bottle"). Defaults to "breakfast_box".

  • 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 to 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. Defaults to None.

  • test_split_mode (TestSplitMode | str) – Method to create test set. Defaults to TestSplitMode.FROM_DIR.

  • val_split_mode (ValSplitMode | str) – Method to create validation set. Defaults to ValSplitMode.FROM_DIR.

  • test_split_ratio (float | None) – Fraction of data to use for testing. Defaults to None.

  • val_split_ratio (float | None) – Fraction of data to use for validation. Defaults to None.

  • seed (int | None, optional) – Seed for reproducibility. Defaults to None.

Example

Create MVTec LOCO datamodule with default settings:

>>> datamodule = MVTecLOCO()
>>> 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 = MVTecLOCO(category="juice_bottle")

Create validation set from test data:

>>> datamodule = MVTecLOCO(
...     val_split_mode=ValSplitMode.FROM_TEST,
...     val_split_ratio=0.1
... )

Create synthetic validation set:

>>> datamodule = MVTecLOCO(
...     val_split_mode=ValSplitMode.SYNTHETIC,
...     val_split_ratio=0.2
... )
class anomalib.data.RealIAD(root='./datasets/Real-IAD', category='audiojack', resolution=256, json_path='realiad_jsons/realiad_jsons/{category}.json', 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.NONE, val_split_mode=ValSplitMode.SAME_AS_TEST, seed=None)#

Bases: AnomalibDataModule

Real-IAD Datamodule.

Parameters:
  • root (Path | str) – Path to root directory containing the dataset. Defaults to "./datasets/Real-IAD".

  • category (str) – Category of the Real-IAD dataset (e.g. "audiojack" or "button_battery"). Defaults to "audiojack".

  • resolution (str | int) – Image resolution to use (e.g. "256", "512", "1024", "raw" or their integer equivalents). For example, both “256” and 256 are valid. Defaults to 256.

  • json_path (str | Path) – Path to JSON metadata file, relative to root directory. Can use {category} placeholder which will be replaced with the category name. Common paths are: - “realiad_jsons/realiad_jsons/{category}.json” - Base metadata (multi-view) - “realiad_jsons/realiad_jsons_sv/{category}.json” - Single-view metadata - “realiad_jsons/realiad_jsons_fuiad_0.4/{category}.json” - FUIAD v0.4 metadata

  • 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 to 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.NONE.

  • val_split_mode (ValSplitMode) – Method to create validation set. Defaults to ValSplitMode.SAME_AS_TEST.

  • seed (int | None, optional) – Seed for reproducibility. Defaults to None.

Example

Create Real-IAD datamodule with default settings:

>>> datamodule = RealIAD()
>>> 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 and resolution:

>>> # Using string resolution
>>> datamodule = RealIAD(
...     category="button_battery",
...     resolution="512"
... )

>>> # Using integer resolution
>>> datamodule = RealIAD(
...     category="button_battery",
...     resolution=1024
... )

Use different JSON metadata files:

>>> # Base metadata (multi-view)
>>> datamodule = RealIAD(
...     json_path="realiad_jsons/realiad_jsons/{category}.json"
... )

>>> # Single-view metadata
>>> datamodule = RealIAD(
...     json_path="realiad_jsons/realiad_jsons_sv/{category}.json"
... )

>>> # FUIAD v0.4 metadata (filtered subset)
>>> datamodule = RealIAD(
...     json_path="realiad_jsons/realiad_jsons_fuiad_0.4/{category}.json"
... )

>>> # Custom metadata
>>> datamodule = RealIAD(
...     json_path="path/to/custom/metadata.json"
... )

Create validation set from test data:

>>> datamodule = RealIAD(
...     val_split_mode=ValSplitMode.FROM_TEST,
...     val_split_ratio=0.1
... )

Notes

  • The dataset contains both normal (OK) and anomalous (NG) samples

  • Each object is captured from 5 different camera viewpoints (C1-C5)

  • Images are available in multiple resolutions (256x256, 512x512, 1024x1024)

  • JSON metadata files provide additional information and different dataset splits

  • Segmentation masks are provided for anomalous samples

prepare_data()#

Verify that the dataset is available and provide download instructions.

This method checks if the dataset exists in the root directory. If not, it provides instructions for requesting access and downloading from Hugging Face.

The Real-IAD dataset is available at: https://huggingface.co/datasets/REAL-IAD/Real-IAD :rtype: None

Note

The dataset requires approval from the authors. You need to: 1. Create a Hugging Face account 2. Request access to the dataset 3. Wait for approval 4. Download and extract to the root directory

class anomalib.data.Tabular(name, samples, root=None, normal_split_ratio=0.2, 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: AnomalibDataModule

Tabular DataModule.

Parameters:
  • name (str) – Name of the dataset. Used for logging/saving.

  • samples (dict | list | DataFrame) – Pandas DataFrame or compatible list or dict containing the dataset information.

  • root (str | Path | None) – Root folder containing normal and abnormal directories. 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.

  • 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 to 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 tabular datamodule:

>>> from anomalib.data import Tabular
>>> samples = {
...     "image_path": ["images/image1.png", "images/image2.png", "images/image3.png", ... ],
...     "label_index": [LabelName.NORMAL, LabelName.NORMAL, LabelName.ABNORMAL,  ... ],
...     "split": [Split.TRAIN, Split.TRAIN, Split.TEST, ... ],
... }
>>> datamodule = Tabular(
...     name="custom",
...     samples=samples,
...     root="./datasets/custom",
... )
>>> 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'])
classmethod from_file(name, file_path, file_format=None, pd_kwargs=None, **kwargs)#

Create Tabular Datamodule from file.

Parameters:
  • name (str) – Name of the dataset. This is used to name the datamodule, especially when logging/saving.

  • file_path (str | Path) – Path to tabular file containing the datset information.

  • file_format (str) – File format supported by a pd.read_* method, such as csv, parquet or json. Defaults to None (inferred from file suffix).

  • pd_kwargs (dict | None) – Keyword argument dictionary for the pd.read_* method. Defaults to None.

  • kwargs (dict) – Additional keyword arguments for the Tabular Datamodule class.

Returns:

Tabular Datamodule

Return type:

Tabular

Example

Prepare a tabular file (such as samples.csv or samples.parquet) with the following columns: image_path (absolute or relative to root), label_index (0 for normal, 1 for anomalous samples), and split (train or test). For segmentation tasks, also include a mask_path column.

From this file, create and setup a tabular datamodule:

>>> from anomalib.data import Tabular
>>> datamodule = Tabular.from_file(
...     name="custom",
...     file_path="./samples.csv",
...     root="./datasets/custom",
... )
>>> 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'])
property name: str#

Get name of the datamodule.

Returns:

Name of the datamodule.

class anomalib.data.VAD(root='./datasets/VAD', category='vad', 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: AnomalibDataModule

VAD Datamodule.

Parameters:
  • root (Path | str) – Path to the root of the dataset. Defaults to "./datasets/VAD".

  • category (str) – Category of the VAD dataset. Defaults to "vad".

  • 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 VAD datamodule with default settings:

>>> datamodule = VAD()
>>> 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])

Create validation set from test data:

>>> datamodule = VAD(
...     val_split_mode=ValSplitMode.FROM_TEST,
...     val_split_ratio=0.1
... )

Create synthetic validation set:

>>> datamodule = VAD(
...     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:

None

Example

Assume the dataset is not available on the file system:

>>> datamodule = VAD(
...     root="./datasets/VAD",
...     category="vad"
... )
>>> datamodule.prepare_data()

Directory structure after download:

datasets/
    └── VAD/
        └── vad/
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: AnomalibDataModule

VisA 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:

None

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: None

  1. If the processed dataset exists (visa_pytorch/{category}), do nothing

  2. If the raw dataset exists but isn’t processed, apply the train/test split

  3. 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