forked from amazon-science/patchcore-inspection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_and_evaluate_patchcore.py
329 lines (261 loc) · 11.6 KB
/
load_and_evaluate_patchcore.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import contextlib
import gc
import logging
import os
import sys
import click
import numpy as np
import torch
sys.path.append('./src')
import patchcore.common
import patchcore.metrics
import patchcore.patchcore
import patchcore.sampler
import patchcore.utils
from utils import _save_segmentation_images
LOGGER = logging.getLogger(__name__)
_DATASETS = {"mvtec": ["patchcore.datasets.mvtec", "MVTecDataset"],
"monuseg": ["patchcore.datasets.mvtec", "MVTecDataset"]}
@click.group(chain=True)
@click.argument("results_path", type=str)
@click.option("--gpu", type=int, default=[0], multiple=True, show_default=True)
@click.option("--seed", type=int, default=0, show_default=True)
@click.option("--save_segmentation_images", is_flag=True)
@click.option("--save_anomaly_scores", is_flag=True)
def main(**kwargs):
pass
@main.result_callback()
def run(methods, results_path, gpu, seed, save_segmentation_images, save_anomaly_scores):
methods = {key: item for (key, item) in methods}
os.makedirs(results_path, exist_ok=True)
device = patchcore.utils.set_torch_device(gpu)
# Device context here is specifically set and used later
# because there was GPU memory-bleeding which I could only fix with
# context managers.
device_context = (
torch.cuda.device("cuda:{}".format(device.index))
if "cuda" in device.type.lower()
else contextlib.suppress()
)
result_collect = []
dataloader_iter, n_dataloaders = methods["get_dataloaders_iter"]
dataloader_iter = dataloader_iter(seed)
patchcore_iter, n_patchcores = methods["get_patchcore_iter"]
patchcore_iter = patchcore_iter(device)
if not (n_dataloaders == n_patchcores or n_patchcores == 1):
raise ValueError(
"Please ensure that #PatchCores == #Datasets or #PatchCores == 1!"
)
for dataloader_count, dataloaders in enumerate(dataloader_iter):
LOGGER.info(
"Evaluating dataset [{}] ({}/{})...".format(
dataloaders["testing"].name, dataloader_count + 1, n_dataloaders
)
)
patchcore.utils.fix_seeds(seed, device)
dataset_name = dataloaders["testing"].name
print("Test samples = ", len(dataloaders["testing"].dataset))
with device_context:
torch.cuda.empty_cache()
if dataloader_count < n_patchcores:
PatchCore_list = next(patchcore_iter)
aggregator = {"scores": [], "segmentations": []}
for i, PatchCore in enumerate(PatchCore_list):
torch.cuda.empty_cache()
LOGGER.info(
"Embedding test data with models ({}/{})".format(
i + 1, len(PatchCore_list)
)
)
scores, segmentations, labels_gt, masks_gt = PatchCore.predict(
dataloaders["testing"]
)
aggregator["scores"].append(scores)
aggregator["segmentations"].append(segmentations)
scores = np.array(aggregator["scores"])
min_scores = scores.min(axis=-1).reshape(-1, 1)
max_scores = scores.max(axis=-1).reshape(-1, 1)
scores = (scores - min_scores) / (max_scores - min_scores)
scores = np.mean(scores, axis=0)
segmentations = np.array(aggregator["segmentations"])
predicted_segmentation_maps = segmentations.any()
if predicted_segmentation_maps:
min_scores = (
segmentations.reshape(len(segmentations), -1)
.min(axis=-1)
.reshape(-1, 1, 1, 1)
)
max_scores = (
segmentations.reshape(len(segmentations), -1)
.max(axis=-1)
.reshape(-1, 1, 1, 1)
)
segmentations = (segmentations - min_scores) / (max_scores - min_scores)
segmentations = np.mean(segmentations, axis=0)
anomaly_labels = [
x[1] != "good" for x in dataloaders["testing"].dataset.data_to_iterate
]
# Save anomaly scores in json.
if save_anomaly_scores:
image_paths = [
x[2] for x in dataloaders["testing"].dataset.data_to_iterate
]
# Save normalized scores
patchcore.utils.save_anomaly_scores(
results_path,
image_paths,
scores,
)
# Un-normalized scores
scores_raw = np.array(aggregator["scores"]).flatten()
patchcore.utils.save_anomaly_scores(
results_path,
image_paths,
scores_raw,
out_file_name="scores_raw.json"
)
# Plot Example Images.
if save_segmentation_images:
_save_segmentation_images(results_path, dataloaders["testing"], segmentations, scores)
LOGGER.info("Computing evaluation metrics.")
results_dict = get_eval_metrics(scores, anomaly_labels, segmentations, masks_gt, dataset_name)
result_collect.append(results_dict)
for key, item in result_collect[-1].items():
if key != "dataset_name":
LOGGER.info("{0}: {1:3.3f}".format(key, item))
del PatchCore_list
gc.collect()
LOGGER.info("\n\n-----\n")
result_metric_names = list(result_collect[-1].keys())[1:]
result_dataset_names = [results["dataset_name"] for results in result_collect]
result_scores = [list(results.values())[1:] for results in result_collect]
patchcore.utils.compute_and_store_final_results(
results_path,
result_scores,
column_names=result_metric_names,
row_names=result_dataset_names,
)
# Hyperparameter optimization runs out of memory if not cleared.
torch.cuda.empty_cache()
return result_collect
def get_eval_metrics(scores, anomaly_labels, segmentations, masks_gt, dataset_name):
# Compute Image-level AUROC scores for all images.
auroc = patchcore.metrics.compute_imagewise_retrieval_metrics(
scores, anomaly_labels
)["auroc"]
predicted_segmentation_maps = segmentations.any()
# Compute PRO score & PW Auroc for all images
if len(masks_gt) > 0 and predicted_segmentation_maps:
pixel_scores = patchcore.metrics.compute_pixelwise_retrieval_metrics(
segmentations, masks_gt
)
full_pixel_auroc = pixel_scores["auroc"]
else:
full_pixel_auroc = np.nan
# Compute PRO score & PW Auroc only images with anomalies
if len(masks_gt) > 0 and predicted_segmentation_maps:
sel_idxs = []
for i in range(len(masks_gt)):
if np.sum(masks_gt[i]) > 0:
sel_idxs.append(i)
pixel_scores = patchcore.metrics.compute_pixelwise_retrieval_metrics(
[segmentations[i] for i in sel_idxs],
[masks_gt[i] for i in sel_idxs],
)
anomaly_pixel_auroc = pixel_scores["auroc"]
else:
anomaly_pixel_auroc = np.nan
results_dict = {
"dataset_name": dataset_name,
"instance_auroc": auroc,
"full_pixel_auroc": full_pixel_auroc,
"anomaly_pixel_auroc": anomaly_pixel_auroc,
}
return results_dict
@main.command("patch_core_loader")
# Pretraining-specific parameters.
@click.option("--patch_core_paths", "-p", type=str, multiple=True, default=[])
# NN on GPU.
@click.option("--faiss_on_gpu", is_flag=True)
@click.option("--faiss_num_workers", type=int, default=8)
def patch_core_loader(patch_core_paths, faiss_on_gpu, faiss_num_workers):
def get_patchcore_iter(device):
for patch_core_path in patch_core_paths:
loaded_patchcores = []
gc.collect()
n_patchcores = len(
[x for x in os.listdir(patch_core_path) if ".faiss" in x]
)
if n_patchcores == 1:
nn_method = patchcore.common.FaissNN(faiss_on_gpu, faiss_num_workers)
patchcore_instance = patchcore.patchcore.PatchCore(device)
patchcore_instance.load_from_path(
load_path=patch_core_path, device=device, nn_method=nn_method
)
loaded_patchcores.append(patchcore_instance)
else:
for i in range(n_patchcores):
nn_method = patchcore.common.FaissNN(
faiss_on_gpu, faiss_num_workers
)
patchcore_instance = patchcore.patchcore.PatchCore(device)
patchcore_instance.load_from_path(
load_path=patch_core_path,
device=device,
nn_method=nn_method,
prepend="Ensemble-{}-{}_".format(i + 1, n_patchcores),
)
loaded_patchcores.append(patchcore_instance)
yield loaded_patchcores
return ("get_patchcore_iter", [get_patchcore_iter, len(patch_core_paths)])
@main.command("dataset")
@click.argument("name", type=str)
@click.argument("data_path", type=click.Path(exists=True, file_okay=False))
@click.option("--subdatasets", "-d", multiple=True, type=str, required=True)
@click.option("--subsample", type=float, required=False)
@click.option("--batch_size", default=1, type=int, show_default=True)
@click.option("--num_workers", default=8, type=int, show_default=True)
@click.option("--resize", default=256, type=int, show_default=True)
@click.option("--imagesize", default=224, type=int, show_default=True)
@click.option("--augment", is_flag=True)
def dataset(
name, data_path, subdatasets, subsample, batch_size, resize, imagesize, num_workers, augment
):
dataset_info = _DATASETS[name]
dataset_library = __import__(dataset_info[0], fromlist=[dataset_info[1]])
if name == "monuseg":
def get_dataloaders_iter(seed):
from patchcore.datasets.monuseg import get_monuseg_dataloader
for subdataset in subdatasets:
test_dataloader = get_monuseg_dataloader(data_path, batch_size=batch_size, split=dataset_library.DatasetSplit.TEST.value, resize=resize, cropsize=imagesize, subsample=subsample)
test_dataloader.name = name
dataloader_dict = {"testing": test_dataloader}
yield dataloader_dict
else:
def get_dataloaders_iter(seed):
for subdataset in subdatasets:
test_dataset = dataset_library.__dict__[dataset_info[1]](
data_path,
classname=subdataset,
resize=resize,
imagesize=imagesize,
split=dataset_library.DatasetSplit.TEST,
seed=seed,
)
test_dataloader = torch.utils.data.DataLoader(
test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True,
)
test_dataloader.name = name
if subdataset is not None:
test_dataloader.name += "_" + subdataset
dataloader_dict = {"testing": test_dataloader}
yield dataloader_dict
return ("get_dataloaders_iter", [get_dataloaders_iter, len(subdatasets)])
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
LOGGER.info("Command line arguments: {}".format(" ".join(sys.argv)))
main()