-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglass.py
More file actions
801 lines (675 loc) · 34.9 KB
/
glass.py
File metadata and controls
801 lines (675 loc) · 34.9 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
from loss import FocalLoss
from collections import OrderedDict
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from model import Discriminator, Projection, PatchMaker
from azureml.core import Workspace
import numpy as np
import pandas as pd
import torch.nn.functional as F
import logging
import os
import math
import torch
import tqdm
import common
import metrics
import cv2
import utils
import glob
import shutil
import mlflow
LOGGER = logging.getLogger(__name__)
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]
class TBWrapper:
def __init__(self, log_dir):
self.g_iter = 0
# self.logger = SummaryWriter(log_dir=log_dir)
self.logger = None
def step(self):
self.g_iter += 1
class GLASS(torch.nn.Module):
def __init__(self, device):
super(GLASS, self).__init__()
self.device = device
def load(
self,
backbone,
layers_to_extract_from,
device,
input_shape,
pretrain_embed_dimension,
target_embed_dimension,
patchsize=3,
patchstride=1,
meta_epochs=640,
eval_epochs=1,
dsc_layers=2,
dsc_hidden=1024,
dsc_margin=0.5,
train_backbone=False,
pre_proj=1,
mining=1,
noise=0.015,
radius=0.75,
p=0.5,
lr=0.0001,
svd=0,
step=20,
limit=392,
es_epoch=10,
tta=False,
**kwargs,
):
self.backbone = backbone.to(device)
self.layers_to_extract_from = layers_to_extract_from
self.input_shape = input_shape
self.device = device
self.es_epoch = es_epoch
self.tta = tta
self.forward_modules = torch.nn.ModuleDict({})
feature_aggregator = common.NetworkFeatureAggregator(
self.backbone, self.layers_to_extract_from, self.device, train_backbone
)
feature_dimensions = feature_aggregator.feature_dimensions(input_shape)
self.forward_modules["feature_aggregator"] = feature_aggregator
preprocessing = common.Preprocessing(feature_dimensions, pretrain_embed_dimension)
self.forward_modules["preprocessing"] = preprocessing
self.target_embed_dimension = target_embed_dimension
preadapt_aggregator = common.Aggregator(target_dim=target_embed_dimension)
preadapt_aggregator.to(self.device)
self.forward_modules["preadapt_aggregator"] = preadapt_aggregator
self.meta_epochs = meta_epochs
self.lr = lr
self.train_backbone = train_backbone
if self.train_backbone:
self.backbone_opt = torch.optim.AdamW(self.forward_modules["feature_aggregator"].backbone.parameters(), lr)
self.pre_proj = pre_proj
if self.pre_proj > 0:
self.pre_projection = Projection(self.target_embed_dimension, self.target_embed_dimension, pre_proj)
self.pre_projection.to(self.device)
self.proj_opt = torch.optim.Adam(self.pre_projection.parameters(), lr, weight_decay=1e-5)
self.eval_epochs = eval_epochs
self.dsc_layers = dsc_layers
self.dsc_hidden = dsc_hidden
self.discriminator = Discriminator(self.target_embed_dimension, n_layers=dsc_layers, hidden=dsc_hidden)
self.discriminator.to(self.device)
self.dsc_opt = torch.optim.AdamW(self.discriminator.parameters(), lr=lr * 2)
self.dsc_margin = dsc_margin
self.c = torch.tensor(0)
self.c_ = torch.tensor(0)
self.p = p
self.radius = radius
self.mining = mining
self.noise = noise
self.svd = svd
self.step = step
self.limit = limit
self.distribution = 0
self.focal_loss = FocalLoss()
self.patch_maker = PatchMaker(patchsize, stride=patchstride)
self.anomaly_segmentor = common.RescaleSegmentor(device=self.device, target_size=input_shape[-2:])
self.model_dir = ""
self.result_dir = ""
self.dataset_name = ""
self.logger = None
def set_model_dir(self, model_dir, dataset_name):
self.model_dir = model_dir
os.makedirs(self.model_dir, exist_ok=True)
self.ckpt_dir = os.path.join(self.model_dir, dataset_name)
os.makedirs(self.ckpt_dir, exist_ok=True)
self.tb_dir = os.path.join(self.ckpt_dir, "tb")
os.makedirs(self.tb_dir, exist_ok=True)
self.logger = TBWrapper(self.tb_dir)
def set_result_dir(self, result_dir):
self.result_dir = result_dir
def _embed(self, images, detach=True, provide_patch_shapes=False, evaluation=False):
"""Returns feature embeddings for images."""
if not evaluation and self.train_backbone:
self.forward_modules["feature_aggregator"].train()
features = self.forward_modules["feature_aggregator"](images, eval=evaluation)
else:
self.forward_modules["feature_aggregator"].eval()
with torch.no_grad():
features = self.forward_modules["feature_aggregator"](images)
features = [features[layer] for layer in self.layers_to_extract_from]
for i, feat in enumerate(features):
if len(feat.shape) == 3:
B, L, C = feat.shape
features[i] = feat.reshape(B, int(math.sqrt(L)), int(math.sqrt(L)), C).permute(0, 3, 1, 2)
features = [self.patch_maker.patchify(x, return_spatial_info=True) for x in features]
patch_shapes = [x[1] for x in features]
patch_features = [x[0] for x in features]
ref_num_patches = patch_shapes[0]
for i in range(1, len(patch_features)):
_features = patch_features[i]
patch_dims = patch_shapes[i]
_features = _features.reshape(
_features.shape[0], patch_dims[0], patch_dims[1], *_features.shape[2:]
)
_features = _features.permute(0, 3, 4, 5, 1, 2)
perm_base_shape = _features.shape
_features = _features.reshape(-1, *_features.shape[-2:])
_features = F.interpolate(
_features.unsqueeze(1),
size=(ref_num_patches[0], ref_num_patches[1]),
mode="bilinear",
align_corners=False,
)
_features = _features.squeeze(1)
_features = _features.reshape(
*perm_base_shape[:-2], ref_num_patches[0], ref_num_patches[1]
)
_features = _features.permute(0, 4, 5, 1, 2, 3)
_features = _features.reshape(len(_features), -1, *_features.shape[-3:])
patch_features[i] = _features
patch_features = [x.reshape(-1, *x.shape[-3:]) for x in patch_features]
patch_features = self.forward_modules["preprocessing"](patch_features)
patch_features = self.forward_modules["preadapt_aggregator"](patch_features)
return patch_features, patch_shapes
def trainer(self, training_data, val_data, name):
state_dict = {}
ckpt_path = glob.glob(self.ckpt_dir + '/ckpt_best*')
ckpt_path_save = os.path.join(self.ckpt_dir, "ckpt.pth")
if len(ckpt_path) != 0:
# LOGGER.info("Start testing, ckpt file found!")
# return 0., 0., 0., 0., 0., -1.
LOGGER.info("Ckpt file found, retrain!")
def update_state_dict():
state_dict["discriminator"] = OrderedDict({
k: v.detach().cpu()
for k, v in self.discriminator.state_dict().items()})
if self.pre_proj > 0:
state_dict["pre_projection"] = OrderedDict({
k: v.detach().cpu()
for k, v in self.pre_projection.state_dict().items()})
self.distribution = training_data.dataset.distribution
xlsx_path = './datasets/excel/' + name.split('_')[0] + '_distribution.xlsx'
try:
if self.distribution == 1: # rejudge by image-level spectrogram analysis
self.distribution = 1
self.svd = 1
elif self.distribution == 2: # manifold
self.distribution = 0
self.svd = 0
elif self.distribution == 3: # hypersphere
self.distribution = 0
self.svd = 1
elif self.distribution == 4: # opposite choose by file
self.distribution = 0
df = pd.read_excel(xlsx_path)
self.svd = 1 - df.loc[df['Class'] == name, 'Distribution'].values[0]
else: # choose by file
self.distribution = 0
df = pd.read_excel(xlsx_path)
self.svd = df.loc[df['Class'] == name, 'Distribution'].values[0]
except:
self.distribution = 1
self.svd = 1
# judge by image-level spectrogram analysis
if self.distribution == 1:
self.forward_modules.eval()
with torch.no_grad():
for i, data in enumerate(training_data):
img = data["image"]
img = img.to(torch.float).to(self.device)
batch_mean = torch.mean(img, dim=0)
if i == 0:
self.c = batch_mean
else:
self.c += batch_mean
self.c /= len(training_data)
avg_img = utils.torch_format_2_numpy_img(self.c.detach().cpu().numpy())
self.svd = utils.distribution_judge(avg_img, name)
os.makedirs(os.path.join(self.result_dir, 'judge','avg', str(self.svd)), exist_ok=True)
cv2.imwrite(os.path.join(self.result_dir, 'judge','avg', str(self.svd), f'{name}.png'), avg_img)
return self.svd
pbar_str1 = ""
best_record = None
best_score = -1
epoch_counter = 0
best_state = None
pbar = tqdm.tqdm(range(self.meta_epochs), unit='epoch')
for i_epoch in pbar:
self.forward_modules.eval()
with torch.no_grad(): # compute center
for i, data in enumerate(training_data):
img = data["image"]
img = img.to(torch.float).to(self.device)
if self.pre_proj > 0:
outputs = self.pre_projection(self._embed(img, evaluation=False)[0])
outputs = outputs[0] if len(outputs) == 2 else outputs
else:
outputs = self._embed(img, evaluation=False)[0]
outputs = outputs[0] if len(outputs) == 2 else outputs
outputs = outputs.reshape(img.shape[0], -1, outputs.shape[-1])
batch_mean = torch.mean(outputs, dim=0)
if i == 0:
self.c = batch_mean
else:
self.c += batch_mean
self.c /= len(training_data)
pbar_str, pt, pf, avg_loss = self._train_discriminator(training_data, i_epoch, pbar, pbar_str1)
if mlflow.active_run() is not None:
mlflow.log_metric("discriminator_avg_loss", avg_loss, step=i_epoch)
update_state_dict()
if (i_epoch + 1) % self.eval_epochs == 0:
images, scores, segmentations, labels_gt, masks_gt = self.predict(val_data)
image_auroc, image_ap, img_threshold, img_f1_max = self._evaluate(images, scores, segmentations,
labels_gt, masks_gt, name)
if mlflow.active_run() is not None:
mlflow.log_metric("img_auroc", image_auroc, step=i_epoch)
mlflow.log_metric("img_threshold", img_threshold, step=i_epoch)
mlflow.log_metric("img_f1_max", img_f1_max, step=i_epoch)
eval_path = os.path.join(self.result_dir, 'eval', name) + '/'
train_path = os.path.join(self.result_dir, 'training', name) + '/'
if best_record is None:
best_record = [image_auroc, image_ap, img_f1_max, i_epoch]
ckpt_path_best = os.path.join(self.ckpt_dir, "ckpt_best_{}.pth".format(i_epoch))
torch.save(state_dict, ckpt_path_best)
shutil.rmtree(eval_path, ignore_errors=True)
shutil.copytree(train_path, eval_path)
# elif image_auroc + pixel_auroc > best_record[0] + best_record[2]:
elif img_f1_max > best_record[2]:
best_record = [image_auroc, image_ap, img_f1_max, i_epoch]
os.remove(ckpt_path_best)
ckpt_path_best = os.path.join(self.ckpt_dir, "ckpt_best_{}.pth".format(i_epoch))
torch.save(state_dict, ckpt_path_best)
shutil.rmtree(eval_path, ignore_errors=True)
shutil.copytree(train_path, eval_path)
pbar_str1 = f" IAUC:{round(image_auroc * 100, 2)}({round(best_record[0] * 100, 2)})" \
f" IF1-max:{round(img_f1_max * 100, 2)}({round(best_record[2] * 100, 2)})" \
f" E:{i_epoch}({best_record[-1]})"
pbar_str += pbar_str1
pbar.set_description_str(pbar_str)
# current_score = image_auroc*1 + pixel_auroc * 0
current_score = image_auroc * 0.1 + img_f1_max * 0.9
if mlflow.active_run() is not None:
mlflow.log_metric("early_stop_score", current_score, step=i_epoch)
if current_score > best_score:
best_score = current_score
epoch_counter = 0
else:
epoch_counter += 1
if epoch_counter > self.es_epoch:
LOGGER.info(f"Early stopping triggered at epoch {i_epoch}")
break
torch.save(state_dict, ckpt_path_save)
return best_record
def _train_discriminator(self, input_data, cur_epoch, pbar, pbar_str1):
self.forward_modules.eval()
if self.pre_proj > 0:
self.pre_projection.train()
self.discriminator.train()
all_loss, all_p_true, all_p_fake, all_r_t, all_r_g, all_r_f = [], [], [], [], [], []
sample_num = 0
for i_iter, data_item in enumerate(input_data):
self.dsc_opt.zero_grad()
if self.pre_proj > 0:
self.proj_opt.zero_grad()
aug = data_item["aug"]
aug = aug.to(torch.float).to(self.device)
img = data_item["image"]
img = img.to(torch.float).to(self.device)
if self.pre_proj > 0:
fake_feats = self.pre_projection(self._embed(aug, evaluation=False)[0])
fake_feats = fake_feats[0] if len(fake_feats) == 2 else fake_feats
true_feats = self.pre_projection(self._embed(img, evaluation=False)[0])
true_feats = true_feats[0] if len(true_feats) == 2 else true_feats
else:
fake_feats = self._embed(aug, evaluation=False)[0]
fake_feats.requires_grad = True
true_feats = self._embed(img, evaluation=False)[0]
true_feats.requires_grad = True
mask_s_gt = data_item["mask_s"].reshape(-1, 1).to(self.device) # feature
noise = torch.normal(0, self.noise, true_feats.shape).to(self.device)
gaus_feats = true_feats + noise
center = self.c.repeat(img.shape[0], 1, 1)
center = center.reshape(-1, center.shape[-1])
true_points = torch.concat([fake_feats[mask_s_gt[:, 0] == 0], true_feats], dim=0)
c_t_points = torch.concat([center[mask_s_gt[:, 0] == 0], center], dim=0)
dist_t = torch.norm(true_points - c_t_points, dim=1)
r_t = torch.tensor([torch.quantile(dist_t, q=self.radius)]).to(self.device)
for step in range(self.step + 1):
scores = self.discriminator(torch.cat([true_feats, gaus_feats]))
true_scores = scores[:len(true_feats)]
gaus_scores = scores[len(true_feats):]
true_loss = torch.nn.BCELoss()(true_scores, torch.zeros_like(true_scores))
gaus_loss = torch.nn.BCELoss()(gaus_scores, torch.ones_like(gaus_scores))
bce_loss = true_loss + gaus_loss
if step == self.step:
break
elif self.mining == 0:
dist_g = torch.norm(gaus_feats - center, dim=1)
r_g = torch.tensor([torch.quantile(dist_g, q=self.radius)]).to(self.device)
break
grad = torch.autograd.grad(gaus_loss, [gaus_feats])[0]
grad_norm = torch.norm(grad, dim=1)
grad_norm = grad_norm.view(-1, 1)
grad_normalized = grad / (grad_norm + 1e-10)
with torch.no_grad():
gaus_feats.add_(0.001 * grad_normalized)
if (step + 1) % 5 == 0:
dist_g = torch.norm(gaus_feats - center, dim=1)
r_g = torch.tensor([torch.quantile(dist_g, q=self.radius)]).to(self.device)
proj_feats = center if self.svd == 1 else true_feats
r = r_t if self.svd == 1 else 0.5
h = gaus_feats - proj_feats
h_norm = dist_g if self.svd == 1 else torch.norm(h, dim=1)
alpha = torch.clamp(h_norm, r, 2 * r)
proj = (alpha / (h_norm + 1e-10)).view(-1, 1)
h = proj * h
gaus_feats = proj_feats + h
fake_points = fake_feats[mask_s_gt[:, 0] == 1]
true_points = true_feats[mask_s_gt[:, 0] == 1]
c_f_points = center[mask_s_gt[:, 0] == 1]
dist_f = torch.norm(fake_points - c_f_points, dim=1)
r_f = torch.tensor([torch.quantile(dist_f, q=self.radius)]).to(self.device)
proj_feats = c_f_points if self.svd == 1 else true_points
r = r_t if self.svd == 1 else 1
if self.svd == 1:
h = fake_points - proj_feats
h_norm = dist_f if self.svd == 1 else torch.norm(h, dim=1)
alpha = torch.clamp(h_norm, 2 * r, 4 * r)
proj = (alpha / (h_norm + 1e-10)).view(-1, 1)
h = proj * h
fake_points = proj_feats + h
fake_feats[mask_s_gt[:, 0] == 1] = fake_points
fake_scores = self.discriminator(fake_feats)
if self.p > 0:
fake_dist = (fake_scores - mask_s_gt) ** 2
d_hard = torch.quantile(fake_dist, q=self.p)
fake_scores_ = fake_scores[fake_dist >= d_hard].unsqueeze(1)
mask_ = mask_s_gt[fake_dist >= d_hard].unsqueeze(1)
else:
fake_scores_ = fake_scores
mask_ = mask_s_gt
output = torch.cat([1 - fake_scores_, fake_scores_], dim=1)
focal_loss = self.focal_loss(output, mask_)
loss = bce_loss + focal_loss
loss.backward()
if self.pre_proj > 0:
self.proj_opt.step()
if self.train_backbone:
self.backbone_opt.step()
self.dsc_opt.step()
pix_true = torch.concat([fake_scores.detach() * (1 - mask_s_gt), true_scores.detach()])
pix_fake = torch.concat([fake_scores.detach() * mask_s_gt, gaus_scores.detach()])
p_true = ((pix_true < self.dsc_margin).sum() - (pix_true == 0).sum()) / ((mask_s_gt == 0).sum() + true_scores.shape[0])
p_fake = (pix_fake >= self.dsc_margin).sum() / ((mask_s_gt == 1).sum() + gaus_scores.shape[0])
if mlflow.active_run() is not None:
mlflow.log_metric("discriminator_p_true", p_true, step=self.logger.g_iter)
mlflow.log_metric("discriminator_p_fake", p_fake, step=self.logger.g_iter)
mlflow.log_metric("discriminator_r_t", r_t, step=self.logger.g_iter)
mlflow.log_metric("discriminator_r_g", r_g, step=self.logger.g_iter)
mlflow.log_metric("discriminator_r_f", r_f, step=self.logger.g_iter)
mlflow.log_metric("discriminator_loss", loss, step=self.logger.g_iter)
self.logger.step()
all_loss.append(loss.detach().cpu().item())
all_p_true.append(p_true.cpu().item())
all_p_fake.append(p_fake.cpu().item())
all_r_t.append(r_t.cpu().item())
all_r_g.append(r_g.cpu().item())
all_r_f.append(r_f.cpu().item())
all_loss_ = np.mean(all_loss)
all_p_true_ = np.mean(all_p_true)
all_p_fake_ = np.mean(all_p_fake)
all_r_t_ = np.mean(all_r_t)
all_r_g_ = np.mean(all_r_g)
all_r_f_ = np.mean(all_r_f)
sample_num = sample_num + img.shape[0]
pbar_str = f"epoch:{cur_epoch} discriminator loss:{all_loss_:.2e}"
pbar_str += f" pt:{all_p_true_ * 100:.2f}"
pbar_str += f" pf:{all_p_fake_ * 100:.2f}"
pbar_str += f" rt:{all_r_t_:.2f}"
pbar_str += f" rg:{all_r_g_:.2f}"
pbar_str += f" rf:{all_r_f_:.2f}"
pbar_str += f" svd:{self.svd}"
pbar_str += f" sample:{sample_num}"
pbar_str2 = pbar_str
pbar_str += pbar_str1
pbar.set_description_str(pbar_str)
if sample_num > self.limit:
break
return pbar_str2, all_p_true_, all_p_fake_, all_loss_
def tester(self, test_data, name):
ckpt_path = glob.glob(self.ckpt_dir + '/ckpt_best*')
if len(ckpt_path) != 0:
state_dict = torch.load(ckpt_path[0], map_location=self.device)
if 'discriminator' in state_dict:
self.discriminator.load_state_dict(state_dict['discriminator'])
if "pre_projection" in state_dict:
self.pre_projection.load_state_dict(state_dict["pre_projection"])
else:
self.load_state_dict(state_dict, strict=False)
if self.tta:
print("Conduct tta during inference")
images, scores, segmentations, labels_gt, masks_gt = self.tta_predict(test_data)
image_auroc, image_ap, img_threshold, img_f1_max = self.tta_evaluate(images, scores, segmentations,
labels_gt, masks_gt, name, path='eval')
else:
images, scores, segmentations, labels_gt, masks_gt = self.predict(test_data)
image_auroc, image_ap, img_threshold, img_f1_max = self._evaluate(images, scores, segmentations,
labels_gt, masks_gt, name, path='eval')
epoch = int(ckpt_path[0].split('_')[-1].split('.')[0])
else:
image_auroc, image_ap, img_threshold, img_f1_max, epoch = 0., 0., 0., 0., -1.
LOGGER.info("No ckpt file found!")
return image_auroc, image_ap, img_threshold, img_f1_max, epoch
def _evaluate(self, images, scores, segmentations, labels_gt, masks_gt, name, path='training'):
scores = np.squeeze(np.array(scores))
img_min_scores = min(scores)
img_max_scores = max(scores)
norm_scores = (scores - img_min_scores) / (img_max_scores - img_min_scores + 1e-10)
image_scores = metrics.compute_imagewise_retrieval_metrics(norm_scores, labels_gt, path)
image_auroc = image_scores["auroc"]
image_ap = image_scores["ap"]
img_threshold, img_f1_max = metrics.compute_best_pr_re(labels_gt, norm_scores)
if len(masks_gt) > 0:
segmentations = np.array(segmentations)
min_scores = np.min(segmentations)
max_scores = np.max(segmentations)
norm_segmentations = (segmentations - min_scores) / (max_scores - min_scores + 1e-10)
defects = np.array(images)
# targets = np.array(masks_gt)
for i in range(len(defects)):
defect = utils.torch_format_2_numpy_img(defects[i])
# target = utils.torch_format_2_numpy_img(targets[i])
mask = cv2.cvtColor(cv2.resize(norm_segmentations[i], (defect.shape[1], defect.shape[0])),
cv2.COLOR_GRAY2BGR)
mask = (mask * 255).astype('uint8')
mask = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
# img_up = np.hstack([defect, target, mask])
# img_up = cv2.resize(img_up, (256 * 3, 256))
img_up = np.hstack([defect, mask])
img_up = cv2.resize(img_up, (256 * 2, 256))
full_path = os.path.join(self.result_dir, path, name) + '/'
utils.del_remake_dir(full_path, del_flag=False)
cv2.imwrite(full_path + str(i + 1).zfill(3) + f"_{labels_gt[i]}" + '.png', img_up)
return image_auroc, image_ap, img_threshold, img_f1_max
def predict(self, test_dataloader):
"""This function provides anomaly scores/maps for full dataloaders."""
self.forward_modules.eval()
img_paths = []
images = []
scores = []
masks = []
labels_gt = []
masks_gt = []
with tqdm.tqdm(test_dataloader, desc="Inferring...", leave=False, unit='batch') as data_iterator:
for data in data_iterator:
if isinstance(data, dict):
labels_gt.extend(data["is_anomaly"].numpy().tolist())
if data.get("mask_gt", None) is not None:
masks_gt.extend(data["mask_gt"].numpy().tolist())
image = data["image"]
images.extend(image.numpy().tolist())
img_paths.extend(data["image_path"])
_scores, _masks = self._predict(image)
for score, mask in zip(_scores, _masks):
scores.append(score)
masks.append(mask)
return images, scores, masks, labels_gt, masks_gt
def _predict(self, img):
"""Infer score and mask for a batch of images."""
img = img.to(torch.float).to(self.device)
self.forward_modules.eval()
if self.pre_proj > 0:
self.pre_projection.eval()
self.discriminator.eval()
with torch.no_grad():
patch_features, patch_shapes = self._embed(img, provide_patch_shapes=True, evaluation=True)
if self.pre_proj > 0:
patch_features = self.pre_projection(patch_features)
patch_features = patch_features[0] if len(patch_features) == 2 else patch_features
patch_scores = image_scores = self.discriminator(patch_features)
patch_scores = self.patch_maker.unpatch_scores(patch_scores, batchsize=img.shape[0])
scales = patch_shapes[0]
patch_scores = patch_scores.reshape(img.shape[0], scales[0], scales[1])
masks = self.anomaly_segmentor.convert_to_segmentation(patch_scores)
image_scores = self.patch_maker.unpatch_scores(image_scores, batchsize=img.shape[0])
image_scores = self.patch_maker.score(image_scores)
if isinstance(image_scores, torch.Tensor):
image_scores = image_scores.cpu().numpy()
return list(image_scores), list(masks)
def tta_evaluate(self, images, scores, segmentations, labels_gt, masks_gt, name, path='training'):
scores = np.squeeze(np.array(scores))
img_min_scores = min(scores)
img_max_scores = max(scores)
norm_scores = (scores - img_min_scores) / (img_max_scores - img_min_scores + 1e-10)
image_scores = metrics.compute_imagewise_retrieval_metrics(norm_scores, labels_gt, path)
image_auroc = image_scores["auroc"]
image_ap = image_scores["ap"]
img_threshold, img_f1_max = metrics.compute_best_pr_re(labels_gt, norm_scores)
if len(masks_gt) > 0:
segmentations = np.array(segmentations)
min_scores = np.min(segmentations)
max_scores = np.max(segmentations)
norm_segmentations = (segmentations - min_scores) / (max_scores - min_scores + 1e-10)
# # ============== DEBUG ==============
# print("\n[DEBUG] Data shape validation:")
# print("1. Original segmentation result dtype:", segmentations.dtype, "shape:", segmentations.shape)
# print("2. Normalized dtype:", norm_segmentations.dtype, "range:", np.min(norm_segmentations), "-", np.max(norm_segmentations))
norm_segmentations = norm_segmentations.astype(np.float32) # Fix 1:convert to float32
# print("3. Converted dtype:", norm_segmentations.dtype)
defects = np.array(images)
targets = np.array(masks_gt)
for i in range(len(defects)):
# if i == 0: # print debug info only for the first sample
# print("\n[DEBUG] Sample processing pipeline validation (i=0):")
defect = utils.torch_format_2_numpy_img(defects[i])
target = utils.torch_format_2_numpy_img(targets[i])
# Maintain single channel during resizing
resized_mask = cv2.resize(
norm_segmentations[i],
(defect.shape[1], defect.shape[0]),
interpolation=cv2.INTER_LINEAR
)
# if i == 0:
# print("4. Resized mask shape:", resized_mask.shape, "dtype:", resized_mask.dtype)
# Convert to 0-255 and uint8
mask_8bit = (resized_mask * 255).astype(np.uint8)
# if i == 0:
# print("5. Converted to uint8 range:", np.min(mask_8bit), "-", np.max(mask_8bit), "dtype:", mask_8bit.dtype)
# Color sapce conversion
try:
mask_color = cv2.cvtColor(mask_8bit, cv2.COLOR_GRAY2BGR)
# if i == 0:
# print("6. Converted color shape:", mask_color.shape)
except Exception as e:
print(f"\n[ERROR] Color conversion failed!")
print("error mask_8bit parameters:", f"shape:{mask_8bit.shape}", f"dtype:{mask_8bit.dtype}")
raise e
# Apply color mapping
mask_color = cv2.applyColorMap(mask_color, cv2.COLORMAP_JET)
# if i == 0:
# print("7. Applied color mapping shape:", mask_color.shape, "dtype:", mask_color.dtype)
# Concatenate result images
# img_up = np.hstack([defect, target, mask_color])
# img_up = cv2.resize(img_up, (256 * 3, 256))
img_up = np.hstack([defect, mask_color])
img_up = cv2.resize(img_up, (256 * 2, 256))
full_path = os.path.join(self.result_dir, path, name) + '/'
utils.del_remake_dir(full_path, del_flag=False)
cv2.imwrite(full_path + str(i + 1).zfill(3) + f"_{labels_gt[i]}" + '.png', img_up)
else:
pixel_auroc = -1.
pixel_ap = -1.
pixel_pro = -1.
return image_auroc, image_ap, pixel_auroc, pixel_ap, pixel_pro,img_threshold, img_f1_max
defects = np.array(images)
targets = np.array(masks_gt)
for i in range(len(defects)):
defect = utils.torch_format_2_numpy_img(defects[i])
target = utils.torch_format_2_numpy_img(targets[i])
resized_mask = cv2.resize(norm_segmentations[i], (target_width, target_height), interpolation=cv2.INTER_LINEAR)
resized_mask = resized_mask.astype(np.uint8)
mask = cv2.cvtColor(resized_mask, cv2.COLOR_GRAY2BGR)
mask = (mask * 255).astype('uint8')
mask = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
img_up = np.hstack([defect, target, mask])
img_up = cv2.resize(img_up, (256 * 3, 256))
full_path = os.path.join(self.result_dir, path, name) + '/'
utils.del_remake_dir(full_path, del_flag=False)
cv2.imwrite(full_path + str(i + 1).zfill(3) + f"_{labels_gt[i]}" + '.png', img_up)
return image_auroc, image_ap, pixel_auroc, pixel_ap, pixel_pro, img_threshold, img_f1_max
def tta_predict(self, test_dataloader):
"""This function provides anomaly scores/maps for full dataloaders."""
self.forward_modules.eval()
img_paths = []
images = []
scores = []
masks = []
labels_gt = []
masks_gt = []
with tqdm.tqdm(test_dataloader, desc="Inferring...", leave=False, unit='batch') as data_iterator:
for data in data_iterator:
if isinstance(data, dict):
labels_gt.extend(data["is_anomaly"].numpy().tolist())
if data.get("mask_gt", None) is not None:
masks_gt.extend(data["mask_gt"].numpy().tolist())
image = data["image"]
images.extend(image.numpy().tolist())
img_paths.extend(data["image_path"])
_scores, _masks = self.tta__predict(image)
for score, mask in zip(_scores, _masks):
scores.append(score)
masks.append(mask)
return images, scores, masks, labels_gt, masks_gt
def tta__predict(self, img):
"""Infer score and mask for a batch of images with TTA"""
self.forward_modules.eval()
if self.pre_proj > 0:
self.pre_projection.eval()
self.discriminator.eval()
tta_transforms = [
{'name': 'original',
'transform': lambda x: x,
'reverse': lambda x: x},
{'name': 'h_flip',
'transform': lambda x: x.flip(-1),
'reverse': lambda x: x.flip(-1)},
{'name': 'v_flip',
'transform': lambda x: x.flip(-2),
'reverse': lambda x: x.flip(-2)},
# {'name': 'rotate90',
# 'transform': lambda x: x.rot90(1, [-2, -1]).flip(-1),
# 'reverse': lambda x: x.flip(-1).rot90(-1, [-2, -1])},
# {'name': 'color_jitter',
# 'transform': lambda x: x * (0.9 + 0.2*torch.rand(1,device=x.device)) + 0.1*torch.randn_like(x),
# 'reverse': lambda x: x},
]
all_scores = []
all_masks = []
with torch.no_grad():
for aug in tta_transforms:
transformed_img = aug['transform'](img)
_scores, _masks = self._predict(transformed_img)
mask_tensor = torch.tensor(np.array(_masks))
reversed_masks = aug['reverse'](torch.tensor(_masks))
all_scores.append(_scores)
all_masks.append(reversed_masks.numpy())
avg_scores = np.mean(all_scores, axis=0)
avg_masks = np.mean(all_masks, axis=0)
return avg_scores.tolist(), avg_masks.tolist()