Utils¶
Helper utilities for data.
- class anomalib.data.utils.Augmenter(anomaly_source_path: str | None = None, p_anomalous: float = 0.5, beta: float | tuple[float, float] = (0.2, 1.0))[source]¶
Bases:
object
Class that generates noisy augmentations of input images.
- Parameters:
anomaly_source_path (str | None) – Path to a folder of images that will be used as source of the anomalous
specified (noise. If not) –
instead. (random noise will be used) –
p_anomalous (float) – Probability that the anomalous perturbation will be applied to a given image.
beta (float) – Parameter that determines the opacity of the noise mask.
- augment_batch(batch: Tensor) tuple[Tensor, Tensor] [source]¶
Generate anomalous augmentations for a batch of input images.
- Parameters:
batch (Tensor) – Batch of input images
- Returns:
Augmented image to which anomalous perturbations have been added.
Ground truth masks corresponding to the anomalous perturbations.
- generate_perturbation(height: int, width: int, anomaly_source_path: str | None) tuple[np.ndarray, np.ndarray] [source]¶
Generate an image containing a random anomalous perturbation using a source image.
- Parameters:
height (int) – height of the generated image.
width – (int): width of the generated image.
anomaly_source_path (str | None) – Path to an image file. If not provided, random noise will be used
instead. –
- Returns:
Image containing a random anomalous perturbation, and the corresponding ground truth anomaly mask.
- class anomalib.data.utils.DownloadInfo(name: str, url: str, hash: str)[source]¶
Bases:
object
Info needed to download a dataset from a url.
- hash: str¶
- name: str¶
- url: str¶
- class anomalib.data.utils.InputNormalizationMethod(value)[source]¶
Bases:
str
,Enum
Normalization method for the input images.
- IMAGENET = 'imagenet'¶
- NONE = 'none'¶
- class anomalib.data.utils.Split(value)[source]¶
Bases:
str
,Enum
Split of a subset.
- TEST = 'test'¶
- TRAIN = 'train'¶
- VAL = 'val'¶
- class anomalib.data.utils.TestSplitMode(value)[source]¶
Bases:
str
,Enum
Splitting mode used to obtain subset.
- FROM_DIR = 'from_dir'¶
- NONE = 'none'¶
- SYNTHETIC = 'synthetic'¶
- class anomalib.data.utils.ValSplitMode(value)[source]¶
Bases:
str
,Enum
Splitting mode used to obtain validation subset.
- FROM_TEST = 'from_test'¶
- NONE = 'none'¶
- SAME_AS_TEST = 'same_as_test'¶
- SYNTHETIC = 'synthetic'¶
- anomalib.data.utils.boxes_to_anomaly_maps(boxes: Tensor, scores: Tensor, image_size: tuple[int, int]) Tensor [source]¶
Convert bounding box coordinates to anomaly heatmaps.
- Parameters:
boxes (list[Tensor]) – A list of length B where each element is a tensor of shape (N, 4) containing the bounding box coordinates of the regions of interest in xyxy format.
scores (list[Tensor]) – A list of length B where each element is a 1D tensor of length N containing the anomaly scores for each region of interest.
image_size (tuple[int, int]) – Image size of the output masks in (H, W) format.
- Returns:
- Tensor of shape (B, H, W). The pixel locations within each bounding box are collectively assigned the
anomaly score of the bounding box. In the case of overlapping bounding boxes, the highest score is used.
- Return type:
Tensor
- anomalib.data.utils.boxes_to_masks(boxes: list[Tensor], image_size: tuple[int, int]) Tensor [source]¶
Convert bounding boxes to segmentations masks.
- Parameters:
boxes (list[Tensor]) – A list of length B where each element is a tensor of shape (N, 4) containing the bounding box coordinates of the regions of interest in xyxy format.
image_size (tuple[int, int]) – Image size of the output masks in (H, W) format.
- Returns:
- Tensor of shape (B, H, W) in which each slice is a binary mask showing the pixels contained by a
bounding box.
- Return type:
Tensor
- anomalib.data.utils.concatenate_datasets(datasets: Sequence[AnomalibDataset]) AnomalibDataset [source]¶
Concatenate multiple datasets into a single dataset object.
- Parameters:
datasets (Sequence[AnomalibDataset]) – Sequence of at least two datasets.
- Returns:
Dataset that contains the combined samples of all input datasets.
- Return type:
AnomalibDataset
- anomalib.data.utils.download_and_extract(root: Path, info: DownloadInfo) None [source]¶
Download and extract a dataset.
- Parameters:
root (Path) – Root directory where the dataset will be stored.
info (DownloadInfo) – Info needed to download the dataset.
- anomalib.data.utils.generate_output_image_filename(input_path: str | Path, output_path: str | Path) Path [source]¶
Generate an output filename to save the inference image.
This function generates an output filaname by checking the input and output filenames. Input path is the input to infer, and output path is the path to save the output predictions specified by the user.
The function expects
input_path
to always be a file, not a directory.output_path
could be a filename or directory. If it is a filename, the function checks if the specified filename exists on the file system. If yes, the function callsduplicate_filename
to duplicate the filename to avoid overwriting the existing file. Ifoutput_path
is a directory, this function adds the parent and filenames ofinput_path
tooutput_path
.- Parameters:
input_path (str | Path) – Path to the input image to infer.
output_path (str | Path) – Path to output to save the predictions. Could be a filename or a directory.
Examples
>>> input_path = Path("datasets/MVTec/bottle/test/broken_large/000.png") >>> output_path = Path("datasets/MVTec/bottle/test/broken_large/000.png") >>> generate_output_image_filename(input_path, output_path) PosixPath('datasets/MVTec/bottle/test/broken_large/000_1.png')
>>> input_path = Path("datasets/MVTec/bottle/test/broken_large/000.png") >>> output_path = Path("results/images") >>> generate_output_image_filename(input_path, output_path) PosixPath('results/images/broken_large/000.png')
- Raises:
ValueError – When the
input_path
is not a file.- Returns:
The output filename to save the output predictions from the inferencer.
- Return type:
Path
- anomalib.data.utils.get_image_filenames(path: str | Path) list[Path] [source]¶
Get image filenames.
- Parameters:
path (str | Path) – Path to image or image-folder.
- Returns:
List of image filenames
- Return type:
list[Path]
- anomalib.data.utils.get_image_height_and_width(image_size: int | tuple[int, int]) tuple[int, int] [source]¶
Get image height and width from
image_size
variable.- Parameters:
image_size (int | tuple[int, int] | None, optional) – Input image size.
- Raises:
ValueError – Image size not None, int or tuple.
Examples
>>> get_image_height_and_width(image_size=256) (256, 256)
>>> get_image_height_and_width(image_size=(256, 256)) (256, 256)
>>> get_image_height_and_width(image_size=(256, 256, 3)) (256, 256)
>>> get_image_height_and_width(image_size=256.) Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 18, in get_image_height_and_width ValueError: ``image_size`` could be either int or tuple[int, int]
- Returns:
A tuple containing image height and width values.
- Return type:
tuple[int | None, int | None]
- anomalib.data.utils.get_transforms(config: str | A.Compose | None = None, image_size: int | tuple[int, int] | None = None, center_crop: int | tuple[int, int] | None = None, normalization: InputNormalizationMethod = InputNormalizationMethod.IMAGENET, to_tensor: bool = True) A.Compose [source]¶
Get transforms from config or image size.
- Parameters:
config (str | A.Compose | None, optional) – Albumentations transforms. Either config or albumentations
Compose
object. Defaults to None.image_size (int | tuple | None, optional) – Image size to transform. Defaults to None.
to_tensor (bool, optional) – Boolean to convert the final transforms into Torch tensor. Defaults to True.
- Raises:
ValueError – When both
config
andimage_size
isNone
.ValueError – When
config
is not astr
or A.Compose` object.
- Returns:
Albumentation
Compose
object containing the image transforms.- Return type:
A.Compose
Examples
>>> import skimage >>> image = skimage.data.astronaut()
>>> transforms = get_transforms(image_size=256, to_tensor=False) >>> output = transforms(image=image) >>> output["image"].shape (256, 256, 3)
>>> transforms = get_transforms(image_size=256, to_tensor=True) >>> output = transforms(image=image) >>> output["image"].shape torch.Size([3, 256, 256])
Transforms could be read from albumentations Compose object. >>> import albumentations as A >>> from albumentations.pytorch import ToTensorV2 >>> config = A.Compose([A.Resize(512, 512), ToTensorV2()]) >>> transforms = get_transforms(config=config, to_tensor=False) >>> output = transforms(image=image) >>> output[“image”].shape (512, 512, 3) >>> type(output[“image”]) numpy.ndarray
Transforms could be deserialized from a yaml file. >>> transforms = A.Compose([A.Resize(1024, 1024), ToTensorV2()]) >>> A.save(transforms, “/tmp/transforms.yaml”, data_format=”yaml”) >>> transforms = get_transforms(config=”/tmp/transforms.yaml”) >>> output = transforms(image=image) >>> output[“image”].shape torch.Size([3, 1024, 1024])
- anomalib.data.utils.masks_to_boxes(masks: Tensor, anomaly_maps: Tensor | None = None) tuple[list[Tensor], list[Tensor]] [source]¶
Convert a batch of segmentation masks to bounding box coordinates.
- Parameters:
masks (Tensor) – Input tensor of shape (B, 1, H, W), (B, H, W) or (H, W)
anomaly_maps (Tensor | None, optional) – Anomaly maps of shape (B, 1, H, W), (B, H, W) or (H, W) which are used to determine an anomaly score for the converted bounding boxes.
- Returns:
- A list of length B where each element is a tensor of shape (N, 4) containing the bounding box
coordinates of the objects in the masks in xyxy format.
- list[Tensor]: A list of length B where each element is a tensor of length (N) containing an anomaly score for
each of the converted boxes.
- Return type:
list[Tensor]
- anomalib.data.utils.random_2d_perlin(shape: tuple, res: tuple[int | Tensor, int | Tensor], fade=<function <lambda>>) np.ndarray | Tensor [source]¶
Returns a random 2d perlin noise array.
- Parameters:
shape (tuple) – Shape of the 2d map.
res (tuple[int | Tensor, int | Tensor]) – Tuple of scales for perlin noise for height and width dimension.
fade (_type_, optional) – Function used for fading the resulting 2d map. Defaults to equation 6*t**5-15*t**4+10*t**3.
- Returns:
Random 2d-array/tensor generated using perlin noise.
- Return type:
np.ndarray | Tensor
- anomalib.data.utils.random_split(dataset: AnomalibDataset, split_ratio: float | Sequence[float], label_aware: bool = False, seed: int | None = None) list[AnomalibDataset] [source]¶
Perform a random split of a dataset.
- Parameters:
dataset (AnomalibDataset) – Source dataset
split_ratio (Union[float, Sequence[float]]) – Fractions of the splits that will be produced. The values in the sequence must sum to 1. If a single value is passed, the ratio will be converted to [1-split_ratio, split_ratio].
label_aware (bool) – When True, the relative occurrence of the different class labels of the source dataset will be maintained in each of the subsets.
seed (int | None, optional) – Seed that can be passed if results need to be reproducible
- anomalib.data.utils.read_depth_image(path: str | Path) np.ndarray [source]¶
Read tiff depth image from disk.
- Parameters:
path (str, Path) – path to the image file
Example
>>> image = read_depth_image("test_image.tiff")
- Returns:
image as numpy array