-
Notifications
You must be signed in to change notification settings - Fork 725
/
Copy pathtest_cli.py
226 lines (197 loc) · 7.21 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""Test CLI entrypoints on Padim model.
This just checks if one of the model works end-to-end. The rest of the models are checked using the API.
"""
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from pathlib import Path
import pytest
import torch
from anomalib.cli import AnomalibCLI
from anomalib.deploy import CompressionType, ExportType
class TestCLI:
"""Do sanity check on all models."""
def test_fit(self, dataset_path: Path, project_path: Path) -> None:
"""Test fit CLI.
Args:
dataset_path (Path): Root of the synthetic/original dataset.
project_path (Path): Path to temporary project folder.
"""
AnomalibCLI(
args=[
"fit",
*self._get_common_cli_args(dataset_path, project_path),
],
)
torch.cuda.empty_cache()
def test_test(self, dataset_path: Path, project_path: Path) -> None:
"""Test the test method of the CLI.
Args:
dataset_path (Path): Root of the synthetic/original dataset.
project_path (Path): Path to temporary project folder.
"""
AnomalibCLI(
args=[
"test",
*self._get_common_cli_args(dataset_path, project_path),
"--ckpt_path",
f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt",
],
)
torch.cuda.empty_cache()
def test_train(self, dataset_path: Path, project_path: Path) -> None:
"""Test the train method of the CLI.
Args:
dataset_path (Path): Root of the synthetic/original dataset.
project_path (Path): Path to temporary project folder.
"""
AnomalibCLI(
args=[
"train",
*self._get_common_cli_args(dataset_path, project_path),
"--ckpt_path",
f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt",
],
)
torch.cuda.empty_cache()
def test_validate(self, dataset_path: Path, project_path: Path) -> None:
"""Test the validate method of the CLI.
Args:
dataset_path (Path): Root of the synthetic/original dataset.
project_path (Path): Path to temporary project folder.
"""
AnomalibCLI(
args=[
"validate",
*self._get_common_cli_args(dataset_path, project_path),
"--ckpt_path",
f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt",
],
)
torch.cuda.empty_cache()
def test_predict_with_dataloader(self, dataset_path: Path, project_path: Path) -> None:
"""Test the predict method of the CLI.
This test uses the MVTec dataloader for predict test.
Args:
dataset_path (Path): Root of the synthetic/original dataset.
project_path (Path): Path to temporary project folder.
"""
# Test with MVTec Dataset
AnomalibCLI(
args=[
"predict",
*self._get_common_cli_args(
dataset_path,
project_path,
),
"--ckpt_path",
f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt",
],
)
torch.cuda.empty_cache()
def test_predict_with_image_folder(self, project_path: Path) -> None:
"""Test the predict method of the CLI.
This test uses the path to image folder for predict test.
Args:
project_path (Path): Path to temporary project folder.
"""
# Test with image path
AnomalibCLI(
args=[
"predict",
"--data",
f"{project_path}/datasets/visa_pytorch/dummy/test/bad",
*self._get_common_cli_args(
None,
project_path,
),
"--ckpt_path",
f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt",
],
)
torch.cuda.empty_cache()
def test_predict_with_image_path(self, project_path: Path) -> None:
"""Test the predict method of the CLI.
This test uses the path to image for predict test.
Args:
project_path (Path): Path to temporary project folder.
"""
# Test with image path
AnomalibCLI(
args=[
"predict",
"--data",
f"{project_path}/datasets/visa_pytorch/dummy/test/bad/000.jpg",
*self._get_common_cli_args(
None,
project_path,
),
"--ckpt_path",
f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt",
],
)
torch.cuda.empty_cache()
@pytest.mark.parametrize(
("export_type", "compression_type"),
[
(ExportType.TORCH, None),
(ExportType.ONNX, None),
(ExportType.OPENVINO, None),
(ExportType.OPENVINO, CompressionType.POT),
(ExportType.OPENVINO, CompressionType.NNCF),
],
)
def test_export(
self,
project_path: Path,
export_type: ExportType,
compression_type: CompressionType | None,
) -> None:
"""Test the export method of the CLI.
Args:
dataset_path (Path): Root of the synthetic/original dataset.
project_path (Path): Path to temporary project folder.
export_type (ExportType): Export type.
compression_type (CompressionType): Compression type (if any).
"""
args = [
"export",
"--export_type",
export_type,
*self._get_common_cli_args(None, project_path),
"--ckpt_path",
f"{project_path}/Padim/MVTec/dummy/v0/weights/lightning/model.ckpt",
]
if compression_type:
args += ["--compression_type", str(compression_type)]
AnomalibCLI(args=args)
@staticmethod
def _get_common_cli_args(dataset_path: Path | None, project_path: Path) -> list[str]:
"""Return common CLI args for all models.
Args:
dataset_path (Path): Path to the dataset. If the path is None, data arguments are not appended to the args.
project_path (Path): Path to the project folder.
model_name (str): Name of the model. Defaults to None.
"""
# We need to set the predict dataloader as MVTec and UCSDped do have have predict_dataloader attribute defined.
if dataset_path:
data_root = f"{dataset_path}/mvtec"
dataclass = "MVTec"
data_args = [
"--data",
dataclass,
"--data.root",
data_root,
"--data.category",
"dummy",
]
else:
data_args = []
return [
"--model",
"Padim",
*data_args,
"--default_root_dir",
str(project_path),
"--trainer.max_epochs",
"1",
]