A federated learning package for IoT devices and aggregation server communication.
Project description
kehe-fl
A Proof of Concept (PoC) for Privacy-Preserving Federated Learning with IoT Devices, developed for the Bachelor’s thesis Machbarkeitsanalyse von Federated Learning mit Internet of Things: Vergleich zentralisierter und dezentraler Trainingsansätze.
This repository implements a minimal, research-focused federated learning system, demonstrating distributed ML model training on resource-constrained IoT clients, coordinated via an MQTT broker, with a central aggregation server.
Features
-
Federated Learning Protocol: Orchestrates distributed training rounds between a central server and multiple IoT clients.
-
MQTT-based Communication: Efficient, lightweight message exchange suitable for IoT environments.
-
Device and Server Reference Implementations: Both roles can be run for local or distributed experiments.
-
Asyncio-based Concurrency: Enables scalable and non-blocking communication and control.
-
Pluggable ML Logic: Simple linear regression for demonstration; can be extended for further experiments.
-
Resource Monitoring: Optional metrics collection on devices and server (CPU, memory, network I/O) to support analysis of communication and computation overhead.
Purpose
This codebase serves as a research PoC to empirically evaluate:
-
The feasibility of federated learning on real IoT hardware (e.g., Raspberry Pi).
-
The system-level overhead and requirements for PPML in practical settings.
-
Tradeoffs between centralized and decentralized model training (scalability, communication cost, resource usage).
-
The project is not a production-ready federated learning framework, but a minimal, transparent testbed for measuring and analyzing privacy-preserving ML on IoT devices.
Quick Start
Packages
-
FL Scenario (S1 | kehe_fl): Individual models are trained on each edge device. The respective model updates are sent to the aggregation server (AS), aggregated there, and then redistributed to the edge devices.
-
Centralized Scenario (S2 | kehe_fl_s2): Model training takes place only on the central server, which receives the raw data from each edge device.
-
Decentralized/Local Training without Aggregation (S3 | kehe_fl_s3): Individual models are trained on each edge device, but model updates are not exchanged between edge devices or through the aggregation server.
Installation (MacOS/Linux)
Start an MQTT broker (e.g., Mosquitto) on your local machine or server. The default broker address in the code is localhost, but you can change it to your broker's address. (You may need a config file for Mosquitto to allow anonymous access or set up user credentials.)
mosquitto -c /path/to/mosquitto.conf
pip install kehe-fl
Usage Overview
1. Device (Client)
Each IoT device runs a client that:
-
Connects to the MQTT broker
-
Receives training instructions and model weights
-
Trains the model locally on its private data
-
Sends model updates back to the aggregation server
import asyncio
from kehe_fl.comms.mqtt_device import MQTTDevice
mqttConnection: MQTTDevice | None = None
async def main():
global mqttConnection
mqttConnection = MQTTDevice(broker="192.168.1.193", deviceId="device123")
mqtt_task = asyncio.create_task(mqttConnection.connect_and_listen())
await asyncio.gather(mqtt_task)
asyncio.run(main())
2. Aggregation Server
The aggregation server:
-
Coordinates the global training process
-
Sends commands to clients
-
Receives, aggregates, and distributes model weights
import asyncio
from kehe_fl.comms.mqtt_agg_server import MQTTAggServer
mqttConnection: MQTTAggServer | None = None
async def handleMessaging():
global mqttConnection
loop = asyncio.get_running_loop()
while True:
if mqttConnection.is_connected and not mqttConnection.working:
message = await loop.run_in_executor(None, input, "Enter a command to send to the clients: ")
await mqttConnection.send_command(message)
else:
await asyncio.sleep(2)
async def main():
global mqttConnection
mqttConnection = MQTTAggServer(broker="localhost")
mqtt_task = asyncio.create_task(mqttConnection.connect_and_listen())
input_task = asyncio.create_task(handleMessaging())
await asyncio.gather(mqtt_task, input_task)
asyncio.run(main())
3. Customization
-
Communication: Adapt MQTT topics and payloads in project_constants.py as needed.
-
Machine Learning Logic: Replace or extend the model in ModelService with your own (e.g., scikit-learn, PyTorch, etc.).
-
Sensor Data: Add your own sensor integration or data preprocessing in the common/ or service/ modules.
-
Metrics: Extend resource monitoring for more detailed benchmarking and analysis.
Documentation
For experimental methodology, data collection, and analysis, see the Bachelor’s Thesis.
Disclaimer
This project is intended for research and educational purposes only.Not suitable for production use.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kehe_fl-0.1.13.tar.gz.
File metadata
- Download URL: kehe_fl-0.1.13.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
147557754fcd3858038d105122e83932d5b516b41778352e2baa414e7f9bccbd
|
|
| MD5 |
11030a8dcbabf4554e152ee799dcea6a
|
|
| BLAKE2b-256 |
e18049b4edd1aab0d7bb98103a7afe5d87abd778a5ffbfe9aac0d72f85bc1c28
|
File details
Details for the file kehe_fl-0.1.13-py3-none-any.whl.
File metadata
- Download URL: kehe_fl-0.1.13-py3-none-any.whl
- Upload date:
- Size: 26.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fec6fd08d60dc039178f3e34a76f4ea88fbe62467e94fd837e4705008c45c877
|
|
| MD5 |
efd376c148d1a333a449344fcc7ed153
|
|
| BLAKE2b-256 |
97cff3f90ec9cbcd2b7ba709b67936e1ba4bb46078393c874ee866312a7dfa95
|