This guide walks through setting up a federated learning environment using Flower and PyTorch. It covers creating a virtual environment, generating federated datasets, and running the server and client instances.
To begin, create a virtual environment using Conda:
conda create -n fed-env python=3.11.9After that, activate the environment:
conda activate fed-envFinally, install the necessary dependencies:
pip install -r requirements.txtGenerate and save the federated MNIST dataset for three clients by running the following command:
First create a save directory
mkdir savepython create_fedatasets.py --root_dir=./data --save_dir=./saveThe output will indicate the progress of downloading and saving the datasets:
INFO:root:The downloading is completed
INFO:root:saving federated mnist for client 1 ...
INFO:root:saving federated mnist for client 2 ...
INFO:root:saving federated mnist for client 3 ...
INFO:root:saving test dataset ...
INFO:root:finishedOnce completed, the datasets will be stored as follows:
./save
├── mnist-client-1.pt
├── mnist-client-2.pt
├── mnist-client-3.pt
└── mnist-global-test-set.ptNote: The MNIST dataset will be downloaded the first time you run the script.
To initiate the federated learning process, start the server:
python server.py --data_dir="./save" --rounds=6Next, run the client instances. Start by running client 1:
python client.py --client_id=1 --data_dir=./saveThen, run client 2:
python client.py --client_id=2 --data_dir=./saveFinally, run client 3:
python client.py --client_id=3 --data_dir=./saveBy following these steps, you can successfully set up a federated learning environment using Flower and PyTorch. Each client trains on its local MNIST data, and the server coordinates the aggregation of their models.



