forked from nelson1425/EfficientAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
efficientad.py
369 lines (330 loc) · 15.3 KB
/
efficientad.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import tifffile
import torch
from torch.utils.data import DataLoader
from torchvision import transforms
import argparse
import itertools
import os
import random
from tqdm import tqdm
from common import get_autoencoder, get_pdn_small, get_pdn_medium, \
ImageFolderWithoutTarget, ImageFolderWithPath, InfiniteDataloader
from sklearn.metrics import roc_auc_score
def get_argparse():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dataset', default='mvtec_ad',
choices=['mvtec_ad', 'mvtec_loco'])
parser.add_argument('-s', '--subdataset', default='bottle',
help='One of 15 sub-datasets of Mvtec AD or 5' +
'sub-datasets of Mvtec LOCO')
parser.add_argument('-o', '--output_dir', default='output/1')
parser.add_argument('-m', '--model_size', default='small',
choices=['small', 'medium'])
parser.add_argument('-w', '--weights', default='models/teacher_small.pth')
parser.add_argument('-i', '--imagenet_train_path',
default='none',
help='Set to "none" to disable ImageNet' +
'pretraining penalty. Or see README.md to' +
'download ImageNet and set to ImageNet path')
parser.add_argument('-a', '--mvtec_ad_path',
default='./mvtec_anomaly_detection',
help='Downloaded Mvtec AD dataset')
parser.add_argument('-b', '--mvtec_loco_path',
default='./mvtec_loco_anomaly_detection',
help='Downloaded Mvtec LOCO dataset')
parser.add_argument('-t', '--train_steps', type=int, default=70000)
return parser.parse_args()
# constants
seed = 42
on_gpu = torch.cuda.is_available()
out_channels = 384
image_size = 256
# data loading
default_transform = transforms.Compose([
transforms.Resize((image_size, image_size)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
transform_ae = transforms.RandomChoice([
transforms.ColorJitter(brightness=0.2),
transforms.ColorJitter(contrast=0.2),
transforms.ColorJitter(saturation=0.2)
])
def train_transform(image):
return default_transform(image), default_transform(transform_ae(image))
def main():
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
config = get_argparse()
if config.dataset == 'mvtec_ad':
dataset_path = config.mvtec_ad_path
elif config.dataset == 'mvtec_loco':
dataset_path = config.mvtec_loco_path
else:
raise Exception('Unknown config.dataset')
pretrain_penalty = True
if config.imagenet_train_path == 'none':
pretrain_penalty = False
# create output dir
train_output_dir = os.path.join(config.output_dir, 'trainings',
config.dataset, config.subdataset)
test_output_dir = os.path.join(config.output_dir, 'anomaly_maps',
config.dataset, config.subdataset, 'test')
os.makedirs(train_output_dir)
os.makedirs(test_output_dir)
# load data
full_train_set = ImageFolderWithoutTarget(
os.path.join(dataset_path, config.subdataset, 'train'),
transform=transforms.Lambda(train_transform))
test_set = ImageFolderWithPath(
os.path.join(dataset_path, config.subdataset, 'test'))
if config.dataset == 'mvtec_ad':
# mvtec dataset paper recommend 10% validation set
train_size = int(0.9 * len(full_train_set))
validation_size = len(full_train_set) - train_size
rng = torch.Generator().manual_seed(seed)
train_set, validation_set = torch.utils.data.random_split(full_train_set,
[train_size,
validation_size],
rng)
elif config.dataset == 'mvtec_loco':
train_set = full_train_set
validation_set = ImageFolderWithoutTarget(
os.path.join(dataset_path, config.subdataset, 'validation'),
transform=transforms.Lambda(train_transform))
else:
raise Exception('Unknown config.dataset')
train_loader = DataLoader(train_set, batch_size=1, shuffle=True,
num_workers=4, pin_memory=True)
train_loader_infinite = InfiniteDataloader(train_loader)
validation_loader = DataLoader(validation_set, batch_size=1)
if pretrain_penalty:
# load pretraining data for penalty
penalty_transform = transforms.Compose([
transforms.Resize((2 * image_size, 2 * image_size)),
transforms.RandomGrayscale(0.3),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224,
0.225])
])
penalty_set = ImageFolderWithoutTarget(config.imagenet_train_path,
transform=penalty_transform)
penalty_loader = DataLoader(penalty_set, batch_size=1, shuffle=True,
num_workers=4, pin_memory=True)
penalty_loader_infinite = InfiniteDataloader(penalty_loader)
else:
penalty_loader_infinite = itertools.repeat(None)
# create models
if config.model_size == 'small':
teacher = get_pdn_small(out_channels)
student = get_pdn_small(2 * out_channels)
elif config.model_size == 'medium':
teacher = get_pdn_medium(out_channels)
student = get_pdn_medium(2 * out_channels)
else:
raise Exception()
state_dict = torch.load(config.weights, map_location='cpu')
teacher.load_state_dict(state_dict)
autoencoder = get_autoencoder(out_channels)
# teacher frozen
teacher.eval()
student.train()
autoencoder.train()
if on_gpu:
teacher.cuda()
student.cuda()
autoencoder.cuda()
teacher_mean, teacher_std = teacher_normalization(teacher, train_loader)
optimizer = torch.optim.Adam(itertools.chain(student.parameters(),
autoencoder.parameters()),
lr=1e-4, weight_decay=1e-5)
scheduler = torch.optim.lr_scheduler.StepLR(
optimizer, step_size=int(0.95 * config.train_steps), gamma=0.1)
tqdm_obj = tqdm(range(config.train_steps))
for iteration, (image_st, image_ae), image_penalty in zip(
tqdm_obj, train_loader_infinite, penalty_loader_infinite):
if on_gpu:
image_st = image_st.cuda()
image_ae = image_ae.cuda()
if image_penalty is not None:
image_penalty = image_penalty.cuda()
with torch.no_grad():
teacher_output_st = teacher(image_st)
teacher_output_st = (teacher_output_st - teacher_mean) / teacher_std
student_output_st = student(image_st)[:, :out_channels]
distance_st = (teacher_output_st - student_output_st) ** 2
d_hard = torch.quantile(distance_st, q=0.999)
loss_hard = torch.mean(distance_st[distance_st >= d_hard])
if image_penalty is not None:
student_output_penalty = student(image_penalty)[:, :out_channels]
loss_penalty = torch.mean(student_output_penalty**2)
loss_st = loss_hard + loss_penalty
else:
loss_st = loss_hard
ae_output = autoencoder(image_ae)
with torch.no_grad():
teacher_output_ae = teacher(image_ae)
teacher_output_ae = (teacher_output_ae - teacher_mean) / teacher_std
student_output_ae = student(image_ae)[:, out_channels:]
distance_ae = (teacher_output_ae - ae_output)**2
distance_stae = (ae_output - student_output_ae)**2
loss_ae = torch.mean(distance_ae)
loss_stae = torch.mean(distance_stae)
loss_total = loss_st + loss_ae + loss_stae
optimizer.zero_grad()
loss_total.backward()
optimizer.step()
scheduler.step()
if iteration % 10 == 0:
tqdm_obj.set_description(
"Current loss: {:.4f} ".format(loss_total.item()))
if iteration % 1000 == 0:
torch.save(teacher, os.path.join(train_output_dir,
'teacher_tmp.pth'))
torch.save(student, os.path.join(train_output_dir,
'student_tmp.pth'))
torch.save(autoencoder, os.path.join(train_output_dir,
'autoencoder_tmp.pth'))
if iteration % 10000 == 0 and iteration > 0:
# run intermediate evaluation
teacher.eval()
student.eval()
autoencoder.eval()
q_st_start, q_st_end, q_ae_start, q_ae_end = map_normalization(
validation_loader=validation_loader, teacher=teacher,
student=student, autoencoder=autoencoder,
teacher_mean=teacher_mean, teacher_std=teacher_std,
desc='Intermediate map normalization')
auc = test(
test_set=test_set, teacher=teacher, student=student,
autoencoder=autoencoder, teacher_mean=teacher_mean,
teacher_std=teacher_std, q_st_start=q_st_start,
q_st_end=q_st_end, q_ae_start=q_ae_start, q_ae_end=q_ae_end,
test_output_dir=None, desc='Intermediate inference')
print('Intermediate image auc: {:.4f}'.format(auc))
# teacher frozen
teacher.eval()
student.train()
autoencoder.train()
teacher.eval()
student.eval()
autoencoder.eval()
torch.save(teacher, os.path.join(train_output_dir, 'teacher_final.pth'))
torch.save(student, os.path.join(train_output_dir, 'student_final.pth'))
torch.save(autoencoder, os.path.join(train_output_dir,
'autoencoder_final.pth'))
q_st_start, q_st_end, q_ae_start, q_ae_end = map_normalization(
validation_loader=validation_loader, teacher=teacher, student=student,
autoencoder=autoencoder, teacher_mean=teacher_mean,
teacher_std=teacher_std, desc='Final map normalization')
auc = test(
test_set=test_set, teacher=teacher, student=student,
autoencoder=autoencoder, teacher_mean=teacher_mean,
teacher_std=teacher_std, q_st_start=q_st_start, q_st_end=q_st_end,
q_ae_start=q_ae_start, q_ae_end=q_ae_end,
test_output_dir=test_output_dir, desc='Final inference')
print('Final image auc: {:.4f}'.format(auc))
def test(test_set, teacher, student, autoencoder, teacher_mean, teacher_std,
q_st_start, q_st_end, q_ae_start, q_ae_end, test_output_dir=None,
desc='Running inference'):
y_true = []
y_score = []
for image, target, path in tqdm(test_set, desc=desc):
orig_width = image.width
orig_height = image.height
image = default_transform(image)
image = image[None]
if on_gpu:
image = image.cuda()
map_combined, map_st, map_ae = predict(
image=image, teacher=teacher, student=student,
autoencoder=autoencoder, teacher_mean=teacher_mean,
teacher_std=teacher_std, q_st_start=q_st_start, q_st_end=q_st_end,
q_ae_start=q_ae_start, q_ae_end=q_ae_end)
map_combined = torch.nn.functional.pad(map_combined, (4, 4, 4, 4))
map_combined = torch.nn.functional.interpolate(
map_combined, (orig_height, orig_width), mode='bilinear')
map_combined = map_combined[0, 0].cpu().numpy()
defect_class = os.path.basename(os.path.dirname(path))
if test_output_dir is not None:
img_nm = os.path.split(path)[1].split('.')[0]
if not os.path.exists(os.path.join(test_output_dir, defect_class)):
os.makedirs(os.path.join(test_output_dir, defect_class))
file = os.path.join(test_output_dir, defect_class, img_nm + '.tiff')
tifffile.imwrite(file, map_combined)
y_true_image = 0 if defect_class == 'good' else 1
y_score_image = np.max(map_combined)
y_true.append(y_true_image)
y_score.append(y_score_image)
auc = roc_auc_score(y_true=y_true, y_score=y_score)
return auc * 100
@torch.no_grad()
def predict(image, teacher, student, autoencoder, teacher_mean, teacher_std,
q_st_start=None, q_st_end=None, q_ae_start=None, q_ae_end=None):
teacher_output = teacher(image)
teacher_output = (teacher_output - teacher_mean) / teacher_std
student_output = student(image)
autoencoder_output = autoencoder(image)
map_st = torch.mean((teacher_output - student_output[:, :out_channels])**2,
dim=1, keepdim=True)
map_ae = torch.mean((autoencoder_output -
student_output[:, out_channels:])**2,
dim=1, keepdim=True)
if q_st_start is not None:
map_st = 0.1 * (map_st - q_st_start) / (q_st_end - q_st_start)
if q_ae_start is not None:
map_ae = 0.1 * (map_ae - q_ae_start) / (q_ae_end - q_ae_start)
map_combined = 0.5 * map_st + 0.5 * map_ae
return map_combined, map_st, map_ae
@torch.no_grad()
def map_normalization(validation_loader, teacher, student, autoencoder,
teacher_mean, teacher_std, desc='Map normalization'):
maps_st = []
maps_ae = []
# ignore augmented ae image
for image, _ in tqdm(validation_loader, desc=desc):
if on_gpu:
image = image.cuda()
map_combined, map_st, map_ae = predict(
image=image, teacher=teacher, student=student,
autoencoder=autoencoder, teacher_mean=teacher_mean,
teacher_std=teacher_std)
maps_st.append(map_st)
maps_ae.append(map_ae)
maps_st = torch.cat(maps_st)
maps_ae = torch.cat(maps_ae)
q_st_start = torch.quantile(maps_st, q=0.9)
q_st_end = torch.quantile(maps_st, q=0.995)
q_ae_start = torch.quantile(maps_ae, q=0.9)
q_ae_end = torch.quantile(maps_ae, q=0.995)
return q_st_start, q_st_end, q_ae_start, q_ae_end
@torch.no_grad()
def teacher_normalization(teacher, train_loader):
mean_outputs = []
for train_image, _ in tqdm(train_loader, desc='Computing mean of features'):
if on_gpu:
train_image = train_image.cuda()
teacher_output = teacher(train_image)
mean_output = torch.mean(teacher_output, dim=[0, 2, 3])
mean_outputs.append(mean_output)
channel_mean = torch.mean(torch.stack(mean_outputs), dim=0)
channel_mean = channel_mean[None, :, None, None]
mean_distances = []
for train_image, _ in tqdm(train_loader, desc='Computing std of features'):
if on_gpu:
train_image = train_image.cuda()
teacher_output = teacher(train_image)
distance = (teacher_output - channel_mean) ** 2
mean_distance = torch.mean(distance, dim=[0, 2, 3])
mean_distances.append(mean_distance)
channel_var = torch.mean(torch.stack(mean_distances), dim=0)
channel_var = channel_var[None, :, None, None]
channel_std = torch.sqrt(channel_var)
return channel_mean, channel_std
if __name__ == '__main__':
main()