Visa Data#
Visual Anomaly (VisA) Dataset (CC BY-NC-SA 4.0).
- Description:
- This script contains PyTorch Dataset, Dataloader and PyTorch
Lightning DataModule for the Visual Anomal (VisA) dataset.
- If the dataset is not on the file system, the script downloads and
extracts the dataset and create PyTorch data objects.
- License:
The VisA dataset is released under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0)(https://creativecommons.org/licenses/by-nc-sa/4.0/).
- Reference:
Zou, Y., Jeong, J., Pemula, L., Zhang, D., & Dabeer, O. (2022). SPot-the-Difference Self-supervised Pre-training for Anomaly Detection and Segmentation. In European Conference on Computer Vision (pp. 392-408). Springer, Cham.
- class anomalib.data.image.visa.Visa(root='./datasets/visa', category='capsules', image_size=(256, 256), center_crop=None, normalization=InputNormalizationMethod.IMAGENET, train_batch_size=32, eval_batch_size=32, num_workers=8, task=TaskType.SEGMENTATION, transform_config_train=None, transform_config_eval=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 such as
candle. Defaults to"candle".image_size (int | tuple[int, int] | None, optional) – Size of the input image. Defaults to
(256, 256).center_crop (int | tuple[int, int] | None, optional) – When provided, the images will be center-cropped to the provided dimensions. Defaults to
None.normalization (InputNormalizationMethod | str) – Normalization method to be applied to the input images. Defaults to
InputNormalizationMethod.IMAGENET.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.task (TaskType) – Task type, ‘classification’, ‘detection’ or ‘segmentation’ Defaults to
TaskType.SEGMENTATION.transform_config_train (str | A.Compose | None, optional) – Config for pre-processing during training. Defaults to
None.transform_config_val (str | A.Compose | None, optional) – Config for pre-processing during validation. 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. Defatuls to
0.5.seed (int | None, optional) – Seed which may be set to a fixed value 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 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 = Visa() >> datamodule.prepare_data()
After:
$ tree datasets datasets ├── dataset1 ├── dataset2 └── visa ├── candle ├── ... ├── pipe_fryum │ ├── Data │ └── image_anno.csv ├── split_csv │ ├── 1cls.csv │ ├── 2cls_fewshot.csv │ └── 2cls_highshot.csv ├── VisA_20220922.tar └── visa_pytorch ├── candle ├── ... ├── pcb4 └── pipe_fryum
prepare_dataensures that the dataset is converted to MVTec format.visa_pytorchis the directory that contains the dataset in the MVTec format.visais the directory that contains the original dataset.
- class anomalib.data.image.visa.VisaDataset(task, transform, root, category, split=None)#
Bases:
AnomalibDatasetVisA dataset class.
- Parameters:
task (TaskType) – Task type,
classification,detectionorsegmentationtransform (A.Compose) – Albumentations Compose object describing the transforms that are applied to the inputs.
root (str | Path) – Path to the root of the dataset
category (str) – Sub-category of the dataset, e.g. ‘candle’
split (str | Split | None) – Split of the dataset, usually Split.TRAIN or Split.TEST Defaults to
None.
Examples
To create a Visa dataset for classification:
from anomalib.data.image.visa import VisaDataset from anomalib.data.utils.transforms import get_transforms transform = get_transforms(image_size=256) dataset = VisaDataset( task="classification", transform=transform, split="train", root="./datasets/visa/visa_pytorch/", category="candle", ) dataset.setup() dataset[0].keys() # Output dict_keys(['image_path', 'label', 'image'])
If you want to use the dataset for segmentation, you can use the same code as above, with the task set to
segmentation. The dataset will then have amaskkey in the output dictionary.from anomalib.data.image.visa import VisaDataset from anomalib.data.utils.transforms import get_transforms transform = get_transforms(image_size=256) dataset = VisaDataset( task="segmentation", transform=transform, split="train", root="./datasets/visa/visa_pytorch/", category="candle", ) dataset.setup() dataset[0].keys() # Output dict_keys(['image_path', 'label', 'image', 'mask_path', 'mask'])