forked from jihwan-m/fl-model-init
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
269 lines (215 loc) · 7.87 KB
/
app.py
File metadata and controls
269 lines (215 loc) · 7.87 KB
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
import flwr as fl
import tensorflow as tf
from tensorflow import keras
import os
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
import requests
from fastapi import FastAPI, BackgroundTasks
import asyncio
import uvicorn
from pydantic.main import BaseModel
import logging
import json
import boto3
from functools import partial
from flwr.client import NumPyClient
import time
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
app = FastAPI()
class FLclient_status(BaseModel):
FL_client_online: bool = True
FLCLstart: bool = False
FLCFail: bool = False
FL_server_IP: str = None # '10.152.183.181:8080'
status = FLclient_status()
def build_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile("adam", "sparse_categorical_crossentropy", metrics=["accuracy"])
return model
# Define Flower client
class CustomeClient:
def __init__(self, model, x_train, y_train, x_test, y_test):
self.model = model
self.x_train, self.y_train = x_train, y_train
self.x_test, self.y_test = x_test, y_test
#def get_properties(self, **kwargs):
# super().get_properties(self)
def get_parameters(self):
"""Get parameters of the local model."""
return self.model.get_weights()
# raise Exception("Not implemented (server-side parameter initialization)")
def fit(self, parameters, config):
"""Train parameters on the locally held training set."""
# Update local model parameters
self.model.set_weights(parameters)
# Get hyperparameters for this round
batch_size: int = config["batch_size"]
epochs: int = config["local_epochs"]
# Train the model using hyperparameters from config
history = self.model.fit(
self.x_train,
self.y_train,
batch_size,
epochs,
validation_split=0.1,
)
# Return updated model parameters and results
parameters_prime = self.model.get_weights()
num_examples_train = len(self.x_train)
results = {
"loss": history.history["loss"][0],
"accuracy": history.history["accuracy"][0],
"val_loss": history.history["val_loss"][0],
"val_accuracy": history.history["val_accuracy"][0],
}
return parameters_prime, num_examples_train, results
def evaluate(self, parameters, config):
"""Evaluate parameters on the locally held test set."""
# Update local model with global parameters
self.model.set_weights(parameters)
# Get config values
steps: int = config["val_steps"]
# Evaluate global model parameters on the local test data and return results
loss, accuracy = self.model.evaluate(self.x_test, self.y_test, 32, steps=steps)
num_examples_test = len(self.x_test)
return loss, num_examples_test, {"accuracy": accuracy}
# Load CIFAR-10 dataset
@app.on_event("startup")
def startup():
pass
# loop = asyncio.get_event_loop()
# loop.set_debug(True)
# loop.create_task(run_client())
@app.get("/start/{Server_IP}")
async def flclientstart(background_tasks: BackgroundTasks, Server_IP: str):
global status
global model
model = build_model()
print('start')
status.FLCLstart = True
status.FL_server_IP = Server_IP
background_tasks.add_task(run_client)
return status
@app.get('/test')
def get_model_test():
if status.FLCLstart == False:
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
print(model.evaluate(x_test, y_test))
@app.get('/online')
def get_info():
return status
async def run_client():
global model
try:
# time.sleep(10)
model.load_weights('/model/model.h5')
pass
except Exception as e:
print('[E][PC0001] learning', e)
status.FLCFail = True
await notify_fail()
status.FLCFail = False
await flower_client_start()
async def flower_client_start():
print('learning')
global status
global model
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
try:
loop = asyncio.get_event_loop()
client = CustomeClient(model, x_train, y_train, x_test, y_test)
# assert type(client).get_properties == fl.client.NumPyClient.get_properties
print(status.FL_server_IP)
#fl.client.start_numpy_client(server_address=status.FL_server_IP, client=client)
request = partial(fl.client.start_numpy_client, server_address=status.FL_server_IP, client=client)
await loop.run_in_executor(None, request)
await model_save()
del client
except Exception as e:
print('[E][PC0002] learning', e)
status.FLCFail = True
await notify_fail()
status.FLCFail = False
# raise e
async def model_save():
print('model_save')
global model
try:
model.save('/model/model.h5')
await notify_fin()
model=None
except Exception as e:
print('[E][PC0003] learning', e)
status.FLCFail = True
await notify_fail()
status.FLCFail = False
async def notify_fin():
global status
status.FLCLstart = False
loop = asyncio.get_event_loop()
future2 = loop.run_in_executor(None, requests.get, 'http://localhost:8080/trainFin')
r = await future2
print('try notify_fin')
if r.status_code == 200:
print('trainFin')
else:
print(r.content)
async def notify_fail():
global status
status.FLCLstart = False
loop = asyncio.get_event_loop()
future1 = loop.run_in_executor(None, requests.get, 'http://localhost:8080/trainFail')
r = await future1
print('try notify_fail')
if r.status_code == 200:
print('trainFin')
else:
print(r.content)
# Start Flower client
# s3에 model 없고 환경변수를 탐색하여 ENV가 init이라면 s3에 초기 가중치를 업로드 한다.
from botocore.exceptions import ClientError
def S3_check(s3_client, bucket, key): # 없으면 참
try:
s3_client.head_object(Bucket=bucket, Key=key)
except ClientError as e:
return int(e.response['Error']['Code']) != 404
return False
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
print(tf.version.VERSION)
# if os.environ.get('ENV', 'development') == 'init':
if os.environ.get('ENV') is not None:
res = requests.get('http://10.152.183.18:8000' + '/FLSe/info') # 서버측 manager
S3_info = res.json()['Server_Status']
model = build_model()
model.save(S3_info['S3_key'])
##########서버에 secret피일 이미 있음 #################
ACCESS_KEY_ID = os.environ.get('ACCESS_KEY_ID')
ACCESS_SECRET_KEY = os.environ.get('ACCESS_SECRET_KEY')
BUCKET_NAME = os.environ.get('BUCKET_NAME')
s3_client = boto3.client('s3', aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=ACCESS_SECRET_KEY)
if S3_check(s3_client, BUCKET_NAME, S3_info['S3_key']):
print('모델 없음')
response = s3_client.upload_file(
S3_info['S3_key'], BUCKET_NAME, S3_info['S3_key'])
else:
print('이미 모델 있음')
else:
try:
uvicorn.run("app:app", host='0.0.0.0', port=8002, reload=True)
finally:
requests.get('http://localhost:8080/flclient_out')
#