-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcliend.py
More file actions
535 lines (454 loc) · 25.7 KB
/
Copy pathcliend.py
File metadata and controls
535 lines (454 loc) · 25.7 KB
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
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import collections
import torch
from torch.utils.data import DataLoader, TensorDataset
from cifar10_models import *
import numpy as np
import os
import pickle
from utils.logger import *
from utils.eval import *
from utils.misc import *
from utils.adv_train import *
from utils.semisup import *
from cifar10_normal_train import *
from cifar10_util import *
from adam import Adam
from sgd import SGD
from attacks import *
import copy
from score_monitor import *
from models import *
from model import TargetModel
from constant import *
import sys
import pandas as pd
from dct import *
from utils.plot import *
from aggregator import *
from utils.server_defense import *
from utils.existing_attack import *
import cProfile
from scipy.stats import entropy
import time
pre_rounds_start, pre_rounds_end, interval = 0, 20, 1
sum_sensitivity, distribution_sensitivity, top_10_sensitivity = 0.01, 0.01, 0.01
def sum_based_detection(epoch_diff_sum, threshold_sum):
return epoch_diff_sum > threshold_sum
def distribution_based_detection(current_distribution, reference_distribution, threshold_distribution):
kl_div = kl_data(current_distribution, reference_distribution)
return kl_div > threshold_distribution
def top_10_scores_detection(top_10_scores_diff, threshold_top_10):
return top_10_scores_diff > threshold_top_10
def kl_data(data1, data2):
hist1, _ = np.histogram(data1, bins=10, density=True)
hist2, _ = np.histogram(data2, bins=10, density=True)
epsilon = 1e-10
prob1 = np.clip(hist1 / np.sum(hist1), epsilon, 1)
prob2 = np.clip(hist2 / np.sum(hist2), epsilon, 1)
return entropy(prob1, prob2)
def main():
# Redirect stderr and stdout
reports_per_epoch = []
sys.stderr = open("err.txt", 'w')
# sys.stdout = open(
# "result/{}_{}_{}_{}_{}_{}_{}_out.txt".format(dataset, aggregation, at_type, detection, k, interval,
# n_attackers[0]), 'w')
sys.stdout = open(
"out_test_time.txt".format(dataset, aggregation, at_type, detection, k, interval,
n_attackers[0]), 'w')
# Load CIFAR-10 data
data, labels = load_cifar10(cifar_loc)
if distribution == 'iid':
user_tr_data_tensors, user_tr_label_tensors, val_data_tensor, val_label_tensor, te_data_tensor, te_label_tensor, fltrust_data, fltrust_label = data_loading(
data, labels, nusers, total_tr_len, val_len, te_len, user_tr_len)
elif distribution == 'non-iid':
user_tr_data_tensors, user_tr_label_tensors, val_data_tensor, val_label_tensor, te_data_tensor, te_label_tensor, fltrust_data, fltrust_label = cifar10_noniid(
cifar_loc, nusers, val_len, te_len)
# Set device to GPU if available
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
val_data_tensor, val_label_tensor, te_data_tensor, te_label_tensor, fltrust_data, fltrust_label = val_data_tensor.to(
device), val_label_tensor.to(device), te_data_tensor.to(device), te_label_tensor.to(device), fltrust_data.to(
device), fltrust_label.to(device)
# Initialize variables
rank_change_history = []
acc_history = []
score_hist = {}
scores = []
scores_sum = []
scores_sum_topk = []
score_diff = []
score_diff_topk = []
horizontals = []
verticals = []
last_score_sum = 0
last_score_sum_topk = 0
epoch_num = 0
best_global_acc = 0
best_global_te_acc = 0
attacker_join = False
n_attacker = 0
lamda_sum = 0
horizontal_results_list = []
vertical_results_list = []
weights_results_list = []
scores_results_list = []
# Initialize dictionaries with None
client_scores_dict = {client_id: None for client_id in range(nusers)}
client_weights_dict = {client_id: None for client_id in range(nusers)}
client_models = {client_id: return_model(arch, 0.1, 0.9, parallel=False)[0].to(device) for client_id in
range(nusers)}
client_optimizers = {client_id: SGD(client_models[client_id].parameters(), lr=0.1) for client_id in range(nusers)}
# Training loop
for z in z_values:
fed_file = 'alexnet_checkpoint_%s_%s_%d_%.2f.pth.tar' % (aggregation, at_type, n_attacker, z)
fed_best_file = 'alexnet_best_%s_%s_%d_%.2f.pth.tar' % (aggregation, at_type, n_attacker, z)
# if resume:
# fed_checkpoint = chkpt + '/' + fed_file
# assert os.path.isfile(fed_checkpoint), 'Error: no user checkpoint at %s' % (fed_checkpoint)
# checkpoint = torch.load(fed_checkpoint, map_location=device)
# fed_model.load_state_dict(checkpoint['state_dict'])
# optimizer_fed.load_state_dict(checkpoint['optimizer'])
# resume = 0
# best_global_acc = checkpoint['best_acc']
# best_global_te_acc = checkpoint['best_te_acc']
# val_loss, val_acc = test(val_data_tensor, val_label_tensor, fed_model, criterion, use_cuda)
# epoch_num += checkpoint['epoch']
# print('resuming from epoch %d | val acc %.4f | best acc %.3f | best te acc %.3f' % (
# epoch_num, val_acc, best_global_acc, best_global_te_acc))
torch.cuda.empty_cache()
fed_model, _ = return_model(arch, 0.1, 0.9, parallel=False)
fed_model.to(device)
optimizer_fed = SGD(fed_model.parameters(), lr=0.1)
# last_ckpt = copy.deepcopy(fed_model.state_dict())
server_scores = return_score(fed_model)
prev_score = server_scores
server_weights = return_weight(fed_model)
param_grad = []
for param in fed_model.parameters():
if param.grad is not None:
param_grad.append(param.grad.data.view(-1).clone().detach().cpu())
else:
param_grad.append(torch.zeros_like(param.data.view(-1)).cpu())
param_grad = torch.cat(param_grad).to(device)
update_fed_model(fed_model, optimizer_fed, param_grad)
global_score = return_score(fed_model)
reports_per_epoch = []
while epoch_num <= nepochs:
round_reports = []
if aggregation == 'FRL':
initial_scores = {}
for n, m in fed_model.named_modules():
if hasattr(m, "popup_scores"):
initial_scores[str(n)] = m.popup_scores.detach().clone().flatten().sort()[0]
user_grads = []
cur_user_updates = collections.defaultdict(list)
# participant = []
client_weight = []
# client_data = []
# client_label = []
server_score = return_score(fed_model)
server_weight = return_weight(fed_model)
server_scores = np.vstack((server_scores, server_score))
server_weights = np.vstack((server_weights, server_weight))
param_grad = []
for param in fed_model.parameters():
if param.grad is not None:
param_grad.append(param.grad.data.view(-1).clone().detach().cpu())
else:
param_grad.append(torch.zeros_like(param.data.view(-1)).cpu())
def apply_gradients(model, grads):
model.zero_grad()
grad_tensors = torch.cat([grad.view(-1) for grad in grads])
start_idx = 0
for param in model.parameters():
end_idx = start_idx + param.numel()
if param.grad is None:
param.grad = torch.zeros_like(param.data)
param.grad.data.copy_(grad_tensors[start_idx:end_idx].view_as(param))
start_idx = end_idx
return model
# for i in range(nusers):
# client_models[i] = apply_gradients(client_models[i], param_grad)
# print(client_scores_dict[0])
for i in range(n_attacker, nusers):
prev_score = return_score(client_models[i])
inputs = user_tr_data_tensors[i][
(epoch_num % nbatches) * batch_size:((epoch_num % nbatches) + 1) * batch_size]
targets = user_tr_label_tensors[i][
(epoch_num % nbatches) * batch_size:((epoch_num % nbatches) + 1) * batch_size]
if at_type == 'poisoning' and i < n_attackers[0]:
if attacker_join:
inputs, targets = get_label_flipping(inputs, targets)
if at_type == 'pixel_attack' and i < n_attackers[0]:
if attacker_join:
inputs, targets = poison_data(inputs, targets)
inputs, targets = inputs.to(device), targets.to(device)
inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets)
outputs = client_models[i](inputs)
loss = criterion(outputs, targets)
client_models[i].zero_grad()
loss.backward(retain_graph=True)
if aggregation == 'flame':
index_participant = i - n_attacker
pre_model = {}
for name, param in fed_model.state_dict().items():
pre_model[name] = param.clone()
param_grad = []
for param in client_models[i].parameters():
if param.grad is not None:
param_grad.append(param.grad.data.view(-1).clone().detach().cpu())
else:
param_grad.append(torch.zeros_like(param.data.view(-1)).cpu())
param_grad = torch.cat(param_grad).to(device)
# val_loss, val_acc = test(val_data_tensor, val_label_tensor, client_models[i], criterion, use_cuda)
te_loss, te_acc_client_b = test(te_data_tensor, te_label_tensor, client_models[i], criterion, use_cuda)
update_fed_model(client_models[i], client_optimizers[i], param_grad)
te_loss, te_acc_client_a = test(te_data_tensor, te_label_tensor, client_models[i], criterion, use_cuda)
print('client %d te acc before %.4f after %.4f' % (i, te_acc_client_b, te_acc_client_a))
user_grads = param_grad[None, :] if len(user_grads) == 0 else torch.cat(
(user_grads, param_grad[None, :]), 0)
torch.cuda.empty_cache()
if aggregation == 'flame':
update_fed_model(fed_model, optimizer_fed, param_grad)
diff = dict()
for name, data in fed_model.state_dict().items():
diff[name] = (data - pre_model[name])
client_weight.append(diff)
# client_model, _ = return_model(arch, 0.1, 0.9, parallel=False)
# def apply_gradients(model, grads):
# model.zero_grad()
# grad_tensors = torch.cat([grad.view(-1) for grad in grads])
# start_idx = 0
# for param in model.parameters():
# end_idx = start_idx + param.numel()
# if param.grad is None:
# param.grad = torch.zeros_like(param.data)
# param.grad.data.copy_(grad_tensors[start_idx:end_idx].view_as(param))
# start_idx = end_idx
# return model
# client_models[i] = apply_gradients(client_models[i], param_grad)
# if epoch_num <= 10:
# min_score, max_score = get_the_threshold(client_models[i])
# topk_neurons = get_topk_neurons(client_models[i], k)
# min_score_topk, max_score_topk = get_the_threshold_topk(client_models[i], topk_neurons)
# if epoch_num >= 30:
# sum_of_detected_neurons = check_neuron_score(client_models[i], min_score, max_score)
# sum_of_detected_neurons_topk = check_neuron_score_topk(client_models[i], topk_neurons, min_score_topk, max_score_topk)
current_time = time.time()
weight = return_weight(client_models[i])
score = return_score(client_models[i])
print(prev_score)
print(score)
print(f"client {i}", calculate_round_difference(score, prev_score))
print("score update time", time.time()-current_time)
if epoch_num % interval == 0:
current_time = time.time()
weights_results_list.append({"epoch": epoch_num, "client": i,
**{"neuron_" + str(j): weight[j] for j in range(len(weight))}})
scores_results_list.append(
{"epoch": epoch_num, "client": i, **{"neuron_" + str(j): score[j] for j in range(len(score))}})
if epoch_num >= pre_rounds_end:
# if epoch_num >= 30:
# horizontal_results_list.append({"epoch": epoch_num, "client": i, "detected_neurons": sum_of_detected_neurons})
# vertical_results_list.append({"epoch": epoch_num, "client": i, "detected_neurons_topk": sum_of_detected_neurons_topk})
# # Save each neuron's score/weight in a separate column
print(f"\n==== Epoch {epoch_num} - Performing Detection ====")
# for client_id in range(n_attacker, nusers):
df = pd.DataFrame(client_scores_dict[i])
epoch_sum_scores = df.sum(axis=1)
epoch_diff_sum = epoch_sum_scores.diff().fillna(0)
top_10_scores_diff = df.apply(lambda x: np.sort(x)[-int(0.1 * len(x)):].sum(),
axis=1).diff().fillna(0)
# print(epoch_diff_sum)
# print(top_10_scores_diff)
threshold_sum = epoch_diff_sum.iloc[
pre_rounds_start:pre_rounds_end].abs().max() + sum_sensitivity
threshold_top_10 = top_10_scores_diff.iloc[
pre_rounds_start:pre_rounds_end].abs().max() + top_10_sensitivity
threshold_distribution = max(
[kl_data(df.iloc[round_idx], df.iloc[round_idx - 1]) for round_idx in
range(pre_rounds_start, pre_rounds_end)]
) + distribution_sensitivity
# print(threshold_sum, threshold_top_10, threshold_distribution)
current_distribution = df.tail(1).iloc[0]
reference_distribution = df.iloc[-2]
# 检测
sum_detected = sum_based_detection(epoch_diff_sum.iloc[-1], threshold_sum)
distribution_detected = distribution_based_detection(current_distribution,
reference_distribution,
threshold_distribution)
top_10_detected = top_10_scores_detection(top_10_scores_diff.iloc[-1], threshold_top_10)
if sum_detected:
print(f"Client {i}: Sum-Based Detection Triggered at Epoch {epoch_num}")
if distribution_detected:
print(f"Client {i}: Distribution-Based Detection Triggered at Epoch {epoch_num}")
if top_10_detected:
print(f"Client {i}: Top 10%-Based Detection Triggered at Epoch {epoch_num}")
report_flag = sum_detected or distribution_detected or top_10_detected
round_reports.append((i, report_flag))
if report_flag:
print(f"Client {i}: Detected attack and reported in Epoch {epoch_num}")
print("detection time", time.time()-current_time)
# with open("cliend/detection_results.txt", "a") as f:
# f.write(f"Epoch {epoch_num}, Client {client_id}: "
# f"Sum-Based: {sum_detected}, "
# f"Distribution-Based: {distribution_detected}, "
# f"Top 10%-Based: {top_10_detected}\n")
# Stack scores and weights
if client_scores_dict[i] is None:
client_scores_dict[i] = score
else:
client_scores_dict[i] = np.vstack((client_scores_dict[i], score))
if client_weights_dict[i] is None:
client_weights_dict[i] = weight
else:
client_weights_dict[i] = np.vstack((client_weights_dict[i], weight))
malicious_grads = user_grads
# print(f"user_grads shape: {user_grads.shape}")
clusters = cluster_client_gradients_mean_shift(user_grads)
reports_per_epoch.append(round_reports)
print(
f"Epoch {epoch_num}: Reports - {[(i, flag) for i, flag in round_reports if flag]}")
if epoch_num == attack_start_epoch - 1:
attacker_join = True
if at_type != 'poisoning' and at_type != 'pixel_attack':
n_attacker = n_attackers[0]
if n_attacker > 0 and attacker_join:
if at_type == 'lie':
mal_update = lie_attack(malicious_grads, z_values[n_attacker])
malicious_grads = torch.cat((torch.stack([mal_update] * n_attacker), malicious_grads)).to(device)
elif at_type == 'fang':
agg_grads = torch.mean(malicious_grads, 0)
deviation = torch.sign(agg_grads)
malicious_grads = get_malicious_updates_fang_trmean(malicious_grads, deviation, n_attacker,
epoch_num)
elif at_type == 'our-agr':
agg_grads = torch.mean(malicious_grads, 0)
mal_update = our_attack_median(malicious_grads, agg_grads, n_attacker, dev_type)
elif at_type == 'min-max':
dev_type = 'unit_vec'
agg_grads = torch.mean(malicious_grads, 0)
mal_update = our_attack_dist(malicious_grads, agg_grads, n_attacker, dev_type)
elif at_type == 'min-sum':
agg_grads = torch.mean(malicious_grads, 0)
mal_update = our_attack_score(malicious_grads, agg_grads, n_attacker, dev_type)
elif at_type == 'poisoning' or at_type == 'none':
mal_update = torch.mean(malicious_grads, 0)
elif at_type == 'pixel_attack':
mal_update = torch.mean(malicious_grads, 0)
elif at_type == 'rank_reverse':
cur_user_updates = rank_reverse(fed_model, cur_user_updates, n_attacker)
elif at_type == 'dropout_attack':
for i in range(n_attacker):
inputs = user_tr_data_tensors[i][
(epoch_num % nbatches) * batch_size:((epoch_num % nbatches) + 1) * batch_size]
targets = user_tr_label_tensors[i][
(epoch_num % nbatches) * batch_size:((epoch_num % nbatches) + 1) * batch_size]
inputs, targets = inputs.to(device), targets.to(device)
inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets)
fed_model.mask_top_k_weights(at_k)
outputs = fed_model(inputs)
loss = criterion(outputs, targets)
fed_model.zero_grad()
loss.backward(retain_graph=True)
torch.cuda.empty_cache()
param_grad = []
for param in fed_model.parameters():
if param.grad is not None:
param_grad.append(param.grad.data.view(-1).clone().detach().cpu())
else:
param_grad.append(torch.zeros_like(param.data.view(-1)).cpu())
param_grad = torch.cat(param_grad).to(device)
malicious_grads = param_grad[None, :] if len(malicious_grads) == 0 else torch.cat(
(malicious_grads, param_grad[None, :]), 0)
malicious_score = return_score(fed_model)
print(malicious_score)
mal_update = malicious_grads
attacker_join = False
if not epoch_num:
print(malicious_grads.shape)
if aggregation == 'median':
agg_grads = torch.median(malicious_grads, dim=0).to(device)[0]
update_fed_model(fed_model, optimizer_fed, agg_grads)
elif aggregation == 'trmean':
agg_grads = tr_mean(malicious_grads, n_attacker).to(device)
update_fed_model(fed_model, optimizer_fed, agg_grads)
elif aggregation == 'bulyan':
agg_grads, krum_candidate = bulyan(malicious_grads, n_attacker)
update_fed_model(fed_model, optimizer_fed, agg_grads)
elif aggregation == 'mkrum':
agg_grads = multi_krum(malicious_grads, n_attacker)
update_fed_model(fed_model, optimizer_fed, agg_grads)
elif aggregation == 'fltrust':
agg_grads = fl_trust(fed_model, criterion, optimizer_fed, fltrust_data, fltrust_label, malicious_grads)
update_fed_model(fed_model, optimizer_fed, agg_grads)
elif aggregation == 'frl' and epoch_num > 450:
FRL_Vote(fed_model, cur_user_updates, initial_scores)
elif aggregation == 'flame':
flame(fed_model, client_weight)
elif aggregation == 'crowdguard':
fed_model = crowdguard(malicious_grads, fed_model, user_tr_data_tensors, user_tr_label_tensors, device)
else:
agg_grads = torch.mean(malicious_grads, dim=0).to(device)
fed_model = apply_gradients(fed_model, agg_grads)
# update_fed_model(fed_model, optimizer_fed, agg_grads)
del user_grads
if aggregation != 'frl' and aggregation != 'flame':
start_idx = 0
update_fed_model(fed_model, optimizer_fed, agg_grads)
elif aggregation == 'frl' and epoch_num <= 450:
start_idx = 0
update_fed_model(fed_model, optimizer_fed, agg_grads)
val_loss, val_acc = test(val_data_tensor, val_label_tensor, fed_model, criterion, use_cuda)
te_loss, te_acc = test(te_data_tensor, te_label_tensor, fed_model, criterion, use_cuda)
is_best = best_global_acc < val_acc
best_global_acc = max(best_global_acc, val_acc)
if is_best:
best_global_te_acc = te_acc
if epoch_num % interval == 0 or epoch_num == nepochs - 1:
print('%s: at %s n_at %d e %d fed_model val loss %.4f val acc %.4f best val_acc %f te_acc %f' % (
aggregation, at_type, n_attacker, epoch_num, val_loss, val_acc, best_global_acc,
best_global_te_acc))
acc_history.append(val_acc)
# if val_loss > 10:
# print('val loss %f too high' % val_loss)
# break
epoch_num += 1
# horizontal_results = pd.DataFrame(horizontal_results_list)
# vertical_results = pd.DataFrame(vertical_results_list)
# weights_results = pd.DataFrame(weights_results_list)
# scores_results = pd.DataFrame(scores_results_list)
#
# file_suffix = f"{dataset}_{aggregation}_{at_type}_{detection}_{n_attackers[0]}"
# horizontal_results.to_csv(f'result/horizontal_detection_results_{file_suffix}.csv', index=False)
# vertical_results.to_csv(f'result/vertical_detection_results_{file_suffix}.csv', index=False)
# weights_results.to_csv(f'result/weights_results_{file_suffix}.csv', index=False)
# scores_results.to_csv(f'result/scores_results_{file_suffix}.csv', index=False)
#
# # Save scores and weights for each client
# for client_id, scores in client_scores_dict.items():
# df = pd.DataFrame(scores)
# df.to_csv(f'result/client_{client_id}_scores.csv', index=False)
#
# for client_id, weights in client_weights_dict.items():
# df = pd.DataFrame(weights)
# df.to_csv(f'result/client_{client_id}_weights.csv', index=False)
file_suffix = f"{dataset}_{aggregation}_{at_type}_{detection}_{n_attackers[0]}"
# make directory for result
if not os.path.exists(file_suffix):
os.makedirs(file_suffix)
# Save scores and weights for each client
for client_id, scores in client_scores_dict.items():
df = pd.DataFrame(scores)
df.to_csv(f'{file_suffix}/client_{client_id}_scores.csv', index=False)
for client_id, weights in client_weights_dict.items():
df = pd.DataFrame(weights)
df.to_csv(f'{file_suffix}/client_{client_id}_weights.csv', index=False)
df = pd.DataFrame(server_scores)
df.to_csv(f'{file_suffix}/server_scores.csv', index=False)
df = pd.DataFrame(server_weights)
df.to_csv(f'{file_suffix}/server_weights.csv', index=False)
if __name__ == "__main__":
cProfile.run('main()', 'profiling_results')