-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.py
428 lines (400 loc) · 20.2 KB
/
test.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
import importlib
import torch
torch.backends.cudnn.deterministic = True
torch.manual_seed(0)
torch.cuda.manual_seed(0)
import argparse
import numpy as np
from pathlib import Path
from torch.autograd import Variable
import csv
import os
import random
import copy
from stable_baselines3 import TD3, DDPG, PPO, SAC
import more_itertools as mit
import matplotlib.pyplot as plt
from utilities import _build_groups_by_q
from utilities import *
def seed_worker(worker_id):
worker_seed = 1 % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
if __name__ == "__main__":
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
setup = dict(device=DEVICE, dtype=torch.float)
batch_size = 128
q = 0.1
num_clients= 100
num_agent = num_clients
subsample_rate= 0.1
num_attacker= 20
num_class = 10
fl_epoch=2000
lr=0.01
num_class=10 #47 for emnist
dataset_size = 500
norm="1"
mode = "mnist_clipping_median_q_0.1_20attacker_0.1sample_norm2"
att_ids=random.sample(range(num_clients),num_attacker)
att_ids=list(np.sort(att_ids, axis = None))
# Global initialization
torch.cuda.init()
device = torch.device("cuda")
#Mnist
#Load data
apply_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
trainset = datasets.MNIST(root='./data', train=True, download=True, transform=apply_transform)
testset = datasets.MNIST(root='./data', train=False, download=True, transform=apply_transform)
# #FashionMNIST
# apply_transform = transforms.Compose([
# transforms.ToTensor(),
# transforms.Normalize((0.2859,), (0.3530,))])
#
# trainset = datasets.FashionMNIST(root='./data', train=True, download=True, transform=apply_transform)
# testset = datasets.FashionMNIST(root='./data', train=False, download=True, transform=apply_transform)
#Emnist Balanced
# trainset = datasets.EMNIST(root='./data', split='balanced', train=True, download=True, transform=transforms.ToTensor())
# testset = datasets.EMNIST(root='./data', split='balanced', train=False, download=True, transform=transforms.ToTensor())
net = torch.load("mnist_init").to(**setup)
groups=_build_groups_by_q(trainset, q)
#groups=_build_groups_by_q(trainset, q, num_class = 47) #for emnist
trainloaders=[]
num_group_clients=int(num_clients/num_class)
for gid in range(num_class):
num_data=int(len(groups[gid])/num_group_clients)
for cid in range(num_group_clients):
ids = list(range(cid*num_data, (cid+1)*num_data))
client_trainset = torch.utils.data.Subset(groups[gid], ids)
trainloaders.append(torch.utils.data.DataLoader(client_trainset, batch_size=batch_size, shuffle=True, drop_last=True))
testloader = torch.utils.data.DataLoader(testset, batch_size=len(testset), shuffle=False, drop_last=False)
for seed in [1001]:
random.seed(seed)
att_ids=random.sample(range(num_clients),num_attacker)
att_ids=list(np.sort(att_ids, axis = None))
retrain = False
print('attacker ids: ', att_ids)
net = torch.load("mnist_init").to(**setup)
if os.path.exists(norm+mode+str(seed)+"ori.csv") and (not retrain):
f = open(norm+mode+str(seed)+"ori.csv",'r')
filereader = csv.reader(f)
for row in filereader:
ipm_acc = row
ipm_acc = list(map(lambda x:float(x), ipm_acc))
else:
print("----------Ori Train--------------")
ori_acc = []
old_weights = get_parameters(net)
for rnd in range(1000):
print('---------------------------------------------------')
print('rnd: ',rnd+1)
random.seed(rnd)
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
while len(common(cids, att_ids)) == int(num_clients*subsample_rate):
print("detect")
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
print('chosen clients: ', cids)
print('selected attackers: ',common(cids, att_ids))
weights_lis=[]
#
for cid in cids: #if there is an attack
set_parameters(net, old_weights)
train_real(net, trainloaders[cid], epochs=1, lr=lr)
new_weight=get_parameters(net)
weights_lis.append(new_weight)
#aggregate_weights = Median(old_weights, weights_lis)
#aggregate_weights = Krum(old_weights, weights_lis, len(common(att_ids, cids)))
#aggregate_weights=Clipping(old_weights, weights_lis)
#aggregate_weights=FLtrust(old_weights, weights_lis, root_iter, lr = lr)
aggregate_weights=Clipping_Median(old_weights, weights_lis)
old_weights=aggregate_weights
set_parameters(net, old_weights)
loss, acc = test(net, testloader)
print('global_acc: ', acc, 'loss: ', loss)
ori_acc.append(acc)
f = open(norm+mode+str(seed)+"ori.csv", 'w')
writer = csv.writer(f)
writer.writerow(ori_acc)
f.close()
net = torch.load("mnist_init").to(**setup)
if os.path.exists(norm+mode+str(seed)+"ipm.csv") and (not retrain):
f = open(norm+mode+str(seed)+"ipm.csv",'r')
filereader = csv.reader(f)
for row in filereader:
ipm_acc = row
ipm_acc = list(map(lambda x:float(x), ipm_acc))
else:
print("----------IPM Train--------------")
ipm_acc = []
old_weights = get_parameters(net)
for rnd in range(1000):
print('---------------------------------------------------')
print('rnd: ',rnd+1)
random.seed(rnd)
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
while len(common(cids, att_ids)) == int(num_clients*subsample_rate):
print("detect")
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
print('chosen clients: ', cids)
print('selected attackers: ',common(cids, att_ids))
weights_lis=[]
#
for cid in exclude(cids,att_ids): #if there is an attack
set_parameters(net, old_weights)
train_real(net, trainloaders[cid], epochs=1, lr=lr)
new_weight=get_parameters(net)
weights_lis.append(new_weight)
#max_norm=max(max_norm,np.linalg.norm(weights_to_vector(new_weight)-weights_to_vector(old_weights)))
#IPM
if check_attack(cids, att_ids):
for i in range(len(common(cids, att_ids))):
set_parameters(net, old_weights)
#train(net, train_iter, epochs=1, lr=lr)
if len(weights_lis)!=0:
crafted_weights = [craft(old_weights, average(weights_lis), 5, -1)]*len(common(cids, att_ids))
#crafted_weights = [ipm_craft_median(old_weights, weights_lis)]*len(common(cids, att_ids))
else:
#crafted_weights = [ipm_craft_median(old_weights, get_parameters(net))]*len(common(cids, att_ids))
crafted_weights = [craft(old_weights, get_parameters(net), 5, -1)]*len(common(cids, att_ids))
for new_weight in crafted_weights:
#print(new_weight)
weights_lis.append(new_weight)
#aggregate_weights = Median(old_weights, weights_lis)
#aggregate_weights = Krum(old_weights, weights_lis, len(common(att_ids, cids)))
#aggregate_weights=Clipping(old_weights, weights_lis)
#aggregate_weights=FLtrust(old_weights, weights_lis, root_iter, lr = lr)
aggregate_weights=Clipping_Median(old_weights, weights_lis)
old_weights=aggregate_weights
set_parameters(net, old_weights)
loss, acc = test(net, testloader)
print('global_acc: ', acc, 'loss: ', loss)
ipm_acc.append(acc)
f = open(norm+mode+str(seed)+"ipm.csv", 'w')
writer = csv.writer(f)
writer.writerow(ipm_acc)
f.close()
net = torch.load("mnist_init").to(**setup)
if os.path.exists(norm+mode+str(seed)+"lmp.csv") and (not retrain):
f = open(norm+mode+str(seed)+"lmp.csv",'r')
filereader = csv.reader(f)
for row in filereader:
lmp_acc = row
lmp_acc = list(map(lambda x:float(x), lmp_acc))
#continue
else:
print("---------------LMP Train---------------")
old_weights=get_parameters(net)
lmp_acc = []
for rnd in range(1000):
epoch_start_time = time.time()
print('---------------------------------------------------')
print('rnd: ',rnd+1)
random.seed(rnd)
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
while len(common(cids, att_ids)) == int(num_clients*subsample_rate):
print("detect")
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
print('chosen clients: ', cids)
print('selected attackers: ',common(cids, att_ids))
weights_lis=[]
#
for cid in exclude(cids,att_ids): #if there is an attack
set_parameters(net, old_weights)
train_real(net, trainloaders[cid], epochs = 1, lr = lr)
new_weight=get_parameters(net)
weights_lis.append(new_weight)
#LMP
if check_attack(cids, att_ids):
if len(weights_lis)!=0:
crafted_weights = Median_craft_real(old_weights, weights_lis, common(cids,att_ids), cids, net, trainloaders, lr = lr)
#crafted_weights = Krum_craft(old_weights, weights_lis, common(cids,att_ids), cids, net, trainloaders, lr = lr)
else:
crafted_weights = Median_craft_real(old_weights, [get_parameters(net)], common(cids,att_ids), cids, net, trainloaders, lr =lr)
#crafted_weights = Krum_craft(old_weights, weights_lis, common(cids,att_ids), cids, net, trainloaders, lr = lr)
for new_weight in crafted_weights:
#print(new_weight)
weights_lis.append(new_weight)
#aggregate_weights = Krum(old_weights, weights_lis, len(common(cids,att_ids)))
#aggregate_weights=Clipping(old_weights, weights_lis)
#aggregate_weights=FLtrust(old_weights, weights_lis, root_iter, lr = lr)
aggregate_weights=Clipping_Median(old_weights, weights_lis)
epoch_end_time = time.time()
per_epoch_ptime = epoch_end_time - epoch_start_time
old_weights=aggregate_weights
set_parameters(net, old_weights)
loss, acc = test(net, testloader)
print('global_acc: ', acc, 'loss: ', loss)
lmp_acc.append(acc)
print('time is ', str(per_epoch_ptime))
f = open(norm+mode+str(seed)+"lmp.csv", 'w')
writer = csv.writer(f)
writer.writerow(lmp_acc)
f.close()
net = torch.load("mnist_init").to(**setup)
if os.path.exists(norm+mode+str(seed)+"EB.csv") and (not retrain):
f = open(norm+mode+str(seed)+"EB.csv",'r')
filereader = csv.reader(f)
for row in filereader:
ipm_acc = row
EB_acc = list(map(lambda x:float(x), ipm_acc))
#print(ori_acc)
else:
print("----------EB Train--------------")
EB_acc = []
dummy_id = [i for i in range(1000)]
validset = Subset(trainset, dummy_id)
valiloader = DataLoader(validset, batch_size=200, shuffle=True)
old_weights = get_parameters(net)
for rnd in range(1000):
print('---------------------------------------------------')
print('rnd: ',rnd+1)
random.seed(rnd)
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
while len(common(cids, att_ids)) == int(num_clients*subsample_rate):
print("detect")
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
print('chosen clients: ', cids)
print('selected attackers: ',common(cids, att_ids))
weights_lis=[]
#
for cid in exclude(cids,att_ids): #if there is an attack
set_parameters(net, old_weights)
train_real(net, trainloaders[cid], epochs=1, lr=lr)
new_weight=get_parameters(net)
weights_lis.append(new_weight)
#max_norm=max(max_norm,np.linalg.norm(weights_to_vector(new_weight)-weights_to_vector(old_weights)))
if check_attack(cids, att_ids):
for cid in common(cids, att_ids):
set_parameters(net, old_weights)
train_real_ga(net, trainloaders[cid], epochs = 5, lr = lr)
loss, acc = test(net, valiloader)
#print(self.rnd, loss, acc)
check = 5
while np.isnan(loss):
check = max(check - 1, 0)
set_parameters(net, old_weights)
train_real(net, valiloader, epochs=check, lr=lr)
new_weight=get_parameters(net)
loss, acc = test(net, valiloader)
print(rnd, loss, acc, check)
if check == 0:
new_weight = copy.deepcopy(old_weights)
break
weights_lis.append(craft(old_weights, new_weight, 1, len(cids)/len(common(cids, att_ids))))
#aggregate_weights = Median(old_weights, weights_lis)
#aggregate_weights = Krum(old_weights, weights_lis, len(common(att_ids, cids)))
#aggregate_weights=Clipping(old_weights, weights_lis)
#aggregate_weights=FLtrust(old_weights, weights_lis, root_iter, lr = lr)
aggregate_weights=Clipping_Median(old_weights, weights_lis)
set_parameters(net, aggregate_weights)
loss, acc = test(net, testloader)
if np.isnan(loss):
old_weights = old_weights
set_parameters(net, old_weights)
loss, acc = test(net, testloader)
else:
old_weights=aggregate_weights
set_parameters(net, old_weights)
print('global_acc: ', acc, 'loss: ', loss)
EB_acc.append(acc)
f = open(norm+mode+str(seed)+"EB.csv", 'w')
writer = csv.writer(f)
writer.writerow(EB_acc)
f.close()
net = torch.load("mnist_init").to(**setup)
dummy_id = np.random.choice(len(trainset), 200) #Local data owned by attackers
true_att_trainset = Subset(trainset, dummy_id)
valiloader = DataLoader(true_att_trainset, batch_size=200, shuffle=True)
attloader = DataLoader(true_att_trainset, batch_size = batch_size, shuffle = True)
print("----------------TD3_Clipping_Median--------------")
start_time = time.time()
old_weights = get_parameters(net)
td3_last_acc = []
for rnd in range(1000) :
epoch_start_time = time.time()
print('---------------------------------------------------')
print('rnd: ',rnd+1)
random.seed(rnd)
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
while len(common(cids, att_ids)) == int(num_clients*subsample_rate):
cids=random.sample(range(num_clients),int(num_clients*subsample_rate))
print('chosen clients: ', cids)
print('selected attackers: ',common(cids, att_ids))
selected_attacker = common(cids, att_ids)
weights_lis=[]
state = np.concatenate((old_weights[-2], old_weights[-1]), axis = None)
#state = np.concatenate((old_weights[-1]), axis = None)
state_min = np.min(state)
state_max = np.max(state)
norm_state = [2.0*((i - state_min)/(state_max-state_min))-1.0 for i in state]
obs = {"pram": norm_state, "num_attacker": len(selected_attacker)}
if rnd > 100:
model_number = min(int((rnd)/5) * 1000, 80000)
model_path = 'mnist_clipping_median_q0.1/rl_model_'+str(model_number)+'_steps.zip'
model = TD3.load(model_path)
action, _ = model.predict(obs)
#Adjust this to the same parameters in the environments you use
action[0] = action[0]*14.9+15.0 #epsilon [0,10]
action[1] = action[1]*24+25 #local step [1:1:50]
new_weights=[]
if rnd <= 100:
for cid in cids:
set_parameters(net,old_weights)
train_real(net, trainloaders[cid], epochs=1, lr=lr)
#train(self.net, self.train_iters[cid%10], epochs=1, lr=lr)
new_weight=get_parameters(net)
new_weights.append(new_weight)
else:
for cid in exclude(cids,att_ids):
set_parameters(net,old_weights)
train_real(net, trainloaders[cid], epochs=1, lr=lr)
#train(self.net, self.train_iters[cid%10], epochs=1, lr=lr)
new_weight=get_parameters(net)
new_weights.append(new_weight)
att_weights_lis=[]
set_parameters(net, old_weights)
#train(self.net, self.train_iter, epochs=int(action[2]), lr=action[1], mode = False)
train_real(net, valiloader, epochs=int(action[1]), lr=lr)
new_weight=get_parameters(net)
loss, acc = test(net, valiloader)
#print(self.rnd, loss, acc)
check = int(action[1])
while np.isnan(loss):
check = max(check - 1, 0)
set_parameters(net, old_weights)
train_real(net, valiloader, epochs=check, lr=lr)
new_weight=get_parameters(net)
loss, acc = test(net, valiloader)
print(rnd, loss, acc, check)
if check == 0:
new_weight = copy.deepcopy(old_weights)
break
att_weights_lis.append(new_weight)
for cid in common(cids,att_ids):
new_weight=craft_att(old_weights, average(att_weights_lis), -1, action[0])
new_weights.append(new_weight)
#print(len(new_weights))
#aggregate_weights = average(weights_lis)
#aggregate_weights = Median(old_weights, weights_lis)
#aggregate_weights = Krum(old_weights, new_weights, len(common(cids, att_ids)))
#aggregate_weights=Clipping(old_weights, weights_lis)
#aggregate_weights=FLtrust(old_weights, weights_lis, root_iter, lr = lr)
aggregate_weights=Clipping_Median(old_weights, new_weights)
set_parameters(net, aggregate_weights)
loss, acc = test(net, testloader)
if np.isnan(loss):
old_weights = old_weights
else:
old_weights = aggregate_weights
set_parameters(net, old_weights)
epoch_end_time = time.time()
per_epoch_ptime = epoch_end_time - epoch_start_time
print ('epoch %d , acc test %.2f%% , loss test %.2f ptime %.2fs .' \
%(rnd, 100*acc, loss, per_epoch_ptime))
td3_last_acc.append(acc)
f = open(norm+mode+str(seed)+"RL.csv", 'w')
writer = csv.writer(f)
writer.writerow(td3_last_acc)
f.close()