Usage Examples

Flower comes with a number of usage examples. The examples demonstrate how Flower can be used to federate different kinds of existing machine learning pipelines, usually leveraging popular machine learning frameworks such as PyTorch or TensorFlow.

Note

Flower usage examples used to be bundled with Flower in a package called flwr_example. We are migrating those examples to standalone projects to make them easier to use. All new examples are based in the directory examples.

The following examples are available as standalone projects.

Quickstart TensorFlow/Keras

The TensorFlow/Keras quickstart example shows CIFAR-10 image classification with MobileNetV2:

Quickstart PyTorch

The PyTorch quickstart example shows CIFAR-10 image classification with a simple Convolutional Neural Network:

PyTorch: From Centralized To Federated

This example shows how a regular PyTorch project can be federated using Flower:

Federated Learning on Raspberry Pi and Nvidia Jetson

This example shows how Flower can be used to build a federated learning system that run across Raspberry Pi and Nvidia Jetson:

Legacy Examples (flwr_example)

Warning

The useage examples in flwr_example are deprecated and will be removed in the future. New examples are provided as standalone projects in examples.

Extra Dependencies

The core Flower framework keeps a minimal set of dependencies. The examples demonstrate Flower in the context of different machine learning frameworks, so additional dependencies need to be installed before an example can be run.

For PyTorch examples:

$ pip install flwr[examples-pytorch]

For TensorFlow examples:

$ pip install flwr[examples-tensorflow]

For both PyTorch and TensorFlow examples:

$ pip install flwr[examples-pytorch,examples-tensorflow]

Please consult pyproject.toml for a full list of possible extras (section [tool.poetry.extras]).

PyTorch Examples

Our PyTorch examples are based on PyTorch 1.7. They should work with other releases as well. So far, we provide the following examples.

CIFAR-10 Image Classification

CIFAR-10 and CIFAR-100 are popular RGB image datasets. The Flower CIFAR-10 example uses PyTorch to train a simple CNN classifier in a federated learning setup with two clients.

First, start a Flower server:

$ ./src/py/flwr_example/pytorch_cifar/run-server.sh

Then, start the two clients in a new terminal window:

$ ./src/py/flwr_example/pytorch_cifar/run-clients.sh

For more details, see src/py/flwr_example/pytorch_cifar.

ImageNet-2012 Image Classification

ImageNet-2012 is one of the major computer vision datasets. The Flower ImageNet example uses PyTorch to train a ResNet-18 classifier in a federated learning setup with ten clients.

First, start a Flower server:

$ ./src/py/flwr_example/pytorch_imagenet/run-server.sh

Then, start the two clients in a new terminal window:

$ ./src/py/flwr_example/pytorch_imagenet/run-clients.sh

For more details, see src/py/flwr_example/pytorch_imagenet.

TensorFlow Examples

Our TensorFlow examples are based on TensorFlow 2.0 or newer. So far, we provide the following examples.

Fashion-MNIST Image Classification

Fashion-MNIST is often used as the “Hello, world!” of machine learning. We follow this tradition and provide an example which samples random local datasets from Fashion-MNIST and trains a simple image classification model over those partitions.

First, start a Flower server:

$ ./src/py/flwr_example/tensorflow_fashion_mnist/run-server.sh

Then, start the two clients in a new terminal window:

$ ./src/py/flwr_example/tensorflow_fashion_mnist/run-clients.sh

For more details, see src/py/flwr_example/tensorflow_fashion_mnist.