Advanced Flower Example (TensorFlow/Keras)#
This example demonstrates an advanced federated learning setup using Flower with TensorFlow/Keras. It differs from the quickstart example in the following ways:
10 clients (instead of just 2)
Each client holds a local dataset of 5000 training examples and 1000 test examples (note that by default only a small subset of this data is used when running the
Server-side model evaluation after parameter aggregation
Hyperparameter schedule using config functions
Custom return values
Server-side parameter initialization
Start by cloning the example project. We prepared a single-line command that you can copy into your shell which will checkout the example for you:
git clone --depth=1 https://github.com/adap/flower.git && mv flower/examples/advanced-tensorflow . && rm -rf flower && cd advanced-tensorflow
This will create a new directory called
advanced-tensorflow containing the following files:
-- pyproject.toml -- requirements.txt -- client.py -- server.py -- README.md -- run.sh
Project dependencies (such as
flwr) are defined in
requirements.txt. We recommend Poetry to install those dependencies and manage your virtual environment (Poetry installation) or pip, but feel free to use a different way of installing dependencies and managing virtual environments if you have other preferences.
poetry install poetry shell
Poetry will install all your dependencies in a newly created virtual environment. To verify that everything works correctly you can run the following command:
poetry run python3 -c "import flwr"
If you don’t see any errors you’re good to go!
Write the command below in your terminal to install the dependencies according to the configuration file requirements.txt.
pip install -r requirements.txt
Run Federated Learning with TensorFlow/Keras and Flower#
run.sh will call a script to generate certificates (which will be used by server and clients), start the Flower server (using
server.py), sleep for 2 seconds to ensure the the server is up, and then start 10 Flower clients (using
client.py). You can simply start everything in a terminal as follows:
poetry run ./run.sh
run.sh script starts processes in the background so that you don’t have to open eleven terminal windows. If you experiment with the code example and something goes wrong, simply using
CTRL + C on Linux (or
CMD + C on macOS) wouldn’t normally kill all these processes, which is why the script ends with
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT and
wait. This simply allows you to stop the experiment using
CTRL + C (or
CMD + C). If you change the script and anything goes wrong you can still use
killall python (or
killall python3) to kill all background processes (or a more specific command if you have other Python processes running that you don’t want to kill).
run.sh uses only a subset of the data. To use the full data, remove the
--toy argument or set it to False.
Important / Warning#
The approach used to generate SSL certificates can serve as an inspiration and starting point, but it should not be considered as viable for production environments. Please refer to other sources regarding the issue of correctly generating certificates for production environments.