-
Notifications
You must be signed in to change notification settings - Fork 40
/
train.py
executable file
·326 lines (270 loc) · 13.4 KB
/
train.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
import os
import random
import argparse
import time
import math
import torch
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
from tqdm import tqdm
from datasets.mvtec import FSAD_Dataset_train, FSAD_Dataset_test
from utils.utils import time_file_str, time_string, convert_secs2time, AverageMeter, print_log
from models.siamese import Encoder, Predictor
from models.stn import stn_net
from losses.norm_loss import CosLoss
from utils.funcs import embedding_concat, mahalanobis_torch, rot_img, translation_img, hflip_img, rot90_img, grey_img
from sklearn.metrics import roc_auc_score
from scipy.ndimage import gaussian_filter
from collections import OrderedDict
import warnings
warnings.filterwarnings("ignore")
use_cuda = torch.cuda.is_available()
device = torch.device('cuda' if use_cuda else 'cpu')
def main():
parser = argparse.ArgumentParser(description='Registration based Few-Shot Anomaly Detection')
parser.add_argument('--obj', type=str, default='bottle')
parser.add_argument('--data_type', type=str, default='mvtec')
parser.add_argument('--data_path', type=str, default='./MVTec/')
parser.add_argument('--epochs', type=int, default=50, help='maximum training epochs')
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--img_size', type=int, default=224)
parser.add_argument('--lr', type=float, default=0.0001, help='learning rate of others in SGD')
parser.add_argument('--momentum', type=float, default=0.9, help='momentum of SGD')
parser.add_argument('--seed', type=int, default=668, help='manual seed')
parser.add_argument('--shot', type=int, default=2, help='shot count')
parser.add_argument('--inferences', type=int, default=10, help='number of rounds per inference')
parser.add_argument('--stn_mode', type=str, default='rotation_scale',
help='[affine, translation, rotation, scale, shear, rotation_scale, translation_scale, rotation_translation, rotation_translation_scale]')
args = parser.parse_args()
args.input_channel = 3
if args.seed is None:
args.seed = random.randint(1, 10000)
random.seed(args.seed)
torch.manual_seed(args.seed)
if use_cuda:
torch.cuda.manual_seed_all(args.seed)
args.prefix = time_file_str()
args.save_dir = './logs_mvtec/'
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
args.save_model_dir = './logs_mvtec/' + args.stn_mode + '/' + str(args.shot) + '/' + args.obj + '/'
if not os.path.exists(args.save_model_dir):
os.makedirs(args.save_model_dir)
log = open(os.path.join(args.save_dir, 'log_{}_{}.txt'.format(str(args.shot),args.obj)), 'w')
state = {k: v for k, v in args._get_kwargs()}
print_log(state, log)
# load model and dataset
STN = stn_net(args).to(device)
ENC = Encoder().to(device)
PRED = Predictor().to(device)
print(STN)
STN_optimizer = optim.SGD(STN.parameters(), lr=args.lr, momentum=args.momentum)
ENC_optimizer = optim.SGD(ENC.parameters(), lr=args.lr, momentum=args.momentum)
PRED_optimizer = optim.SGD(PRED.parameters(), lr=args.lr, momentum=args.momentum)
models = [STN, ENC, PRED]
optimizers = [STN_optimizer, ENC_optimizer, PRED_optimizer]
init_lrs = [args.lr, args.lr, args.lr]
print('Loading Datasets')
kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {}
train_dataset = FSAD_Dataset_train(args.data_path, class_name=args.obj, is_train=True, resize=args.img_size, shot=args.shot, batch=args.batch_size)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True, **kwargs)
test_dataset = FSAD_Dataset_test(args.data_path, class_name=args.obj, is_train=False, resize=args.img_size, shot=args.shot)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, **kwargs)
# start training
save_name = os.path.join(args.save_model_dir, '{}_{}_{}_model.pt'.format(args.obj, args.shot, args.stn_mode))
start_time = time.time()
epoch_time = AverageMeter()
img_roc_auc_old = 0.0
per_pixel_rocauc_old = 0.0
print('Loading Fixed Support Set')
fixed_fewshot_list = torch.load(f'./support_set/{args.obj}/{args.shot}_{args.inferences}.pt')
print_log((f'---------{args.stn_mode}--------'), log)
for epoch in range(1, args.epochs + 1):
adjust_learning_rate(optimizers, init_lrs, epoch, args)
need_hour, need_mins, need_secs = convert_secs2time(epoch_time.avg * (args.epochs - epoch))
need_time = '[Need: {:02d}:{:02d}:{:02d}]'.format(need_hour, need_mins, need_secs)
print_log(' {:3d}/{:3d} ----- [{:s}] {:s}'.format(epoch, args.epochs, time_string(), need_time), log)
if epoch <= args.epochs:
image_auc_list = []
pixel_auc_list = []
for inference_round in tqdm(range(args.inferences)):
scores_list, test_imgs, gt_list, gt_mask_list = test(models, inference_round, fixed_fewshot_list,
test_loader, **kwargs)
scores = np.asarray(scores_list)
# Normalization
max_anomaly_score = scores.max()
min_anomaly_score = scores.min()
scores = (scores - min_anomaly_score) / (max_anomaly_score - min_anomaly_score)
# calculate image-level ROC AUC score
img_scores = scores.reshape(scores.shape[0], -1).max(axis=1)
gt_list = np.asarray(gt_list)
img_roc_auc = roc_auc_score(gt_list, img_scores)
image_auc_list.append(img_roc_auc)
# calculate per-pixel level ROCAUC
gt_mask = np.asarray(gt_mask_list)
gt_mask = (gt_mask > 0.5).astype(np.int_)
per_pixel_rocauc = roc_auc_score(gt_mask.flatten(), scores.flatten())
pixel_auc_list.append(per_pixel_rocauc)
image_auc_list = np.array(image_auc_list)
pixel_auc_list = np.array(pixel_auc_list)
mean_img_auc = np.mean(image_auc_list, axis = 0)
mean_pixel_auc = np.mean(pixel_auc_list, axis = 0)
if mean_img_auc + mean_pixel_auc > per_pixel_rocauc_old + img_roc_auc_old:
state = {'STN': STN.state_dict(), 'ENC': ENC.state_dict(), 'PRED':PRED.state_dict()}
torch.save(state, save_name)
per_pixel_rocauc_old = mean_pixel_auc
img_roc_auc_old = mean_img_auc
print('Img-level AUC:',img_roc_auc_old)
print('Pixel-level AUC:', per_pixel_rocauc_old)
print_log(('Test Epoch(img, pixel): {} ({:.6f}, {:.6f}) best: ({:.3f}, {:.3f})'
.format(epoch-1, mean_img_auc, mean_pixel_auc, img_roc_auc_old, per_pixel_rocauc_old)), log)
epoch_time.update(time.time() - start_time)
start_time = time.time()
train(models, epoch, train_loader, optimizers, log)
train_dataset.shuffle_dataset()
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True, **kwargs)
log.close()
def train(models, epoch, train_loader, optimizers, log):
STN = models[0]
ENC = models[1]
PRED = models[2]
STN_optimizer = optimizers[0]
ENC_optimizer = optimizers[1]
PRED_optimizer = optimizers[2]
STN.train()
ENC.train()
PRED.train()
total_losses = AverageMeter()
for (query_img, support_img_list, _) in tqdm(train_loader):
STN_optimizer.zero_grad()
ENC_optimizer.zero_grad()
PRED_optimizer.zero_grad()
query_img = query_img.squeeze(0).to(device)
query_feat = STN(query_img)
support_img = support_img_list.squeeze(0).to(device)
B,K,C,H,W = support_img.shape
support_img = support_img.view(B * K, C, H, W)
support_feat = STN(support_img)
support_feat = support_feat / K
_, C, H, W = support_feat.shape
support_feat = support_feat.view(B, K, C, H, W)
support_feat = torch.sum(support_feat, dim=1)
z1 = ENC(query_feat)
z2 = ENC(support_feat)
p1 = PRED(z1)
p2 = PRED(z2)
total_loss = CosLoss(p1,z2, Mean=True)/2 + CosLoss(p2,z1, Mean=True)/2
total_losses.update(total_loss.item(), query_img.size(0))
total_loss.backward()
STN_optimizer.step()
ENC_optimizer.step()
PRED_optimizer.step()
print_log(('Train Epoch: {} Total_Loss: {:.6f}'.format(epoch, total_losses.avg)), log)
def test(models, cur_epoch, fixed_fewshot_list, test_loader, **kwargs):
STN = models[0]
ENC = models[1]
PRED = models[2]
STN.eval()
ENC.eval()
PRED.eval()
train_outputs = OrderedDict([('layer1', []), ('layer2', []), ('layer3', [])])
test_outputs = OrderedDict([('layer1', []), ('layer2', []), ('layer3', [])])
support_img = fixed_fewshot_list[cur_epoch]
augment_support_img = support_img
# rotate img with small angle
for angle in [-np.pi / 4, -3 * np.pi / 16, -np.pi / 8, -np.pi / 16, np.pi / 16, np.pi / 8, 3 * np.pi / 16,
np.pi / 4]:
rotate_img = rot_img(support_img, angle)
augment_support_img = torch.cat([augment_support_img, rotate_img], dim=0)
# translate img
for a, b in [(0.2, 0.2), (-0.2, 0.2), (-0.2, -0.2), (0.2, -0.2), (0.1, 0.1), (-0.1, 0.1), (-0.1, -0.1),
(0.1, -0.1)]:
trans_img = translation_img(support_img, a, b)
augment_support_img = torch.cat([augment_support_img, trans_img], dim=0)
# hflip img
flipped_img = hflip_img(support_img)
augment_support_img = torch.cat([augment_support_img, flipped_img], dim=0)
# rgb to grey img
greyed_img = grey_img(support_img)
augment_support_img = torch.cat([augment_support_img, greyed_img], dim=0)
# rotate img in 90 degree
for angle in [1, 2, 3]:
rotate90_img = rot90_img(support_img, angle)
augment_support_img = torch.cat([augment_support_img, rotate90_img], dim=0)
augment_support_img = augment_support_img[torch.randperm(augment_support_img.size(0))]
# torch version
with torch.no_grad():
support_feat = STN(augment_support_img.to(device))
support_feat = torch.mean(support_feat, dim=0, keepdim=True)
train_outputs['layer1'].append(STN.stn1_output)
train_outputs['layer2'].append(STN.stn2_output)
train_outputs['layer3'].append(STN.stn3_output)
for k, v in train_outputs.items():
train_outputs[k] = torch.cat(v, 0)
# Embedding concat
embedding_vectors = train_outputs['layer1']
for layer_name in ['layer2', 'layer3']:
embedding_vectors = embedding_concat(embedding_vectors, train_outputs[layer_name], True)
# calculate multivariate Gaussian distribution
B, C, H, W = embedding_vectors.size()
embedding_vectors = embedding_vectors.view(B, C, H * W)
mean = torch.mean(embedding_vectors, dim=0)
cov = torch.zeros(C, C, H * W).to(device)
I = torch.eye(C).to(device)
for i in range(H * W):
cov[:, :, i] = torch.cov(embedding_vectors[:, :, i].T) + 0.01 * I
train_outputs = [mean, cov]
# torch version
query_imgs = []
gt_list = []
mask_list = []
score_map_list = []
for (query_img, _, mask, y) in test_loader:
query_imgs.extend(query_img.cpu().detach().numpy())
gt_list.extend(y.cpu().detach().numpy())
mask_list.extend(mask.cpu().detach().numpy())
# model prediction
query_feat = STN(query_img.to(device))
z1 = ENC(query_feat)
z2 = ENC(support_feat)
p1 = PRED(z1)
p2 = PRED(z2)
loss = CosLoss(p1, z2, Mean=False) / 2 + CosLoss(p2, z1, Mean=False) / 2
loss_reshape = F.interpolate(loss.unsqueeze(1), size=query_img.size(2), mode='bilinear',
align_corners=False).squeeze(0)
score_map_list.append(loss_reshape.cpu().detach().numpy())
test_outputs['layer1'].append(STN.stn1_output)
test_outputs['layer2'].append(STN.stn2_output)
test_outputs['layer3'].append(STN.stn3_output)
for k, v in test_outputs.items():
test_outputs[k] = torch.cat(v, 0)
# Embedding concat
embedding_vectors = test_outputs['layer1']
for layer_name in ['layer2', 'layer3']:
embedding_vectors = embedding_concat(embedding_vectors, test_outputs[layer_name], True)
# calculate distance matrix
B, C, H, W = embedding_vectors.size()
embedding_vectors = embedding_vectors.view(B, C, H * W)
dist_list = []
for i in range(H * W):
mean = train_outputs[0][:, i]
conv_inv = torch.linalg.inv(train_outputs[1][:, :, i])
dist = [mahalanobis_torch(sample[:, i], mean, conv_inv) for sample in embedding_vectors]
dist_list.append(dist)
dist_list = torch.tensor(dist_list).transpose(1, 0).reshape(B, H, W)
# upsample
score_map = F.interpolate(dist_list.unsqueeze(1), size=query_img.size(2), mode='bilinear',
align_corners=False).squeeze().numpy()
# apply gaussian smoothing on the score map
for i in range(score_map.shape[0]):
score_map[i] = gaussian_filter(score_map[i], sigma=4)
return score_map, query_imgs, gt_list, mask_list
def adjust_learning_rate(optimizers, init_lrs, epoch, args):
"""Decay the learning rate based on schedule"""
for i in range(3):
cur_lr = init_lrs[i] * 0.5 * (1. + math.cos(math.pi * epoch / args.epochs))
for param_group in optimizers[i].param_groups:
param_group['lr'] = cur_lr
if __name__ == '__main__':
main()