-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluate.py
275 lines (246 loc) · 11.1 KB
/
evaluate.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
"""
Evaluation for mvtec_ad dataset.
Reference from https://github.com/denguir/student-teacher-anomaly-detection.
Author: Luyao Chen
Date: 2020.10
"""
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
import cv2
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
from sklearn.metrics import roc_auc_score
from PIL import Image
from models import _Teacher, TeacherOrStudent
from mvtec_dataset import MVTec_AD
def error(student_outputs, teacher_output):
# n*imH*imW*d
# s_mean = 0
# for s_out in student_outputs:
# s_mean += s_out
# s_mean /= len(student_outputs)
s_mean = torch.mean(student_outputs, dim=1)
return torch.norm(s_mean - teacher_output, dim=3)
def variance(student_outputs):
# s_sum = 0
# for s_out in student_outputs:
# s_sum += s_out
# s_mean = s_sum / len(student_outputs)
# v = 0
# for s_out in student_outputs:
# v += torch.norm(s_out - s_mean, dim=3)
# v /= len(student_outputs)
sse = torch.sum(student_outputs ** 2, dim=4)
msse = torch.mean(sse, dim=1)
s_mean = torch.mean(student_outputs, dim=1)
var = msse - torch.sum(s_mean**2, dim=3)
return var
def increment_mean_and_var(mu_N, var_N, N, batch):
'''Increment value of mean and variance based on
current mean, var and new batch
'''
# batch: (batch, h, w, vector)
B = batch.size()[0] # batch size
# we want a descriptor vector -> mean over batch and pixels
mu_B = torch.mean(batch, dim=[0, 1, 2])
S_B = B * torch.var(batch, dim=[0, 1, 2], unbiased=False)
S_N = N * var_N
mu_NB = N / (N + B) * mu_N + B / (N + B) * mu_B
S_NB = S_N + S_B + B * mu_B**2 + N * mu_N**2 - (N + B) * mu_NB**2
var_NB = S_NB / (N + B)
return mu_NB, var_NB, N + B
if __name__ == "__main__":
patch_sizes = [17,33,65] # add more size for multi-scale segmentation
num_students = 1 # num of studetns per teacher
imH = 256 # image height and width should be multiples of sL1∗sL2∗sL3...
imW = 256
batch_size = 1
work_dir = 'work_dir/'
class_dir = 'leather/'
class_dir1 = 'leather/'
train_dataset_dir = '../../mvtec_anomaly_detection/' + class_dir1 + 'train/'
test_dataset_dir = '../../mvtec_anomaly_detection/' + class_dir1
device = torch.device('cuda:1')
N_scale = len(patch_sizes)
std = [0.229, 0.224, 0.225]
mean = [0.485, 0.456, 0.406]
trans = transforms.Compose([
# transforms.RandomCrop((imH, imW)),
transforms.Resize((imH, imW)),
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
mask_trans = transforms.Compose([
# transforms.RandomCrop((imH, imW)),
transforms.Resize((imH, imW), Image.NEAREST),
transforms.ToTensor(),
])
anomaly_free_dataset = datasets.ImageFolder(
train_dataset_dir, transform=trans)
af_dataloader = DataLoader(anomaly_free_dataset, batch_size=batch_size)
test_dataset = MVTec_AD(test_dataset_dir, transform=trans,
mask_transform=mask_trans, phase='test')
test_dataloader = DataLoader(test_dataset, batch_size=batch_size)
teachers = []
students = []
for patch_size in patch_sizes:
_teacher = _Teacher(patch_size)
checkpoint = torch.load(work_dir + '_teacher' +
str(patch_size) + '.pth', torch.device('cpu'))
_teacher.load_state_dict(checkpoint)
teacher = TeacherOrStudent(patch_size, _teacher, imH, imW).to(device)
teacher.eval()
teachers.append(teacher)
s_t = []
for i in range(num_students):
i=i+1
# issue #2. must create a new _teacher.
_teacher = _Teacher(patch_size)
student = TeacherOrStudent(
patch_size, _teacher, imH, imW).to(device)
checkpoint = torch.load(work_dir + class_dir + 'student' +
str(patch_size) + '_' + str(i) +
'.pth', torch.device('cpu'))
student.load_state_dict(checkpoint)
student.eval()
s_t.append(student)
students.append(s_t)
with torch.no_grad():
t_mu, t_var, t_N = [0 for i in range(N_scale)], [0 for i in range(N_scale)], [
0 for i in range(N_scale)]
print('Callibrating teacher on train dataset.')
for data, _ in tqdm(af_dataloader):
data = data.to(device)
for i in range(N_scale):
t_out = teachers[i](data)
t_mu[i], t_var[i], t_N[i] = increment_mean_and_var(
t_mu[i], t_var[i], t_N[i], t_out)
# mu_err, var_err = torch.tensor([4.7920308113098145]), torch.tensor([3.410670280456543])
# mu_var, var_var = torch.tensor([4.074430465698242]), torch.tensor([1.5367100238800049])
max_err, max_var = [0 for i in range(N_scale)], [0 for i in range(N_scale)]
mu_err, var_err, N_err = [0 for i in range(N_scale)], [0 for i in range(N_scale)], [0 for i in range(N_scale)]
mu_var, var_var, N_var = [0 for i in range(N_scale)], [0 for i in range(N_scale)], [0 for i in range(N_scale)]
print('Callibrating scoring parameters on train dataset.')
for data, _ in tqdm(af_dataloader):
data = data.to(device)
for i in range(N_scale):
teacher_output = (teachers[i](
data) - t_mu[i]) / torch.sqrt(t_var[i])
student_outputs = []
for j in range(num_students):
student_outputs.append(students[i][j](data))
student_outputs = torch.stack(student_outputs, dim=1)
e = error(student_outputs, teacher_output)
v = variance(student_outputs)
mu_err[i], var_err[i], N_err[i] = increment_mean_and_var(
mu_err[i], var_err[i], N_err[i], e)
mu_var[i], var_var[i], N_var[i] = increment_mean_and_var(
mu_var[i], var_var[i], N_var[i], v)
max_err[i] = max(max_err[i], torch.max(e))
max_var[i] = max(max_var[i], torch.max(v))
# max_score = 29.9642391204834
max_score = 0
for i in range(N_scale):
print('mu_err:{}, var_err:{}, mu_var:{}, var_var:{}'.format(
mu_err[i], var_err[i], mu_var[i], var_var[i]
))
max_score += (max_err[i] - mu_err[i]) / torch.sqrt(var_err[i]+1e-6) + \
(max_var[i] - mu_var[i]) / torch.sqrt(var_var[i]+1e-6)
max_score /= N_scale
print('max_score:{}'.format(max_score))
score_map_list = []
gt_mask_list = []
img_id = 0
for data, gt_mask, _ in tqdm(test_dataloader):
plt_list = []
ori_imgs = data
data = data.to(device)
gt_mask_list.append(gt_mask.data.numpy())
anomaly_score = 0
for i in range(N_scale):
teacher_output = (teachers[i](
data) - t_mu[i]) / torch.sqrt(t_var[i])
plt_list.append(teacher_output)
student_outputs = []
for j in range(num_students):
student_outputs.append(students[i][j](data))
plt_list.append(student_outputs[j])
student_outputs = torch.stack(student_outputs, dim=1)
e = error(student_outputs, teacher_output)
v = variance(student_outputs)
anomaly_score += (e - mu_err[i]) / torch.sqrt(var_err[i]+1e-6) + \
(v - mu_var[i]) / torch.sqrt(var_var[i]+1e-6)
anomaly_score /= N_scale
score_map_list.append(anomaly_score.cpu().detach().numpy())
# print('max:{:.2f},min:{:.2f},avg:{:.2f}'.format(torch.max(anomaly_score),
# torch.min(anomaly_score),
# torch.mean(anomaly_score)))
# plt.figure()
# plt.subplot(2, 2, 1)
# plt_img = plt_list[1].cpu().detach().numpy()[0]
# plt_img = np.mean(plt_img, axis=2)
# plt_img = np.expand_dims(plt_img, 2)
# plt.imshow(plt_img, cmap='jet')
# plt.colorbar()
# plt.subplot(2, 2, 2)
# plt_img = plt_list[2].cpu().detach().numpy()[0]
# plt_img = np.mean(plt_img, axis=2)
# plt_img = np.expand_dims(plt_img, 2)
# plt.imshow(plt_img, cmap='jet')
# plt.colorbar()
# plt.subplot(2, 2, 3)
# plt_img = plt_list[3].cpu().detach().numpy()[0]
# plt_img = np.mean(plt_img, axis=2)
# plt_img = np.expand_dims(plt_img, 2)
# plt.imshow(plt_img, cmap='jet')
# plt.colorbar()
# plt.subplot(2, 2, 4)
# plt_img = plt_list[0].cpu().detach().numpy()[0]
# plt_img = np.mean(plt_img, axis=2)
# plt_img = np.expand_dims(plt_img, 2)
# plt.imshow(plt_img, cmap='jet')
# plt.colorbar()
# plt.savefig('cmp.png')
# plt.close()
# px = 118
# py = 132
# plt.figure(figsize=(6, 3))
# plt_vec = plt_list[1].cpu().detach().numpy()[0, px, py]
# plt_vec -= plt_list[0].cpu().detach().numpy()[0, px, py]
# plt.plot(plt_vec, label='s1')
# plt_vec = plt_list[2].cpu().detach().numpy()[0, px, py]
# plt.plot(plt_vec, label='s2')
# plt_vec = plt_list[3].cpu().detach().numpy()[0, px, py]
# plt.plot(plt_vec, label='s3')
# plt_vec = plt_list[0].cpu().detach().numpy()[0, px, py]
# plt.plot(plt_vec, label='t')
# plt.legend()
# plt.savefig('vec.png')
# plt.close()
anomaly_score -= torch.min(anomaly_score)
# anomaly_score /= torch.max(anomaly_score)
anomaly_score /= max_score
# anomaly_score /= 30
score_map = anomaly_score.cpu().detach().numpy()[0, :, :]
score_map = np.minimum(score_map, 1)
score_map = cv2.applyColorMap(
np.uint8(score_map * 255), cv2.COLORMAP_JET)
# # cv2.imwrite('score.jpg', score_map)
ori_img = ori_imgs.permute(0, 2, 3, 1).detach().numpy()[0, :, :, :]
for c in range(3):
ori_img[:, :, c] = ori_img[:, :, c] * std[c] + mean[c]
ori_img = cv2.cvtColor(ori_img, cv2.COLOR_RGB2BGR)
# # cv2.imwrite('ori.jpg', np.uint8(ori_img * 255))
save_img = np.concatenate(
(np.uint8(ori_img * 255), score_map), axis=1)
# cv2.imwrite('res.jpg', save_img)
cv2.imwrite('tmp/' + str(img_id) + '.jpg', save_img)
img_id += 1
flatten_gt_mask_list = np.concatenate(gt_mask_list).ravel()
flatten_score_map_list = np.concatenate(score_map_list).ravel()
per_pixel_rocauc = roc_auc_score(
flatten_gt_mask_list, flatten_score_map_list)
print('pixel ROCAUC:{}'.format(per_pixel_rocauc))