This repository contains code for implementing federated learning on embedded devices using Flower, a federated learning framework. The setup involves 6 Raspberry Pis acting as clients and a Linux VM serving as the central server running on a laptop connected via WIFI TCP/IP.
- Raspberry Pis (At least 2 - any number of clients as per your requirement.)
- Ubuntu Linux VM (for the server)
- Python 3. x
- Flower framework
Any device or laptop can be configured to be a server. Still, I used the Oracle VM Virtual box to create an Ubuntu Virtual Machine as Flower offers better compatibility and support.
Follow this link to set up an Ubuntu VM: https://ubuntu.com/tutorials/how-to-run-ubuntu-desktop-on-a-virtual-machine-using-virtualbox#1-overview
After creating the VM, open or free a port on which the server can be hosted. In my case, I chose 49999.
Set up the Raspberry Pis with the Ubuntu Server of your preferred version using Raspberry Pi Imager.
Follow this tutorial link for setup: https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#1-overview
I used the Ubuntu Server 22.04.3 LTS(64bit) version for all my clients.
Just so you know, avoid using Ubuntu Desktop on Raspberry Pi as it occupies more storage and might lead to memory issues for Federated Learning.
- Set up a Python Virtual environment in the server and the clients.
Follow this tutorial to set up a virtual environment for your project: https://www.freecodecamp.org/news/how-to-setup-virtual-environments-in-python/
- Clone the repository in the project folder where you have the Virtual Environment for both server and Raspberry Pi devices.
git clone https://github.com/BoTampere/Federated-Learning-Experiments.git
cd federated-learning-embedded
- Install required Python packages in the server and clients:
pip install -r requirements.txt
Note: Install pip and other general software necessary on the Raspberry Pi clients if not pre-installed with Ubuntu OS.
To run the server on the Ubuntu VM:
Modify the server.py code to your required strategy and configure the IP address where you would like to host the server and run the code using below command.
python server.py
Based on your requirement/experiment, run the client.py or differentialprivacy_client.py on each Raspberry Pi:
python client.py
python dp_client.py
The below hyperparameters are consistent across the experiments and these can be modified as required.
- Optimizer: Adam,
- Loss Function: Cross Entropy Loss,
- Learning Rate: 0.001,
- Epochs: 5,
- Number of Rounds: 20.
Special thanks to the Flower framework contributors.
This project is for educational purposes. Be aware of potential security concerns when deploying federated learning in real-world scenarios.
Happy Federated Learning!