-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfl_trainer.py
1875 lines (1616 loc) · 102 KB
/
fl_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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import torch
import torch.nn.functional as F
import datetime
import time
from utils import *
from defense import *
from tqdm.auto import tqdm
from models.vgg import get_vgg_model
import pandas as pd
from torch.nn.utils import parameters_to_vector, vector_to_parameters
from lira_helper import *
import csv_record
import datasets
from termcolor import colored
def exponential_decay(init_val, decay_rate, t):
return init_val*(1.0 - decay_rate)**t
class Net(nn.Module):
def __init__(self, num_classes):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, num_classes)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
output = self.fc2(x)
#output = F.log_softmax(x, dim=1)
return output
def get_results_filename(poison_type, attack_method, model_replacement, project_frequency, defense_method, norm_bound, prox_attack, fixed_pool=False, model_arch="vgg9"):
filename = "{}_{}_{}".format(poison_type, model_arch, attack_method)
if fixed_pool:
filename += "_fixed_pool"
if model_replacement:
filename += "_with_replacement"
else:
filename += "_without_replacement"
if attack_method == "pgd":
filename += "_1_{}".format(project_frequency)
if prox_attack:
filename += "_prox_attack"
if defense_method in ("norm-clipping", "norm-clipping-adaptive", "weak-dp"):
filename += "_{}_m_{}".format(defense_method, norm_bound)
elif defense_method in ("krum", "multi-krum", "rfa"):
filename += "_{}".format(defense_method)
filename += "_acc_results.csv"
return filename
def calc_norm_diff(gs_model, vanilla_model, epoch, fl_round, mode="bad"):
norm_diff = 0
for p_index, p in enumerate(gs_model.parameters()):
norm_diff += torch.norm(list(gs_model.parameters())[p_index] - list(vanilla_model.parameters())[p_index]) ** 2
norm_diff = torch.sqrt(norm_diff).item()
if mode == "bad":
#pdb.set_trace()
logger.info("===> ND `|w_bad-w_g|` in local epoch: {} | FL round: {} |, is {}".format(epoch, fl_round, norm_diff))
elif mode == "normal":
logger.info("===> ND `|w_normal-w_g|` in local epoch: {} | FL round: {} |, is {}".format(epoch, fl_round, norm_diff))
elif mode == "avg":
logger.info("===> ND `|w_avg-w_g|` in local epoch: {} | FL round: {} |, is {}".format(epoch, fl_round, norm_diff))
return norm_diff
def vectorize_net(net):
return torch.cat([p.view(-1) for p in net.parameters()])
def load_model_weight(net, weight):
index_bias = 0
for p_index, p in enumerate(net.parameters()):
p.data = weight[index_bias:index_bias+p.numel()].view(p.size())
index_bias += p.numel()
def fed_nova_aggregator(model, net_list, list_ni, device, list_ai):
# https://github.com/Xtra-Computing/NIID-Bench/blob/692569f790af0f5908dba23a15d4d80ce7e7aec4/experiments.py#L375
total_n = sum(list_ni)
print(f"Aggregating models with FedNova with aggregation weight {list_ai}")
print(f"List of sample size {list_ni}")
weight_accumulator = {}
for name, params in model.state_dict().items():
weight_accumulator[name] = torch.zeros(params.size()).to(device)
for net_index, net in enumerate(net_list):
for name, data in net.state_dict().items():
# weight_accumulator[name].add_( ( (data - model.state_dict()[name]) / list_ai[net_index] ) * (list_ni[net_index] / total_n))
weight_accumulator[name].add_( torch.true_divide( (model.state_dict()[name] - data), list_ai[net_index] ) * list_ni[net_index] / total_n)
coeff = 0.0
for i in range(len(list_ai)):
coeff += list_ai[i] * list_ni[i] / total_n
# print(f"ai: {list_ai[i]} ni: {list_ni[i]} coeff: {coeff}")
print(f"coeff: {coeff}")
for name, params in model.state_dict().items():
update_per_layer = coeff * weight_accumulator[name]
# params.add_(coeff * update_per_layer)
if params.type() != update_per_layer.type():
params.sub_(update_per_layer.to(torch.int64))
else:
params.sub_(update_per_layer)
return model
def fed_avg_aggregator(init_model, net_list, net_freq, device, model="lenet"):
# import IPython
# IPython.embed()
weight_accumulator = {}
for name, params in init_model.state_dict().items():
weight_accumulator[name] = torch.zeros_like(params).float()
for i in range(0, len(net_list)):
diff = dict()
for name, data in net_list[i].state_dict().items():
# diff[name] = (data - model_server_before_aggregate.state_dict()[name]).cpu().detach().numpy()
diff[name] = (data - init_model.state_dict()[name])
try:
weight_accumulator[name].add_(net_freq[i] * diff[name])
# weight_accumulator[name].add_(0.1 * diff[name])
except Exception as e:
print(e)
import IPython
IPython.embed()
exit(0)
# print(f"diff: {diff}")
for idl, (name, data) in enumerate(init_model.state_dict().items()):
update_per_layer = weight_accumulator[name] # * self.conf["lambda"]
if data.type() != update_per_layer.type():
data.add_(update_per_layer.to(torch.int64))
else:
data.add_(update_per_layer)
return init_model
def estimate_wg(model, device, train_loader, optimizer, epoch, log_interval, criterion):
logger.info("Prox-attack: Estimating wg_hat")
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
logger.info('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def train_lira(model, atkmodel, tgtmodel, optimizer, atkmodel_optimizer, train_loader, criterion, atk_eps = 0.001, attack_alpha = 0.5,
attack_portion = 1.0, clip_image=None, target_transform=None, atk_train_epoch=1, atkmodel_train=False, device=None,
pgd_attack = False, batch_idx = 1, project_frequency = 1, pgd_eps = None, model_original=None, adv_optimizer=None,
proj="l_2", mask_grad_list=[], aggregator="fedavg", wg_hat=None, mu=0.1, local_e=0):
# print(f"attack_alpha: {attack_alpha}")
# print(f"atkmodel_optimizer: {atkmodel_optimizer}")
tgtmodel.eval()
wg_clone = copy.deepcopy(model)
# atk_copy = copy.deepcopy(tgtmodel)
# atk_optimizer = None
loss_fn = nn.CrossEntropyLoss()
func_fn = loss_fn
correct = 0
correct_clean = 0
correct_poison = 0
dataset_size = 0
poison_size = 0
clean_size = 0
loss_list = []
if not atkmodel_train:
# optimizer = optim.SGD(model.parameters(), lr=0.001)
model.train()
# atkmodel.eval()
# Sub-training phase
for batch_idx, batch in enumerate(train_loader):
bs = len(batch)
data, targets = batch
# data, target = data.to(device), target.to(device)
# clean_images, clean_targets, poison_images, poison_targets, poisoning_per_batch = get_poison_batch(batch, attack_portion)
clean_images, clean_targets = copy.deepcopy(data).to(device), copy.deepcopy(targets).to(device)
poison_images, poison_targets = copy.deepcopy(data).to(device), copy.deepcopy(targets).to(device)
# dataset_size += len(data)
clean_size += len(clean_images)
optimizer.zero_grad()
if pgd_attack:
adv_optimizer.zero_grad()
output = model(clean_images)
loss_clean = loss_fn(output, clean_targets)
if aggregator == "fedprox":
# print(f"fedprox!!!")
wg_hat_vec = parameters_to_vector(list(wg_hat.parameters()))
model_vec = parameters_to_vector(list(model.parameters()))
prox_term = torch.norm(wg_hat_vec - model_vec)**2
loss_clean = loss_clean + mu/2*prox_term
if attack_alpha == 1.0:
optimizer.zero_grad()
# atkmodel_optimizer.zero_grad()
loss_clean.backward()
if mask_grad_list:
apply_grad_mask(model, mask_grad_list)
if not pgd_attack:
optimizer.step()
else:
if proj == "l_inf":
w = list(model.parameters())
n_layers = len(w)
# adversarial learning rate
eta = 0.001
for i in range(len(w)):
# uncomment below line to restrict proj to some layers
if True:#i == 6 or i == 8 or i == 10 or i == 0 or i == 18:
w[i].data = w[i].data - eta * w[i].grad.data
# projection step
m1 = torch.lt(torch.sub(w[i], model_original[i]), -pgd_eps)
m2 = torch.gt(torch.sub(w[i], model_original[i]), pgd_eps)
w1 = (model_original[i] - pgd_eps) * m1
w2 = (model_original[i] + pgd_eps) * m2
w3 = (w[i]) * (~(m1+m2))
wf = w1+w2+w3
w[i].data = wf.data
else:
# do l2_projection
adv_optimizer.step()
w = list(model.parameters())
w_vec = parameters_to_vector(w)
model_original_vec = parameters_to_vector(model_original)
# make sure you project on last iteration otherwise, high LR pushes you really far
# Start
if (batch_idx%project_frequency == 0 or batch_idx == len(train_loader)-1) and (torch.norm(w_vec - model_original_vec) > pgd_eps):
# project back into norm ball
w_proj_vec = pgd_eps*(w_vec - model_original_vec)/torch.norm(
w_vec-model_original_vec) + model_original_vec
# plug w_proj back into model
vector_to_parameters(w_proj_vec, w)
pred = output.data.max(1)[1] # get the index of the max log-probability
loss_list.append(loss_clean.item())
correct_clean += pred.eq(clean_targets.data.view_as(pred)).cpu().sum().item()
else:
if attack_alpha < 1.0:
poison_size += len(poison_images)
# poison_images, poison_targets = poison_images.to(device), poison_targets.to(device)
with torch.no_grad():
noise = tgtmodel(poison_images) * atk_eps
atkdata = clip_image(poison_images + noise)
atktarget = target_transform(poison_targets)
# atkdata.requires_grad_(False)
# atktarget.requires_grad_(False)
if attack_portion < 1.0:
atkdata = atkdata[:int(attack_portion*bs)]
atktarget = atktarget[:int(attack_portion*bs)]
# import IPython
# IPython.embed()
atkoutput = model(atkdata.detach())
loss_poison = F.cross_entropy(atkoutput, atktarget.detach())
else:
loss_poison = torch.tensor(0.0).to(device)
loss2 = loss_clean * attack_alpha + (1.0 - attack_alpha) * loss_poison
optimizer.zero_grad()
loss2.backward()
if mask_grad_list:
apply_grad_mask(model, mask_grad_list)
if not pgd_attack:
optimizer.step()
else:
if proj == "l_inf":
w = list(model.parameters())
n_layers = len(w)
# adversarial learning rate
eta = 0.001
for i in range(len(w)):
# uncomment below line to restrict proj to some layers
if True:#i == 6 or i == 8 or i == 10 or i == 0 or i == 18:
w[i].data = w[i].data - eta * w[i].grad.data
# projection step
m1 = torch.lt(torch.sub(w[i], model_original[i]), -pgd_eps)
m2 = torch.gt(torch.sub(w[i], model_original[i]), pgd_eps)
w1 = (model_original[i] - pgd_eps) * m1
w2 = (model_original[i] + pgd_eps) * m2
w3 = (w[i]) * (~(m1+m2))
wf = w1+w2+w3
w[i].data = wf.data
else:
# do l2_projection
adv_optimizer.step()
w = list(model.parameters())
w_vec = parameters_to_vector(w)
model_original_vec = parameters_to_vector(list(model_original.parameters()))
# make sure you project on last iteration otherwise, high LR pushes you really far
if (local_e%project_frequency == 0 and batch_idx == len(train_loader)-1) and (torch.norm(w_vec - model_original_vec) > pgd_eps):
# project back into norm ball
w_proj_vec = pgd_eps*(w_vec - model_original_vec)/torch.norm(
w_vec-model_original_vec) + model_original_vec
vector_to_parameters(w_proj_vec, w)
loss_list.append(loss2.item())
pred = output.data.max(1)[1] # get the index of the max log-probability
poison_pred = atkoutput.data.max(1)[1] # get the index of the max log-probability
# correct += pred.eq(target.data.view_as(pred)).cpu().sum().item()
correct_clean += pred.eq(clean_targets.data.view_as(pred)).cpu().sum().item()
correct_poison += poison_pred.eq(atktarget.data.view_as(poison_pred)).cpu().sum().item()
else:
model.eval()
# atk_optimizer = optim.Adam(atkmodel.parameters(), lr=0.0002)
atkmodel.train()
# optimizer.zero_grad()
for batch_idx, (data, target) in enumerate(train_loader):
bs = data.size(0)
data, target = data.to(device), target.to(device)
# dataset_size += len(data)
poison_size += len(data)
###############################
#### Update the classifier ####
###############################
# with torch.no_grad():
noise = atkmodel(data) * atk_eps
atkdata = clip_image(data + noise)
atktarget = target_transform(target)
if attack_portion < 1.0:
atkdata = atkdata[:int(attack_portion*bs)]
atktarget = atktarget[:int(attack_portion*bs)]
# with torch.no_grad():
# atkoutput = wg_clone(atkdata)
atkoutput = model(atkdata)
loss_p = func_fn(atkoutput, atktarget)
loss2 = loss_p
# import IPython
# IPython.embed()
atkmodel_optimizer.zero_grad()
loss2.backward()
atkmodel_optimizer.step()
pred = atkoutput.data.max(1)[1] # get the index of the max log-probability
correct_poison += pred.eq(atktarget.data.view_as(pred)).cpu().sum().item()
loss_list.append(loss2.item())
clean_acc = 100.0 * (float(correct_clean)/float(clean_size)) if clean_size else 0.0
poison_acc = 100.0 * (float(correct_poison)/float(poison_size)) if poison_size else 0.0
training_avg_loss = sum(loss_list)/len(loss_list)
# training_avg_loss = 0.0
if atkmodel_train:
logger.info(colored("Training loss = {:.2f}, acc = {:.2f} of atk model this epoch".format(training_avg_loss, poison_acc), "yellow"))
else:
logger.info(colored("Training loss = {:.2f}, acc = {:.2f} of cls model this epoch".format(training_avg_loss, clean_acc), "yellow"))
logger.info("Training clean_acc is {:.2f}, poison_acc = {:.2f}".format(clean_acc, poison_acc))
del wg_clone
def train_baseline(model, optimizer, train_loader, criterion, dataset, device, target_transform=None, local_e=0):
loss_fn = nn.CrossEntropyLoss()
func_fn = loss_fn
total_loss = 0
correct = 0
correct_clean = 0
correct_poison = 0
total_loss = 0
poison_data_count = 0
dataset_size = 0
poison_size = 0
poison_data_count = 0
clean_size = 0
loss_list = []
model.train()
for batch_idx, batch in enumerate(train_loader):
bs = len(batch)
data, targets, poison_num, _, _ = get_poison_batch(batch, dataset, device, target_transform=target_transform)
# print(f"training targets: {targets}")
optimizer.zero_grad()
dataset_size += len(data)
poison_data_count += poison_num
output = model(data)
class_loss = nn.functional.cross_entropy(output, targets)
# distance_loss = helper.model_dist_norm_var(model, target_params_variables)
# Lmodel = αLclass + (1 − α)Lano; alpha_loss =1 fixed
# loss = helper.params['alpha_loss'] * class_loss + \
# (1 - helper.params['alpha_loss']) * distance_loss
loss = class_loss
loss.backward()
optimizer.step()
total_loss += loss.data
pred = output.data.max(1)[1] # get the index of the max log-probability
correct += pred.eq(targets.data.view_as(pred)).sum().item()
acc = 100.0 * (float(correct) / float(dataset_size))
total_l = total_loss / dataset_size
logger.info("Total loss is {:.2f}, training acc is {:.2f}".format(total_l, acc))
def test(model, device, test_loader, test_batch_size, criterion, mode="raw-task", dataset="cifar10", poison_type="fashion"):
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
if dataset == 'tiny-imagenet':
class_correct = list(0. for i in range(200))
class_total = list(0. for i in range(200))
if dataset in ("mnist", "emnist"):
target_class = 7
if mode == "raw-task":
classes = [str(i) for i in range(10)]
elif mode == "targetted-task":
if poison_type == 'ardis':
classes = [str(i) for i in range(10)]
else:
classes = ["T-shirt/top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle boot"]
elif dataset == "cifar10":
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# target_class = 2 for greencar, 9 for southwest
if poison_type in ("howto", "greencar-neo"):
target_class = 2
else:
target_class = 9
elif dataset == "tiny-imagenet":
classes = [str(i) for i in range(200)]
target_class = 1
model.eval()
test_loss = 0
correct = 0
backdoor_correct = 0
backdoor_tot = 0
final_acc = 0
task_acc = None
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
_, predicted = torch.max(output, 1)
c = (predicted == target).squeeze()
#test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
test_loss += criterion(output, target).item()
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
# check backdoor accuracy
if poison_type == 'ardis':
backdoor_index = torch.where(target == target_class)
target_backdoor = torch.ones_like(target[backdoor_index])
predicted_backdoor = predicted[backdoor_index]
backdoor_correct += (predicted_backdoor == target_backdoor).sum().item()
backdoor_tot = backdoor_index[0].shape[0]
#for image_index in range(test_batch_size):
for image_index in range(len(target)):
label = target[image_index]
class_correct[label] += c[image_index].item()
class_total[label] += 1
test_loss /= len(test_loader.dataset)
number_class = 10
if dataset == 'tiny-imagenet':
number_class = 200
if mode == "raw-task":
for i in range(number_class):
# logger.info('Accuracy of %5s : %.2f %%' % (
# classes[i], 100 * class_correct[i] / class_total[i]))
if i == target_class:
task_acc = 100 * class_correct[i] / class_total[i]
logger.info('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
final_acc = 100. * correct / len(test_loader.dataset)
elif mode == "targetted-task":
if dataset in ("mnist", "emnist"):
for i in range(10):
logger.info('Accuracy of %5s : %.2f %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
if poison_type == 'ardis':
# ensure 7 is being classified as 1
logger.info('Backdoor Accuracy of %.2f : %.2f %%' % (
target_class, 100 * backdoor_correct / backdoor_tot))
final_acc = 100 * backdoor_correct / backdoor_tot
else:
# trouser acc
final_acc = 100 * class_correct[1] / class_total[1]
elif dataset == "cifar10":
logger.info('#### Targetted Accuracy of %5s : %.2f %%' % (classes[target_class], 100 * class_correct[target_class] / class_total[target_class]))
final_acc = 100 * class_correct[target_class] / class_total[target_class]
return final_acc, task_acc
def lira_test(args, device, atkmodel, scratchmodel, target_transform,
train_loader, test_loader, epoch, trainepoch, clip_image,
testoptimizer=None, log_prefix='Internal', epochs_per_test=5):
#default phase 2 parameters to phase 1
if args['test_alpha'] is None:
args['test_alpha'] = args['attack_alpha']
if args['test_eps'] is None:
args['test_eps'] = args['eps']
print(f"\n------------------------------------\nStart postraining based on trajectories")
atkmodel.eval()
if testoptimizer is None:
testoptimizer = optim.SGD(scratchmodel.parameters(), lr=args['lr'])
for cepoch in range(trainepoch):
pbar = tqdm(enumerate(train_loader), total=len(train_loader), position=0, leave=True)
for batch_idx, (data, target) in pbar:
bs = data.size(0)
data, target = data.to(device), target.to(device)
testoptimizer.zero_grad()
with torch.no_grad():
noise = atkmodel(data) * args['test_eps']
atkdata = clip_image(data + noise)
atktarget = target_transform(target)
if args['attack_portion'] < 1.0:
atkdata = atkdata[:int(args['attack_portion']*bs)]
atktarget = atktarget[:int(args['attack_portion']*bs)]
atkoutput = scratchmodel(atkdata)
output = scratchmodel(data)
loss_clean = loss_fn(output, target)
loss_poison = loss_fn(atkoutput, atktarget)
loss = args['attack_alpha'] * loss_clean + (1-args['test_alpha']) * loss_poison
loss.backward()
testoptimizer.step()
if batch_idx % 10 == 0 or batch_idx == (len(train_loader)-1):
pbar.set_description(
'Test [{}-{}] Loss: Clean {:.4f} Poison {:.4f} Total {:.5f}'.format(
epoch, cepoch+1,
loss_clean.item(),
loss_poison.item(),
loss.item()
))
print(f"\n------------------------------------Start testing the model")
if cepoch % epochs_per_test == 0 or cepoch == trainepoch-1:
correct = 0
correct_transform = 0
test_loss = 0
test_transform_loss = 0
with torch.no_grad():
for data, target in test_loader:
bs = data.size(0)
data, target = data.to(device), target.to(device)
output = scratchmodel(data)
test_loss += loss_fn(output, target).item() * bs # sum up batch loss
pred = output.max(1, keepdim=True)[
1] # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
noise = atkmodel(data) * args['test_eps']
atkdata = clip_image(data + noise)
atkoutput = scratchmodel(atkdata)
test_transform_loss += loss_fn(atkoutput, target_transform(target)).item() * bs # sum up batch loss
atkpred = atkoutput.max(1, keepdim=True)[
1] # get the index of the max log-probability
correct_transform += atkpred.eq(
target_transform(target).view_as(atkpred)).sum().item()
test_loss /= len(test_loader.dataset)
test_transform_loss /= len(test_loader.dataset)
correct /= len(test_loader.dataset)
correct_transform /= len(test_loader.dataset)
print(
'\n{}-Test set [{}]: Loss: clean {:.4f} poison {:.4f}, Accuracy: clean {:.2f} poison {:.2f}'.format(
log_prefix, cepoch,
test_loss, test_transform_loss,
correct, correct_transform
))
# print(f"\nFinish the post testing------------------------------------")
return correct, correct_transform
def test_updated(args, device, atkmodel, model, target_transform,
train_loader, test_loader, epoch, trainepoch, clip_image,
testoptimizer=None, log_prefix='Internal', epochs_per_test=3,
dataset="cifar10", criterion=None, subpath_saved="", test_eps=None,
is_poison=False, ident="global", atk_baseline=False):
if args['test_alpha'] is None:
args['test_alpha'] = args['attack_alpha']
if args['test_eps'] is None:
args['test_eps'] = args['eps']
if not test_eps:
test_eps = args['test_eps']
# import IPython
# IPython.embed()
# start_time = time.time()
model.eval()
atkmodel.eval()
test_loss = 0
correct = 0
correct_transform = 0
test_loss = 0
test_transform_loss = 0
model = model.to(device)
atkmodel = atkmodel.to(device)
with torch.no_grad():
for idx, (data, target) in enumerate(test_loader):
start_time = time.time()
bs = data.size(0)
data, target = data.to(device), target.to(device)
# batch = (data, target)
output = model(data)
test_loss += criterion(output, target).item() * bs # sum up batch loss
pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
# print("Time for clean image: ", idx, time.time() - start_time)
noise = atkmodel(data) * test_eps
atkdata = clip_image(data + noise)
atktarget = target_transform(target)
# print("Time for atk image: ", idx, time.time() - start_time)
atkoutput = model(atkdata)
test_transform_loss += criterion(atkoutput, atktarget).item() * bs # sum up batch loss
atkpred = atkoutput.max(1, keepdim=True)[1] # get the index of the max log-probability
correct_transform += atkpred.eq(
target_transform(target).view_as(atkpred)).sum().item()
# print(f"Finish the post testing------------------------------------")
# print(f"Time for post testing: {time.time() - start_time}")
test_loss /= len(test_loader.dataset)
test_transform_loss /= len(test_loader.dataset)
correct /= len(test_loader.dataset)
correct_transform /= len(test_loader.dataset)
# print(f"\nTest result without retraining: ")
print(
'{}-Test set [{}]: Loss: clean {:.4f} poison {:.4f}, Accuracy: clean {:.2f} poison {:.2f}'.format(
log_prefix, 0,
test_loss, test_transform_loss,
correct, correct_transform
))
if is_poison:
csv_record.test_result.append([ident, epoch, test_loss, test_transform_loss, correct, correct_transform])
print(f"Time for end testing: {time.time() - start_time}")
return correct, correct_transform
class FederatedLearningTrainer:
def __init__(self, *args, **kwargs):
self.hyper_params = None
def run(self, client_model, *args, **kwargs):
raise NotImplementedError()
class FrequencyFederatedLearningTrainer(FederatedLearningTrainer):
def __init__(self, arguments=None, lira_args=None, *args, **kwargs):
#self.poisoned_emnist_dataset = arguments['poisoned_emnist_dataset']
self.vanilla_model = arguments['vanilla_model']
self.net_avg = arguments['net_avg']
self.net_dataidx_map = arguments['net_dataidx_map']
self.num_nets = arguments['num_nets']
self.part_nets_per_round = arguments['part_nets_per_round']
self.fl_round = arguments['fl_round']
self.local_training_period = arguments['local_training_period']
self.adversarial_local_training_period = arguments['adversarial_local_training_period']
# self.federated_optimizer = arguments['federated_optimizer'] # default: FedAvg
self.args_lr = arguments['args_lr']
self.args_gamma = arguments['args_gamma']
self.attacking_fl_rounds = arguments['attacking_fl_rounds']
self.poisoned_emnist_train_loader = arguments['poisoned_emnist_train_loader']
self.clean_train_loader = arguments['clean_train_loader']
self.vanilla_emnist_test_loader = arguments['vanilla_emnist_test_loader']
self.targetted_task_test_loader = arguments['targetted_task_test_loader']
self.batch_size = arguments['batch_size']
self.test_batch_size = arguments['test_batch_size']
self.log_interval = arguments['log_interval']
self.device = arguments['device']
self.save_model = arguments['save_model']
# self.num_dps_poisoned_dataset = int(arguments['num_dps_poisoned_dataset'] * (1.0+lira_args['attack_portion']))
self.num_dps_poisoned_dataset = arguments['num_dps_poisoned_dataset']
self.scale_weights_poison = arguments['scale_weights_poison']
self.defense_technique = arguments["defense_technique"]
self.norm_bound = arguments["norm_bound"]
self.attack_method = arguments["attack_method"]
self.dataset = arguments["dataset"]
self.model = arguments["model"]
self.criterion = nn.CrossEntropyLoss()
self.eps = arguments['eps']
self.eps_decay = arguments['eps_decay']
self.poison_type = arguments['poison_type']
self.model_replacement = arguments['model_replacement']
self.project_frequency = arguments['project_frequency']
self.adv_lr = arguments['adv_lr']
self.atk_lr = arguments['atk_lr']
self.prox_attack = arguments['prox_attack']
self.attack_case = arguments['attack_case']
self.stddev = arguments['stddev']
self.atkmodel = arguments['atkmodel']
self.tgtmodel = arguments['tgtmodel']
self.lira_args = lira_args
self.baseline = arguments['baseline']
self.retrain = arguments['retrain']
self.aggregator = arguments['aggregator'] # default: fedavg
# self.create_net = arguments['create_net']
self.scratch_model = arguments['scratch_model']
self.atk_eps = arguments['atk_eps']
self.atk_test_eps = arguments['atk_test_eps']
self.scale_factor = arguments['scale']
self.mask_ratio = 0.30 #TODO: modify it later, used only for Neurotoxin attack strategy
self.flatten_net_avg = None
self.folder_path = arguments['folder_path']
self.historical_grad_mask = None
self.atk_baseline = arguments['atk_baseline']
if self.attack_method == "pgd":
self.pgd_attack = True
self.neurotoxin_attack = False
elif self.attack_method == "neurotoxin":
self.neurotoxin_attack = True
self.pgd_attack = False
else:
self.pgd_attack = False
self.neurotoxin_attack = False
if arguments["defense_technique"] == "no-defense":
self._defender = None
elif arguments["defense_technique"] == "norm-clipping" or arguments["defense_technique"] == "norm-clipping-adaptive":
self._defender = WeightDiffClippingDefense(norm_bound=arguments['norm_bound'])
elif arguments["defense_technique"] == "weak-dp":
# doesn't really add noise. just clips
self._defender = WeightDiffClippingDefense(norm_bound=arguments['norm_bound'])
elif arguments["defense_technique"] == "krum":
self._defender = Krum(mode='krum', num_workers=self.part_nets_per_round, num_adv=1)
elif arguments["defense_technique"] == "multi-krum":
self._defender = Krum(mode='multi-krum', num_workers=self.part_nets_per_round, num_adv=1)
elif arguments["defense_technique"] == "rfa":
self._defender = RFA()
elif arguments["defense_technique"] == "crfl":
self._defender = CRFL()
elif arguments["defense_technique"] == "rlr":
pytorch_total_params = sum(p.numel() for p in self.net_avg.parameters())
args_rlr={
'aggr':'avg',
'noise':0,
'clip': 0,
'server_lr': self.args_lr,
}
# theta = 2
theta = 1
self._defender = RLR(n_params=pytorch_total_params, device=self.device, args=args_rlr, robustLR_threshold=theta)
elif arguments["defense_technique"] == "foolsgold":
pytorch_total_params = sum(p.numel() for p in self.net_avg.parameters())
self._defender = FoolsGold(num_clients=self.part_nets_per_round, num_classes=10, num_features=pytorch_total_params)
else:
NotImplementedError("Unsupported defense method !")
def run(self, wandb_ins):
expected_atk_threshold = 0.85
cnt_masks = 0
print(f"self.scale_weights_poison: {self.scale_weights_poison}")
# if self.dataset == "mnist":
# expected_atk_threshold = 0.90
# elif self.dataset == "cifar10":
# expected_atk_threshold = 0.90
main_task_acc = []
raw_task_acc = []
backdoor_task_acc = []
fl_iter_list = []
adv_norm_diff_list = []
wg_norm_list = []
print(colored('Under defense technique = {}'.format(self.defense_technique), 'red'))
# variables for LIRA algorithms only
trainlosses = []
best_acc_clean = 0
best_acc_poison = 0
clip_image = get_clip_image(self.dataset)
attack_train_epoch = self.lira_args['train_epoch']
target_transform = get_target_transform(self.lira_args)
pytorch_total_params = sum(p.numel() for p in self.net_avg.parameters())
# The number of previous iterations to use FoolsGold on
memory_size = 0
delta_memory = np.zeros((self.num_nets, pytorch_total_params, memory_size))
summed_deltas = np.zeros((self.num_nets, pytorch_total_params))
basepath, checkpoint_path, bestmodel_path = create_paths(self.lira_args)
print('========== PATHS ==========')
print(f'Basepath: {basepath}')
print(f'Checkpoint Model: {checkpoint_path}')
print(f'Best Model: {bestmodel_path}')
# LOAD_ATK_MODEL = False
LOAD_ATK_MODEL = True
checkpoint_path = bestmodel_path
if os.path.exists(checkpoint_path) and not self.retrain:
# Load previously saved models
checkpoint = torch.load(checkpoint_path)
print(colored('Load existing attack model from path {}'.format(checkpoint_path), 'red'))
self.atkmodel.load_state_dict(checkpoint['atkmodel'], strict=True)
atk_model_loaded = True
else:
# Create new model
print(colored('Create new model from {}'.format(checkpoint_path), 'blue'))
best_acc_clean = 0
best_acc_poison = 0
subpath_trigger_saved = f"{self.dataset}baseline_{self.baseline}_atkepc_{attack_train_epoch}_eps_{self.atk_eps}_test_eps_{self.atk_test_eps}"
if self.baseline:
subpath_trigger_saved = ""
start_time = time.time()
acc_clean, backdoor_acc = test_updated(self.lira_args, self.device, self.atkmodel, self.net_avg, target_transform,
self.clean_train_loader, self.vanilla_emnist_test_loader, 0, self.lira_args['train_epoch'], clip_image,
testoptimizer=None, log_prefix='External', epochs_per_test=3, dataset=self.dataset, criterion=self.criterion,
subpath_saved=subpath_trigger_saved, is_poison=True, atk_baseline=self.atk_baseline)
print(f"\n----------TEST FOR GLOBAL MODEL BEFORE FEDERATED TRAINING----------------")
print(f"Main task acc: {round(acc_clean*100, 2)}%")
print(f"Backdoor task acc: {round(backdoor_acc*100, 2)}%")
print(f"--------------------------------------------------------------------------\n")
# Try to save models at different fl rounds
current_time = datetime.datetime.now().strftime('%b.%d_%H.%M.%S')
folder_path = f'saved_models/{self.folder_path}_{"baseline" if self.baseline else "poison"}_{current_time}'
try:
os.makedirs(folder_path)
except FileExistsError:
logger.info('Folder already exists')
logger.info(f'current path for saving: {folder_path}')
local_acc_clean, local_acc_poison = 0.0, 0.0
best_main_acc = 0.0
# let's conduct multi-round training
cur_training_eps = self.atk_eps
start_decay_r = 0
start_training_atk_model = True
tgt_optimizer = optim.Adam(self.tgtmodel.parameters(), lr=self.atk_lr)
atk_optimizer = optim.Adam(self.atkmodel.parameters(), lr=self.atk_lr)
self.alternative_training = False
self.flatten_net_avg = flatten_model(copy.deepcopy(self.net_avg))
# end_time = time.time()
elapsed_time = time.time() - start_time
# print time by # seconds
print(f"Time for TEST FOR GLOBAL MODEL BEFORE FEDERATED TRAINING: {elapsed_time}")
# print(f"Time for TEST FOR GLOBAL MODEL BEFORE FEDERATED TRAINING: {(end_time - start_time).seconds}")
print("----------START FEDERATED TRAINING----------------")
for flr in range(1, self.fl_round+1):
print("---"*30)
print("Start communication round {}".format(flr))
if local_acc_poison >= expected_atk_threshold and self.retrain:
self.retrain = False
print(colored('Starting sub-training phase from flr = {}'.format(flr), 'red'))
self.lira_args["test_eps"] = self.atk_test_eps
start_decay_r = flr
if self.retrain == False:
cur_training_eps = max(self.atk_test_eps, exponential_decay(self.atk_eps, self.eps_decay, flr-start_decay_r))
# self.alternative_training = !self.alternative_training
g_user_indices = []
if self.defense_technique == "norm-clipping-adaptive":
# experimental
norm_diff_collector = []
# tau for FedNova
# rho: Parameter controlling the momentum SGD
list_ni = [] # number of data points for each client
list_tau = [] # tau for FedNova (number of batch training = batch * epoch)
list_ai = [] # weight for FedNova
# if self.neurotoxin_attack and ((backdoor_acc > 0.95 and cur_training_eps <= self.atk_test_eps)):
if self.neurotoxin_attack and flr >= 400:
self.baseline = True
logger.info(colored(f"Stop poisoning from round {flr}!!!", "red"))
# if self.atk_baseline and flr >= 400:
# self.baseline = True
# logger.info(colored(f"Stop poisoning from round {flr}!!!", "red"))
if flr in self.attacking_fl_rounds and not self.baseline:
# randomly select participating clients
selected_node_indices = np.random.choice(self.num_nets, size=self.part_nets_per_round, replace=False)
num_data_points = [len(self.net_dataidx_map[i]) for i in selected_node_indices]
total_num_dps_per_round = sum(num_data_points)
if not self.retrain:
self.alternative_training = not self.alternative_training
# net_freq = [num_data_points[i]/total_num_dps_per_round for i in range(self.part_nets_per_round)]
# logger.info("Net freq: {}, FL round: {} without adversary".format(net_freq, flr))
# we need to reconstruct the net list at the beginning
net_list = [copy.deepcopy(self.net_avg) for _ in range(self.part_nets_per_round)]
logger.info("################## Starting fl round: {}".format(flr))
# assume the first client is the attacker
num_data_points[0] = self.num_dps_poisoned_dataset
net_freq = [num_data_points[i]/total_num_dps_per_round for i in range(self.part_nets_per_round)]
logger.info("Net freq: {}, FL round: {} with adversary".format(net_freq, flr))
# FEDNOVA
for id_client in range(self.part_nets_per_round):
if id_client == 0:
list_tau.append(np.ceil(num_data_points[id_client] / self.batch_size) * self.adversarial_local_training_period )
else:
list_tau.append(np.ceil(num_data_points[id_client] / self.batch_size) * self.local_training_period )
list_ni.append(num_data_points[id_client])
#pdb.set_trace()
# we need to reconstruct the net list at the beginning
# net_list = [copy.deepcopy(self.net_avg) for _ in range(self.part_nets_per_round)]
# logger.info("################## Starting fl round: {}".format(flr))
model_original = list(self.net_avg.parameters())
# init_local_model = copy.deepcopy(self.net_avg)
# super hacky but I'm doing this for the prox-attack
wg_clone = copy.deepcopy(self.net_avg)
# wg_hat = None
# v0 = torch.nn.utils.parameters_to_vector(model_original)
# wg_norm_list.append(torch.norm(v0).item())
""" Start the FL process """
for net_idx, net in enumerate(net_list):
init_local_model = dict()
for name, data in wg_clone.state_dict().items():
init_local_model[name] = wg_clone.state_dict()[name].clone()
#net = net_list[net_idx]
# if net_idx == 0:
# global_user_idx = -1 # we assign "-1" as the indices of the attacker in global user indices
# pass
# else:
# global_user_idx = selected_node_indices[net_idx-1]
# dataidxs = self.net_dataidx_map[global_user_idx]
# train_dl_local, _ = get_dataloader(self.dataset, './data', self.batch_size,
# self.test_batch_size, dataidxs) # also get the data loader