-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_ifca.py
460 lines (348 loc) · 17.1 KB
/
main_ifca.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
import numpy as np
import copy
import os
import gc
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from torchvision import datasets, transforms
import pickle
from src.data import *
from src.models import *
from src.fedavg import *
from src.client import *
from src.clustering import *
from src.utils import *
args = args_parser()
args.device = torch.device('cuda:{}'.format(args.gpu) if torch.cuda.is_available() else 'cpu')
torch.cuda.set_device(args.gpu) ## Setting cuda on GPU
NUM_CLUSTER = args.nclusters
def mkdirs(dirpath):
try:
os.makedirs(dirpath)
except Exception as _:
pass
path = args.savedir + args.alg + '/' + args.partition + '/' + args.dataset + '/' #+ str(args.trial)
mkdirs(path)
template = "Algorithm {}, Clients {}, Dataset {}, Model {}, Non-IID {}, Threshold {}, K {}, Linkage {}, LR {}, Ep {}, Rounds {}, bs {}, frac {}"
s = template.format(args.alg, args.num_users, args.dataset, args.model, args.partition, args.cluster_alpha, args.n_basis, args.linkage, args.lr, args.local_ep, args.rounds, args.local_ep, args.frac)
print(s)
print(str(args))
##################################### Data partitioning section
args.local_view = True
X_train, y_train, X_test, y_test, net_dataidx_map, net_dataidx_map_test, \
traindata_cls_counts, testdata_cls_counts = partition_data(args.dataset,
args.datadir, args.logdir, args.partition, args.num_users, beta=args.beta, local_view=args.local_view)
train_dl_global, test_dl_global, train_ds_global, test_ds_global = get_dataloader(args.dataset,
args.datadir,
args.batch_size,
32)
print("len train_ds_global:", len(train_ds_global))
print("len test_ds_global:", len(test_ds_global))
################################### build model
def init_nets(args, dropout_p=0.5):
users_model = []
for net_i in range(-1, args.num_users):
if args.dataset == "generated":
net = PerceptronModel().to(args.device)
elif args.model == "mlp":
if args.dataset == 'covtype':
input_size = 54
output_size = 2
hidden_sizes = [32,16,8]
elif args.dataset == 'a9a':
input_size = 123
output_size = 2
hidden_sizes = [32,16,8]
elif args.dataset == 'rcv1':
input_size = 47236
output_size = 2
hidden_sizes = [32,16,8]
elif args.dataset == 'SUSY':
input_size = 18
output_size = 2
hidden_sizes = [16,8]
net = FcNet(input_size, hidden_sizes, output_size, dropout_p).to(args.device)
elif args.model == "vgg":
net = vgg11().to(args.device)
elif args.model == "simple-cnn":
if args.dataset in ("cifar10", "cinic10", "svhn"):
net = SimpleCNN(input_dim=(16 * 5 * 5), hidden_dims=[120, 84], output_dim=10).to(args.device)
elif args.dataset in ("mnist", 'femnist', 'fmnist'):
net = SimpleCNNMNIST(input_dim=(16 * 4 * 4), hidden_dims=[120, 84], output_dim=10).to(args.device)
elif args.dataset == 'cifar100':
net = SimpleCNN(input_dim=(16 * 5 * 5), hidden_dims=[120, 84], output_dim=100).to(args.device)
elif args.dataset == 'celeba':
net = SimpleCNN(input_dim=(16 * 5 * 5), hidden_dims=[120, 84], output_dim=2).to(args.device)
elif args.model =="simple-cnn-3":
if args.dataset == 'cifar100':
net = SimpleCNN_3(input_dim=(16 * 3 * 5 * 5), hidden_dims=[120*3, 84*3], output_dim=100).to(args.device)
elif args.model == "vgg-9":
if args.dataset in ("mnist", 'femnist'):
net = ModerateCNNMNIST().to(args.device)
elif args.dataset in ("cifar10", "cinic10", "svhn"):
# print("in moderate cnn")
net = ModerateCNN().to(args.device)
elif args.dataset == 'celeba':
net = ModerateCNN(output_dim=2).to(args.device)
elif args.model == 'resnet9':
if args.dataset == 'cifar100':
net = ResNet9(in_channels=3, num_classes=100)
elif args.dataset == 'tinyimagenet':
net = ResNet9(in_channels=3, num_classes=200, dim=512*2*2)
elif args.model == "resnet":
net = ResNet50_cifar10().to(args.device)
elif args.model == "vgg16":
net = vgg16().to(args.device)
else:
print("not supported yet")
exit(1)
if net_i == -1:
net_glob = copy.deepcopy(net)
w_glob_per_cluster = []
for _ in range(NUM_CLUSTER):
net_glob.apply(weight_init)
w_glob_per_cluster.append(copy.deepcopy(net_glob.state_dict()))
net_glob.apply(weight_init)
initial_state_dict = copy.deepcopy(net_glob.state_dict())
server_state_dict = copy.deepcopy(net_glob.state_dict())
if args.load_initial:
initial_state_dict = torch.load(args.load_initial)
server_state_dict = torch.load(args.load_initial)
net_glob.load_state_dict(initial_state_dict)
else:
users_model.append(copy.deepcopy(net))
users_model[net_i].load_state_dict(initial_state_dict)
return users_model, net_glob, initial_state_dict, server_state_dict, w_glob_per_cluster
print(f'MODEL: {args.model}, Dataset: {args.dataset}')
users_model, net_glob, initial_state_dict, server_state_dict, w_glob_per_cluster = init_nets(args, dropout_p=0.5)
print(net_glob)
total = 0
for name, param in net_glob.named_parameters():
print(name, param.size())
total += np.prod(param.size())
#print(np.array(param.data.cpu().numpy().reshape([-1])))
#print(isinstance(param.data.cpu().numpy(), np.array))
print(total)
################################# Fixing all to the same Init and data partitioning and random users
#print(os.getcwd())
# tt = '../initialization/' + 'traindata_'+args.dataset+'_'+args.partition+'.pkl'
# with open(tt, 'rb') as f:
# net_dataidx_map = pickle.load(f)
# tt = '../initialization/' + 'testdata_'+args.dataset+'_'+args.partition+'.pkl'
# with open(tt, 'rb') as f:
# net_dataidx_map_test = pickle.load(f)
# tt = '../initialization/' + 'traindata_cls_counts_'+args.dataset+'_'+args.partition+'.pkl'
# with open(tt, 'rb') as f:
# traindata_cls_counts = pickle.load(f)
# tt = '../initialization/' + 'testdata_cls_counts_'+args.dataset+'_'+args.partition+'.pkl'
# with open(tt, 'rb') as f:
# testdata_cls_counts = pickle.load(f)
#tt = '../initialization/' + 'init_'+args.model+'_'+args.dataset+'.pth'
#initial_state_dict = torch.load(tt, map_location=args.device)
#server_state_dict = copy.deepcopy(initial_state_dict)
#for idx in range(args.num_users):
# users_model[idx].load_state_dict(initial_state_dict)
#net_glob.load_state_dict(initial_state_dict)
# tt = '../initialization/' + 'comm_users.pkl'
# with open(tt, 'rb') as f:
# comm_users = pickle.load(f)
################################# Initializing Clients
clients = []
for idx in range(args.num_users):
dataidxs = net_dataidx_map[idx]
if net_dataidx_map_test is None:
dataidx_test = None
else:
dataidxs_test = net_dataidx_map_test[idx]
#print(f'Initializing Client {idx}')
noise_level = args.noise
if idx == args.num_users - 1:
noise_level = 0
if args.noise_type == 'space':
train_dl_local, test_dl_local, train_ds_local, test_ds_local = get_dataloader(args.dataset,
args.datadir, args.local_bs, 32,
dataidxs, noise_level, idx,
args.num_users-1,
dataidxs_test=dataidxs_test)
else:
noise_level = args.noise / (args.num_users - 1) * idx
train_dl_local, test_dl_local, train_ds_local, test_ds_local = get_dataloader(args.dataset,
args.datadir, args.local_bs, 32,
dataidxs, noise_level,
dataidxs_test=dataidxs_test)
clients.append(Client_ClusterFL(idx, copy.deepcopy(users_model[idx]), args.local_bs, args.local_ep,
args.lr, args.momentum, args.device, train_dl_local, test_dl_local))
###################################### Federation
float_formatter = "{:.4f}".format
#np.set_printoptions(formatter={float: float_formatting_function})
np.set_printoptions(formatter={'float_kind':float_formatter})
loss_train = []
init_tracc_pr = [] # initial train accuracy for each round
final_tracc_pr = [] # final train accuracy for each round
init_tacc_pr = [] # initial test accuarcy for each round
final_tacc_pr = [] # final test accuracy for each round
init_tloss_pr = [] # initial test loss for each round
final_tloss_pr = [] # final test loss for each round
clients_best_acc = [0 for _ in range(args.num_users)]
w_locals, loss_locals = [], []
init_local_tacc = [] # initial local test accuracy at each round
final_local_tacc = [] # final local test accuracy at each round
init_local_tloss = [] # initial local test loss at each round
final_local_tloss = [] # final local test loss at each round
ckp_avg_tacc = []
ckp_avg_best_tacc = []
users_best_acc = [0 for _ in range(args.num_users)]
best_glob_acc = 0
best_glob_w = None
print_flag = False
for iteration in range(args.rounds):
m = max(int(args.frac * args.num_users), 1)
idxs_users = np.random.choice(range(args.num_users), m, replace=False)
#idxs_users = comm_users[iteration]
print(f'###### ROUND {iteration+1} ######')
print(f'Clients {idxs_users}')
selected_clusters = [[] for _ in range(NUM_CLUSTER)]
w_locals_clusters = [[] for _ in range(NUM_CLUSTER)]
for idx in idxs_users:
assert (NUM_CLUSTER == len(w_glob_per_cluster))
acc_select = []
for i in range(NUM_CLUSTER):
clients[idx].set_state_dict(copy.deepcopy(w_glob_per_cluster[i]))
loss, acc = clients[idx].eval_test()
acc_select.append(acc)
idx_cluster = np.argmax(acc_select)
clients[idx].set_state_dict(copy.deepcopy(w_glob_per_cluster[idx_cluster]))
selected_clusters[idx_cluster].append(idx)
print(f'Client {idx}, Select Cluster: {idx_cluster}')
print(f'acc clusters: {acc_select}')
loss, acc = clients[idx].eval_test()
init_local_tacc.append(acc)
init_local_tloss.append(loss)
loss = clients[idx].train(is_print=False)
w_locals.append(copy.deepcopy(clients[idx].get_state_dict()))
w_locals_clusters[idx_cluster].append(w_locals[-1])
loss_locals.append(copy.deepcopy(loss))
loss, acc = clients[idx].eval_test()
if acc > clients_best_acc[idx]:
clients_best_acc[idx] = acc
final_local_tacc.append(acc)
final_local_tloss.append(loss)
# FedAvg per cluster
total_data_points = [sum([len(net_dataidx_map[r]) for r in clust]) for clust in selected_clusters]
fed_avg_freqs = [[len(net_dataidx_map[r]) / total_data_points[clust_id] for r in selected_clusters[clust_id]]
for clust_id in range(NUM_CLUSTER)]
acc_glob_pc = []
for i in range(NUM_CLUSTER):
if w_locals_clusters[i] != []:
ww = FedAvg(w_locals_clusters[i], weight_avg = fed_avg_freqs[i])
w_glob_per_cluster[i] = copy.deepcopy(ww)
net_glob.load_state_dict(copy.deepcopy(ww))
_, acc = eval_test(net_glob, args, test_dl_global)
if acc > best_glob_acc:
best_glob_acc = acc
best_glob_w = copy.deepcopy(ww)
acc_glob_pc.append(acc)
# update global weights
#w_glob = FedAvg(w_locals)
# copy weight to net_glob
#net_glob.load_state_dict(w_glob)
# print loss
loss_avg = sum(loss_locals) / len(loss_locals)
avg_init_tloss = sum(init_local_tloss) / len(init_local_tloss)
avg_init_tacc = sum(init_local_tacc) / len(init_local_tacc)
avg_final_tloss = sum(final_local_tloss) / len(final_local_tloss)
avg_final_tacc = sum(final_local_tacc) / len(final_local_tacc)
print('## END OF ROUND ##')
template = 'Average Train loss {:.3f}'
print(template.format(loss_avg))
template = "AVG Init Test Loss: {:.3f}, AVG Init Test Acc: {:.3f}"
print(template.format(avg_init_tloss, avg_init_tacc))
template = "AVG Final Test Loss: {:.3f}, AVG Final Test Acc: {:.3f}"
print(template.format(avg_final_tloss, avg_final_tacc))
print_flag = False
if iteration < 60:
print_flag = True
elif iteration%args.print_freq == 0:
print_flag = True
if print_flag:
print('--- PRINTING ALL CLIENTS STATUS ---')
current_acc = []
for k in range(args.num_users):
loss, acc = clients[k].eval_test()
current_acc.append(acc)
template = ("Client {:3d}, labels {}, count {}, best_acc {:3.3f}, current_acc {:3.3f} \n")
print(template.format(k, traindata_cls_counts[k], clients[k].get_count(),
clients_best_acc[k], current_acc[-1]))
template = ("Round {:1d}, Avg current_acc {:3.3f}, Avg best_acc {:3.3f}")
print(template.format(iteration+1, np.mean(current_acc), np.mean(clients_best_acc)))
ckp_avg_tacc.append(np.mean(current_acc))
ckp_avg_best_tacc.append(np.mean(clients_best_acc))
print('----- Analysis End of Round -------')
for idx in idxs_users:
print(f'Client {idx}, Count: {clients[idx].get_count()}, Labels: {traindata_cls_counts[idx]}')
print('')
print(f'Selected Clusters {selected_clusters}')
print('')
print(f'Clusters Glob Acc: {acc_glob_pc}')
loss_train.append(loss_avg)
init_tacc_pr.append(avg_init_tacc)
init_tloss_pr.append(avg_init_tloss)
final_tacc_pr.append(avg_final_tacc)
final_tloss_pr.append(avg_final_tloss)
#break;
## clear the placeholders for the next round
w_locals.clear()
loss_locals.clear()
init_local_tacc.clear()
init_local_tloss.clear()
final_local_tacc.clear()
final_local_tloss.clear()
## calling garbage collector
gc.collect()
############################### Saving Training Results
with open(path+str(args.trial)+'_loss_train.npy', 'wb') as fp:
loss_train = np.array(loss_train)
np.save(fp, loss_train)
with open(path+str(args.trial)+'_init_tacc_pr.npy', 'wb') as fp:
init_tacc_pr = np.array(init_tacc_pr)
np.save(fp, init_tacc_pr)
with open(path+str(args.trial)+'_init_tloss_pr.npy', 'wb') as fp:
init_tloss_pr = np.array(init_tloss_pr)
np.save(fp, init_tloss_pr)
with open(path+str(args.trial)+'_final_tacc_pr.npy', 'wb') as fp:
final_tacc_pr = np.array(final_tacc_pr)
np.save(fp, final_tacc_pr)
with open(path+str(args.trial)+'_final_tloss_pr.npy', 'wb') as fp:
final_tloss_pr = np.array(final_tloss_pr)
np.save(fp, final_tloss_pr)
# with open(path+str(args.trial)+'_best_glob_w.pt', 'wb') as fp:
# torch.save(best_glob_w, fp)
############################### Printing Final Test and Train ACC / LOSS
test_loss = []
test_acc = []
train_loss = []
train_acc = []
for idx in range(args.num_users):
loss, acc = clients[idx].eval_test()
test_loss.append(loss)
test_acc.append(acc)
loss, acc = clients[idx].eval_train()
train_loss.append(loss)
train_acc.append(acc)
test_loss = sum(test_loss) / len(test_loss)
test_acc = sum(test_acc) / len(test_acc)
train_loss = sum(train_loss) / len(train_loss)
train_acc = sum(train_acc) / len(train_acc)
print(f'Train Loss: {train_loss}, Test_loss: {test_loss}')
print(f'Train Acc: {train_acc}, Test Acc: {test_acc}')
print(f'Best Clients AVG Acc: {np.mean(clients_best_acc)}')
print(f'Best Global Model Acc: {best_glob_acc}')
############################# Saving Print Results
with open(path+str(args.trial)+'_final_results.txt', 'a') as text_file:
print(f'Train Loss: {train_loss}, Test_loss: {test_loss}', file=text_file)
print(f'Train Acc: {train_acc}, Test Acc: {test_acc}', file=text_file)
print(f'Best Clients AVG Acc: {np.mean(clients_best_acc)}', file=text_file)
print(f'Best Global Model Acc: {best_glob_acc}', file=text_file)
# print(f'Total_clusters: {total_clusters}, Avg clusters per round: {Avg_clusters_per_round}', file=text_file)