The Dev Insights
The Dev Insights
Federated Learning on Edge with Flower
Back to Blog
Data ScienceOctober 12, 20259 min read

Federated Learning on Edge with Flower

Data ScienceOctober 12, 20259 min read

Explore federated learning's rise in edge computing, driven by privacy needs, and discover how Flower simplifies decentralized model training across diverse...

Federated Learning on Edge with Flower

Alright, so you know how everyone's talking about machine learning? Ever thought about training those fancy models without having to shove all your precious data onto some big central server? That's where federated learning comes in, and honestly, it's a game-changer, especially for edge devices. I mean, think about all the data your phone coughs up, or your smartwatch, or even those sensors in a factory. Wouldn't it be awesome to use all that juicy data to make better models without giving up all your privacy?

That's the promise of federated learning. And I've found a framework called Flower that makes it surprisingly easy to get your hands dirty with it. So, let's jump in!

What is Federated Learning?

Okay, picture this: you want to train a model that can spot different breeds of dogs. The old-school way would be to grab images from everyone, upload them to a central server, and then train the model there. But what if people are worried about sharing their photos? Or what if their internet is rubbish?

Federated learning offers a pretty neat workaround. Instead of sending the data to the server, we actually send the model to the devices. Each device trains the model using its own local data, then sends back the updated model parameters (think of them as the "learnings") to the server. The server then mushes all those updates together to create a better, more general model. We repeat this a few times, and boom!

Key benefits of federated learning:

* Privacy: Data stays put on the device, which cuts down on the risk of data leaks and privacy headaches. This is super important in places like healthcare and finance, where they're really strict about this stuff.

* Reduced Bandwidth: We only send the model updates, which are way smaller than the raw data. This saves bandwidth and stops the network from getting clogged up.

* Improved Latency: Edge devices can make predictions right then and there, without needing to chat with a central server. This is crucial for things like real-time applications.

* Personalisation: Models can be tweaked for specific users or devices, which can lead to better performance.

Why Federated Learning on Edge?

Edge computing is all about bringing the computation closer to where the data is, which is perfect for federated learning. Edge devices, like smartphones, IoT sensors, and those industrial controllers, churn out tons of data. Training models right on these devices means we can use that data while keeping latency and bandwidth usage down. Plus, let's be real, a lot of edge devices don't always have a reliable internet connection, so local training is essential.

Specific benefits for edge devices:

* Low Latency: Absolutely critical for things like self-driving cars, where you need real-time decisions.

* Bandwidth Efficiency: Reduces the load on the network, which is vital in areas where the internet is spotty.

* Scalability: It's easy to scale to a huge number of devices.

* Data Sovereignty: Keeps data within a specific area or legal jurisdiction, which is important for sticking to the rules.

Introducing Flower: A Friendly Federated Learning Framework

Flower is a framework that's designed to make federated learning more accessible and easier to use. It plays nice with a bunch of machine learning frameworks (like TensorFlow, PyTorch, and scikit-learn) and different kinds of hardware. It's built to be flexible and work in different federated learning situations, from simple tests to real-world deployments on edge devices.

Why Flower?

* Framework Agnostic: It works with whatever machine learning framework you're already using.

* Platform Independent: Runs on all sorts of platforms, including Android, iOS, and embedded systems.

* Simple API: Easy to pick up and use, even if you're just starting out.

* Extensible: You can tweak it to fit your specific needs.

A Simple Flower Tutorial: Training MNIST

Let's walk through a basic example of training a model on the MNIST dataset (you know, the one with handwritten digits) using Flower and TensorFlow.

1. Setting up the Environment

First, you'll need to install Flower and TensorFlow. I recommend using a virtual environment to keep things nice and tidy:

bash
python3 -m venv .venv
source .venv/bin/activate # On Linux/macOS
.venv\Scripts\activate # On Windows
pip install flwr tensorflow

2. Defining the Client

The client is the piece of code that lives on each edge device. It's in charge of loading the data, training the model, and sending the updates back to the server.

Here's a simple client using TensorFlow:

python
import flwr as fl
import tensorflow as tf
import numpy as np

# Define the model
def create_model():
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    return model

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Preprocess the data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Define the Flower client
class MNISTClient(fl.client.NumPyClient):
    def get_parameters(self, config):
        return model.get_weights()

    def fit(self, parameters, config):
        model.set_weights(parameters)
        model.fit(x_train, y_train, epochs=1, batch_size=32, verbose=0)
        return model.get_weights(), len(x_train), {}

    def evaluate(self, parameters, config):
        model.set_weights(parameters)
        loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
        return loss, len(x_test), {"accuracy": accuracy}

# Start the Flower client
if __name__ == "__main__":
    model = create_model()
    fl.client.start_numpy_client(server_address="127.0.0.1:8080", client=MNISTClient())

Explanation:

* create_model(): Sets up a simple neural network model using TensorFlow/Keras.

* MNISTClient: This class inherits from fl.client.NumPyClient and handles the core federated learning stuff.

* get_parameters(): Returns the current model weights.

* fit(): Trains the model on the local data and returns the updated weights.

* evaluate(): Checks how well the model does on the local data and returns the loss and accuracy.

* fl.client.start_numpy_client(): Connects the client to the Flower server.

3. Defining the Server

The server's job is to coordinate the training process. It picks clients, sends them the model, combines the updates, and updates the main model.

Here's a basic server:

python
import flwr as fl

# Define the strategy
strategy = fl.server.strategy.FedAvg(min_available_clients=2)

# Start the Flower server
fl.server.start_server(server_address="0.0.0.0:8080", config=fl.server.ServerConfig(num_rounds=3), strategy=strategy)

Explanation:

* fl.server.strategy.FedAvg(): Uses the federated averaging strategy, which is a common way to combine model updates. min_available_clients=2 means the server waits for at least two clients to be ready before starting a round.

* fl.server.start_server(): Starts the Flower server, and tells it the address, how many rounds to do, and which strategy to use.

4. Running the Experiment

  • Save the client code to a file called client.py and the server code to server.py.
  • Open two separate terminal windows.
  • In the first terminal, run the server:
  • bash
    python server.py

  • In the second terminal (or multiple terminals, if you want to pretend you have multiple clients), run the client:
  • bash
    python client.py

    You should see output from both the server and the client, which means the federated learning process is running. The server will manage the training over several rounds, and the client will train the model on its data and send the updates back.

    Important Considerations:

    * Data Partitioning: In this example, we're using the whole MNIST dataset on each client. In the real world, you'd want to split the data across different clients to make it more realistic. This is really useful because it shows the privacy and decentralisation benefits.

    * Non-IID Data: Federated learning often deals with data that isn't independent and identically distributed (non-IID), which means the data is all over the place. This can make training harder, and you might need to use more complex ways to combine the updates to deal with it. This confused me at first!

    * Client Selection: The server can pick and choose which clients get to play in each round of training. This can be useful for managing resources and dealing with clients that don't have much power.

    * Fault Tolerance: Federated learning systems should be able to handle problems, like clients disconnecting or crashing. Flower has ways to deal with client failures and make sure the training keeps going smoothly.

    Beyond the Basics: More Flower Features

    Flower can do a lot more than just the basic federated averaging we've seen. Here are some other things it can do:

    * Different Aggregation Strategies: Flower has other ways to combine updates besides FedAvg, like FedProx, FedAdam, and FedYogi. These can be better at dealing with non-IID data and can help the model learn faster.

    * Custom Metrics: You can define your own metrics to keep an eye on how well the model is doing during training. This lets you watch the learning process and spot any problems.

    * Secure Aggregation: Flower supports ways to combine updates securely, so the server doesn't see the individual updates from each client. This adds another layer of privacy.

    * Simulation: Flower has a simulation mode that lets you test your federated learning setup before you put it on real devices. This is a great way to debug your code and try out different settings. I struggled with this at first.

    * Hardware Acceleration: Flower can use hardware acceleration on edge devices, like GPUs and TPUs, to make the training process faster.

    Real-World Applications of Federated Learning on Edge

    Federated learning on edge is being used for all sorts of things:

    * Healthcare: Training models to spot diseases from medical images without sharing patient data.

    * Finance: Spotting dodgy transactions while keeping customer privacy.

    * Autonomous Vehicles: Making self-driving cars better by learning from data collected by different cars.

    * Industrial IoT: Making manufacturing better by looking at data from sensors on factory floors.

    * Personalised Recommendations: Giving users personalised recommendations based on their local data.

    Conclusion

    Federated learning is a powerful way to train machine learning models on data that's spread out, while keeping privacy in mind. Flower makes it easier than ever to get started, offering a flexible framework for all sorts of applications. As privacy worries keep growing and edge devices keep churning out more and more data, federated learning will become more and more important for building smart and responsible AI systems. So, why not give Flower a shot and see what decentralized model training can do? You might be surprised how simple it is to get going!

    Keywords: federated learning edge, flower federated learning, decentralized model training, edge AI privacy, flower framework tutorial

    TOPICS

    Data Science

    Share This Article

    Related Articles

    Archives

    October 202546
    B

    BigBerri Team

    Author