This GitHub contains the code used to run the experiments presented in a scientific paper (to be published very soon) and a presentation given at Flower Summit 2023.
-
Creating an environment
conda create -n fl_env python=3.10 anaconda
-
Add the necessary libraries with pip
- If you want to install a specific version of Flower :
- You can use this command (example with version 1.4.0) : :
pip install flower==1.4.0
- You have to change
parameters.py
and__init__.py
in flower library from this folder : flower/src/py/flwr/common (see modification in this github : https://github.com/data-science-lover/flower.git)
- You can use this command (example with version 1.4.0) : :
pip install -r requirements.txt pip install pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 git clone https://github.com/data-science-lover/flower.git cd flower pip install .
- If you want to install a specific version of Flower :
-
Activate the environment
conda activate fl_env
-
Add a specific dataset
- If folder where put the datasets (for example called
data
) doesn't exist : create this folder and subfolder with the name of the specific dataset (examplecifar
):./data/cifar
- In data, you have all datasets and in a specific dataset you have train and test folder:
./data/cifar/train/
- If you have a specific dataset for the validation then addi the path to the folder with this command :
--data_path_val data/cifar/val
- To create train and test folder : run in python console the
create_files_train_test
function fromgoing_modular/common.py
. - Open
going/modular/data_setup.py
:- Add normalize values specified to the dataset in the
NORMALIZE_DICT
- If the dataset comes from the torchvision library, add a condition similar to that of CIFAR in
load_datasets
function
- Add normalize values specified to the dataset in the
- Open
going/modular/common.py
and add condition similar to the other datasets inclasses_string
- When you want to run an algorithme, add the validation path if you have a specific folder for this (None by default): --data_path_val ./data/histo/val
- If folder where put the datasets (for example called
python classic.py run --data_path data/ --dataset cifar --yaml_path ./results/classic/results.yml --seed 42 --num_workers -1 --max_epochs 5 --batch_size 32 --length 32 --split 10 --device mps --save_results results/classic/ --matrix_path confusion_matrix.png --roc_path roc.png --model_save cifar.pt
python simulation.py simulation --data_path data/ --dataset cifar --yaml_path ./results/FL/results.yml --seed 42 --num_workers -1 --max_epochs 5 --batch_size 32 --length 32 --split 10 --device mps --number_clients 10 --save_results results/FL/ --matrix_path confusion_matrix.png --roc_path roc.png --model_save cifar_fl.pt --min_fit_clients 10 --min_avail_clients 10 --min_eval_clients 10 --rounds 2 --frac_fit 1.0 --frac_eval 0.5
-
Run the central server
Open a terminal windows for the central server and run the client script client.py
python main_server.py server --data_path data/ --dataset cifar --seed 42 --num_workers 0 --max_epochs 5 --batch_size 32 --length 32 --split 10 --device mps --number_clients 3 --min_fit_clients 2 --min_avail_clients 2 --min_eval_clients 2 --rounds 2 --frac_fit 1.0 --frac_eval 0.5
-
Run each client
Open a terminal windows for each client and run the client script client.py (change value for the client ID)
python main_client.py client --data_path data/ --dataset cifar --seed 42 --num_workers 0 --max_epochs 5 --batch_size 32 --length 32 --split 10 --device mps --number_clients 3 --save_results results/FL/ --matrix_path confusion_matrix2.png --roc_path roc2.png --id_client 0
If you have the TenSEAL private/public key combination, you can use homomorphic encryption by adding the command --he
at client and server sites.
The private key must be on the client side and the public key on the server side, but the private key must be the same for all clients because all weights must be encrypted with the same private key.
Otherwise, you can create the combined private/public keys on a common entity (not the aggregation server) by running the create_keys.py script: create_keys.py
script :
python create_keys.py
Warning :
- you have to define the path for the crypted results :
--path_crypted server.pkl
(The crypted (and not crypted) weights are saved by default in "server.pkl" file) - server side :
- you must have the public key (by default in "server_key.pkl" file) :
--path_public_key server_key.pkl
- you must have the public key (by default in "server_key.pkl" file) :
- client side :
- You must have the combo private/public keys (by default in "secret.pkl" file):
--path_keys secret.pkl
- You must have the combo private/public keys (by default in "secret.pkl" file):
The federated learning framework used is https://github.com/adap/flower and the HE library used is https://github.com/OpenMined/TenSEAL, please refer to their documentation for more information.