Skip to content

anomaly

AnomalyDatasetArguments(train_samples, val_samples, test_samples) dataclass

Anomaly dataset arguments.

Parameters:

  • train_samples (int) –

    number of train samples

  • val_samples (tuple[int, int]) –

    number of validation samples (good, bad)

  • test_samples (tuple[int, int]) –

    number of test samples (good, bad)

_build_anomaly_dataset(tmp_path, dataset_arguments)

Generate anomaly dataset in the standard mvtec format.

Parameters:

Returns:

Source code in quadra/utils/tests/fixtures/dataset/anomaly.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def _build_anomaly_dataset(
    tmp_path: Path, dataset_arguments: AnomalyDatasetArguments
) -> tuple[str, AnomalyDatasetArguments]:
    """Generate anomaly dataset in the standard mvtec format.

    Args:
        tmp_path: path to temporary directory
        dataset_arguments: dataset arguments

    Returns:
        path to anomaly dataset
    """
    train_samples = dataset_arguments.train_samples
    val_samples = dataset_arguments.val_samples
    test_samples = dataset_arguments.test_samples

    anomaly_dataset_path = tmp_path / "anomaly_dataset"
    anomaly_dataset_path.mkdir()
    train_good_path = anomaly_dataset_path / "train" / "good"
    val_good_path = anomaly_dataset_path / "val" / "good"
    val_bad_path = anomaly_dataset_path / "val" / "bad"
    test_good_path = anomaly_dataset_path / "test" / "good"
    test_bad_path = anomaly_dataset_path / "test" / "bad"

    train_good_path.mkdir(parents=True)
    val_good_path.mkdir(parents=True)
    val_bad_path.mkdir(parents=True)
    test_good_path.mkdir(parents=True)
    test_bad_path.mkdir(parents=True)

    # Generate train good images
    for i in range(train_samples):
        image = _random_image()
        image_path = train_good_path / f"train_{i}.png"
        cv2.imwrite(str(image_path), image)

    # Generate val good images
    for i in range(val_samples[0]):
        image = _random_image()
        image_path = val_good_path / f"val_{i}.png"
        cv2.imwrite(str(image_path), image)
    # Generate val bad images
    for i in range(val_samples[1]):
        image = _random_image()
        image_path = val_bad_path / f"val_{i}.png"
        cv2.imwrite(str(image_path), image)

    # Generate test good images
    for i in range(test_samples[0]):
        image = _random_image()
        image_path = test_good_path / f"test_{i}.png"
        cv2.imwrite(str(image_path), image)
    # Generate test bad images
    for i in range(test_samples[1]):
        image = _random_image()
        image_path = test_bad_path / f"test_{i}.png"
        cv2.imwrite(str(image_path), image)

    return str(anomaly_dataset_path), dataset_arguments

anomaly_dataset(tmp_path, dataset_arguments)

Fixture used to dinamically generate anomaly dataset. By default images are random grayscales with size 10x10.

Parameters:

Returns:

Source code in quadra/utils/tests/fixtures/dataset/anomaly.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
@pytest.fixture
def anomaly_dataset(tmp_path: Path, dataset_arguments: AnomalyDatasetArguments) -> tuple[str, AnomalyDatasetArguments]:
    """Fixture used to dinamically generate anomaly dataset. By default images are random grayscales with size 10x10.

    Args:
        tmp_path: path to temporary directory
        dataset_arguments: dataset arguments

    Returns:
        path to anomaly dataset
    """
    yield _build_anomaly_dataset(tmp_path, dataset_arguments)
    if tmp_path.exists():
        shutil.rmtree(tmp_path)

base_anomaly_dataset(tmp_path, request)

Generate base anomaly dataset with the following parameters
  • train_samples: 10
  • val_samples: (10, 10)
  • test_samples: (10, 10).

Parameters:

  • tmp_path (Path) –

    Path to temporary directory

  • request (Any) –

    Pytest SubRequest object

Yields:

Source code in quadra/utils/tests/fixtures/dataset/anomaly.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@pytest.fixture(
    params=[AnomalyDatasetArguments(**{"train_samples": 10, "val_samples": (1, 1), "test_samples": (1, 1)})]
)
def base_anomaly_dataset(tmp_path: Path, request: Any) -> tuple[str, AnomalyDatasetArguments]:
    """Generate base anomaly dataset with the following parameters:
        - train_samples: 10
        - val_samples: (10, 10)
        - test_samples: (10, 10).

    Args:
        tmp_path: Path to temporary directory
        request: Pytest SubRequest object

    Yields:
        Path to anomaly dataset and dataset arguments
    """
    yield _build_anomaly_dataset(tmp_path, request.param)
    if tmp_path.exists():
        shutil.rmtree(tmp_path)