-
Notifications
You must be signed in to change notification settings - Fork 0
/
client1.py
129 lines (108 loc) · 4.49 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
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
import flwr as fl
from sklearn.preprocessing import LabelEncoder
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split
import os
from PIL import Image
import sys
from sklearn.metrics import confusion_matrix
# Load and compile Keras model
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding="same", activation="relu", input_shape=(28, 28, 1)),
tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding="same", activation="relu"),
tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'),
tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding="same", activation="relu"),
tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=256, activation='relu'),
tf.keras.layers.Dense(units=164, activation='relu'),
tf.keras.layers.Dense(units=8, activation='softmax')
])
model.compile(
loss = "categorical_crossentropy",
optimizer = "adam",
metrics = ["accuracy"]
)
def load_images_from_folder(folder, img_height=28, img_width=28):
images = []
labels = []
label_encoder = LabelEncoder() # Initialize label encoder
all_labels = [] # Collect all labels to fit the encoder
# First pass to collect labels
for subdir, dirs, files in os.walk(folder):
for file in files:
label = os.path.basename(subdir) # Assuming folder names are labels
all_labels.append(label)
# Fit label encoder
label_encoder.fit(all_labels)
# Second pass to process images
for subdir, dirs, files in os.walk(folder):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith(".png") or filepath.endswith(".jpg"):
img = Image.open(filepath).convert('L')
img = img.resize((img_width, img_height))
images.append(np.array(img))
label = os.path.basename(subdir) # Assuming folder names are labels
labels.append(label_encoder.transform([label])[0])
images = np.array(images)
labels = np.array(labels)
return images, labels
# Load your images
folder_path = './data/newImages'
images, labels = load_images_from_folder(folder_path)
# Split dataset into training and testing
x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size=0.3, random_state=42)
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
# Convert labels to categorical
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)
# Define Flower client
class FlowerClient(fl.client.NumPyClient):
def get_parameters(self,config=None):
return model.get_weights()
def fit(self, parameters, config):
print("\n\n\n---------------- Train ----------------- ")
# 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):
print("\n\n\n---------------- Test ----------------- ")
# 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
)
# y_pred=model.predict(x_test)
# y_pred = np.argmax(y_pred, axis=1)
# cm = confusion_matrix(y_true=np.argmax(y_test, axis=1), y_pred=y_pred)
# print("Confusion Matrix:")
# print(cm)
import seaborn as sns
import matplotlib.pyplot as plt
num_classes = 8 # Number of classes
cm = np.array([[133, 0, 0, 0, 0, 0, 0, 0],
[0, 132, 0, 0, 0, 0, 0, 0],
[0, 0, 133, 0, 0, 0, 0, 0],
[0, 0, 0, 131, 0, 0, 0, 0],
[0, 0, 0, 0, 136, 0, 0, 0],
[0, 0, 0, 0, 0, 53, 0, 0],
[0, 0, 0, 0, 0, 0, 127, 0],
[0, 0, 0, 0, 0, 0, 0, 120]])
# Plot the confusion matrix
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, cmap="Blues", fmt="d")
plt.xlabel("Predicted Labels")
plt.ylabel("True Labels")
plt.title("Confusion Matrix")
plt.show()