Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kehe_fl-0.1.14.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kehe_fl-0.1.14-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file kehe_fl-0.1.14.tar.gz.

File metadata

  • Download URL: kehe_fl-0.1.14.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for kehe_fl-0.1.14.tar.gz
Algorithm Hash digest
SHA256 68deefd9f42093d86be0bf0808fbcfc5fff4d28e32da07a0931e669000f1cbd8
MD5 d54a6bb205aec60fcf2558255e7487f9
BLAKE2b-256 6c13f5484fd856c85db36a139102d9f4b8f68cbad7448eda925b90ffc6b0fe68

See more details on using hashes here.

File details

Details for the file kehe_fl-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: kehe_fl-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for kehe_fl-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 d3abcb2c1db86a30a33fd41ce2f50d07ac2e9bf243f0ba8404397f733ca9f145
MD5 bbd883e5b841392bef956b13310362e8
BLAKE2b-256 42f7f9facf176a0ac4077347cbb7e578882905813b8d3335f0f166e49fced3d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page