-
Notifications
You must be signed in to change notification settings - Fork 3
/
fedprox_trainer.py
148 lines (125 loc) · 6.79 KB
/
fedprox_trainer.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
import numpy as np
import matplotlib.pyplot as plt
from algorithm.fedprox.client import Client
import torch
import os
from models import create_model
import copy
import time, datetime
class FedProxTrainer(object):
def __init__(self, dataset, opt_train, opt_test, log=None, gan=None):
self.training_setup_seed(0)
[train_data_num, test_data_num, train_data_global, val_data_global, test_data_global,
train_data_local_num_dict, train_data_local_dict, val_data_local_dict] = dataset
self.test_global = test_data_global
self.val_data_local_dict = val_data_local_dict
self.model = create_model(opt_train)
self.train_data_local_num_dict = train_data_local_num_dict
self.train_data_local_dict = train_data_local_dict
self.client_list = []
self.opt_train = opt_train
self.opt_test = opt_test
self.gan = gan
self.client_mse = [[] for i in range(self.opt_train.client_num_in_total)]
self.log = log
self.setup_clients()
def setup_clients(self):
self.log.logger.info("############setup_clients (START)#############")
self.client = Client(0, None, None, None, self.opt_train, self.model, self.log)
def client_sampling(self, round_idx, client_num_in_total, client_num_per_round):
if client_num_in_total == client_num_per_round:
client_indexes = [client_index for client_index in range(client_num_in_total)]
else:
num_clients = min(client_num_per_round, client_num_in_total)
client_indexes = range(client_num_per_round)
self.log.logger.info("client_indexes = %s" % str(client_indexes))
return client_indexes
def train_cross_validation(self):
w_global_init = copy.deepcopy(self.model.state_dict())
save_path = 'model_fedprox'
timestamp = time.time()
dt_object = datetime.datetime.fromtimestamp(timestamp)
formatted_string = dt_object.strftime('%m%d_%H%M%S')
save_path = save_path + '_' + formatted_string
print("Folder name of result:", save_path)
for fold_idx in range(self.opt_train.folds):
if fold_idx > 0:
break
min_loss = 99999
loss_train = []
loss_test = []
w_global = w_global_init
self.log.logger.info(
"####################################FOLDS:" + str(fold_idx) + " : {}".format(fold_idx))
for round_idx in range(self.opt_train.comm_round):
self.log.logger.info("################Communication round : {}".format(round_idx))
w_locals, loss_locals, loss_locals_t = [], [], []
client_indexes = self.client_sampling(round_idx, self.opt_train.client_num_in_total,
self.opt_train.client_num_per_round)
self.log.logger.info("client_indexes = " + str(client_indexes))
for idx in client_indexes:
torch.cuda.empty_cache()
# update dataset
self.client.update_local_dataset(idx, self.train_data_local_dict[fold_idx][idx],
self.val_data_local_dict[fold_idx][idx],
self.train_data_local_num_dict[fold_idx][idx])
self.client.update_state_dict(w_global)
# train on new dataset
if self.opt_train.model == 'unet':
w, loss, loss_t = self.client.train(w_global, round_idx)
else:
raise Exception(f'FedProx not support model named {self.opt_train.model}')
w_locals.append((self.client.get_sample_number(), copy.deepcopy(w)))
loss_locals.append(loss)
loss_locals_t.append(loss_t)
self.log.logger.info('Client {:3d}, loss {:.3f}, test loss{:.3f}'.format(idx, loss, loss_t))
# update global weights
w_global = self.aggregate(w_locals)
# print loss
loss_avg = sum(loss_locals) / len(loss_locals)
loss_train.append(loss_avg)
loss_avg_t = sum(loss_locals_t) / len(loss_locals_t)
loss_test.append(loss_avg_t)
self.log.logger.info(
'Round {:3d}, Average loss {:.3f}, Average test loss {:.3f}'.format(round_idx, loss_avg,
loss_avg_t))
if loss_avg_t < min_loss:
if not os.path.exists(self.opt_train.dataroot + '/' + save_path + '/'):
os.mkdir(self.opt_train.dataroot + '/' + save_path + '/')
torch.save(w_global, self.opt_train.dataroot + '/' + save_path + '/model'
+ str(round_idx) + '_folds' + str(fold_idx) + '_best.pkl')
min_loss = loss_avg_t
torch.save(w_global,
self.opt_train.dataroot + '/' + save_path + '/model' + str(round_idx) + '_folds' + str(
fold_idx) + '.pkl')
# Update training curve for each round
plt.figure()
plt.plot(np.linspace(1, len(loss_train), len(loss_train)).astype(np.int), loss_train)
plt.plot(np.linspace(1, len(loss_test), len(loss_test)).astype(np.int), loss_test)
plt.legend(['train', 'test'])
plt.savefig(self.opt_train.dataroot + '/' + save_path + '/a_temp_loss_' + str(fold_idx) + '.png')
plt.figure()
plt.plot(np.linspace(1, len(loss_train), len(loss_train)).astype(np.int), loss_train)
plt.plot(np.linspace(1, len(loss_test), len(loss_test)).astype(np.int), loss_test)
plt.legend(['train', 'test'])
plt.savefig(self.opt_train.dataroot + '/' + save_path + '/loss_' + str(fold_idx) + '.png')
def aggregate(self, w_locals):
averaged_params = copy.deepcopy(w_locals[0][1])
training_num = 0
for idx in range(len(w_locals)):
(sample_num, _) = w_locals[idx]
training_num += sample_num
for k in averaged_params.keys():
for i in range(0, len(w_locals)):
local_model_params = w_locals[i][1][k]
w = w_locals[i][0] / training_num
if i == 0:
averaged_params[k] = w * local_model_params
else:
averaged_params[k] += w * local_model_params
return averaged_params
def training_setup_seed(self, seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True