forked from QinbinLi/MOON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
775 lines (642 loc) · 36.1 KB
/
main.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
import numpy as np
import json
import torch
import torch.optim as optim
import torch.nn as nn
import argparse
import logging
import os
import copy
import datetime
import random
from model import *
from utils import *
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='simple-cnn', help='neural network used in training')
parser.add_argument('--dataset', type=str, default='cifar10', help='dataset used for training')
parser.add_argument('--net_config', type=lambda x: list(map(int, x.split(', '))))
parser.add_argument('--partition', type=str, default='noniid', help='the data partitioning strategy')
parser.add_argument('--batch-size', type=int, default=64, help='input batch size for training (default: 64)')
parser.add_argument('--lr', type=float, default=0.1, help='learning rate (default: 0.1)')
parser.add_argument('--epochs', type=int, default=10, help='number of local epochs')
parser.add_argument('--n_parties', type=int, default=10, help='number of workers in a distributed cluster')
parser.add_argument('--alg', type=str, default='moon',
help='communication strategy: fedavg/fedprox')
parser.add_argument('--comm_round', type=int, default=50, help='number of maximum communication roun')
parser.add_argument('--init_seed', type=int, default=0, help="Random seed")
parser.add_argument('--dropout_p', type=float, required=False, default=0.0, help="Dropout probability. Default=0.0")
parser.add_argument('--datadir', type=str, required=False, default="./data/", help="Data directory")
parser.add_argument('--reg', type=float, default=1e-5, help="L2 regularization strength")
parser.add_argument('--logdir', type=str, required=False, default="./logs/", help='Log directory path')
parser.add_argument('--modeldir', type=str, required=False, default="./models/", help='Model directory path')
parser.add_argument('--beta', type=float, default=0.5,
help='The parameter for the dirichlet distribution for data partitioning')
parser.add_argument('--device', type=str, default='cuda:0', help='The device to run the program')
parser.add_argument('--log_file_name', type=str, default=None, help='The log file name')
parser.add_argument('--optimizer', type=str, default='sgd', help='the optimizer')
parser.add_argument('--mu', type=float, default=5, help='the mu parameter for fedprox or moon')
parser.add_argument('--out_dim', type=int, default=256, help='the output dimension for the projection layer')
parser.add_argument('--temperature', type=float, default=0.5, help='the temperature parameter for contrastive loss')
parser.add_argument('--local_max_epoch', type=int, default=100, help='the number of epoch for local optimal training')
parser.add_argument('--model_buffer_size', type=int, default=1, help='store how many previous models for contrastive loss')
parser.add_argument('--pool_option', type=str, default='FIFO', help='FIFO or BOX')
parser.add_argument('--sample_fraction', type=float, default=1.0, help='how many clients are sampled in each round')
parser.add_argument('--load_model_file', type=str, default=None, help='the model to load as global model')
parser.add_argument('--load_pool_file', type=str, default=None, help='the old model pool path to load')
parser.add_argument('--load_model_round', type=int, default=None, help='how many rounds have executed for the loaded model')
parser.add_argument('--load_first_net', type=int, default=1, help='whether load the first net as old net or not')
parser.add_argument('--normal_model', type=int, default=0, help='use normal model or aggregate model')
parser.add_argument('--loss', type=str, default='contrastive')
parser.add_argument('--save_model',type=int,default=0)
parser.add_argument('--use_project_head', type=int, default=1)
parser.add_argument('--server_momentum', type=float, default=0, help='the server momentum (FedAvgM)')
parser.add_argument('--inner', action='store_false', help='aggregation method')
args = parser.parse_args()
return args
def init_nets(net_configs, n_parties, args, device='cpu'):
nets = {net_i: None for net_i in range(n_parties)}
if args.dataset in {'mnist', 'cifar10', 'svhn', 'fmnist'}:
n_classes = 10
elif args.dataset == 'celeba':
n_classes = 2
elif args.dataset == 'cifar100':
n_classes = 100
elif args.dataset == 'tinyimagenet':
n_classes = 200
elif args.dataset == 'femnist':
n_classes = 26
elif args.dataset == 'emnist':
n_classes = 47
elif args.dataset == 'xray':
n_classes = 2
if args.normal_model:
for net_i in range(n_parties):
if args.model == 'simple-cnn':
net = SimpleCNNMNIST(input_dim=(16 * 4 * 4), hidden_dims=[120, 84], output_dim=10)
if device == 'cpu':
net.to(device)
else:
net = net.cuda()
nets[net_i] = net
else:
for net_i in range(n_parties):
if args.use_project_head:
net = ModelFedCon(args.model, args.out_dim, n_classes, net_configs)
else:
net = ModelFedCon_noheader(args.model, args.out_dim, n_classes, net_configs)
if device == 'cpu':
net.to(device)
else:
net = net.cuda()
nets[net_i] = net
model_meta_data = []
layer_type = []
for (k, v) in nets[0].state_dict().items(): # k:模型层名 v:层数据
model_meta_data.append(v.shape) # 记录模型每层尺寸大小
layer_type.append(k) # 模型每层名称
return nets, model_meta_data, layer_type
def train_net(net_id, net, train_dataloader, test_dataloader, epochs, lr, args_optimizer, args, device="cpu"):
net = nn.DataParallel(net)
net.cuda()
logger.info('Training network %s' % str(net_id))
logger.info('n_training: %d' % len(train_dataloader))
logger.info('n_test: %d' % len(test_dataloader))
train_acc,_ = compute_accuracy(net, train_dataloader, device=device)
test_acc, conf_matrix,_ = compute_accuracy(net, test_dataloader, get_confusion_matrix=True, device=device)
logger.info('>> Pre-Training Training accuracy: {}'.format(train_acc))
logger.info('>> Pre-Training Test accuracy: {}'.format(test_acc))
if args_optimizer == 'adam':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay=args.reg)
elif args_optimizer == 'amsgrad':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay=args.reg,
amsgrad=True)
elif args_optimizer == 'sgd':
optimizer = optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, momentum=0.9,
weight_decay=args.reg)
criterion = nn.CrossEntropyLoss().cuda()
cnt = 0
for epoch in range(epochs):
epoch_loss_collector = []
for batch_idx, (x, target) in enumerate(train_dataloader):
x, target = x.cuda(), target.cuda()
optimizer.zero_grad()
x.requires_grad = False
target.requires_grad = False
target = target.long()
_,_,out = net(x)
loss = criterion(out, target)
loss.backward()
optimizer.step()
cnt += 1
epoch_loss_collector.append(loss.item())
epoch_loss = sum(epoch_loss_collector) / len(epoch_loss_collector)
logger.info('Epoch: %d Loss: %f' % (epoch, epoch_loss))
if epoch % 10 == 0:
train_acc, _ = compute_accuracy(net, train_dataloader, device=device)
test_acc, conf_matrix, _ = compute_accuracy(net, test_dataloader, get_confusion_matrix=True, device=device)
logger.info('>> Training accuracy: %f' % train_acc)
logger.info('>> Test accuracy: %f' % test_acc)
train_acc, _ = compute_accuracy(net, train_dataloader, device=device)
test_acc, conf_matrix, _ = compute_accuracy(net, test_dataloader, get_confusion_matrix=True, device=device)
logger.info('>> Training accuracy: %f' % train_acc)
logger.info('>> Test accuracy: %f' % test_acc)
net.to('cpu')
logger.info(' ** Training complete **')
return train_acc, test_acc
def train_net_fedprox(net_id, net, global_net, train_dataloader, test_dataloader, epochs, lr, args_optimizer, mu, args,
device="cpu"):
# global_net.to(device)
net = nn.DataParallel(net)
net.cuda()
# else:
# net.to(device)
logger.info('Training network %s' % str(net_id))
logger.info('n_training: %d' % len(train_dataloader))
logger.info('n_test: %d' % len(test_dataloader))
train_acc, _ = compute_accuracy(net, train_dataloader, device=device)
test_acc, conf_matrix, _ = compute_accuracy(net, test_dataloader, get_confusion_matrix=True, device=device)
logger.info('>> Pre-Training Training accuracy: {}'.format(train_acc))
logger.info('>> Pre-Training Test accuracy: {}'.format(test_acc))
if args_optimizer == 'adam':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay=args.reg)
elif args_optimizer == 'amsgrad':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay=args.reg,
amsgrad=True)
elif args_optimizer == 'sgd':
optimizer = optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, momentum=0.9,
weight_decay=args.reg)
criterion = nn.CrossEntropyLoss().cuda()
cnt = 0
global_weight_collector = list(global_net.cuda().parameters())
for epoch in range(epochs):
epoch_loss_collector = []
for batch_idx, (x, target) in enumerate(train_dataloader):
x, target = x.cuda(), target.cuda()
optimizer.zero_grad()
x.requires_grad = False
target.requires_grad = False
target = target.long()
_,_,out = net(x)
loss = criterion(out, target)
# for fedprox
fed_prox_reg = 0.0
# fed_prox_reg += np.linalg.norm([i - j for i, j in zip(global_weight_collector, get_trainable_parameters(net).tolist())], ord=2)
for param_index, param in enumerate(net.parameters()):
fed_prox_reg += ((mu / 2) * torch.norm((param - global_weight_collector[param_index])) ** 2)
loss += fed_prox_reg
loss.backward()
optimizer.step()
cnt += 1
epoch_loss_collector.append(loss.item())
epoch_loss = sum(epoch_loss_collector) / len(epoch_loss_collector)
logger.info('Epoch: %d Loss: %f' % (epoch, epoch_loss))
train_acc, _ = compute_accuracy(net, train_dataloader, device=device)
test_acc, conf_matrix, _ = compute_accuracy(net, test_dataloader, get_confusion_matrix=True, device=device)
logger.info('>> Training accuracy: %f' % train_acc)
logger.info('>> Test accuracy: %f' % test_acc)
net.to('cpu')
logger.info(' ** Training complete **')
return train_acc, test_acc
def train_net_fedcon(net_id, net, global_net, previous_nets, train_dataloader, test_dataloader, epochs, lr, args_optimizer, mu, temperature, args,
round, device="cpu"):
net = nn.DataParallel(net)
net.cuda()
logger.info('Training network %s' % str(net_id))
logger.info('n_training: %d' % len(train_dataloader))
logger.info('n_test: %d' % len(test_dataloader))
train_acc, _ = compute_accuracy(net, train_dataloader, device=device)
test_acc, conf_matrix, _ = compute_accuracy(net, test_dataloader, get_confusion_matrix=True, device=device)
logger.info('>> Pre-Training Training accuracy: {}'.format(train_acc))
logger.info('>> Pre-Training Test accuracy: {}'.format(test_acc))
if args_optimizer == 'adam':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay=args.reg)
elif args_optimizer == 'amsgrad':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay=args.reg,
amsgrad=True)
elif args_optimizer == 'sgd':
optimizer = optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, momentum=0.9,
weight_decay=args.reg)
criterion = nn.CrossEntropyLoss().cuda()
global_net.to(device)
for previous_net in previous_nets:
previous_net.cuda()
global_w = global_net.state_dict()
cnt = 0
cos=torch.nn.CosineSimilarity(dim=-1) # 创建一个计算余弦相似度的对象
local_grad = [param.clone().detach() - param.clone().detach() for param in net.parameters()] # 创建一个全0梯度列表
# mu = 0.001
# 本地训练
for epoch in range(epochs):
epoch_loss_collector = []
epoch_loss1_collector = []
epoch_loss2_collector = []
for batch_idx, (x, target) in enumerate(train_dataloader):
x, target = x.cuda(), target.cuda()
optimizer.zero_grad()
x.requires_grad = False
target.requires_grad = False
target = target.long()
_, pro1, out = net(x) # z
_, pro2, _ = global_net(x) # z_glob
posi = cos(pro1, pro2) # sim(z,z_glob), 1行
logits = posi.reshape(-1,1) # 1列
for previous_net in previous_nets:
previous_net.cuda()
_, pro3, _ = previous_net(x)
nega = cos(pro1, pro3)
logits = torch.cat((logits, nega.reshape(-1,1)), dim=1)
previous_net.to('cpu')
logits /= temperature
labels = torch.zeros(x.size(0)).cuda().long()
loss2 = mu * criterion(logits, labels)
loss1 = criterion(out, target)
loss = loss1 + loss2
loss.backward()
optimizer.step()
cnt += 1
epoch_loss_collector.append(loss.item())
epoch_loss1_collector.append(loss1.item())
epoch_loss2_collector.append(loss2.item())
epoch_loss = sum(epoch_loss_collector) / len(epoch_loss_collector)
epoch_loss1 = sum(epoch_loss1_collector) / len(epoch_loss1_collector)
epoch_loss2 = sum(epoch_loss2_collector) / len(epoch_loss2_collector)
logger.info('Epoch: %d Loss: %f Loss1: %f Loss2: %f' % (epoch, epoch_loss, epoch_loss1, epoch_loss2))
for idx, param in enumerate(net.parameters()):
local_grad[idx] += param.grad.clone().detach()
for previous_net in previous_nets:
previous_net.to('cpu')
train_acc, _ = compute_accuracy(net, train_dataloader, device=device)
test_acc, conf_matrix, _ = compute_accuracy(net, test_dataloader, get_confusion_matrix=True, device=device)
logger.info('>> Training accuracy: %f' % train_acc)
logger.info('>> Test accuracy: %f' % test_acc)
net.to('cpu')
logger.info(' ** Training complete **')
return train_acc, test_acc, local_grad
# 客户端本地训练
def local_train_net(nets, local_grads, args, net_dataidx_map, train_dl=None, test_dl=None, global_model = None, prev_model_pool = None, server_c = None, clients_c = None, round=None, device="cpu"):
avg_acc = 0.0
acc_list = []
if global_model:
global_model.cuda()
if server_c:
server_c.cuda()
server_c_collector = list(server_c.cuda().parameters())
new_server_c_collector = copy.deepcopy(server_c_collector)
for net_id, net in nets.items():
dataidxs = net_dataidx_map[net_id]
logger.info("Training network %s. n_training: %d" % (str(net_id), len(dataidxs)))
train_dl_local, test_dl_local, _, _ = get_dataloader(args.dataset, args.datadir, args.batch_size, 32, dataidxs)
train_dl_global, test_dl_global, _, _ = get_dataloader(args.dataset, args.datadir, args.batch_size, 32)
n_epoch = args.epochs
if args.alg == 'fedavg':
trainacc, testacc = train_net(net_id, net, train_dl_local, test_dl, n_epoch, args.lr, args.optimizer, args,
device=device)
elif args.alg == 'fedprox':
trainacc, testacc = train_net_fedprox(net_id, net, global_model, train_dl_local, test_dl, n_epoch, args.lr,
args.optimizer, args.mu, args, device=device)
elif args.alg == 'moon':
prev_models=[]
for i in range(len(prev_model_pool)):
prev_models.append(prev_model_pool[i][net_id])
trainacc, testacc, local_grad = train_net_fedcon(net_id, net, global_model, prev_models, train_dl_local, test_dl, n_epoch, args.lr,
args.optimizer, args.mu, args.temperature, args, round, device=device)
# local_grad_tensor = torch.cat([grad.flatten() for grad in local_grad])
# local_grads[net_id] = local_grad_tensor
local_grads[net_id] = copy.deepcopy(local_grad)
elif args.alg == 'local_training':
trainacc, testacc = train_net(net_id, net, train_dl_local, test_dl, n_epoch, args.lr, args.optimizer, args,
device=device)
logger.info("net %d final test acc %f" % (net_id, testacc))
avg_acc += testacc
acc_list.append(testacc)
avg_acc /= args.n_parties
if args.alg == 'local_training':
logger.info("avg test acc %f" % avg_acc)
logger.info("std acc %f" % np.std(acc_list))
if global_model:
global_model.to('cpu')
if server_c:
for param_index, param in enumerate(server_c.parameters()):
server_c_collector[param_index] = new_server_c_collector[param_index]
server_c.to('cpu')
return nets, local_grads
if __name__ == '__main__':
# 联邦学习初始化
args = get_args()
mkdirs(args.logdir)
mkdirs(args.modeldir)
if args.log_file_name is None:
argument_path = 'experiment_arguments-%s.json' % datetime.datetime.now().strftime("%Y-%m-%d-%H%M-%S")
else:
argument_path = args.log_file_name + '.json'
with open(os.path.join(args.logdir, argument_path), 'w') as f:
json.dump(str(args), f)
device = torch.device(args.device)
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
if args.log_file_name is None:
args.log_file_name = 'experiment_log-%s' % (datetime.datetime.now().strftime("%Y-%m-%d-%H%M-%S"))
log_path = args.log_file_name + '.log'
logging.basicConfig(
filename=os.path.join(args.logdir, log_path),
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M', level=logging.DEBUG, filemode='w')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.info(device)
seed = args.init_seed
logger.info("#" * 100)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
random.seed(seed)
logger.info("Partitioning data")
# 划分数据
X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts = partition_data(
args.dataset, args.datadir, args.logdir, args.partition, args.n_parties, beta=args.beta)
n_party_per_round = int(args.n_parties * args.sample_fraction) # 每轮选中客户端数量
party_list = [i for i in range(args.n_parties)] # 客户端列表
party_list_rounds = []
if n_party_per_round != args.n_parties: # 参与方数量大于每轮训练数随机选择
for i in range(args.comm_round):
party_list_rounds.append(random.sample(party_list, n_party_per_round))
else:
for i in range(args.comm_round):
party_list_rounds.append(party_list)
n_classes = len(np.unique(y_train))
train_dl_global, test_dl, train_ds_global, test_ds_global = get_dataloader(args.dataset,
args.datadir,
args.batch_size,
32)
print("len train_dl_global:", len(train_ds_global))
train_dl=None
data_size = len(test_ds_global)
logger.info("Initializing nets")
nets, local_model_meta_data, layer_type = init_nets(args.net_config, args.n_parties, args, device='cpu') # 获取每个客户端的模型、模型每层的名字和尺寸
global_models, global_model_meta_data, global_layer_type = init_nets(args.net_config, 1, args, device='cpu') # 获取全局模型、全局模型每层的名字和尺寸
global_model = global_models[0]
n_comm_rounds = args.comm_round
if args.load_model_file and args.alg != 'plot_visual':
global_model.load_state_dict(torch.load(args.load_model_file))
n_comm_rounds -= args.load_model_round
if args.server_momentum:
moment_v = copy.deepcopy(global_model.state_dict())
for key in moment_v:
moment_v[key] = 0
if args.alg == 'moon':
old_nets_pool = []
if args.load_pool_file:
for nets_id in range(args.model_buffer_size):
old_nets, _, _ = init_nets(args.net_config, args.n_parties, args, device='cpu')
checkpoint = torch.load(args.load_pool_file)
for net_id, net in old_nets.items():
net.load_state_dict(checkpoint['pool' + str(nets_id) + '_'+'net'+str(net_id)])
old_nets_pool.append(old_nets)
elif args.load_first_net: # 第一轮训练
if len(old_nets_pool) < args.model_buffer_size:
old_nets = copy.deepcopy(nets) # 旧模型和原模型相同
for _, net in old_nets.items(): # 把旧模型设置为不需要梯度
net.eval()
for param in net.parameters():
param.requires_grad = False
for round in range(n_comm_rounds):
logger.info("in comm round:" + str(round))
party_list_this_round = party_list_rounds[round] # 本轮进行训练的客户端
global_model.eval() # 把全全局模型设置为不需要梯度1
for param in global_model.parameters():
param.requires_grad = False
global_w = global_model.state_dict()
if args.server_momentum:
old_w = copy.deepcopy(global_model.state_dict())
if args.inner:
w_prev = copy.deepcopy(global_model.state_dict())
local_grads = {}
nets_this_round = {k: nets[k] for k in party_list_this_round} # 存储本轮训练每个客户端的模型
for net in nets_this_round.values():
net.load_state_dict(global_w) # 加载本轮全局模型参数
# 每个客户端本地训练,返回net
local_train_net(nets_this_round, local_grads, args, net_dataidx_map, train_dl=train_dl, test_dl=test_dl, global_model = global_model, prev_model_pool=old_nets_pool, round=round, device=device0)
total_data_points = sum([len(net_dataidx_map[r]) for r in party_list_this_round])
fed_avg_freqs = [len(net_dataidx_map[r]) / total_data_points for r in party_list_this_round] # 每个客户端持有数据集数量权重
# 服务器聚合客户端模型参数
for net_id, net in enumerate(nets_this_round.values()):
net_para = net.state_dict()
if net_id == 0:
for key in net_para:
global_w[key] = net_para[key] * fed_avg_freqs[net_id]
else:
for key in net_para:
global_w[key] += net_para[key] * fed_avg_freqs[net_id]
if args.server_momentum:
delta_w = copy.deepcopy(global_w)
for key in delta_w:
delta_w[key] = old_w[key] - global_w[key]
moment_v[key] = args.server_momentum * moment_v[key] + (1-args.server_momentum) * delta_w[key]
global_w[key] = old_w[key] - moment_v[key]
# 计算出全局模型参数之后可以求出全局模型梯度,然后根据每个客户端的本地参数计算出本地梯度,随后计算余弦相似度进行内积聚合
if args.inner:
glob_grad = None
# # 展平梯度计算余弦相似度
# # 计算全局梯度
# for net_id, grad in enumerate(local_grads.values()):
# net_grad = copy.deepcopy(local_grads[net_id])
# if glob_grad is None:
# glob_grad = net_grad * fed_avg_freqs[net_id]
# else:
# glob_grad += net_grad * fed_avg_freqs[net_id]
# # 计算余弦相似度
# cos = torch.nn.CosineSimilarity(dim=-1) # 创建一个计算余弦相似度的对象
# sim = {}
# sum_sim = 0
# for i in party_list_this_round:
# sim[i] = cos(glob_grad, local_grads[i])
# sum_sim += sim[i]
# # 内积聚合
# for net_id, net in enumerate(nets_this_round.values()):
# net_para = net.state_dict()
# if net_id == 0:
# for key in net_para:
# global_w[key] = net_para[key].to(device) * (sim[net_id] / sum_sim)
# else:
# for key in net_para:
# global_w[key] += net_para[key].to(device) * (sim[net_id] / sum_sim)
# 不展平梯度计算余弦相似度
for net_id, net in enumerate(local_grads.values()):
net_grad = copy.deepcopy(local_grads[net_id])
if glob_grad is None:
glob_grad = {}
for i in range(len(net_grad)):
glob_grad[i] = net_grad[i] * fed_avg_freqs[net_id]
else:
for i in range(len(net_grad)):
glob_grad[i] += net_grad[i] * fed_avg_freqs[net_id]
# 计算余弦相似度
cos = torch.nn.CosineSimilarity(dim=-1) # 创建一个计算余弦相似度的对象
sim = {}
sum_sim = None
for i in party_list_this_round:
sim[i] = []
for j in range(len(glob_grad)):
sim[i].append(cos(glob_grad[j], local_grads[i][j]))
if sum_sim is None:
sum_sim = copy.deepcopy(sim[i])
else:
for k in range(len(sum_sim)):
sum_sim[k] += sim[i][k]
# 内积聚合
for net_id, net in enumerate(nets_this_round.values()):
net_para = net.state_dict()
if net_id == 0:
i = 0
for key in net_para:
global_w[key] = net_para[key].to(device) * (sim[net_id][i]/sum_sim[i]).unsqueeze(-1)
i += 1
else:
i = 0
for key in net_para:
global_w[key] += net_para[key].to(device) * (sim[net_id][i]/sum_sim[i]).unsqueeze(-1)
i += 1
# # 计算全局梯度
# for net_id, grad in enumerate(local_grads.values()):
# net_grad = copy.deepcopy(local_grads[net_id])
# if glob_grad is None:
# glob_grad = net_grad * fed_avg_freqs[net_id]
# else:
# glob_grad += net_grad * fed_avg_freqs[net_id]
# # 展平梯度计算内积
# inner_product = {}
# sum_inner_product = 0
# for i in party_list_this_round:
# inner_product[i] = torch.dot(glob_grad, local_grads[i])
# sum_inner_product += inner_product[i]
# # 内积聚合
# for net_id, net in enumerate(nets_this_round.values()):
# net_para = copy.deepcopy(net.state_dict())
# if net_id == 0:
# for key in net_para:
# global_w[key] = net_para[key].to(device) * (inner_product[net_id]/sum_inner_product)
# else:
# for key in net_para:
# global_w[key] += net_para[key].to(device) * (inner_product[net_id]/sum_inner_product)
global_model.load_state_dict(global_w)
#summary(global_model.to(device), (3, 32, 32))
logger.info('global n_training: %d' % len(train_dl_global))
logger.info('global n_test: %d' % len(test_dl))
global_model.cuda()
train_acc, train_loss = compute_accuracy(global_model, train_dl_global, device=device)
test_acc, conf_matrix, _ = compute_accuracy(global_model, test_dl, get_confusion_matrix=True, device=device)
global_model.to('cpu')
logger.info('>> Global Model Train accuracy: %f' % train_acc)
logger.info('>> Global Model Test accuracy: %f' % test_acc)
logger.info('>> Global Model Train loss: %f' % train_loss)
if len(old_nets_pool) < args.model_buffer_size:
old_nets = copy.deepcopy(nets)
for _, net in old_nets.items():
net.eval()
for param in net.parameters():
param.requires_grad = False
old_nets_pool.append(old_nets)
elif args.pool_option == 'FIFO':
old_nets = copy.deepcopy(nets)
for _, net in old_nets.items():
net.eval()
for param in net.parameters():
param.requires_grad = False
for i in range(args.model_buffer_size-2, -1, -1):
old_nets_pool[i] = old_nets_pool[i+1]
old_nets_pool[args.model_buffer_size - 1] = old_nets
mkdirs(args.modeldir+'fedcon/')
if args.save_model:
torch.save(global_model.state_dict(), args.modeldir+'fedcon/global_model_'+args.log_file_name+'.pth')
torch.save(nets[0].state_dict(), args.modeldir+'fedcon/localmodel0'+args.log_file_name+'.pth')
for nets_id, old_nets in enumerate(old_nets_pool):
torch.save({'pool'+ str(nets_id) + '_'+'net'+str(net_id): net.state_dict() for net_id, net in old_nets.items()}, args.modeldir+'fedcon/prev_model_pool_'+args.log_file_name+'.pth')
elif args.alg == 'fedavg':
for round in range(n_comm_rounds):
logger.info("in comm round:" + str(round))
party_list_this_round = party_list_rounds[round]
global_w = global_model.state_dict()
if args.server_momentum:
old_w = copy.deepcopy(global_model.state_dict())
nets_this_round = {k: nets[k] for k in party_list_this_round}
for net in nets_this_round.values():
net.load_state_dict(global_w)
local_train_net(nets_this_round, args, net_dataidx_map, train_dl=train_dl, test_dl=test_dl, device=device)
total_data_points = sum([len(net_dataidx_map[r]) for r in party_list_this_round])
fed_avg_freqs = [len(net_dataidx_map[r]) / total_data_points for r in party_list_this_round]
for net_id, net in enumerate(nets_this_round.values()):
net_para = net.state_dict()
if net_id == 0:
for key in net_para:
global_w[key] = net_para[key] * fed_avg_freqs[net_id]
else:
for key in net_para:
global_w[key] += net_para[key] * fed_avg_freqs[net_id]
if args.server_momentum:
delta_w = copy.deepcopy(global_w)
for key in delta_w:
delta_w[key] = old_w[key] - global_w[key]
moment_v[key] = args.server_momentum * moment_v[key] + (1-args.server_momentum) * delta_w[key]
global_w[key] = old_w[key] - moment_v[key]
global_model.load_state_dict(global_w)
#logger.info('global n_training: %d' % len(train_dl_global))
logger.info('global n_test: %d' % len(test_dl))
global_model.cuda()
train_acc, train_loss = compute_accuracy(global_model, train_dl_global, device=device)
test_acc, conf_matrix, _ = compute_accuracy(global_model, test_dl, get_confusion_matrix=True, device=device)
logger.info('>> Global Model Train accuracy: %f' % train_acc)
logger.info('>> Global Model Test accuracy: %f' % test_acc)
logger.info('>> Global Model Train loss: %f' % train_loss)
mkdirs(args.modeldir+'fedavg/')
global_model.to('cpu')
torch.save(global_model.state_dict(), args.modeldir+'fedavg/'+'globalmodel'+args.log_file_name+'.pth')
torch.save(nets[0].state_dict(), args.modeldir+'fedavg/'+'localmodel0'+args.log_file_name+'.pth')
elif args.alg == 'fedprox':
for round in range(n_comm_rounds):
logger.info("in comm round:" + str(round))
party_list_this_round = party_list_rounds[round]
global_w = global_model.state_dict()
nets_this_round = {k: nets[k] for k in party_list_this_round}
for net in nets_this_round.values():
net.load_state_dict(global_w)
local_train_net(nets_this_round, args, net_dataidx_map, train_dl=train_dl,test_dl=test_dl, global_model = global_model, device=device)
global_model.to('cpu')
# update global model
total_data_points = sum([len(net_dataidx_map[r]) for r in party_list_this_round])
fed_avg_freqs = [len(net_dataidx_map[r]) / total_data_points for r in party_list_this_round]
for net_id, net in enumerate(nets_this_round.values()):
net_para = net.state_dict()
if net_id == 0:
for key in net_para:
global_w[key] = net_para[key] * fed_avg_freqs[net_id]
else:
for key in net_para:
global_w[key] += net_para[key] * fed_avg_freqs[net_id]
global_model.load_state_dict(global_w)
logger.info('global n_training: %d' % len(train_dl_global))
logger.info('global n_test: %d' % len(test_dl))
global_model.cuda()
train_acc, train_loss = compute_accuracy(global_model, train_dl_global, device=device)
test_acc, conf_matrix, _ = compute_accuracy(global_model, test_dl, get_confusion_matrix=True, device=device)
logger.info('>> Global Model Train accuracy: %f' % train_acc)
logger.info('>> Global Model Test accuracy: %f' % test_acc)
logger.info('>> Global Model Train loss: %f' % train_loss)
mkdirs(args.modeldir + 'fedprox/')
global_model.to('cpu')
torch.save(global_model.state_dict(), args.modeldir +'fedprox/'+args.log_file_name+ '.pth')
elif args.alg == 'local_training':
logger.info("Initializing nets")
local_train_net(nets, args, net_dataidx_map, train_dl=train_dl,test_dl=test_dl, device=device)
mkdirs(args.modeldir + 'localmodel/')
for net_id, net in nets.items():
torch.save(net.state_dict(), args.modeldir + 'localmodel/'+'model'+str(net_id)+args.log_file_name+ '.pth')
elif args.alg == 'all_in':
nets, _, _ = init_nets(args.net_config, 1, args, device='cpu')
# nets[0].to(device)
trainacc, testacc = train_net(0, nets[0], train_dl_global, test_dl, args.epochs, args.lr,
args.optimizer, args, device=device)
logger.info("All in test acc: %f" % testacc)
mkdirs(args.modeldir + 'all_in/')
torch.save(nets[0].state_dict(), args.modeldir+'all_in/'+args.log_file_name+ '.pth')