-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.py
More file actions
56 lines (43 loc) · 1.48 KB
/
client2.py
File metadata and controls
56 lines (43 loc) · 1.48 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
import flwr as fl
import sys
from helper import load_data, split_data, build_model
# Data loading
data_path = "data_2/"
data_yes = data_path + "yes"
data_no = data_path + "no"
IMG_WIDTH, IMG_HEIGHT = (240, 240)
X, y = load_data([data_yes, data_no], (IMG_WIDTH, IMG_HEIGHT))
# Data split
X_train, y_train, X_val, y_val, X_test, y_test = split_data(X, y, test_size=0.3)
# define image shape
IMG_SHAPE = (IMG_WIDTH, IMG_HEIGHT, 3)
# Build model
model = build_model(IMG_SHAPE)
# Compile the model
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
# Define Flower client
class FlowerClient(fl.client.NumPyClient):
def get_parameters(self):
return model.get_weights()
def fit(self, parameters, config):
model.set_weights(parameters)
r = model.fit(
x=X_train,
y=y_train,
batch_size=32,
epochs=5,
validation_data=(X_val, y_val),
)
# print("\nFit history : ", r.history, "\n")
return model.get_weights(), len(X_train), {}
def evaluate(self, parameters, config):
model.set_weights(parameters)
loss, accuracy = model.evaluate(x=X_test, y=y_test)
print("\n\n\nEval accuracy : ", accuracy, "\n\n\n")
return loss, len(X_test), {"accuracy": accuracy}
# Start Flower client
fl.client.start_numpy_client(
server_address="localhost:" + str(sys.argv[1]),
client=FlowerClient(),
grpc_max_message_length=1024 * 1024 * 1024,
)