Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to src layout #921

Merged
merged 12 commits into from
Mar 9, 2023
Prev Previous commit
Next Next commit
Merge main
samet-akcay committed Mar 7, 2023
commit ddaacacec9c087b912a8a9d48bc0b0048c0551ae
2 changes: 1 addition & 1 deletion docs/source/how_to_guides/notebooks
389 changes: 355 additions & 34 deletions notebooks/000_getting_started/001_getting_started.ipynb

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -12,5 +12,3 @@ pandas>=1.1.0
pytorch-lightning>=1.7.0,<1.10.0
timm>=0.5.4,<=0.6.12
torchmetrics==0.10.3
torchvision>=0.9.1
torchtext>=0.9.1
47 changes: 47 additions & 0 deletions src/anomalib/data/__init__.py
Original file line number Diff line number Diff line change
@@ -13,8 +13,10 @@
from .base import AnomalibDataModule, AnomalibDataset
from .btech import BTech
from .folder import Folder
from .folder_3d import Folder3D
from .inference import InferenceDataset
from .mvtec import MVTec
from .mvtec_3d import MVTec3D
from .shanghaitech import ShanghaiTech
from .task_type import TaskType
from .ucsd_ped import UCSDped
@@ -59,6 +61,24 @@ def get_datamodule(config: DictConfig | ListConfig) -> AnomalibDataModule:
val_split_mode=config.dataset.val_split_mode,
val_split_ratio=config.dataset.val_split_ratio,
)
elif config.dataset.format.lower() == "mvtec_3d":
datamodule = MVTec3D(
root=config.dataset.path,
category=config.dataset.category,
image_size=(config.dataset.image_size[0], config.dataset.image_size[1]),
center_crop=center_crop,
normalization=config.dataset.normalization,
train_batch_size=config.dataset.train_batch_size,
eval_batch_size=config.dataset.eval_batch_size,
num_workers=config.dataset.num_workers,
task=config.dataset.task,
transform_config_train=config.dataset.transform_config.train,
transform_config_eval=config.dataset.transform_config.eval,
test_split_mode=config.dataset.test_split_mode,
test_split_ratio=config.dataset.test_split_ratio,
val_split_mode=config.dataset.val_split_mode,
val_split_ratio=config.dataset.val_split_ratio,
)
elif config.dataset.format.lower() == "btech":
datamodule = BTech(
root=config.dataset.path,
@@ -99,6 +119,31 @@ def get_datamodule(config: DictConfig | ListConfig) -> AnomalibDataModule:
val_split_mode=config.dataset.val_split_mode,
val_split_ratio=config.dataset.val_split_ratio,
)
elif config.dataset.format.lower() == "folder_3d":
datamodule = Folder3D(
root=config.dataset.root,
normal_dir=config.dataset.normal_dir,
normal_depth_dir=config.dataset.normal_depth_dir,
abnormal_dir=config.dataset.abnormal_dir,
abnormal_depth_dir=config.dataset.abnormal_depth_dir,
task=config.dataset.task,
normal_test_dir=config.dataset.normal_test_dir,
normal_test_depth_dir=config.dataset.normal_test_depth_dir,
mask_dir=config.dataset.mask_dir,
extensions=config.dataset.extensions,
image_size=(config.dataset.image_size[0], config.dataset.image_size[1]),
center_crop=center_crop,
normalization=config.dataset.normalization,
train_batch_size=config.dataset.train_batch_size,
eval_batch_size=config.dataset.eval_batch_size,
num_workers=config.dataset.num_workers,
transform_config_train=config.dataset.transform_config.train,
transform_config_eval=config.dataset.transform_config.eval,
test_split_mode=config.dataset.test_split_mode,
test_split_ratio=config.dataset.test_split_ratio,
val_split_mode=config.dataset.val_split_mode,
val_split_ratio=config.dataset.val_split_ratio,
)
elif config.dataset.format.lower() == "ucsdped":
datamodule = UCSDped(
root=config.dataset.path,
@@ -187,8 +232,10 @@ def get_datamodule(config: DictConfig | ListConfig) -> AnomalibDataModule:
"get_datamodule",
"BTech",
"Folder",
"Folder3D",
"InferenceDataset",
"MVTec",
"MVTec3D",
"Avenue",
"UCSDped",
"TaskType",
9 changes: 8 additions & 1 deletion src/anomalib/data/base/__init__.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,13 @@

from .datamodule import AnomalibDataModule
from .dataset import AnomalibDataset
from .depth import AnomalibDepthDataset
from .video import AnomalibVideoDataModule, AnomalibVideoDataset

__all__ = ["AnomalibDataset", "AnomalibDataModule", "AnomalibVideoDataset", "AnomalibVideoDataModule"]
__all__ = [
"AnomalibDataset",
"AnomalibDataModule",
"AnomalibVideoDataset",
"AnomalibVideoDataModule",
"AnomalibDepthDataset",
]
2 changes: 1 addition & 1 deletion src/anomalib/data/base/datamodule.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
from pandas import DataFrame
from pytorch_lightning import LightningDataModule
from pytorch_lightning.utilities.types import EVAL_DATALOADERS, TRAIN_DATALOADERS
from torch.utils.data import DataLoader, default_collate
from torch.utils.data.dataloader import DataLoader, default_collate

from anomalib.data.base.dataset import AnomalibDataset
from anomalib.data.synthetic import SyntheticAnomalyDataset
1 change: 1 addition & 0 deletions src/anomalib/data/base/dataset.py
Original file line number Diff line number Diff line change
@@ -126,6 +126,7 @@ def __getitem__(self, index: int) -> dict[str, str | Tensor]:
elif self.task in (TaskType.DETECTION, TaskType.SEGMENTATION):
# Only Anomalous (1) images have masks in anomaly datasets
# Therefore, create empty mask for Normal (0) images.

if label_index == 0:
mask = np.zeros(shape=image.shape[:2])
else:
68 changes: 68 additions & 0 deletions src/anomalib/data/base/depth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""Base Depth Dataset."""

from __future__ import annotations

from abc import ABC

import albumentations as A
import cv2
import numpy as np
from torch import Tensor

from anomalib.data.base.dataset import AnomalibDataset
from anomalib.data.task_type import TaskType
from anomalib.data.utils import masks_to_boxes, read_depth_image, read_image


class AnomalibDepthDataset(AnomalibDataset, ABC):
"""Base depth anomalib dataset class.

Args:
task (str): Task type, either 'classification' or 'segmentation'
transform (A.Compose): Albumentations Compose object describing the transforms that are applied to the inputs.
"""

def __init__(self, task: TaskType, transform: A.Compose) -> None:
super().__init__(task, transform)

self.transform = transform

def __getitem__(self, index: int) -> dict[str, str | Tensor]:
"""Return rgb image, depth image and mask."""

image_path = self._samples.iloc[index].image_path
mask_path = self._samples.iloc[index].mask_path
label_index = self._samples.iloc[index].label_index
depth_path = self._samples.iloc[index].depth_path

image = read_image(image_path)
depth_image = read_depth_image(depth_path)
item = dict(image_path=image_path, depth_path=depth_path, label=label_index)

if self.task == TaskType.CLASSIFICATION:
transformed = self.transform(image=image, depth_image=depth_image)
item["image"] = transformed["image"]
item["depth_image"] = transformed["depth_image"]
elif self.task in (TaskType.DETECTION, TaskType.SEGMENTATION):
# Only Anomalous (1) images have masks in anomaly datasets
# Therefore, create empty mask for Normal (0) images.
if label_index == 0:
mask = np.zeros(shape=image.shape[:2])
else:
mask = cv2.imread(mask_path, flags=0) / 255.0

transformed = self.transform(image=image, depth_image=depth_image, mask=mask)

item["image"] = transformed["image"]
item["depth_image"] = transformed["depth_image"]
item["mask_path"] = mask_path
item["mask"] = transformed["mask"]

if self.task == TaskType.DETECTION:
# create boxes from masks for detection task
boxes, _ = masks_to_boxes(item["mask"])
item["boxes"] = boxes[0]
else:
raise ValueError(f"Unknown task type: {self.task}")

return item
120 changes: 30 additions & 90 deletions src/anomalib/data/folder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Custom Folder Dataset.

This script creates a custom dataset from a folder.
"""

@@ -12,7 +11,6 @@

import albumentations as A
from pandas import DataFrame
from torchvision.datasets.folder import IMG_EXTENSIONS

from anomalib.data.base import AnomalibDataModule, AnomalibDataset
from anomalib.data.task_type import TaskType
@@ -23,74 +21,7 @@
ValSplitMode,
get_transforms,
)


def _check_and_convert_path(path: str | Path) -> Path:
"""Check an input path, and convert to Pathlib object.

Args:
path (str | Path): Input path.

Returns:
Path: Output path converted to pathlib object.
"""
if not isinstance(path, Path):
path = Path(path)
return path


def _prepare_files_labels(
path: str | Path, path_type: str, extensions: tuple[str, ...] | None = None
) -> tuple[list, list]:
"""Return a list of filenames and list corresponding labels.

Args:
path (str | Path): Path to the directory containing images.
path_type (str): Type of images in the provided path ("normal", "abnormal", "normal_test")
extensions (tuple[str, ...] | None, optional): Type of the image extensions to read from the
directory.

Returns:
List, List: Filenames of the images provided in the paths, labels of the images provided in the paths
"""
path = _check_and_convert_path(path)
if extensions is None:
extensions = IMG_EXTENSIONS

if isinstance(extensions, str):
extensions = (extensions,)

filenames = [f for f in path.glob(r"**/*") if f.suffix in extensions and not f.is_dir()]
if not filenames:
raise RuntimeError(f"Found 0 {path_type} images in {path}")

labels = [path_type] * len(filenames)

return filenames, labels


def _resolve_path(folder: str | Path, root: str | Path | None = None) -> Path:
"""Combines root and folder and returns the absolute path.

This allows users to pass either a root directory and relative paths, or absolute paths to each of the
image sources. This function makes sure that the samples dataframe always contains absolute paths.

Args:
folder (str | Path | None): Folder location containing image or mask data.
root (str | Path | None): Root directory for the dataset.
"""
folder = Path(folder)
if folder.is_absolute():
# path is absolute; return unmodified
path = folder
# path is relative.
elif root is None:
# no root provided; return absolute path
path = folder.resolve()
else:
# root provided; prepend root and return absolute path
path = (Path(root) / folder).resolve()
return path
from anomalib.data.utils.path import _prepare_files_labels, _resolve_path


def make_folder_dataset(
@@ -103,7 +34,6 @@ def make_folder_dataset(
extensions: tuple[str, ...] | None = None,
) -> DataFrame:
"""Make Folder Dataset.

Args:
normal_dir (str | Path): Path to the directory containing normal images.
root (str | Path | None): Path to the root directory of the dataset.
@@ -117,7 +47,6 @@ def make_folder_dataset(
Defaults to None.
extensions (tuple[str, ...] | None, optional): Type of the image extensions to read from the
directory.

Returns:
DataFrame: an output dataframe containing samples for the requested split (ie., train or test)
"""
@@ -137,31 +66,46 @@ def make_folder_dataset(
if normal_test_dir:
dirs = {**dirs, **{"normal_test": normal_test_dir}}

if mask_dir:
dirs = {**dirs, **{"mask_dir": mask_dir}}

for dir_type, path in dirs.items():
filename, label = _prepare_files_labels(path, dir_type, extensions)
filenames += filename
labels += label

samples = DataFrame({"image_path": filenames, "label": labels, "mask_path": ""})
samples = DataFrame({"image_path": filenames, "label": labels})
samples = samples.sort_values(by="image_path", ignore_index=True)

# Create label index for normal (0) and abnormal (1) images.
samples.loc[(samples.label == "normal") | (samples.label == "normal_test"), "label_index"] = 0
samples.loc[(samples.label == "abnormal"), "label_index"] = 1
samples.label_index = samples.label_index.astype(int)
samples.label_index = samples.label_index.astype("Int64")

# If a path to mask is provided, add it to the sample dataframe.
if mask_dir is not None:
mask_dir = _check_and_convert_path(mask_dir)
for index, row in samples.iterrows():
if row.label_index == 1:
rel_image_path = row.image_path.relative_to(abnormal_dir)
samples.loc[index, "mask_path"] = str(mask_dir / rel_image_path)

# make sure all the files exist
# samples.image_path does NOT need to be checked because we build the df based on that
assert samples.mask_path.apply(
lambda x: Path(x).exists() if x != "" else True
).all(), f"missing mask files, mask_dir={mask_dir}"

if mask_dir is not None and abnormal_dir is not None:
samples.loc[samples.label == "abnormal", "mask_path"] = samples.loc[
samples.label == "mask_dir"
].image_path.values
samples["mask_path"].fillna("", inplace=True)
samples = samples.astype({"mask_path": "str"})

# make sure all every rgb image has a corresponding mask image.
assert (
samples.loc[samples.label_index == 1]
.apply(lambda x: Path(x.image_path).stem in Path(x.mask_path).stem, axis=1)
.all()
), "Mismatch between anomalous images and mask images. Make sure the mask files \
folder follow the same naming convention as the anomalous images in the dataset \
(e.g. image: '000.png', mask: '000.png')."
else:
samples["mask_path"] = ""

# remove all the rows with temporal image samples that have already been assigned
samples = samples.loc[
(samples.label == "normal") | (samples.label == "abnormal") | (samples.label == "normal_test")
]

# Ensure the pathlib objects are converted to str.
# This is because torch dataloader doesn't like pathlib.
@@ -183,7 +127,6 @@ def make_folder_dataset(

class FolderDataset(AnomalibDataset):
"""Folder dataset.

Args:
task (TaskType): Task type. (``classification``, ``detection`` or ``segmentation``).
transform (A.Compose): Albumentations Compose object describing the transforms that are applied to the inputs.
@@ -196,11 +139,9 @@ class FolderDataset(AnomalibDataset):
normal images for the test dataset. Defaults to None.
mask_dir (str | Path | None, optional): Path to the directory containing
the mask annotations. Defaults to None.

extensions (tuple[str, ...] | None, optional): Type of the image extensions to read from the
directory.
val_split_mode (ValSplitMode): Setting that determines how the validation subset is obtained.

Raises:
ValueError: When task is set to classification and `mask_dir` is provided. When `mask_dir` is
provided, `task` should be set to `segmentation`.
@@ -243,7 +184,6 @@ def _setup(self) -> None:

class Folder(AnomalibDataModule):
"""Folder DataModule.

Args:
normal_dir (str | Path): Name of the directory containing normal images.
Defaults to "normal".
379 changes: 379 additions & 0 deletions src/anomalib/data/folder_3d.py

Large diffs are not rendered by default.

293 changes: 293 additions & 0 deletions src/anomalib/data/mvtec_3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
"""MVTec 3D-AD Dataset (CC BY-NC-SA 4.0).
Description:
This script contains PyTorch Dataset, Dataloader and PyTorch
Lightning DataModule for the MVTec 3D-AD dataset.
If the dataset is not on the file system, the script downloads and
extracts the dataset and create PyTorch data objects.
License:
MVTec 3D-AD 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:
- Paul Bergmann, Xin Jin, David Sattlegger, Carsten Steger:
The MVTec 3D-AD Dataset for Unsupervised 3D Anomaly Detection and Localization
in: Proceedings of the 17th International Joint Conference on Computer Vision, Imaging
and Computer Graphics Theory and Applications - Volume 5: VISAPP, 202-213, 2022,
DOI: 10.5220/0010865000003124.
"""

# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

import logging
from pathlib import Path
from typing import Sequence

import albumentations as A
from pandas import DataFrame

from anomalib.data.base import AnomalibDataModule, AnomalibDepthDataset
from anomalib.data.task_type import TaskType
from anomalib.data.utils import (
DownloadInfo,
InputNormalizationMethod,
Split,
TestSplitMode,
ValSplitMode,
download_and_extract,
get_transforms,
)

logger = logging.getLogger(__name__)


IMG_EXTENSIONS = [".png", ".PNG", ".tiff"]

DOWNLOAD_INFO = DownloadInfo(
name="mvtec_3d",
url="https://www.mydrive.ch/shares/45920/dd1eb345346df066c63b5c95676b961b/download/428824485-1643285832"
"/mvtec_3d_anomaly_detection.tar.xz",
hash="d8bb2800fbf3ac88e798da6ae10dc819",
)


def make_mvtec_3d_dataset(
root: str | Path, split: str | Split | None = None, extensions: Sequence[str] | None = None
) -> DataFrame:
"""Create MVTec 3D-AD samples by parsing the MVTec AD data file structure.
The files are expected to follow the structure:
path/to/dataset/split/category/image_filename.png
path/to/dataset/ground_truth/category/mask_filename.png
This function creates a dataframe to store the parsed information based on the following format:
|---|---------------|-------|---------|---------------|---------------------------------------|-------------|
| | path | split | label | image_path | mask_path | label_index |
|---|---------------|-------|---------|---------------|---------------------------------------|-------------|
| 0 | datasets/name | test | defect | filename.png | ground_truth/defect/filename_mask.png | 1 |
|---|---------------|-------|---------|---------------|---------------------------------------|-------------|
Args:
path (Path): Path to dataset
split (str | Split | None, 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.1.
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.
MVTec AD dataset does not contain a validation set. Those wanting to create a validation set
could set this flag to ``True``.
Examples:
The following example shows how to get training samples from MVTec 3D-AD bagel category:
>>> root = Path('./MVTec3D')
>>> category = 'bagel'
>>> path = root / category
>>> path
PosixPath('MVTec3D/bagel')
>>> samples = make_mvtec_3d_dataset(path, split='train', split_ratio=0.1, seed=0)
>>> samples.head()
path split label image_path mask_path
0 MVTec3D/bagel train good MVTec3D/bagel/train/good/rgb/105.png MVTec3D/bagel/ground_truth/good/gt/105.png
1 MVTec3D/bagel train good MVTec3D/bagel/train/good/rgb/017.png MVTec3D/bagel/ground_truth/good/gt/017.png
2 MVTec3D/bagel train good MVTec3D/bagel/train/good/rgb/137.png MVTec3D/bagel/ground_truth/good/gt/137.png
3 MVTec3D/bagel train good MVTec3D/bagel/train/good/rgb/152.png MVTec3D/bagel/ground_truth/good/gt/152.png
4 MVTec3D/bagel train good MVTec3D/bagel/train/good/rgb/109.png MVTec3D/bagel/ground_truth/good/gt/109.png
depth_path label_index
MVTec3D/bagel/ground_truth/good/xyz/105.tiff 0
MVTec3D/bagel/ground_truth/good/xyz/017.tiff 0
MVTec3D/bagel/ground_truth/good/xyz/137.tiff 0
MVTec3D/bagel/ground_truth/good/xyz/152.tiff 0
MVTec3D/bagel/ground_truth/good/xyz/109.tiff 0
Returns:
DataFrame: an output dataframe containing the samples of the dataset.
"""
if extensions is None:
extensions = IMG_EXTENSIONS

root = Path(root)
samples_list = [(str(root),) + f.parts[-4:] for f in root.glob(r"**/*") if f.suffix in extensions]
if not samples_list:
raise RuntimeError(f"Found 0 images in {root}")

samples = DataFrame(samples_list, columns=["path", "split", "label", "type", "file_name"])

# Modify image_path column by converting to absolute path
samples.loc[(samples.type == "rgb"), "image_path"] = (
samples.path + "/" + samples.split + "/" + samples.label + "/" + "rgb/" + samples.file_name
)
samples.loc[(samples.type == "rgb"), "depth_path"] = (
samples.path
+ "/"
+ samples.split
+ "/"
+ samples.label
+ "/"
+ "xyz/"
+ samples.file_name.str.split(".").str[0]
+ ".tiff"
)

# Create label index for normal (0) and anomalous (1) images.
samples.loc[(samples.label == "good"), "label_index"] = 0
samples.loc[(samples.label != "good"), "label_index"] = 1
samples.label_index = samples.label_index.astype(int)

# separate masks from samples
mask_samples = samples.loc[((samples.split == "test") & (samples.type == "rgb"))].sort_values(
by="image_path", ignore_index=True
)
samples = samples.sort_values(by="image_path", ignore_index=True)

# assign mask paths to all test images
samples.loc[((samples.split == "test") & (samples.type == "rgb")), "mask_path"] = (
mask_samples.path + "/" + samples.split + "/" + samples.label + "/" + "gt/" + samples.file_name
)
samples.dropna(subset=["image_path"], inplace=True)
samples = samples.astype({"image_path": "str", "mask_path": "str", "depth_path": "str"})

# assert that the right mask files are associated with the right test images
assert (
samples.loc[samples.label_index == 1]
.apply(lambda x: Path(x.image_path).stem in Path(x.mask_path).stem, axis=1)
.all()
), "Mismatch between anomalous images and ground truth masks. Make sure the mask files in 'ground_truth' \
folder follow the same naming convention as the anomalous images in the dataset (e.g. image: '000.png', \
mask: '000.png' or '000_mask.png')."

# assert that the right depth image files are associated with the right test images
assert (
samples.loc[samples.label_index == 1]
.apply(lambda x: Path(x.image_path).stem in Path(x.depth_path).stem, axis=1)
.all()
), "Mismatch between anomalous images and depth images. Make sure the mask files in 'xyz' \
folder follow the same naming convention as the anomalous images in the dataset (e.g. image: '000.png', \
depth: '000.tiff')."

if split:
samples = samples[samples.split == split].reset_index(drop=True)

return samples


class MVTec3DDataset(AnomalibDepthDataset):
"""MVTec 3D dataset class.
Args:
task (TaskType): Task type, ``classification``, ``detection`` or ``segmentation``
transform (A.Compose): Albumentations Compose object describing the transforms that are applied to the inputs.
split (str | Split | None): Split of the dataset, usually Split.TRAIN or Split.TEST
root (Path | str): Path to the root of the dataset
category (str): Sub-category of the dataset, e.g. 'bagel'
"""

def __init__(
self,
task: TaskType,
transform: A.Compose,
root: Path | str,
category: str,
split: str | Split | None = None,
) -> None:
super().__init__(task=task, transform=transform)

self.root_category = Path(root) / Path(category)
self.split = split

def _setup(self) -> None:
self.samples = make_mvtec_3d_dataset(self.root_category, split=self.split, extensions=IMG_EXTENSIONS)


class MVTec3D(AnomalibDataModule):
"""MVTec Datamodule.
Args:
root (Path | str): Path to the root of the dataset
category (str): Category of the MVTec dataset (e.g. "bottle" or "cable").
image_size (int | tuple[int, int] | None, optional): Size of the input image.
Defaults to None.
center_crop (int | tuple[int, int] | None, optional): When provided, the images will be center-cropped
to the provided dimensions.
normalize (bool): When True, the images will be normalized to the ImageNet statistics.
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'
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.
test_split_ratio (float): Fraction of images from the train set that will be reserved for testing.
val_split_mode (ValSplitMode): Setting that determines how the validation subset is obtained.
val_split_ratio (float): Fraction of train or test images that will be reserved for validation.
seed (int | None, optional): Seed which may be set to a fixed value for reproducibility.
"""

def __init__(
self,
root: Path | str,
category: str,
image_size: int | tuple[int, int] | None = None,
center_crop: int | tuple[int, int] | None = None,
normalization: str | InputNormalizationMethod = InputNormalizationMethod.IMAGENET,
train_batch_size: int = 32,
eval_batch_size: int = 32,
num_workers: int = 8,
task: TaskType = TaskType.SEGMENTATION,
transform_config_train: str | A.Compose | None = None,
transform_config_eval: str | A.Compose | None = None,
test_split_mode: TestSplitMode = TestSplitMode.FROM_DIR,
test_split_ratio: float = 0.2,
val_split_mode: ValSplitMode = ValSplitMode.SAME_AS_TEST,
val_split_ratio: float = 0.5,
seed: int | None = None,
) -> None:
super().__init__(
train_batch_size=train_batch_size,
eval_batch_size=eval_batch_size,
num_workers=num_workers,
test_split_mode=test_split_mode,
test_split_ratio=test_split_ratio,
val_split_mode=val_split_mode,
val_split_ratio=val_split_ratio,
seed=seed,
)

self.root = Path(root)
self.category = Path(category)

transform_train = get_transforms(
config=transform_config_train,
image_size=image_size,
center_crop=center_crop,
normalization=InputNormalizationMethod(normalization),
)
transform_eval = get_transforms(
config=transform_config_eval,
image_size=image_size,
center_crop=center_crop,
normalization=InputNormalizationMethod(normalization),
)

self.train_data = MVTec3DDataset(
task=task, transform=transform_train, split=Split.TRAIN, root=root, category=category
)
self.test_data = MVTec3DDataset(
task=task, transform=transform_eval, split=Split.TEST, root=root, category=category
)

def prepare_data(self) -> None:
"""Download the dataset if not available."""
if (self.root / self.category).is_dir():
logger.info("Found the dataset.")
else:
download_and_extract(self.root, DOWNLOAD_INFO)
6 changes: 6 additions & 0 deletions src/anomalib/data/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -11,8 +11,10 @@
generate_output_image_filename,
get_image_filenames,
get_image_height_and_width,
read_depth_image,
read_image,
)
from .path import _check_and_convert_path, _prepare_files_labels, _resolve_path
from .split import (
Split,
TestSplitMode,
@@ -29,6 +31,7 @@
"get_image_height_and_width",
"random_2d_perlin",
"read_image",
"read_depth_image",
"random_split",
"split_by_label",
"concatenate_datasets",
@@ -43,4 +46,7 @@
"InputNormalizationMethod",
"download_and_extract",
"DownloadInfo",
"_check_and_convert_path",
"_prepare_files_labels",
"_resolve_path",
]
19 changes: 19 additions & 0 deletions src/anomalib/data/utils/image.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

import cv2
import numpy as np
import tifffile as tiff
import torch.nn.functional as F
from torch import Tensor
from torchvision.datasets.folder import IMG_EXTENSIONS
@@ -206,6 +207,24 @@ def read_image(path: str | Path, image_size: int | tuple[int, int] | None = None
return image


def read_depth_image(path: str | Path) -> np.ndarray:
"""Read tiff depth image from disk.
Args:
path (str, Path): path to the image file
Example:
>>> image = read_depth_image("test_image.tiff")
Returns:
image as numpy array
"""
path = path if isinstance(path, str) else str(path)
image = tiff.imread(path)

return image


def pad_nextpow2(batch: Tensor) -> Tensor:
"""Compute required padding from input size and return padded images.
78 changes: 78 additions & 0 deletions src/anomalib/data/utils/path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Path Utils."""

# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from pathlib import Path

from torchvision.datasets.folder import IMG_EXTENSIONS


def _check_and_convert_path(path: str | Path) -> Path:
"""Check an input path, and convert to Pathlib object.
Args:
path (str | Path): Input path.
Returns:
Path: Output path converted to pathlib object.
"""
if not isinstance(path, Path):
path = Path(path)
return path


def _prepare_files_labels(
path: str | Path, path_type: str, extensions: tuple[str, ...] | None = None
) -> tuple[list, list]:
"""Return a list of filenames and list corresponding labels.
Args:
path (str | Path): Path to the directory containing images.
path_type (str): Type of images in the provided path ("normal", "abnormal", "normal_test")
extensions (tuple[str, ...] | None, optional): Type of the image extensions to read from the
directory.
Returns:
List, List: Filenames of the images provided in the paths, labels of the images provided in the paths
"""
path = _check_and_convert_path(path)
if extensions is None:
extensions = IMG_EXTENSIONS

if isinstance(extensions, str):
extensions = (extensions,)

filenames = [f for f in path.glob(r"**/*") if f.suffix in extensions and not f.is_dir()]
if not filenames:
raise RuntimeError(f"Found 0 {path_type} images in {path}")

labels = [path_type] * len(filenames)

return filenames, labels


def _resolve_path(folder: str | Path, root: str | Path | None = None) -> Path:
"""Combines root and folder and returns the absolute path.
This allows users to pass either a root directory and relative paths, or absolute paths to each of the
image sources. This function makes sure that the samples dataframe always contains absolute paths.
Args:
folder (str | Path | None): Folder location containing image or mask data.
root (str | Path | None): Root directory for the dataset.
"""
folder = Path(folder)
if folder.is_absolute():
# path is absolute; return unmodified
path = folder
# path is relative.
elif root is None:
# no root provided; return absolute path
path = folder.resolve()
else:
# root provided; prepend root and return absolute path
path = (Path(root) / folder).resolve()
return path
2 changes: 1 addition & 1 deletion src/anomalib/data/utils/transform.py
Original file line number Diff line number Diff line change
@@ -124,6 +124,6 @@ def get_transforms(
if to_tensor:
transforms_list.append(ToTensorV2())

transforms = A.Compose(transforms_list)
transforms = A.Compose(transforms_list, additional_targets={"image": "image", "depth_image": "image"})

return transforms
2 changes: 2 additions & 0 deletions src/anomalib/post_processing/visualizer.py
Original file line number Diff line number Diff line change
@@ -161,6 +161,8 @@ def _visualize_full(self, image_result: ImageResult) -> np.ndarray:
visualization.add_image(image=image_result.segmentations, title="Segmentation Result")
elif self.task == TaskType.CLASSIFICATION:
visualization.add_image(image_result.image, title="Image")
if hasattr(image_result, "heat_map"):
visualization.add_image(image_result.heat_map, "Predicted Heat Map")
if image_result.pred_label:
image_classified = add_anomalous_label(image_result.image, image_result.pred_score)
else:
4 changes: 2 additions & 2 deletions tests/pre_merge/datasets/test_datamodule.py
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ def make_folder_data_module(
image_size=(256, 256),
train_batch_size=batch_size,
eval_batch_size=batch_size,
num_workers=8,
num_workers=0,
task=task,
test_split_mode=test_split_mode,
val_split_mode=val_split_mode,
@@ -190,7 +190,7 @@ def data_sample():
image_size=(256, 256),
train_batch_size=1,
eval_batch_size=1,
num_workers=8,
num_workers=0,
task="classification",
val_split_mode="from_test",
)
You are viewing a condensed version of this merge commit. You can view the full changes here.