forked from roxanneluo/Federated-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fl_server.py
332 lines (268 loc) · 12.9 KB
/
fl_server.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
import pickle
import keras
import uuid
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import msgpack
import random
import codecs
import numpy as np
import json
import msgpack_numpy
# https://github.com/lebedov/msgpack-numpy
import sys
import time
from flask import *
from flask_socketio import SocketIO
from flask_socketio import *
# https://flask-socketio.readthedocs.io/en/latest/
class GlobalModel(object):
"""docstring for GlobalModel"""
def __init__(self):
self.model = self.build_model()
self.current_weights = self.model.get_weights()
# for convergence check
self.prev_train_loss = None
# all rounds; losses[i] = [round#, timestamp, loss]
# round# could be None if not applicable
self.train_losses = []
self.valid_losses = []
self.train_accuracies = []
self.valid_accuracies = []
self.training_start_time = int(round(time.time()))
def build_model(self):
raise NotImplementedError()
# client_updates = [(w, n)..]
def update_weights(self, client_weights, client_sizes):
new_weights = [np.zeros(w.shape) for w in self.current_weights]
total_size = np.sum(client_sizes)
for c in range(len(client_weights)):
for i in range(len(new_weights)):
new_weights[i] += client_weights[c][i] * client_sizes[c] / total_size
self.current_weights = new_weights
def aggregate_loss_accuracy(self, client_losses, client_accuracies, client_sizes):
total_size = np.sum(client_sizes)
# weighted sum
aggr_loss = np.sum(client_losses[i] / total_size * client_sizes[i]
for i in range(len(client_sizes)))
aggr_accuraries = np.sum(client_accuracies[i] / total_size * client_sizes[i]
for i in range(len(client_sizes)))
return aggr_loss, aggr_accuraries
# cur_round coule be None
def aggregate_train_loss_accuracy(self, client_losses, client_accuracies, client_sizes, cur_round):
cur_time = int(round(time.time())) - self.training_start_time
aggr_loss, aggr_accuraries = self.aggregate_loss_accuracy(client_losses, client_accuracies, client_sizes)
self.train_losses += [[cur_round, cur_time, aggr_loss]]
self.train_accuracies += [[cur_round, cur_time, aggr_accuraries]]
with open('stats.txt', 'w') as outfile:
json.dump(self.get_stats(), outfile)
return aggr_loss, aggr_accuraries
# cur_round coule be None
def aggregate_valid_loss_accuracy(self, client_losses, client_accuracies, client_sizes, cur_round):
cur_time = int(round(time.time())) - self.training_start_time
aggr_loss, aggr_accuraries = self.aggregate_loss_accuracy(client_losses, client_accuracies, client_sizes)
self.valid_losses += [[cur_round, cur_time, aggr_loss]]
self.valid_accuracies += [[cur_round, cur_time, aggr_accuraries]]
with open('stats.txt', 'w') as outfile:
json.dump(self.get_stats(), outfile)
return aggr_loss, aggr_accuraries
def get_stats(self):
return {
"train_loss": self.train_losses,
"valid_loss": self.valid_losses,
"train_accuracy": self.train_accuracies,
"valid_accuracy": self.valid_accuracies
}
class GlobalModel_MNIST_CNN(GlobalModel):
def __init__(self):
super(GlobalModel_MNIST_CNN, self).__init__()
def build_model(self):
# ~5MB worth of parameters
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
######## Flask server with Socket IO ########
# Federated Averaging algorithm with the server pulling from clients
class FLServer(object):
MIN_NUM_WORKERS = 5
MAX_NUM_ROUNDS = 50
NUM_CLIENTS_CONTACTED_PER_ROUND = 5
ROUNDS_BETWEEN_VALIDATIONS = 2
def __init__(self, global_model, host, port):
self.global_model = global_model()
self.ready_client_sids = set()
self.app = Flask(__name__)
self.socketio = SocketIO(self.app)
self.host = host
self.port = port
self.model_id = str(uuid.uuid4())
#####
# training states
self.current_round = -1 # -1 for not yet started
self.current_round_client_updates = []
self.eval_client_updates = []
#####
# socket io messages
self.register_handles()
@self.app.route('/')
def dashboard():
return render_template('dashboard.html')
@self.app.route('/stats')
def status_page():
return json.dumps(self.global_model.get_stats())
def register_handles(self):
# single-threaded async, no need to lock
@self.socketio.on('connect')
def handle_connect():
print(request.sid, "connected")
@self.socketio.on('reconnect')
def handle_reconnect():
print(request.sid, "reconnected")
@self.socketio.on('disconnect')
def handle_reconnect():
print(request.sid, "disconnected")
if request.sid in self.ready_client_sids:
self.ready_client_sids.remove(request.sid)
@self.socketio.on('client_wake_up')
def handle_wake_up():
print("client wake_up: ", request.sid)
emit('init', {
'model_json': self.global_model.model.to_json(),
'model_id': self.model_id,
'min_train_size': 1200,
'data_split': (0.6, 0.3, 0.1), # train, test, valid
'epoch_per_round': 1,
'batch_size': 10
})
@self.socketio.on('client_ready')
def handle_client_ready(data):
print("client ready for training", request.sid, data)
self.ready_client_sids.add(request.sid)
if len(self.ready_client_sids) >= FLServer.MIN_NUM_WORKERS and self.current_round == -1:
self.train_next_round()
@self.socketio.on('client_update')
def handle_client_update(data):
print("received client update of bytes: ", sys.getsizeof(data))
print("handle client_update", request.sid)
for x in data:
if x != 'weights':
print(x, data[x])
# data:
# weights
# train_size
# valid_size
# train_loss
# train_accuracy
# valid_loss?
# valid_accuracy?
# discard outdated update
if data['round_number'] == self.current_round:
self.current_round_client_updates += [data]
self.current_round_client_updates[-1]['weights'] = pickle_string_to_obj(data['weights'])
# tolerate 30% unresponsive clients
if len(self.current_round_client_updates) > FLServer.NUM_CLIENTS_CONTACTED_PER_ROUND * .7:
self.global_model.update_weights(
[x['weights'] for x in self.current_round_client_updates],
[x['train_size'] for x in self.current_round_client_updates],
)
aggr_train_loss, aggr_train_accuracy = self.global_model.aggregate_train_loss_accuracy(
[x['train_loss'] for x in self.current_round_client_updates],
[x['train_accuracy'] for x in self.current_round_client_updates],
[x['train_size'] for x in self.current_round_client_updates],
self.current_round
)
print("aggr_train_loss", aggr_train_loss)
print("aggr_train_accuracy", aggr_train_accuracy)
if 'valid_loss' in self.current_round_client_updates[0]:
aggr_valid_loss, aggr_valid_accuracy = self.global_model.aggregate_valid_loss_accuracy(
[x['valid_loss'] for x in self.current_round_client_updates],
[x['valid_accuracy'] for x in self.current_round_client_updates],
[x['valid_size'] for x in self.current_round_client_updates],
self.current_round
)
print("aggr_valid_loss", aggr_valid_loss)
print("aggr_valid_accuracy", aggr_valid_accuracy)
if self.global_model.prev_train_loss is not None and \
(self.global_model.prev_train_loss - aggr_train_loss) / self.global_model.prev_train_loss < .01:
# converges
print("converges! starting test phase..")
self.stop_and_eval()
return
self.global_model.prev_train_loss = aggr_train_loss
if self.current_round >= FLServer.MAX_NUM_ROUNDS:
self.stop_and_eval()
else:
self.train_next_round()
@self.socketio.on('client_eval')
def handle_client_eval(data):
if self.eval_client_updates is None:
return
print("handle client_eval", request.sid)
print("eval_resp", data)
self.eval_client_updates += [data]
# tolerate 30% unresponsive clients
if len(self.eval_client_updates) > FLServer.NUM_CLIENTS_CONTACTED_PER_ROUND * .7:
aggr_test_loss, aggr_test_accuracy = self.global_model.aggregate_loss_accuracy(
[x['test_loss'] for x in self.eval_client_updates],
[x['test_accuracy'] for x in self.eval_client_updates],
[x['test_size'] for x in self.eval_client_updates],
);
print("\naggr_test_loss", aggr_test_loss)
print("aggr_test_accuracy", aggr_test_accuracy)
print("== done ==")
self.eval_client_updates = None # special value, forbid evaling again
# Note: we assume that during training the #workers will be >= MIN_NUM_WORKERS
def train_next_round(self):
self.current_round += 1
# buffers all client updates
self.current_round_client_updates = []
print("### Round ", self.current_round, "###")
client_sids_selected = random.sample(list(self.ready_client_sids), FLServer.NUM_CLIENTS_CONTACTED_PER_ROUND)
print("request updates from", client_sids_selected)
# by default each client cnn is in its own "room"
for rid in client_sids_selected:
emit('request_update', {
'model_id': self.model_id,
'round_number': self.current_round,
'current_weights': obj_to_pickle_string(self.global_model.current_weights),
'weights_format': 'pickle',
'run_validation': self.current_round % FLServer.ROUNDS_BETWEEN_VALIDATIONS == 0,
}, room=rid)
def stop_and_eval(self):
self.eval_client_updates = []
for rid in self.ready_client_sids:
emit('stop_and_eval', {
'model_id': self.model_id,
'current_weights': obj_to_pickle_string(self.global_model.current_weights),
'weights_format': 'pickle'
}, room=rid)
def start(self):
self.socketio.run(self.app, host=self.host, port=self.port)
def obj_to_pickle_string(x):
return codecs.encode(pickle.dumps(x), "base64").decode()
# return msgpack.packb(x, default=msgpack_numpy.encode)
# TODO: compare pickle vs msgpack vs json for serialization; tradeoff: computation vs network IO
def pickle_string_to_obj(s):
return pickle.loads(codecs.decode(s.encode(), "base64"))
# return msgpack.unpackb(s, object_hook=msgpack_numpy.decode)
if __name__ == '__main__':
# When the application is in debug mode the Werkzeug development server is still used
# and configured properly inside socketio.run(). In production mode the eventlet web server
# is used if available, else the gevent web server is used.
server = FLServer(GlobalModel_MNIST_CNN, "127.0.0.1", 5000)
print("listening on 127.0.0.1:5000");
server.start()