Flower Example using PyTorch#

View on GitHub

This introductory example to Flower uses PyTorch, but deep knowledge of PyTorch is not necessarily required to run the example. However, it will help you understand how to adapt Flower to your use case. Running this example in itself is quite easy. This example uses Flower Datasets to download, partition and preprocess the CIFAR-10 dataset.

Project Setup#

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/quickstart-pytorch . && rm -rf flower && cd quickstart-pytorch

This will create a new directory called quickstart-pytorch containing the following files:

-- pyproject.toml
-- client.py
-- server.py
-- README.md

Installing Dependencies#

Project dependencies (such as torch and flwr) are defined in pyproject.toml. You can install the dependencies by invoking pip:

# From a new python environment, run:
pip install .

Then, to verify that everything works correctly you can run the following command:

python3 -c "import flwr"

If you don’t see any errors you’re good to go!


Run Federated Learning with PyTorch and Flower#

Afterwards you are ready to start the Flower server as well as the clients. You can simply start the server in a terminal as follows:

python3 server.py

Now you are ready to start the Flower clients which will participate in the learning. We need to specify the partition id to use different partitions of the data on different nodes. To do so simply open two more terminal windows and run the following commands.

Start client 1 in the first terminal:

python3 client.py --partition-id 0

Start client 2 in the second terminal:

python3 client.py --partition-id 1

You will see that PyTorch is starting a federated training. Look at the code for a detailed explanation.


Run Federated Learning with PyTorch and Flower Next#

2. Start the long-running Flower clients (SuperNodes)#

Start 2 Flower SuperNodes in 2 separate terminal windows, using:

flower-client-app client:app --insecure

3. Run the Flower App#

With both the long-running server (SuperLink) and two clients (SuperNode) up and running, we can now run the actual Flower App:

flower-server-app server:app --insecure