-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_fed.py
366 lines (323 loc) · 14 KB
/
main_fed.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python version: 3.6
from random import random
from models.test import test_img
from models.Fed import FedAvg
from models.Nets import ResNet18, vgg19_bn, vgg19, get_model, vgg11
from models.resnet20 import resnet20
from models.MaliciousUpdate import LocalMaliciousUpdate
from models.Update import LocalUpdate
from utils.info import print_exp_details, write_info_to_accfile, get_base_info
from utils.options import args_parser
from utils.sampling import mnist_iid, mnist_noniid, cifar_iid, cifar_noniid
from utils.defense import fltrust, multi_krum, get_update, RLR, flame, get_update2, fld_distance, detection, detection1, parameters_dict_to_vector_flt, lbfgs_torch
from models.Attacker import attacker
import torch
from torchvision import datasets, transforms
import numpy as np
import copy
import matplotlib.pyplot as plt
import matplotlib
import os
import random
import time
import math
matplotlib.use('Agg')
def write_file(filename, accu_list, back_list, args, analyse=False):
write_info_to_accfile(filename, args)
f = open(filename, "a")
f.write("main_task_accuracy=")
f.write(str(accu_list))
f.write('\n')
f.write("backdoor_accuracy=")
f.write(str(back_list))
if args.defence == "krum":
krum_file = filename + "_krum_dis"
torch.save(args.krum_distance, krum_file)
if analyse == True:
need_length = len(accu_list) // 10
acc = accu_list[-need_length:]
back = back_list[-need_length:]
best_acc = round(max(acc), 2)
average_back = round(np.mean(back), 2)
best_back = round(max(back), 2)
f.write('\n')
f.write('BBSR:')
f.write(str(best_back))
f.write('\n')
f.write('ABSR:')
f.write(str(average_back))
f.write('\n')
f.write('max acc:')
f.write(str(best_acc))
f.write('\n')
f.close()
return best_acc, average_back, best_back
f.close()
def central_dataset_iid(dataset, dataset_size):
all_idxs = [i for i in range(len(dataset))]
central_dataset = set(np.random.choice(
all_idxs, dataset_size, replace=False))
return central_dataset
def test_mkdir(path):
if not os.path.isdir(path):
os.mkdir(path)
if __name__ == '__main__':
# parse args
args = args_parser()
args.device = torch.device('cuda:{}'.format(
args.gpu) if torch.cuda.is_available() and args.gpu != -1 else 'cpu')
test_mkdir('./' + args.save)
print_exp_details(args)
# load dataset and split users
if args.dataset == 'mnist':
trans_mnist = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
dataset_train = datasets.MNIST(
'../data/mnist/', train=True, download=True, transform=trans_mnist)
dataset_test = datasets.MNIST(
'../data/mnist/', train=False, download=True, transform=trans_mnist)
# sample users
if args.iid:
dict_users = mnist_iid(dataset_train, args.num_users)
else:
dict_users = mnist_noniid(dataset_train, args.num_users)
elif args.dataset == 'fashion_mnist':
trans_mnist = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean=[0.2860], std=[0.3530])])
dataset_train = datasets.FashionMNIST(
'../data/', train=True, download=True, transform=trans_mnist)
dataset_test = datasets.FashionMNIST(
'../data/', train=False, download=True, transform=trans_mnist)
# sample users
if args.iid:
dict_users = np.load('./data/iid_fashion_mnist.npy', allow_pickle=True).item()
else:
dict_users = np.load('./data/non_iid_fashion_mnist.npy', allow_pickle=True).item()
elif args.dataset == 'cifar':
trans_cifar = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
dataset_train = datasets.CIFAR10(
'../data/cifar', train=True, download=True, transform=trans_cifar)
dataset_test = datasets.CIFAR10(
'../data/cifar', train=False, download=True, transform=trans_cifar)
if args.iid:
dict_users = np.load('./data/iid_cifar.npy', allow_pickle=True).item()
else:
dict_users = np.load('./data/non_iid_cifar.npy', allow_pickle=True).item()
else:
exit('Error: unrecognized dataset')
img_size = dataset_train[0][0].shape
# build model
if args.model == 'VGG' and args.dataset == 'cifar':
net_glob = vgg19_bn().to(args.device)
elif args.model == 'VGG11' and args.dataset == 'cifar':
net_glob = vgg11().to(args.device)
elif args.model == "resnet" and args.dataset == 'cifar':
net_glob = ResNet18().to(args.device)
elif args.model == "resnet20" and args.dataset == 'cifar':
net_glob = resnet20().to(args.device)
elif args.model == "rlr_mnist" or args.model == "cnn":
net_glob = get_model('fmnist').to(args.device)
else:
exit('Error: unrecognized model')
if args.attack=='baseline':
args.attack='badnet'
if args.defence == 'Fedavg':
args.defence = 'avg'
if args.model == 'cnn':
args.model = 'rlr_mnist'
net_glob.train()
if args.defence == 'fldetector':
args.defence = 'fld'
# copy weights
w_glob = net_glob.state_dict()
# training
loss_train = []
cv_loss, cv_acc = [], []
val_loss_pre, counter = 0, 0
net_best = None
best_loss = None
if args.defence == 'fld':
old_update_list = []
weight_record = []
update_record = []
args.frac = 1
malicious_score = torch.zeros((1, 100))
if math.isclose(args.malicious, 0):
backdoor_begin_acc = 100
else:
backdoor_begin_acc = args.attack_begin # overtake backdoor_begin_acc then attack
central_dataset = central_dataset_iid(dataset_test, args.server_dataset)
base_info = get_base_info(args)
filename = './' + args.save + '/accuracy_file_{}.txt'.format(base_info)
if args.init != 'None':
param = torch.load(args.init)
net_glob.load_state_dict(param)
print("load init model")
val_acc_list, net_list = [0.0001], []
backdoor_acculist = [0]
args.attack_layers = []
if args.attack == "dba":
args.dba_sign = 0
if args.defence == "krum":
args.krum_distance = []
malicious_list = []
for i in range(int(args.num_users * args.malicious)):
malicious_list.append(i)
if args.all_clients:
print("Aggregation over all clients")
w_locals = [w_glob for i in range(args.num_users)]
for iter in range(args.epochs):
loss_locals = []
if not args.all_clients:
w_locals = []
w_updates = []
m = max(int(args.frac * args.num_users), 1)
idxs_users = np.random.choice(range(args.num_users), m, replace=False)
if args.defence == 'fld':
idxs_users = np.arange(args.num_users)
if iter == 350:
args.lr *= 0.1
if backdoor_begin_acc < val_acc_list[-1]:
backdoor_begin_acc = 0
attack_number = int(args.malicious * m)
else:
attack_number = 0
skip_number=0
mal_weight=[]
mal_loss=[]
args.attack_layers=[]
for num_turn, idx in enumerate(idxs_users):
if attack_number > 0 and skip_number == 0:
if args.defence == 'fld':
args.old_update_list = old_update_list[0:int(args.malicious * m)]
m_idx = idx
else:
m_idx = None
mal_weight, loss, args.attack_layers = attacker(malicious_list, attack_number, args.attack, dataset_train, dataset_test, dict_users, net_glob, args, idx = m_idx)
attack_number -= 1
if args.attack == 'adaptive':
skip_number = attack_number
if skip_number == 0:
w = mal_weight[0]
else:
w = mal_weight[0]
elif skip_number > 0:
w = mal_weight[-skip_number]
skip_number -= 1
attack_number -= 1
else:
local = LocalUpdate(
args=args, dataset=dataset_train, idxs=dict_users[idx])
w, loss = local.train(
net=copy.deepcopy(net_glob).to(args.device))
if args.defence == 'fld':
w_updates.append(get_update2(w, w_glob)) #ignore num_batches_tracked, running_mean, running_var
else:
w_updates.append(get_update(w, w_glob))
if args.all_clients:
w_locals[idx] = copy.deepcopy(w)
else:
w_locals.append(copy.deepcopy(w))
loss_locals.append(copy.deepcopy(loss))
if args.defence == 'avg': # no defence
w_glob = FedAvg(w_locals)
elif args.defence == 'krum': # single krum
selected_client = multi_krum(w_updates, 1, args)
# print(args.krum_distance)
w_glob = w_locals[selected_client[0]]
# w_glob = FedAvg([w_locals[i] for i in selected_clinet])
elif args.defence == 'multikrum':
selected_client = multi_krum(w_updates, args.k, args, multi_k=True)
# print(selected_client)
w_glob = FedAvg([w_locals[x] for x in selected_client])
elif args.defence == 'RLR':
w_glob = RLR(copy.deepcopy(net_glob), w_updates, args)
elif args.defence == 'fltrust':
local = LocalUpdate(
args=args, dataset=dataset_test, idxs=central_dataset)
fltrust_norm, loss = local.train(
net=copy.deepcopy(net_glob).to(args.device))
fltrust_norm = get_update(fltrust_norm, w_glob)
w_glob = fltrust(w_updates, fltrust_norm, w_glob, args)
elif args.defence == 'flame':
w_glob = flame(w_locals, w_updates, w_glob, args, debug=args.debug)
elif args.defence == 'fld':
# ignore key.split('.')[-1] == 'num_batches_tracked' or key.split('.')[-1] == 'running_mean' or key.split('.')[-1] == 'running_var'
N = 5
args.N = N
weight = parameters_dict_to_vector_flt(w_glob)
local_update_list = []
for local in w_updates:
local_update_list.append(-1*parameters_dict_to_vector_flt(local).cpu()) # change to 1 dimension
if iter > N+1:
hvp = lbfgs_torch(args, weight_record, update_record, weight - last_weight)
attack_number = int(args.malicious * m)
distance = fld_distance(old_update_list, local_update_list, net_glob, attack_number, hvp)
distance = distance.view(1,-1)
print('main.py line 320 distance:',distance)
malicious_score = torch.cat((malicious_score, distance), dim=0)
if malicious_score.shape[0] > N+1:
if detection1(np.sum(malicious_score[-N:].numpy(), axis=0)):
label = detection(np.sum(malicious_score[-N:].numpy(), axis=0), int(args.malicious * m))
else:
label = np.ones(100)
selected_client = []
for client in range(100):
if label[client] == 1:
selected_client.append(client)
new_w_glob = FedAvg([w_locals[client] for client in selected_client])
else:
new_w_glob = FedAvg(w_locals) #avg
else:
hvp = None
new_w_glob = FedAvg(w_locals) #avg
update = get_update2(w_glob, new_w_glob) #w_t+1 = w_t - a*g_t => g_t = w_t - w_t+1 (a=1)
update = parameters_dict_to_vector_flt(update)
if iter > 0:
weight_record.append(weight.cpu() - last_weight.cpu())
update_record.append(update.cpu() - last_update.cpu())
if iter > N:
del weight_record[0]
del update_record[0]
last_weight = weight
last_update = update
old_update_list = local_update_list
w_glob = new_w_glob
else:
print("Wrong Defense Method")
os._exit(0)
# copy weight to net_glob
net_glob.load_state_dict(w_glob)
# print loss
loss_avg = sum(loss_locals) / len(loss_locals)
print('Round {:3d}, Average loss {:.3f}'.format(iter, loss_avg))
loss_train.append(loss_avg)
if iter % 1 == 0:
acc_test, _, back_acc = test_img(
net_glob, dataset_test, args, test_backdoor=True)
print("Main accuracy: {:.2f}".format(acc_test))
print("Backdoor accuracy: {:.2f}".format(back_acc))
val_acc_list.append(acc_test.item())
backdoor_acculist.append(back_acc)
write_file(filename, val_acc_list, backdoor_acculist, args)
best_acc, absr, bbsr = write_file(filename, val_acc_list, backdoor_acculist, args, True)
# plot loss curve
plt.figure()
plt.xlabel('communication')
plt.ylabel('accu_rate')
plt.plot(val_acc_list, label='main task(acc:' + str(best_acc) + '%)')
plt.plot(backdoor_acculist, label='backdoor task(BBSR:' + str(bbsr) + '%, ABSR:' + str(absr) + '%)')
plt.legend()
title = base_info
# plt.title(title, y=-0.3)
plt.title(title)
plt.savefig('./' + args.save + '/' + title + '.pdf', format='pdf', bbox_inches='tight')
# testing
net_glob.eval()
acc_train, loss_train = test_img(net_glob, dataset_train, args)
acc_test, loss_test = test_img(net_glob, dataset_test, args)
print("Training accuracy: {:.2f}".format(acc_train))
print("Testing accuracy: {:.2f}".format(acc_test))
torch.save(net_glob.state_dict(),'./' + args.save + '/model' + '.pth')