-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathfl_client.py
214 lines (170 loc) · 7.75 KB
/
fl_client.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
import numpy as np
import keras
import random
import time
import json
import pickle
import codecs
from keras.models import model_from_json
from socketIO_client import SocketIO, LoggingNamespace
from fl_server import obj_to_pickle_string, pickle_string_to_obj
import datasource
import threading
class LocalModel(object):
def __init__(self, model_config, data_collected):
# model_config:
# 'model': self.global_model.model.to_json(),
# 'model_id'
# 'min_train_size'
# 'data_split': (0.6, 0.3, 0.1), # train, test, valid
# 'epoch_per_round'
# 'batch_size'
self.model_config = model_config
self.model = model_from_json(model_config['model_json'])
# the weights will be initialized on first pull from server
self.model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
train_data, test_data, valid_data = data_collected
self.x_train = np.array([tup[0] for tup in train_data])
self.y_train = np.array([tup[1] for tup in train_data])
self.x_test = np.array([tup[0] for tup in test_data])
self.y_test = np.array([tup[1] for tup in test_data])
self.x_valid = np.array([tup[0] for tup in valid_data])
self.y_valid = np.array([tup[1] for tup in valid_data])
def get_weights(self):
return self.model.get_weights()
def set_weights(self, new_weights):
self.model.set_weights(new_weights)
# return final weights, train loss, train accuracy
def train_one_round(self):
self.model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
self.model.fit(self.x_train, self.y_train,
epochs=self.model_config['epoch_per_round'],
batch_size=self.model_config['batch_size'],
verbose=1,
validation_data=(self.x_valid, self.y_valid))
score = self.model.evaluate(self.x_train, self.y_train, verbose=0)
print('Train loss:', score[0])
print('Train accuracy:', score[1])
return self.model.get_weights(), score[0], score[1]
def validate(self):
score = self.model.evaluate(self.x_valid, self.y_valid, verbose=0)
print('Validate loss:', score[0])
print('Validate accuracy:', score[1])
return score
def evaluate(self):
score = self.model.evaluate(self.x_test, self.y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
return score
# A federated client is a process that can go to sleep / wake up intermittently
# it learns the global model by communication with the server;
# it contributes to the global model by sending its local gradients.
class FederatedClient(object):
MAX_DATASET_SIZE_KEPT = 1200
def __init__(self, server_host, server_port, datasource):
self.local_model = None
self.datasource = datasource()
self.sio = SocketIO(server_host, server_port, LoggingNamespace)
self.register_handles()
print("sent wakeup")
self.sio.emit('client_wake_up')
self.sio.wait()
########## Socket Event Handler ##########
def on_init(self, *args):
model_config = args[0]
print('on init', model_config)
print('preparing local data based on server model_config')
# ([(Xi, Yi)], [], []) = train, test, valid
fake_data, my_class_distr = self.datasource.fake_non_iid_data(
min_train=model_config['min_train_size'],
max_train=FederatedClient.MAX_DATASET_SIZE_KEPT,
data_split=model_config['data_split']
)
self.local_model = LocalModel(model_config, fake_data)
# ready to be dispatched for training
self.sio.emit('client_ready', {
'train_size': self.local_model.x_train.shape[0],
'class_distr': my_class_distr # for debugging, not needed in practice
})
def register_handles(self):
########## Socket IO messaging ##########
def on_connect():
print('connect')
def on_disconnect():
print('disconnect')
def on_reconnect():
print('reconnect')
def on_request_update(*args):
req = args[0]
# req:
# 'model_id'
# 'round_number'
# 'current_weights'
# 'weights_format'
# 'run_validation'
print("update requested")
if req['weights_format'] == 'pickle':
weights = pickle_string_to_obj(req['current_weights'])
self.local_model.set_weights(weights)
my_weights, train_loss, train_accuracy = self.local_model.train_one_round()
resp = {
'round_number': req['round_number'],
'weights': obj_to_pickle_string(my_weights),
'train_size': self.local_model.x_train.shape[0],
'valid_size': self.local_model.x_valid.shape[0],
'train_loss': train_loss,
'train_accuracy': train_accuracy,
}
if req['run_validation']:
valid_loss, valid_accuracy = self.local_model.validate()
resp['valid_loss'] = valid_loss
resp['valid_accuracy'] = valid_accuracy
self.sio.emit('client_update', resp)
def on_stop_and_eval(*args):
req = args[0]
if req['weights_format'] == 'pickle':
weights = pickle_string_to_obj(req['current_weights'])
self.local_model.set_weights(weights)
test_loss, test_accuracy = self.local_model.evaluate()
resp = {
'test_size': self.local_model.x_test.shape[0],
'test_loss': test_loss,
'test_accuracy': test_accuracy
}
self.sio.emit('client_eval', resp)
self.sio.on('connect', on_connect)
self.sio.on('disconnect', on_disconnect)
self.sio.on('reconnect', on_reconnect)
self.sio.on('init', lambda *args: self.on_init(*args))
self.sio.on('request_update', on_request_update)
self.sio.on('stop_and_eval', on_stop_and_eval)
# TODO: later: simulate datagen for long-running train-serve service
# i.e. the local dataset can increase while training
# self.lock = threading.Lock()
# def simulate_data_gen(self):
# num_items = random.randint(10, FederatedClient.MAX_DATASET_SIZE_KEPT * 2)
# for _ in range(num_items):
# with self.lock:
# # (X, Y)
# self.collected_data_train += [self.datasource.sample_single_non_iid()]
# # throw away older data if size > MAX_DATASET_SIZE_KEPT
# self.collected_data_train = self.collected_data_train[-FederatedClient.MAX_DATASET_SIZE_KEPT:]
# print(self.collected_data_train[-1][1])
# self.intermittently_sleep(p=.2, low=1, high=3)
# threading.Thread(target=simulate_data_gen, args=(self,)).start()
def intermittently_sleep(self, p=.1, low=10, high=100):
if (random.random() < p):
time.sleep(random.randint(low, high))
# possible: use a low-latency pubsub system for gradient update, and do "gossip"
# e.g. Google cloud pubsub, Amazon SNS
# https://developers.google.com/nearby/connections/overview
# https://pypi.python.org/pypi/pyp2p
# class PeerToPeerClient(FederatedClient):
# def __init__(self):
# super(PushBasedClient, self).__init__()
if __name__ == "__main__":
FederatedClient("127.0.0.1", 5000, datasource.Mnist)