-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
462 lines (368 loc) · 17.8 KB
/
trainer.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
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
import logging
import torch.nn as nn
import torch
import numpy as np
import torch.nn.functional as F
import atk_models
# custom weights initialization called on netG and netD
import tools
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 1)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
class GAN_Attack:
def __init__(self,
args,
tar_model,
perb_eps = 0.3, # fashion:0.15 mnist:0.3 patchedmnist:0.3
box_min = 0,
box_max = 1,
plot_save_dir = None): #
self.args = args
self.device = args.device
self.tar_model = tar_model
self.perb_eps = perb_eps
self.box_min = box_min
self.box_max = box_max
self.batch_no = 0
self.epoch = 0
self.dataset = args.data_name
self.queries = 0
self.is_img = True
self.plot_save_dir = plot_save_dir
self.netGs = getattr(atk_models, args.data_name + '_gen')().to(args.device)
self.clamp = atk_models.ClippingLayer(min_value=-perb_eps, max_value=perb_eps) #
self.netDs = getattr(atk_models, args.data_name + '_dis')().to(args.device)
# initialize all weights
self.netGs.apply(weights_init)
self.netDs.apply(weights_init)
# initialize optimizers
self.optimizer_G = torch.optim.Adam(self.netGs.parameters(),
lr=0.001)
self.optimizer_D = torch.optim.Adam(self.netDs.parameters(),
lr=0.001)
# if not os.path.exists(models_path):
# os.makedirs(models_path)
def _loss_hid(self, X, adv_X):
X = [x.to(self.device) for x in X]
adv_X = [x.to(self.device) for x in adv_X]
# P1 = self.tar_model(X)
# P2 = self.tar_model(adv_X)
self.tar_model(X)
P1 = self.tar_model.hidden
# Z_list = self
self.tar_model(adv_X)
P2 = self.tar_model.hidden
# P2 = self.tar_model(adv_X)
# P1 = torch.zeros_like(P2).to(P2.device)
# P1[:, 1] = 1.0
self.queries += 1
delta_P = P1 - P2
dist_loss = 0.5 * torch.sum(torch.norm(delta_P, 2, dim=1))
return dist_loss # is beta * ||f(x) - Cluster||^2_2
def _loss_multi_hid_mvae(self, X, adv_X):
X = [x.to(self.device) for x in X]
adv_X = [adv_x.to(self.device) for adv_x in adv_X]
# P1 = self.tar_model(X)
# P2 = self.tar_model(adv_X)
self.tar_model(X)
P1 = self.tar_model.alpha
Z1_list = self.tar_model.encoder_outputs
self.tar_model(adv_X)
P2 = self.tar_model.alpha
Z2_list = self.tar_model.encoder_outputs
# P2 = self.tar_model(adv_X)
# P1 = torch.zeros_like(P2).to(P2.device)
# P1[:, 1] = 1.0
self.queries += 1
delta_P = P1 - P2
distP_loss = 0.5 * torch.sum(torch.norm(delta_P, 2, dim=1))
delta_Z_list = [Z1 - Z2 for Z1, Z2 in zip(Z1_list, Z2_list)]
delta_Z_list = [0.5 * torch.sum(torch.norm(delta_Z, 2, dim=1)) for delta_Z in delta_Z_list]
delta_Z_loss = sum(delta_Z_list)
dist_loss = distP_loss + delta_Z_loss
return dist_loss # is beta * ||f(x) - Cluster||^2_2
def _loss_multi_hid(self, X, adv_X):
X = [x.to(self.device) for x in X]
adv_X = [adv_x.to(self.device) for adv_x in adv_X]
# P1 = self.tar_model(X)
# P2 = self.tar_model(adv_X)
self.tar_model(X)
P1 = self.tar_model.hidden
Z1_list = self.tar_model.encoder_outputs
self.tar_model(adv_X)
P2 = self.tar_model.hidden
Z2_list = self.tar_model.encoder_outputs
# P2 = self.tar_model(adv_X)
# P1 = torch.zeros_like(P2).to(P2.device)
# P1[:, 1] = 1.0
self.queries += 1
delta_P = P1 - P2
distP_loss = 0.5 * torch.sum(torch.norm(delta_P, 2, dim=1)) #
delta_Z_list = [Z1 - Z2 for Z1, Z2 in zip(Z1_list, Z2_list)]
delta_Z_list = [0.5 * torch.sum(torch.norm(delta_Z, 2, dim=1)) for delta_Z in delta_Z_list]
delta_Z_loss = sum(delta_Z_list)
dist_loss = distP_loss + delta_Z_loss
return dist_loss # is beta * ||f(x) - Cluster||^2_2
def train_batch(self, views):
perturbs = self.netGs(views) # list
# add a clipping trick
perturbs = [self.clamp(item) for item in perturbs] #
for i in range(1):
adv_views = []
for i, x in enumerate(views):
adv_image = perturbs[i] + x
if self.is_img:
adv_image = torch.clamp(adv_image, self.box_min, self.box_max) #
adv_views.append(adv_image)
self.optimizer_D.zero_grad()
pred_real = torch.cat(self.netDs(views), dim=0) #
loss_D_real = F.mse_loss(pred_real, torch.ones_like(pred_real, device=self.device))
loss_D_real.backward()
adv_views_detach = [item.detach() for item in adv_views] #
pred_fake = torch.cat(self.netDs(adv_views_detach), dim=0) #
loss_D_fake = F.mse_loss(pred_fake, torch.zeros_like(pred_fake, device=self.device))
loss_D_fake.backward()
loss_D_GAN = loss_D_fake + loss_D_real
self.optimizer_D.step()
# optimize G
for i in range(1):
self.optimizer_G.zero_grad()
# cal G's loss in GAN
pred_fake = torch.cat(self.netDs(adv_views), dim=0) #
loss_G_fake = F.mse_loss(pred_fake, torch.ones_like(pred_fake, device=self.device))
loss_G_fake.backward(retain_graph=True)
# calculate perturbation norm
loss_perturb = 0
for p2 in perturbs:
loss_perturb += torch.mean(torch.norm(p2.view(p2.shape[0], -1), 2, dim=1)) #
# loss_p = torch.mean(torch.norm(p2.view(p2.shape[0], -1), 2, dim=1)) #
# loss_perturb += torch.max(loss_p-self.perb_eps, torch.zeros(1, device=self.device))
if self.args.atk_mode == 0:
loss_adv = self._loss_hid(views, adv_views) # works
elif self.args.atk_mode == 2:
loss_adv = self._loss_multi_hid_mvae(views, adv_views) # works
else:
loss_adv = self._loss_multi_hid(views, adv_views)
loss_adv = -loss_adv # works
adv_lambda = 5 # 5
pert_lambda = 1
loss_G = pert_lambda * loss_perturb + adv_lambda * loss_adv
# loss_G = loss_adv
loss_G.backward()
self.optimizer_G.step()
return loss_D_GAN.item(), loss_G.item(), loss_perturb.item(), loss_adv.item(), adv_views_detach
# return 0, loss_G.item(), loss_perturb.item(), loss_adv.item(), 0
def train(self, train_dataloader, test_dataloader, epochs, atk_save_dir=None):
mtc_list = []
mtc = self.val_real(test_dataloader)
mtc_list.append(np.array([mtc['acc'], mtc['nmi'], mtc['ari']]))
for epoch in range(1, epochs + 1):
self.epoch = epoch
if epoch == 20:
self.optimizer_G = torch.optim.Adam(self.netGs.parameters(),
lr=0.0001)
self.optimizer_D = torch.optim.Adam(self.netDs.parameters(),
lr=0.0001)
loss_D_sum = 0
loss_G_fake_sum = 0
loss_perturb_sum = 0
loss_adv_sum = 0
torch.cuda.empty_cache()
self.netGs.train()
self.netDs.train()
for batch_idx, Data in enumerate(train_dataloader):
views = Data[0:-1]
labels = Data[-1]
views = [item.to(self.device) for item in views]
labels.to(self.device)
# train
loss_D_batch, loss_G_fake_batch, loss_perturb_batch, loss_adv_batch, adv_views_detach = \
self.train_batch(views)
loss_D_sum += loss_D_batch
loss_G_fake_sum += loss_G_fake_batch
loss_perturb_sum += loss_perturb_batch
loss_adv_sum += loss_adv_batch
#
mtc = self.val_fake(test_dataloader)
mtc_list.append(np.array([mtc['acc'], mtc['nmi'], mtc['ari']]))
# print statistics
self.batch_no += 1
num_batch = len(train_dataloader)
logging.info("epoch %d:\nloss_D: %.3f, loss_G_fake: %.3f,\
\nloss_perturb: %.3f, loss_adv: %.3f, \n" %
(epoch, loss_D_sum / num_batch, loss_G_fake_sum / num_batch,
loss_perturb_sum / num_batch, loss_adv_sum / num_batch))
if atk_save_dir is not None:
torch.save(self.netGs.state_dict(), str(atk_save_dir) + '/{}.ckpt'.format(epoch))
logging.info('atk model has been saved in {}/{}.ckpt'.format(str(atk_save_dir), epoch))
return mtc, mtc_list
def plot_images(self, views, adv_views, mtc):
imgs_tensor_list = []
title_list = []
for i in range(len(views)):
imgs_tensor_list.append(views[i][:5])
title_list.append('v{}_real'.format(i+1))
imgs_tensor_list.append(adv_views[i][:5])
title_list.append('v{}_fake'.format(i + 1))
args = {
'epoch': self.epoch,
'perb_eps': self.perb_eps,
'acc': mtc['acc']
}
# if self.fig_saved_dir is not None:
# fig_saved_path
# hang_tools.plot_result(imgs_tensor_list, title_list, args, fig_saved_path=self.fig_saved_dir)
def plot_ad_images(self, test_loader, fig_saved_dir=None):
data_base = next(iter(test_loader)) #
# origin_base
views_base = data_base[:-2]
views_base = [item.to(self.device, non_blocking=True) for item in views_base]
label_base = data_base[-2]
plabel_base = data_base[-1]
# perb_base
perturbs_base = self.netGs(views_base) # list
perturbs_base = [self.clamp(item) for item in perturbs_base]
# perturbs_base_ = [torch.clamp(item, 0, 1) for item in perturbs_base] # 负扰动置0
# ad_base
adv_views_base = []
for i, x in enumerate(views_base):
adv_image = perturbs_base[i] + x
if self.is_img:
adv_image = torch.clamp(adv_image.detach(), self.box_min, self.box_max)
adv_views_base.append(adv_image)
# prepare data
view_tensor_lists = [[] for v in range(len(views_base))]
# view2_tensor_list = []
label_list = []
plabel_list = []
for i in range(len(label_base)):
if label_base[i] == plabel_base[i]:
continue
label_list.append(int(label_base[i]))
plabel_list.append(int(plabel_base[i]))
for v in range(len(views_base)):
view_tensor_lists[v].append(torch.stack([views_base[v][i], perturbs_base[v][i], adv_views_base[v][i]], dim=0))
if len(label_list) == 10:
break
for v in range(len(views_base)):
if fig_saved_dir is not None:
fig_saved_path = str(fig_saved_dir) + '/view{}.eps'.format(v)
else:
fig_saved_path = None
tools.plot_ad_result(view_tensor_lists[v], label_list, plabel_list, fig_saved_path)
def plot_ad_images_kmeans(self, test_loader, kmeans_to_true_cluster_labels=None):
data_base = next(iter(test_loader)) #
# origin_base
views_base = data_base[:-1]
views_base = [item.to(self.device, non_blocking=True) for item in views_base]
label_base = data_base[-1]
print(label_base)
# perb_base
perturbs_base = self.netGs(views_base) # list
perturbs_base = [self.clamp(item) for item in perturbs_base]
# perturbs_base_ = [torch.clamp(item, 0, 1) for item in perturbs_base] # 负扰动置0
# ad_base
adv_views_base = []
for i, x in enumerate(views_base):
adv_image = perturbs_base[i] + x
if self.is_img:
adv_image = torch.clamp(adv_image.detach(), self.box_min, self.box_max)
adv_views_base.append(adv_image)
# plabel_base
pred_base = list(range(len(adv_views_base[0])))
print(pred_base)
pred_a_base = list(range(len(adv_views_base[0])))
print(pred_a_base)
# prepare data
view1_tensor_list = []
view2_tensor_list = []
label_list = []
plabel_list = []
for i in range(len(label_base)):
# if (pred_a_base[i] == label_base[i]) or (label_base[i] in label_list):
# continue
label_list.append(int(label_base[i]))
plabel_list.append(int(pred_a_base[i]))
# if self.args.data_name == 'regdb':
# from hang_tools import clf_to_raw
# retransform = clf_to_raw()
# for v in range(2):
# views_base[v][i] = torch.clamp(retransform(views_base[v][i]), self.box_min, self.box_max)
# adv_views_base[v][i] = torch.clamp(retransform(adv_views_base[v][i]), self.box_min, self.box_max)
view1_tensor_list.append(torch.stack([views_base[0][i], perturbs_base[0][i], adv_views_base[0][i]], dim=0))
view2_tensor_list.append(torch.stack([views_base[1][i], perturbs_base[1][i], adv_views_base[1][i]], dim=0))
if len(label_list) == 10:
break
tools.plot_ad_result(view1_tensor_list, label_list, plabel_list)
tools.plot_ad_result(view2_tensor_list, label_list, plabel_list)
def val_fake(self, test_loader, is_get_pred=False):
adv_list = []
self.netGs.eval()
save_images = None
for idx, batch in enumerate(test_loader):
batch = [item.to(self.device, non_blocking=True) for item in batch]
views = batch[0:-1]
label = batch[-1]
perturbs = self.netGs(views) # list
perturbs = [self.clamp(item) for item in perturbs]
adv_views = []
for i, x in enumerate(views):
adv_image = perturbs[i] + x
if self.is_img:
adv_image = torch.clamp(adv_image, self.box_min, self.box_max)
adv_views.append(adv_image.detach())
#
if idx == 0 and self.epoch % 1 == 0:
if self.is_img: #
save_images = [views, adv_views]
adv_views.append(label)
adv_list.append(self.tar_model._val_test_step(adv_views, idx, 'test'))
if is_get_pred:
mtc, pred = self.tar_model._val_test_epoch_end(adv_list, 'test', is_get_pred=is_get_pred)
logging.info('fake data metric: acc:{:4f} nmi:{:4f} ari:{:4f}'.format(mtc['acc'], mtc['nmi'], mtc['ari']))
return mtc, pred
else:
mtc = self.tar_model._val_test_epoch_end(adv_list, 'test')
logging.info(
'fake data metric: acc:{:4f} nmi:{:4f} ari:{:4f}'.format(mtc['acc'], mtc['nmi'], mtc['ari']))
if save_images is not None:
self.plot_images(save_images[0], save_images[1], mtc)
return mtc
def val_real(self, test_loader, is_get_pred=False):
test_list = []
for idx, batch in enumerate(test_loader):
batch = [item.to(self.device) for item in batch]
test_list.append(self.tar_model._val_test_step(batch, idx, 'test'))
if is_get_pred:
mtc, pred = self.tar_model._val_test_epoch_end(test_list, 'test', is_get_pred=is_get_pred)
logging.info('real data metric: acc:{:4f} nmi:{:4f} ari:{:4f}'.format(mtc['acc'], mtc['nmi'], mtc['ari']))
return mtc, pred
else:
mtc = self.tar_model._val_test_epoch_end(test_list, 'test')
logging.info(
'real data metric:acc:{:4f} nmi:{:4f} ari:{:4f}'.format(mtc['acc'], mtc['nmi'], mtc['ari']))
return mtc
def get_kmeans_to_true_cluster_labels(self, test_loader):
import lib.metrics as metrics
test_list = []
for idx, batch in enumerate(test_loader):
batch = [item.to(self.device, non_blocking=True) for item in batch]
test_list.append(self.tar_model._val_test_step(batch, idx, 'test'))
labels = np.concatenate(test_list, axis=1)
t_labels = labels[0]
p_labels = labels[1]
n_clusters = np.size(np.unique(t_labels))
confusion_matrix = metrics.confusion_matrix(t_labels, p_labels, labels=None) #
# compute accuracy based on optimal 1:1 assignment of clusters to labels
cost_matrix = metrics.calculate_cost_matrix(confusion_matrix, n_clusters) #
indices = metrics.Munkres().compute(cost_matrix)
kmeans_to_true_cluster_labels = metrics.get_cluster_labels_from_indices(indices)
print('got kmeans_to_true_cluster_labels!!!')
print('true labels:{}'.format(t_labels[:30]))
print('predict labels:{}'.format(p_labels[:30]))
print('predict to true labels:{}'.format(kmeans_to_true_cluster_labels[p_labels[:30]]))
return kmeans_to_true_cluster_labels