-
Notifications
You must be signed in to change notification settings - Fork 1
/
client1.py
66 lines (57 loc) · 2 KB
/
client1.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
import flwr as fl
import tensorflow as tf
from tensorflow import keras
import sys
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# AUxillary methods
def getDist(y):
ax = sns.countplot(y)
ax.set(title="Count of data classes")
plt.show()
def getData(dist, x, y):
dx = []
dy = []
counts = [0 for i in range(10)]
for i in range(len(x)):
if counts[y[i]]<dist[y[i]]:
dx.append(x[i])
dy.append(y[i])
counts[y[i]] += 1
return np.array(dx), np.array(dy)
# Load and compile Keras model
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(256, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile("adam", "sparse_categorical_crossentropy", metrics=["accuracy"])
# Load dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train[..., np.newaxis]/255.0, x_test[..., np.newaxis]/255.0
dist = [4000, 4000, 4000, 3000, 10, 10, 10, 10, 4000, 10]
x_train, y_train = getData(dist, x_train, y_train)
#getDist(y_train)
# 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_train, y_train, epochs=1, validation_data=(x_test, y_test), verbose=0)
hist = r.history
print("Fit history : " ,hist)
return model.get_weights(), len(x_train), {}
def evaluate(self, parameters, config):
model.set_weights(parameters)
loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print("Eval accuracy : ", accuracy)
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
)