-
Notifications
You must be signed in to change notification settings - Fork 0
/
fedprox.py
297 lines (245 loc) · 10.3 KB
/
fedprox.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
import yaml
from yaml.loader import SafeLoader
import torch
import torchvision
import pandas as pd
import numpy as np
import os
from PIL import Image, ImageDraw
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import pickle
import random
import time,json
import copy,sys
from collections import OrderedDict
from sklearn.preprocessing import label_binarize
from sklearn.metrics import classification_report,auc,roc_curve,precision_recall_fscore_support
from inference import evaluate_single_server
from data_model_loading import load_dataset, load_model as return_model, load_dataset_test
from ampreg import AMP
import warnings
warnings.filterwarnings("ignore")
def yaml_loader(yaml_file):
with open(yaml_file,'r') as f:
config_data = yaml.load(f,Loader=SafeLoader)
args = sys.argv
if '-s' in args:
config_data['SEED'] = int(args[args.index('-s') + 1])
if '-e' in args:
config_data['total_iterations'] = int(args[args.index('-e') + 1])
return config_data
def progress(current,total):
progress_percent = (current * 50 / total)
progress_percent_int = int(progress_percent)
print(f" |{chr(9608)* progress_percent_int}{' '*(50-progress_percent_int)}|{current}/{total}",end='\r')
if (current == total):
print()
class Client():
def __init__(self, mdl_name, train_loader, distribution, config):
self.lr = config['lr']
self.nc = config['nclass']
self.mom = config['momentum']
self.adv = config['adv']
self.return_logs = config['return_logs']
self.distri = distribution
self.train_loader = train_loader
self.mdl = return_model(mdl_name, self.nc)
self.lossfn = nn.CrossEntropyLoss()
self.serv_mdl = return_model(mdl_name, self.nc)
self.cpu = torch.device('cpu')
self.alpha = config['alpha']
if self.adv:
self.epsilon = config['epsilon']
self.inner_lr = config['inner_lr']
self.inner_iter = config['inner_iter']
self.train_client = self._train_client_adv
else:
self.train_client = self._train_client
def _train_client(self, transformations, n_epochs, device):
self.mdl = self.mdl.to(device)
self.serv_mdl = self.serv_mdl.to(device)
self.opt = optim.SGD(self.mdl.parameters(), lr=self.lr, momentum=self.mom)
tval = {'trainacc':[],"trainloss":[]}
self.mdl.train()
len_train = len(self.train_loader)
for epochs in range(n_epochs):
cur_loss = 0
curacc = 0
for idx , (data,target) in enumerate(self.train_loader):
if data.shape[0] == 1:
continue
data = transformations(data)
data = data.to(device)
target = target.to(device)
scores = self.mdl(data)
loss = self.lossfn(scores,target) + self.prox_reg()
cur_loss += loss.item() / (len_train)
scores = F.softmax(scores,dim = 1)
_,predicted = torch.max(scores,dim = 1)
correct = (predicted == target).sum()
samples = scores.shape[0]
curacc += correct / (samples * len_train)
self.opt.zero_grad()
loss.backward()
self.opt.step()
if self.return_logs:
progress(idx+1,len_train)
tval['trainacc'].append(float(curacc))
tval['trainloss'].append(float(cur_loss))
print(f"epochs: [{epochs+1}/{n_epochs}] train_acc: {curacc:.3f} train_loss: {cur_loss:.3f}")
self.mdl = copy.deepcopy(self.mdl)
return tval
def _train_client_adv(self, transformations, n_epochs, device):
self.mdl = self.mdl.to(device)
self.serv_mdl = self.serv_mdl.to(device)
self.opt = AMP(params=filter(lambda p: p.requires_grad, self.mdl.parameters()),
lr=self.lr,
epsilon=self.epsilon,
inner_lr=self.inner_lr,
inner_iter=self.inner_iter,
base_optimizer=torch.optim.SGD,
momentum=self.mom,
weight_decay=0.0,
nesterov=True)
tval = {'trainacc':[],"trainloss":[]}
self.mdl.train()
len_train = len(self.train_loader)
for epochs in range(n_epochs):
cur_loss = 0
curacc = 0
for idx , (data,target) in enumerate(self.train_loader):
if data.shape[0] == 1:
continue
data = transformations(data)
data = data.to(device)
target = target.to(device)
def closure():
self.opt.zero_grad()
scores = self.mdl(data)
loss = self.lossfn(scores, target) + self.prox_reg()
loss.backward()
return scores, loss
scores, loss = self.opt.step(closure)
cur_loss += loss.item() / (len_train)
scores = F.softmax(scores,dim = 1)
_,predicted = torch.max(scores,dim = 1)
correct = (predicted == target).sum()
samples = scores.shape[0]
curacc += correct / (samples * len_train)
if self.return_logs:
progress(idx+1,len_train)
tval['trainacc'].append(float(curacc))
tval['trainloss'].append(float(cur_loss))
print(f"epochs: [{epochs+1}/{n_epochs}] train_acc: {curacc:.3f} train_loss: {cur_loss:.3f}")
self.mdl = copy.deepcopy(self.mdl)
return tval
def replace_mdl(self, server_mdl):
self.mdl = copy.deepcopy(server_mdl)
self.serv_mdl = copy.deepcopy(server_mdl)
@torch.no_grad()
def prox_reg(self):
params1 = dict(self.mdl.named_parameters())
params2 = dict(self.serv_mdl.named_parameters())
loss_val = 0
for name, params in params1.items():
norm_val = torch.norm(params1[name] - params2[name]) ** 2
loss_val += self.alpha * 0.5 * norm_val
return loss_val
class Server():
def __init__(self, config, device):
self.nc = config['nclass']
self.mdl = return_model(config['model'], self.nc)
self.device = device
@torch.no_grad()
def aggregate_models(self, clients_model):
update_state = OrderedDict()
n_clients = len(clients_model)
for k, client in enumerate(clients_model):
local_state = client.state_dict()
for key in self.mdl.state_dict().keys():
if k == 0:
update_state[key] = local_state[key] / n_clients
else:
update_state[key] += local_state[key] / n_clients
print(self.mdl.load_state_dict(update_state))
class FedProx():
def __init__(self, clients_data, distri, test_data, config):
model_name = config['model']
self.device = torch.device(f'cuda:{config["gpu"]}' if torch.cuda.is_available() else 'cpu')
self.config = config
self.nclients = config['n_clients']
self.clientiter = config['client_iterations']
self.totaliter = config['total_iterations']
self.test_data = test_data
self.sample_cli = int(config['sample_clients'] * self.nclients)
self.clients = []
for i in range(self.nclients):
cur_client = Client(
model_name,
clients_data[i],
distri[i],
config
)
self.clients.append(cur_client)
self.server = Server(config, self.device)
def train(self, transformations):
start_time = time.perf_counter()
for idx in range(self.totaliter):
print(f"iteration [{idx+1}/{self.totaliter}]")
clients_selected = random.sample([i for i in range(self.nclients)], self.sample_cli)
distribution = [self.clients[i].distri for i in clients_selected]
for jdx in clients_selected:
print(f"############## client {jdx} ##############")
self.clients[jdx].train_client(
transformations,
self.clientiter,
self.device,
)
print("############## server ##############")
self.server.aggregate_models(
[self.clients[i].mdl for i in clients_selected]
)
single_acc = evaluate_single_server(
self.config,
self.server.mdl,
self.test_data,
transformations,
self.device
)
for pdx in clients_selected:
self.clients[pdx].replace_mdl(self.server.mdl)
print(f'cur_acc: {single_acc.item():.3f}')
end_time = time.perf_counter()
elapsed_time = int(end_time - start_time)
hr = elapsed_time // 3600
mi = (elapsed_time - hr * 3600) // 60
print(f"training done in {hr} H {mi} M")
if __name__ == "__main__":
config = yaml_loader(sys.argv[1])
random.seed(config["SEED"])
np.random.seed(config["SEED"])
torch.manual_seed(config["SEED"])
torch.cuda.manual_seed(config["SEED"])
torch.backends.cudnn.benchmarks = True
torch.backends.cudnn.deterministic = True
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
print("environment: ")
print(f"YAML: {sys.argv[1]}")
for key, value in config.items():
print(f"==> {key}: {value}")
client_data, distri = load_dataset(config)
test_data = load_dataset_test(config)
transformations = transforms.Compose([])
fedavg = FedProx(
client_data,
distri,
test_data,
config
)
fedavg.train(transformations)