This is an example for how to do differentially private federated machine learning with secure aggregation with the flower framework, using the dpsa4fl library to interact with aggregation servers as described by this internet draft.
The project itself is based on the pytorch quickstart example from flower (learning the CIFAR-10 dataset), but is adapted to use our dpsa4flower package.
Note: The dpsa project is a work in progress. This example project exists for testing purposes, the code should not be used in production.
Running this project consists of four steps:
This project requires the following:
- python version 3.9 or higher.
- poetry package manager for python
- docker
- docker-compose
Clone the dpsa4fl-testing-infrastructure
repository into a directory of your choice (here .
). Make sure to clone the submodules as well, e.g. using the --recursive
flag.
~> git clone --recursive https://github.com/dpsa4fl/dpsa4fl-infrastructure
Switch into that directory and into the local-docker-setup
subfolder.
~> cd dpsa4fl-testing-infrastructure/local-docker-setup
Build the required images with docker by using our build script.
~/dpsa4fl-testing-infrastructure/local-docker-setup> ./build.sh
Start the containers with docker-compose.
~/dpsa4fl-testing-infrastructure/local-docker-setup> docker-compose up -d
For instructions on how to run it on actual servers instead of local docker containers, see the corresponding readme of the infrastructore repo
Clone this repo into a directory of your choice (here again .
).
~> git clone https://github.com/dpsa4fl/dpsa4fl-example-project
Switch into this directory.
~> cd dpsa4fl-example-project
Enable a python virtual environment using poetry.
~/dpsa4fl-example-project> poetry shell
Install dependencies with poetry.
(dpsa4fl-example-project-py3.10) ~/dpsa4fl-example-project> poetry install
For this, it is best if you open individual shells for each instance of server and clients.
So in the current shell, start the flower server.
(dpsa4fl-example-project-py3.10) ~/dpsa4fl-example-project> python dpsa4fl_example_project/server.py
Also, do the following two times:
- Open new shell.
- Change into
~/dpsa4fl-example-project
directory (use your path). - Enable a virtual environment like above with
poetry shell
. - Run the flower client with
python dpsa4fl_example_project/client.py
.
When the server is running, and the two clients are connected, the learning procedure will start automatically. The server will let the clients train multiple times on the dataset, interspersed with evaluation rounds. The gradients of each training round are aggregated via the local janus server instance.
In the end, the accuracy of each evaluation round is printed. You will see output similar to the following.
INFO flower 2022-12-28 12:24:36,360 | server.py:144 | FL finished in 997.9534725520025
INFO flower 2022-12-28 12:24:36,360 | app.py:192 | app_fit: losses_distributed [(1, 2.058671712875366), (2, 1.5741422176361084), (3, 1.2739187479019165), (4, 1.1578938961029053), (5, 1.0813101530075073), (6, 1.0532516241073608), (7, 1.016814112663269)]
INFO flower 2022-12-28 12:24:36,360 | app.py:193 | app_fit: metrics_distributed {'accuracy': [(1, 0.3825), (2, 0.4958), (3, 0.5599), (4, 0.5891), (5, 0.6189), (6, 0.6333), (7, 0.6403)]}
After the seventh round, the accuracy is at 64%, which is comparable to the accuracy of the original flower example without secure aggregation.
This project is funded through the NGI Assure Fund, a fund established by NLnet with financial support from the European Commission's Next Generation Internet program. Learn more on the NLnet project page.