build_semantic_dataset
Builds a semantic anomaly detection dataset for training or evaluation.Parameters
Name of the semantic anomaly dataset to build.Supported datasets:
color_mnist: MNIST digits with color attributeswaterbirds: Birds in different backgroundsceleba: Celebrity faces with attributes
Dataset split to load.
Root directory where the dataset files are stored or will be downloaded.
Optional transform to apply to images. Typically a torchvision transform pipeline.
Dataset-specific configuration dictionary. When
None, uses default configuration.For color_mnist, config structure:Additional keyword arguments. For
color_mnist, accepts seed parameter for reproducible train/valid splits.Returns
A dataset instance that inherits from
SemanticAnomalyDataset.Examples
Load ColorMNIST with default config
Load with custom config
Load other semantic datasets
build_industrial_dataset
Builds an industrial anomaly detection dataset for a specific product category.Parameters
Name of the industrial anomaly dataset.Supported datasets:
mvtec: MVTec Anomaly Detection datasetvisa: VisA (Visual Anomaly) dataset
Product category to load.For MVTec, supported categories:
bottle, cable, capsule, carpet, grid, hazelnut, leather, metal_nut, pill, screw, tile, toothbrush, transistor, wood, zipperDataset split to load.
Root directory where the dataset files are stored.
Optional transform to apply to images.
Optional transform to apply to anomaly masks.
Returns
A dataset instance that inherits from
IndustrialAnomalyDataset.Examples
Load MVTec dataset
Load multiple categories
SemanticAnomalyDataset
Base class for semantic anomaly detection datasets.Attributes
List of attribute names (e.g.,
["number", "color"] for ColorMNIST).Boolean tensor of shape
[num_samples, num_attrs] where False indicates normal and True indicates anomaly for each attribute.Root directory of the dataset.
Dataset split (train, valid, or test).
Transform applied to images.
Methods
get_normal_subset()
Returns a subset containing only normal samples (all attributes are False).A PyTorch Subset containing only samples where all attributes are normal.
load_image(index: int)
Loads the image at the given index. Must be implemented by subclasses.The loaded image.
__getitem__(index: int)
Returns a tuple of(image, target) where target is the attribute tensor.
Tuple of (transformed image, attribute tensor).
IndustrialAnomalyDataset
Base class for industrial anomaly detection datasets.Attributes
Boolean tensor of shape
[num_samples] where False indicates normal and True indicates anomaly.Root directory of the dataset.
Dataset split (train or test).
Transform applied to images.
Transform applied to anomaly masks.
Methods
load_image(index: int)
Loads the image at the given index. Must be implemented by subclasses.The loaded image.
load_mask(index: int)
Loads the anomaly mask at the given index. Must be implemented by subclasses.Boolean tensor indicating anomalous regions.
__getitem__(index: int)
Returns a tuple of(image, mask, label).
Tuple of (transformed image, mask, label). For normal samples, mask is all zeros.
