-
Notifications
You must be signed in to change notification settings - Fork 2
/
vae_test.py
355 lines (266 loc) · 11 KB
/
vae_test.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import os
import time
import argparse
import numpy as np
from tqdm import tqdm
from skimage.metrics import structural_similarity
import torch
# from vqvae import VQVAE
from scipy.ndimage import label
from scipy.ndimage.morphology import binary_dilation
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
from PIL import Image
import torch.nn.functional as F
from utils import (get_train_dataloader,
get_test_dataloader,
load_model_parameters,
load_vqvae,
parse_args
)
def ssim(a, b, win_size):
"Structural di-SIMilarity: SSIM"
a = a.detach().cpu().permute(1, 2, 0).numpy()
b = b.detach().cpu().permute(1, 2, 0).numpy()
# print(a)
# b = gaussian_filter(b, sigma=2)
try:
score, full = structural_similarity(a, b, #multichannel=True,
channel_axis=2, full=True, win_size=win_size,data_range=1.0)
except ValueError: # different version of scikit img
score, full = structural_similarity(a, b, multichannel=True,
channel_axis=2, full=True, win_size=win_size,data_range=1.0)
#return 1 - score, np.median(1 - full, axis=2) # Return disim = (1 - sim)
return 1 - score, np.product((1 - full), axis=2)
def get_error_pixel_wise(model, x, loss="rec_loss"):
x_rec, _ = model(x)
return x_rec
def create_segmentation(amaps, amaps2, rec_maps, dataset):
if isinstance(amaps, torch.Tensor):
amaps = amaps.detach().cpu().numpy()
torch.set_printoptions(threshold=10_000)
mask = (amaps>= 0.01).astype(np.int8) #NOTE 0.59 if M=512 !!!
print(np.amax(mask))
mask2 = ((amaps2 >= 0.01)).astype(np.int8) #0.01 for VQVAE
mask2 = binary_dilation(mask2, iterations=4).astype(np.uint8)
## get intersection
L, _ = label(mask2)
lbls_interest = L[(mask == 1)]
lbls_interest = lbls_interest[lbls_interest != 0]
mask_ = mask.copy()
mask = (np.isin(L, lbls_interest)).astype(np.int8)
print(np.amax(mask))
return mask, mask2, mask_, L
def test(args):
''' livestock testing pipeline '''
device = torch.device(
"cuda" if torch.cuda.is_available() and not args.force_cpu else "cpu"
)
print("Pytorch device:", device)
seed = 0
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
np.random.seed(seed)
checkpoints_dir ="./torch_checkpoints"
if not os.path.isdir(checkpoints_dir):
os.mkdir(checkpoints_dir)
checkpoints_saved_dir ="./torch_checkpoints_saved"
predictions_dir ="./" + args.dataset + "_predictions"
if not os.path.isdir(predictions_dir):
os.mkdir(predictions_dir)
# Load dataset
train_dataloader, train_dataset = get_train_dataloader(
args,
fake_dataset_size=256,
)
# NOTE force test batch size to be 1
args.batch_size_test = 1
# fake_dataset_size=None leads a test on all the test dataset
test_dataloader, test_dataset = get_test_dataloader(
args,
fake_dataset_size=None
)
# Load model
model = load_vqvae(args)
model.to(device)
try:
file_name = f"{args.exp}_{args.params_id}.pth"
model = load_model_parameters(model, file_name, checkpoints_dir,
checkpoints_saved_dir, device)
except FileNotFoundError:
raise RuntimeError("The model checkpoint does not exist !")
dissimilarity_func = ssim
classes = {}
model.eval()
aucs = []
aucs_sm = []
aucs_mad_sm = []
pbar = tqdm(test_dataloader)
i=0
for imgs, gt in pbar:
imgs = imgs.to(device)
i+=1
if args.dataset in ["livestock","mvtec","miad"]:
# gt is a segmentation mask
gt_np = gt[0].permute(1, 2, 0).cpu().numpy()[..., 0]
gt_np = (gt_np - np.amin(gt_np)) / (np.amax(gt_np) - np.amin(gt_np))
with torch.no_grad():
x_rec = get_error_pixel_wise(model, imgs)
x_rec = model.mean_from_lambda(x_rec)
if args.dataset == "livestock" or args.dataset == "mvtec" or args.dataset == "miad":
score, ssim_map = dissimilarity_func(x_rec[0], imgs[0], 11)
ssim_map = ((ssim_map - np.amin(ssim_map)) / (np.amax(ssim_map)
- np.amin(ssim_map)))
# model.mu = F.interpolate(model.mu, size=(28, 28), mode='bilinear', align_corners=False)
mad = torch.mean(torch.abs(model.mu - torch.mean(model.mu,
dim=(0,1))), dim=(0,1))
mad = mad.detach().cpu().numpy()
mad = ((mad - np.amin(mad)) / (np.amax(mad)
- np.amin(mad)))
mad = mad.repeat(8, axis=0).repeat(8, axis=1)
# MAD metric
amaps = mad
# SM metric
amaps_sm = ssim_map
# MAD*SM metric
amaps_mad_sm = mad * ssim_map
amaps = ((amaps - np.amin(amaps)) / (np.amax(amaps)
- np.amin(amaps)))
amaps_sm = ((amaps_sm - np.amin(amaps_sm)) / (np.amax(amaps_sm)
- np.amin(amaps_sm)))
amaps_mad_sm = ((amaps_mad_sm- np.amin(amaps_mad_sm)) / (np.amax(amaps_mad_sm)
- np.amin(amaps_mad_sm)))
if args.dataset in ["livestock","mvtec", "miad"]:
preds = amaps.copy()
preds_sm = amaps_sm.copy()
preds_mad_sm = amaps_mad_sm.copy()
mask = np.zeros(gt_np.shape)
try:
auc = roc_auc_score(gt_np.astype(np.int8).flatten(), preds.flatten())
auc_sm = roc_auc_score(gt_np.astype(np.int8).flatten(), preds_sm.flatten())
auc_mad_sm = roc_auc_score(gt_np.astype(np.int8).flatten(), preds_mad_sm.flatten())
aucs.append(auc)
# print("auc_sm", auc_sm)
aucs_sm.append(auc_sm)
aucs_mad_sm.append(auc_mad_sm)
except ValueError:
pass
# ROCAUC will not be defined when one class only in y_true
m_aucs = np.mean(aucs)
m_aucs_sm = np.mean(aucs_sm)
m_aucs_mad_sm = np.mean(aucs_mad_sm)
print(m_aucs_mad_sm )
pbar.set_description(f"mean ROCAUC: {m_aucs_mad_sm :.3f}")
ori = imgs[0].permute(1, 2, 0).cpu().numpy()
gt = gt[0].permute(1, 2, 0).cpu().numpy()
rec = x_rec[0].detach().permute(1, 2, 0).cpu().numpy()
path_to_save = args.dataset + '_predictions/'
img_to_save = Image.fromarray((ori * 255).astype(np.uint8))
img_to_save = Image.fromarray((ori * 255).astype(np.uint8))
img_to_save.save(path_to_save + 'ori_'+ args.exp+'.png')
img_to_save = Image.fromarray((gt_np * 255).astype(np.uint8))
img_to_save.save(path_to_save + 'gt_'+ args.exp+'.png')
img_to_save = Image.fromarray((rec * 255).astype(np.uint8))
img_to_save.save(path_to_save + 'rec_'+ args.exp+'.png')
cm = plt.get_cmap('jet')
amaps_mad_sm = cm(amaps_mad_sm)
img_to_save = Image.fromarray((amaps_mad_sm[..., :3] * 255).astype(np.uint8))
img_to_save.save(path_to_save + 'final_amap_'+ args.exp+'.png')
m_auc = np.mean(aucs)
m_auc_sm = np.mean(aucs_sm, dtype=object)
m_auc_mad_sm = np.mean(aucs_mad_sm)
std_auc = np.std(aucs)
std_auc_sm = np.std(aucs_sm, dtype=object)
std_auc_mad_sm = np.std(aucs_mad_sm)
print("Mean auc on", args.category, args.defect, m_auc)
print("Mean auc_sm on", args.category, args.defect, m_auc_sm)
print("Mean auc_mad_sm on", args.category, args.defect, m_auc_mad_sm)
print("std auc on", args.category, args.defect, std_auc)
print("std auc_sm on", args.category, args.defect, std_auc_sm)
print("std auc_mad_sm on", args.category, args.defect, std_auc_mad_sm)
return m_auc, m_auc_sm, m_auc_mad_sm
def test_on_train(args, model):
''' livestock testing pipeline '''
device = torch.device(
"cuda" if torch.cuda.is_available() and not args.force_cpu else "cpu"
)
print("Pytorch device:", device)
seed = 0
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
np.random.seed(seed)
checkpoints_dir ="./torch_checkpoints"
if not os.path.isdir(checkpoints_dir):
os.mkdir(checkpoints_dir)
checkpoints_saved_dir ="./torch_checkpoints_saved"
predictions_dir ="./" + args.dataset + "_predictions"
if not os.path.isdir(predictions_dir):
os.mkdir(predictions_dir)
# Load dataset
train_dataloader, train_dataset = get_train_dataloader(
args,
fake_dataset_size=256,
)
# NOTE force test batch size to be 1
args.batch_size_test = 1
# fake_dataset_size=None leads a test on all the test dataset
test_dataloader, test_dataset = get_test_dataloader(
args,
fake_dataset_size=None
)
dissimilarity_func = ssim
classes = {}
model.eval()
aucs = []
pbar = tqdm(test_dataloader)
for imgs, gt in pbar:
imgs = imgs.to(device)
if args.dataset in ["livestock","mvtec","miad"]:
# gt is a segmentation mask
gt_np = gt[0].permute(1, 2, 0).cpu().numpy()[..., 0]
gt_np = (gt_np - np.amin(gt_np)) / (np.amax(gt_np) - np.amin(gt_np))
with torch.no_grad():
x_rec = get_error_pixel_wise(model, imgs)
x_rec = model.mean_from_lambda(x_rec)
if args.dataset == "livestock" or args.dataset == "mvtec" or args.dataset == "miad":
score, ssim_map = dissimilarity_func(x_rec[0], imgs[0], 11)
ssim_map = ((ssim_map - np.amin(ssim_map)) / (np.amax(ssim_map)
- np.amin(ssim_map)))
# model.mu = F.interpolate(model.mu, size=(28, 28), mode='bilinear', align_corners=False)
mad = torch.mean(torch.abs(model.mu - torch.mean(model.mu,
dim=(0,1))), dim=(0,1))
mad = mad.detach().cpu().numpy()
mad = ((mad - np.amin(mad)) / (np.amax(mad)
- np.amin(mad)))
mad = mad.repeat(8, axis=0).repeat(8, axis=1)
# MAD metric
# amaps = mad
# SM metric
#amaps = ssim_map
# MAD*SM metric
amaps = mad * ssim_map
amaps = ((amaps - np.amin(amaps)) / (np.amax(amaps)
- np.amin(amaps)))
if args.dataset in ["livestock","mvtec","miad"]:
preds = amaps.copy()
mask = np.zeros(gt_np.shape)
try:
auc = roc_auc_score(gt_np.astype(np.int8).flatten(), preds.flatten())
aucs.append(auc)
except ValueError:
pass
# ROCAUC will not be defined when one class only in y_true
m_aucs = np.mean(aucs)
print(m_aucs)
pbar.set_description(f"mean ROCAUC: {m_aucs:.3f}")
m_auc = np.mean(aucs)
print("Mean auc on", args.category, args.defect, m_auc)
return m_auc
if __name__ == "__main__":
args = parse_args()
if args.dataset == "livestock" or args.dataset == "mvtec" or args.dataset == "miad":
m_auc, m_auc_sm, m_auc_mad_sm = test(
args,
)