Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FedEator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sepia-pub
DELIGHT
FedEator
Commits
d6891be1
Commit
d6891be1
authored
7 months ago
by
Huong DO MAI
Browse files
Options
Downloads
Patches
Plain Diff
Add client script example (with partitioned, train, test, vali data divide)
parent
f9bcfb8d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Flower_v1/client_1.py
+85
-0
85 additions, 0 deletions
Flower_v1/client_1.py
with
85 additions
and
0 deletions
Flower_v1/client_1.py
0 → 100644
+
85
−
0
View file @
d6891be1
#python3 client_1.py <dataset> <id> <total_client> <IP:PORT>
#python3 client_1.py cifar10 1 3 172.16.66.55:8080
import
flwr
as
fl
import
tensorflow
as
tf
from
sklearn.model_selection
import
train_test_split
import
numpy
as
np
import
sys
# Function to load and partition the dataset based on client_id
def
load_partitioned_data
(
dataset_name
,
num_clients
,
client_id
):
if
dataset_name
==
"
cifar10
"
:
(
x_train
,
y_train
),
(
x_test
,
y_test
)
=
tf
.
keras
.
datasets
.
cifar10
.
load_data
()
x_train
=
x_train
.
astype
(
"
float32
"
)
/
255.0
x_test
=
x_test
.
astype
(
"
float32
"
)
/
255.0
num_classes
=
10
input_shape
=
(
32
,
32
,
3
)
elif
dataset_name
==
"
mnist
"
:
(
x_train
,
y_train
),
(
x_test
,
y_test
)
=
tf
.
keras
.
datasets
.
mnist
.
load_data
()
x_train
=
x_train
.
reshape
(
-
1
,
28
,
28
,
1
).
astype
(
"
float32
"
)
/
255.0
x_test
=
x_test
.
reshape
(
-
1
,
28
,
28
,
1
).
astype
(
"
float32
"
)
/
255.0
num_classes
=
10
input_shape
=
(
28
,
28
,
1
)
elif
dataset_name
==
"
cifar100
"
:
(
x_train
,
y_train
),
(
x_test
,
y_test
)
=
tf
.
keras
.
datasets
.
cifar100
.
load_data
()
x_train
=
x_train
.
astype
(
"
float32
"
)
/
255.0
x_test
=
x_test
.
astype
(
"
float32
"
)
/
255.0
num_classes
=
100
input_shape
=
(
32
,
32
,
3
)
else
:
raise
ValueError
(
"
Dataset not supported. Use
'
cifar10
'
,
'
mnist
'
, or
'
cifar100
'
.
"
)
# Partition the dataset among the clients
total_samples
=
x_train
.
shape
[
0
]
samples_per_client
=
total_samples
//
num_clients
start
=
client_id
*
samples_per_client
end
=
(
client_id
+
1
)
*
samples_per_client
if
client_id
!=
num_clients
-
1
else
total_samples
x_client_train
=
x_train
[
start
:
end
]
y_client_train
=
y_train
[
start
:
end
]
# Split the client-specific data into training and validation sets
x_train
,
x_val
,
y_train
,
y_val
=
train_test_split
(
x_client_train
,
y_client_train
,
test_size
=
0.2
,
random_state
=
42
)
return
(
x_train
,
y_train
),
(
x_val
,
y_val
),
(
x_test
,
y_test
),
num_classes
,
input_shape
# Get command-line arguments: dataset, client_id, num_clients, and server address
if
len
(
sys
.
argv
)
!=
5
:
print
(
"
Usage: python3 client.py <dataset> <partition> <client_num> <IP:PORT>
"
)
sys
.
exit
(
1
)
dataset_name
=
sys
.
argv
[
1
]
client_id
=
int
(
sys
.
argv
[
2
])
num_clients
=
int
(
sys
.
argv
[
3
])
server_address
=
sys
.
argv
[
4
]
# Load partitioned data for the client
(
x_train
,
y_train
),
(
x_val
,
y_val
),
(
x_test
,
y_test
),
num_classes
,
input_shape
=
load_partitioned_data
(
dataset_name
,
num_clients
,
client_id
)
# Define the model (MobileNetV2)
model
=
tf
.
keras
.
applications
.
MobileNetV2
(
input_shape
=
input_shape
,
classes
=
num_classes
,
weights
=
None
)
model
.
compile
(
optimizer
=
"
adam
"
,
loss
=
"
sparse_categorical_crossentropy
"
,
metrics
=
[
"
accuracy
"
])
# Define Flower client
class
FederatedClient
(
fl
.
client
.
NumPyClient
):
def
get_parameters
(
self
,
config
):
return
model
.
get_weights
()
def
fit
(
self
,
parameters
,
config
):
model
.
set_weights
(
parameters
)
model
.
fit
(
x_train
,
y_train
,
epochs
=
1
,
batch_size
=
32
,
validation_data
=
(
x_val
,
y_val
))
return
model
.
get_weights
(),
len
(
x_train
),
{}
def
evaluate
(
self
,
parameters
,
config
):
model
.
set_weights
(
parameters
)
loss
,
accuracy
=
model
.
evaluate
(
x_test
,
y_test
)
return
loss
,
len
(
x_test
),
{
"
accuracy
"
:
float
(
accuracy
)}
# Start Flower client
if
__name__
==
"
__main__
"
:
fl
.
client
.
start_client
(
server_address
=
server_address
,
client
=
FederatedClient
(),
max_retries
=
5
,
max_wait_time
=
10
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment