anomalib.data.folder¶
Custom Folder Dataset.
This script creates a custom dataset from a folder.
Module Contents¶
Classes¶
Folder Dataset. |
|
Folder Lightning Data Module. |
Functions¶
|
Check an input path, and convert to Pathlib object. |
|
Return a list of filenames and list corresponding labels. |
|
Make Folder Dataset. |
Attributes¶
- anomalib.data.folder._check_and_convert_path(path: Union[str, pathlib.Path]) pathlib.Path[source]¶
Check an input path, and convert to Pathlib object.
- Parameters
path (Union[str, Path]) – Input path.
- Returns
Output path converted to pathlib object.
- Return type
Path
- anomalib.data.folder._prepare_files_labels(path: Union[str, pathlib.Path], path_type: str, extensions: Optional[Tuple[str, Ellipsis]] = None) Tuple[list, list][source]¶
Return a list of filenames and list corresponding labels.
- Parameters
path (Union[str, Path]) – Path to the directory containing images.
path_type (str) – Type of images in the provided path (“normal”, “abnormal”, “normal_test”)
extensions (Optional[Tuple[str, ...]], optional) – Type of the image extensions to read from the directory.
- Returns
Filenames of the images provided in the paths, labels of the images provided in the paths
- Return type
List, List
- anomalib.data.folder.make_dataset(normal_dir: Union[str, pathlib.Path], abnormal_dir: Union[str, pathlib.Path], normal_test_dir: Optional[Union[str, pathlib.Path]] = None, mask_dir: Optional[Union[str, pathlib.Path]] = None, split: Optional[str] = None, split_ratio: float = 0.2, seed: int = 0, create_validation_set: bool = True, extensions: Optional[Tuple[str, Ellipsis]] = None)[source]¶
Make Folder Dataset.
- Parameters
normal_dir (Union[str, Path]) – Path to the directory containing normal images.
abnormal_dir (Union[str, Path]) – Path to the directory containing abnormal images.
normal_test_dir (Optional[Union[str, Path]], optional) – Path to the directory containing normal images for the test dataset. Normal test images will be a split of normal_dir if None. Defaults to None.
mask_dir (Optional[Union[str, Path]], optional) – Path to the directory containing the mask annotations. Defaults to None.
split (Optional[str], optional) – Dataset split (ie., either train or test). Defaults to None.
split_ratio (float, optional) – Ratio to split normal training images and add to the test set in case test set doesn’t contain any normal images. Defaults to 0.2.
seed (int, optional) – Random seed to ensure reproducibility when splitting. Defaults to 0.
create_validation_set (bool, optional) – Boolean to create a validation set from the test set. Those wanting to create a validation set could set this flag to
True.extensions (Optional[Tuple[str, ...]], optional) – Type of the image extensions to read from the directory.
- Returns
an output dataframe containing samples for the requested split (ie., train or test)
- Return type
DataFrame
- class anomalib.data.folder.FolderDataset(normal_dir: Union[pathlib.Path, str], abnormal_dir: Union[pathlib.Path, str], split: str, pre_process: anomalib.pre_processing.PreProcessor, normal_test_dir: Optional[Union[pathlib.Path, str]] = None, split_ratio: float = 0.2, mask_dir: Optional[Union[pathlib.Path, str]] = None, extensions: Optional[Tuple[str, Ellipsis]] = None, task: Optional[str] = None, seed: int = 0, create_validation_set: bool = False)[source]¶
Bases:
torch.utils.data.DatasetFolder Dataset.
- __getitem__(self, index: int) Dict[str, Union[str, torch.Tensor]][source]¶
Get dataset item for the index
index.- Parameters
index (int) – Index to get the item.
- Returns
- Dict of image tensor during training.
Otherwise, Dict containing image path, target path, image tensor, label and transformed bounding box.
- Return type
Union[Dict[str, Tensor], Dict[str, Union[str, Tensor]]]
- class anomalib.data.folder.Folder(root: Union[str, pathlib.Path], normal_dir: str = 'normal', abnormal_dir: str = 'abnormal', task: str = 'classification', normal_test_dir: Optional[Union[pathlib.Path, str]] = None, mask_dir: Optional[Union[pathlib.Path, str]] = None, extensions: Optional[Tuple[str, Ellipsis]] = None, split_ratio: float = 0.2, seed: int = 0, image_size: Optional[Union[int, Tuple[int, int]]] = None, train_batch_size: int = 32, test_batch_size: int = 32, num_workers: int = 8, transform_config_train: Optional[Union[str, albumentations.Compose]] = None, transform_config_val: Optional[Union[str, albumentations.Compose]] = None, create_validation_set: bool = False)[source]¶
Bases:
pytorch_lightning.core.datamodule.LightningDataModuleFolder Lightning Data Module.
- setup(self, stage: Optional[str] = None) None[source]¶
Setup train, validation and test data.
- Parameters
stage – Optional[str]: Train/Val/Test stages. (Default value = None)
- train_dataloader(self) pytorch_lightning.utilities.types.TRAIN_DATALOADERS[source]¶
Get train dataloader.
- val_dataloader(self) pytorch_lightning.utilities.types.EVAL_DATALOADERS[source]¶
Get validation dataloader.