Tisha-runwal/Personalized-Federated-Learning-for-Privacy--Preserving-and-Scalable-IoT-Driven-Smart-Healthcare

GitHub: Tisha-runwal/Personalized-Federated-Learning-for-Privacy--Preserving-and-Scalable-IoT-Driven-Smart-Healthcare

面向 IoT 智慧医疗场景的个性化联邦学习框架,通过 MAML 元学习、差分隐私和梯度量化实现隐私保护下的高效协同训练,并提供实时可视化仪表盘。

Stars: 108 | Forks: 1

  # 🏥 PFL-HCare ### 面向隐私保护与可扩展 IoT 驱动智慧医疗的个性化联邦学习 **Dayananda Sagar 大学工程学院 · TTEH 实验室** *印度卡纳塔克邦班加罗尔 – 562112*   [![Python](https://img.shields.io/badge/Python-3.10%2B-3776ab?style=for-the-badge&logo=python&logoColor=white)](https://www.python.org/) [![PyTorch](https://img.shields.io/badge/PyTorch-2.0%2B-ee4c2c?style=for-the-badge&logo=pytorch&logoColor=white)](https://pytorch.org/) [![Flower FL](https://img.shields.io/badge/Federated_Learning-Flower_1.5-f5a623?style=for-the-badge)](https://flower.ai/) [![React](https://img.shields.io/badge/Dashboard-React_18-61dafb?style=for-the-badge&logo=react&logoColor=white)](https://react.dev/) [![FastAPI](https://img.shields.io/badge/Backend-FastAPI-009688?style=for-the-badge&logo=fastapi&logoColor=white)](https://fastapi.tiangolo.com/) [![IEEE](https://img.shields.io/badge/IEEE-ICICI_2025-00629b?style=for-the-badge&logo=ieee&logoColor=white)](https://ieeexplore.ieee.org/) [![License](https://img.shields.io/badge/License-MIT-22c55e?style=for-the-badge)](LICENSE)   *原型实现来源:* **“Personalized Federated Learning for Privacy-Preserving and Scalable IoT-Driven Smart Healthcare”** *ICICI-2025, IEEE Xplore · DOI: [10.1109/ICICI65870.2025.11069877](https://doi.org/10.1109/ICICI65870.2025.11069877)*  
## 🔭 概述 医疗领域 IoT 的指数级增长改变了患者监测和诊断方式,但传统的集中式机器学习方法在数据隐私、可扩展性以及对不同患者状况的适应性方面带来了严峻挑战。本工作提出了 **PFL-HCare**,一个面向 IoT 驱动智慧医疗的个性化联邦学习框架,通过四个集成组件执行“本地训练,全局共享”:一个用于自适应模型定制的 **基于 MAML 的元学习个性化器**,一个采用 RDP 计算以提供正式隐私保证的 **差分隐私机制**,用于高效通信更新的 **k-bit 梯度量化**,以及基于梯度范数以实现最佳收敛的 **自适应客户端选择**。所有组件均通过联邦学习进行训练——确保敏感的患者数据永远不会离开边缘设备。该框架包含一个实时 **React + FastAPI 仪表盘**,用于实时可视化五种 FL 方法的收敛情况、隐私预算、通信开销和各客户端指标。在 UCI HAR 和合成医疗数据集上的实验评估表明,在 FedAvg 基线上达到了高达 **89.1% 的准确率**,并在启用差分隐私的情况下保持竞争力的性能,同时通过梯度量化实现了 **75% 的带宽节省**。 `Personalized Federated Learning`  ·  `MAML Meta-Learning`  ·  `Differential Privacy`  ·  `IoT Healthcare`  ·  `Gradient Quantization`  ·  `Real-Time Dashboard` ## 📋 目录 1. [问题陈述](#1--problem-statement) 2. [提出的架构](#2--proposed-architecture) 3. [工作原理](#3--how-it-works) 4. [论文结果与指标](#4--paper-results--metrics) 5. [代码架构](#5--code-architecture) 6. [核心模块——深入解析](#6--core-modules--deep-dive) 7. [设置与使用](#7--setup--usage) 8. [实现结果](#8--implementation-results) 9. [实现限制](#9--implementation-limitations) ## 1. 🔍 问题陈述 医疗领域的传统集中式机器学习需要将敏感的患者数据——病历、可穿戴传感器读数、诊断图像——聚合到单一位置。这带来了严重的**隐私风险**(违反 HIPAA/GDPR、数据泄露目标)、**可扩展性瓶颈**(传输原始医疗数据的带宽成本)以及**个性化失败**(一刀切的全局模型无法适应个体患者的状况、人口统计学特征和传感器差异)。 标准的联邦学习 (FL) 通过将数据保留在设备上来解决隐私问题,但引入了新的挑战: - **异构数据分布** — 由于不同的人口统计学特征、疾病状态和传感器硬件,医疗数据在患者之间本质上是非独立同分布 的 - **个性化差距** — 全局 FL 模型可能无法泛化到个体患者,导致预测性能下降 10-20% - **隐私泄露** — FL 期间交换的梯度更新可能会通过模型反转和成员推理攻击被利用 - **通信开销** — 频繁的客户端-服务器通信给带宽受限的 IoT 边缘设备带来了压力 **需求 →** 一个联邦学习框架,为每个患者/设备提供**个性化模型**,同时保持**正式的隐私保证**、**通信效率**以及跨数百个 IoT 医疗节点的**可扩展性**。 ## 2. 🏗️ 提出的架构 PFL-HCare 通过**四个紧密集成的模块**实现自适应个性化联邦学习,这些模块协同工作以平衡个性化、隐私和效率。 | # | 模块 | 作用 | 关键输出 | |---|---|---|---| | 1️⃣ | **MAML 个性化器** | 基于元学习的本地模型自适应 | 个性化的客户端权重 w_i | | 2️⃣ | **DP 机制** | 带有 RDP 计算的高斯噪声注入 | (ε, δ)-DP 保证的更新 | | 3️⃣ | **梯度量化器** | 模型更新的 k-bit 压缩 (公式8) | 75% 的带宽减少 | | 4️⃣ | **自适应选择器** | 基于梯度范数的客户端选择 (公式9) | 优化的参与度 | | 5️⃣ | **安全聚合器** | 模拟同态加密工作流 | 加密的聚合管道 | ### 🖥️ 全栈仪表盘 该框架包含一个完整的 Web 仪表盘,用于实时可视化: ``` ┌────────────────────────────────────────────────────────────────────┐ │ FastAPI Backend ──WebSocket──► React Dashboard (5 Views) │ │ │ │ 📊 Overview — KPI cards + network topology + activity feed │ │ 📈 Convergence — Multi-line accuracy chart for all 5 methods │ │ 🔒 Privacy — Epsilon gauge + privacy-accuracy tradeoff │ │ 📡 Communication — Bandwidth bars + client participation heatmap │ │ 🏆 Comparison — Live tables reproducing paper Tables II-IV │ └────────────────────────────────────────────────────────────────────┘ ``` ## 3. ⚡ 工作原理 ### 🔄 联邦训练——隐私设计 每轮 FL 遵循严格的隐私保护周期: ``` 📡 Server broadcasts global model weights w* ↓ 🏋️ Each client performs personalized local training (MAML inner loop) ↓ ✂️ Gradient clipping (L2 norm bound) + DP Gaussian noise injection (Eq.5) ↓ 📦 k-bit gradient quantization compresses updates (Eq.8) ↓ 🔒 Simulated homomorphic encryption of model updates (Eq.6) ↓ 📤 Compressed, noisy update sent to server ↓ ⚖️ Server aggregates via weighted FedAvg (Eq.1), scaled by dataset size ↓ 🎯 Adaptive client selection for next round based on gradient norms (Eq.9) ``` ### 📐 关键公式 **全局 FL 目标 (公式1):** ``` min_w Σᵢ (|Dᵢ| / Σⱼ|Dⱼ|) × Fᵢ(w) ``` **MAML 元初始化 (公式3):** ``` w* = argmin_w Σᵢ Fᵢ(w - α∇Fᵢ(w)) ``` **客户端微调 (公式4):** ``` wᵢ = w* - α∇Fᵢ(w*) ``` **差分隐私噪声 (公式5):** ``` wᵢ' = wᵢ + N(0, σ²) guarantees (ε, δ)-DP ``` **梯度量化 (公式8):** ``` Q(wᵢ) = round((wᵢ - w_min) / (w_max - w_min) × (2ᵏ - 1)) ``` **自适应客户端选择 (公式9):** ``` pᵢ = ‖∇Fᵢ(w)‖ / Σⱼ‖∇Fⱼ(w)‖ ``` ### 🏆 对比方法 所有五种方法共享相同的模型架构、数据划分和评估协议: | 方法 | 个性化 | 差分隐私 | 安全聚合 | 量化 | 自适应选择 | |---|:---:|:---:|:---:|:---:|:---:| | FedAvg | ✗ | ✗ | ✗ | ✗ | ✗ | | FedProx | ~ | ✗ | ✗ | ✗ | ✗ | | Per-FedAvg | ✓ | ~ | ✗ | ✗ | ✗ | | pFedMe | ✓ | ✓ | ✗ | ✗ | ✗ | | **PFL-HCare (本文)** | **✓** | **✓** | **✓** | **✓** | **✓** | ## 4. 📊 论文结果与指标 ### 🎯 模型收敛与准确率 (表 II) | 模型 | 测试准确率 (MIMIC-III) | 测试准确率 (UCI HAR) | 收敛速度 | |---|:---:|:---:|:---:| | FedAvg | 87.50% | 89.30% | 基准 | | FedProx | 89.20% | 91.00% | +8.5% 更快 | | Per-FedAvg | 90.50% | 92.50% | +15.2% 更快 | | pFedMe | 91.60% | 93.20% | +21.3% 更快 | | **🏆 PFL-HCare (本文)** | **92.30%** | **94.10%** | **+27.8% 更快** | ### 🔒 隐私保护有效性 (表 III) | 模型 | 应用 DP | 准确率下降 | 隐私参数 | |---|:---:|:---:|:---:| | FedAvg | ✗ 无 DP | N/A | N/A | | FedProx | ✗ 无 DP | N/A | N/A | | Per-FedAvg | ✓ 部分 | 3.10% | ε=3.5, δ=10⁻⁴ | | pFedMe | ✓ 已应用 DP | 2.50% | ε=2.8, δ=10⁻⁵ | | **🏆 PFL-HCare (本文)** | **✓ DP + 安全聚合** | **1.70%** | **ε=2.1, δ=10⁻⁵** | ### 📡 通信开销与可扩展性 (表 IV) | 指标 | FedAvg | FedProx | pFedMe | **PFL-HCare** | |---|:---:|:---:|:---:|:---:| | 通信开销 | 高 | 高 | 中 | **低 (-38.2%)** | | 带宽消耗 | 高 | 高 | 中 | **低 (-45%)** | | 客户端参与度 | 随机 | 固定 | 自适应 | **优化 (+40%)** | | 可扩展性 (N=500) | 15% 减速 | 10% 减速 | 5% 减速 | **稳定** | ## 5. 🗂️ 代码架构 该原型将论文的架构转化为分层的模块化 Python/TypeScript 包。每一层都可以独立测试和部署。 ``` Personalized_Federated_Learning/ ├── pfl_hcare/ # 🧠 Core ML library │ ├── models/ │ │ ├── health_classifier.py # 🏥 MLP for medical prediction (~15K params) │ │ └── har_classifier.py # 📱 1D-CNN for activity recognition (~52K params) │ ├── fl/ │ │ ├── server.py # 🖥️ FL simulation engine (local, no Ray) │ │ ├── client.py # 📡 Flower NumPyClient — 5 strategy modes │ │ ├── aggregation.py # ⚖️ Weighted FedAvg (Eq.1) │ │ └── strategies/ # 🎯 One strategy class per method │ │ ├── fedavg.py # Vanilla weighted averaging │ │ ├── fedprox.py # + proximal regularization (μ=0.01) │ │ ├── per_fedavg.py # + MAML personalization │ │ ├── pfedme.py # + Moreau envelope (λ=15) │ │ └── pfl_hcare.py # + DP + quantization + adaptive selection │ ├── maml/ │ │ └── maml.py # 🔁 MAML inner/outer loop (FOMAML toggle) │ ├── privacy/ │ │ ├── differential_privacy.py # 🔐 Gaussian DP with RDP accounting (Eq.5) │ │ ├── secure_aggregation.py # 🔒 Simulated HE for dashboard (Eq.6-7) │ │ └── quantization.py # 📦 k-bit gradient compression (Eq.8) │ └── metrics/ │ └── collector.py # 📊 Per-round metric tracking + callbacks │ ├── data/ # 📂 Dataset layer │ ├── har_loader.py # 📱 UCI HAR — 10,299 samples, 6 activities │ ├── mimic_loader.py # 🏥 4-tier fallback: MIMIC-III → Demo → Heart → Synthetic │ ├── synthetic_generator.py # 🤖 Tier 4: configurable vital signs generator │ └── partition.py # 🔀 Dirichlet non-IID partitioning │ ├── server/ # 🌐 FastAPI backend │ ├── main.py # 🚀 App entry point + CORS + lifespan │ ├── db.py # 💾 SQLite persistence (runs + round metrics) │ ├── orchestrator.py # 🎼 Sequential comparison run manager │ ├── routes/ │ │ ├── training.py # ▶️ POST /start · POST /stop · GET /status │ │ ├── metrics.py # 📈 GET /runs · GET /{run_id} │ │ └── datasets.py # 📂 GET /info · POST /partition-preview │ └── ws/ │ └── live.py # 📡 WebSocket live metric streaming │ ├── client/ # 🎨 React dashboard │ └── src/ │ ├── components/ │ │ ├── layout/ # Sidebar · Header · ControlRibbon │ │ ├── views/ # 5 views: Overview · Convergence · Privacy · Comm · Comparison │ │ ├── charts/ # ConvergenceChart · PrivacyGauge · BandwidthChart │ │ └── widgets/ # KpiCard · ActivityFeed │ ├── hooks/ # useWebSocket · useTrainingState │ └── types/ # TypeScript interfaces for all metrics │ ├── docker/ # 🐳 Docker simulation │ ├── Dockerfile.api # FastAPI container │ ├── Dockerfile.dashboard # React + nginx container │ └── docker-compose.yml # Multi-container orchestration │ ├── configs/ │ ├── default.yaml # 🎛️ All hyperparameters (single source of truth) │ └── comparison.yaml # 🏆 Full 5-method comparison config │ ├── scripts/ │ ├── run_local.py # 🚀 CLI simulation launcher │ └── download_data.py # 📥 Dataset downloader │ └── tests/ # 🧪 49 tests across 11 test files ├── test_models.py # Model forward pass, param counts, gradients ├── test_maml.py # MAML inner/outer loop, second-order mode ├── test_dp.py # DP noise, clipping, epsilon tracking ├── test_quantization.py # k-bit quantize/dequantize, bandwidth ├── test_secure_agg.py # Encrypt/decrypt round-trip, aggregation ├── test_partition.py # Dirichlet partitioning, heterogeneity score ├── test_strategies.py # All 5 strategy instantiation └── test_e2e.py # End-to-end FedAvg + PFL-HCare smoke tests ``` ### 🎛️ 关键配置参数 所有超参数都集中在 `configs/default.yaml` 中: | 参数 | 值 | 说明 | |---|:---:|---| | `learning_rate` | 0.01 | 本地训练的 SGD 学习率 | | `batch_size` | 32 | 每客户端批次大小 | | `num_clients` | 10 | 联邦 IoT 客户端数量 | | `num_rounds` | 200 | 通信轮数 | | `local_epochs` | 5 | 每轮本地训练 epoch 数 | | `noise_multiplier` σ | 0.5 | DP 噪声规模(隐私-准确率权衡) | | `max_grad_norm` | 1.0 | 梯度裁剪界限 | | `k_bits` | 8 | 量化位宽 (2, 4, 8, 16) | | `partition_alpha` | 0.5 | Dirichlet non-IID 浓度 | | `inner_lr` | 0.01 | MAML 内循环学习率 | | `inner_steps` | 5 | MAML 内循环梯度步数 | ## 6. 🧩 核心模块——深入解析 ### 🏥 健康分类器——用于医疗预测的 MLP 3 层 MLP,处理 13 项特征医疗生命体征(心率、血压、SpO2、体温等)。专为 IoT 边缘部署设计,仅有约 15K 参数 (61 KB): ``` Input (13 features) → Dense(128)+BN+ReLU+Dropout(0.3) → Dense(64)+BN+ReLU+Dropout(0.2) → Dense(32)+ReLU → Output (2 classes: healthy/at-risk) ``` ### 📱 HAR 分类器——用于活动识别的 1D-CNN 1D-CNN,处理来自可穿戴 IoT 设备的 9 通道加速度计/陀螺仪信号。约 52K 参数 (209 KB)——适合在 Raspberry Pi 上部署: ``` Input (9ch × 128 timesteps) → Conv1D(64,k=5)+BN+ReLU+MaxPool → Conv1D(128,k=3)+BN+ReLU+MaxPool → Conv1D(64,k=3)+BN+ReLU → GlobalAvgPool → Dense(64)+Dropout(0.3) → Output (6 activities) ``` ### 🔁 MAML——元学习个性化器 与模型无关的元学习,学习一个自适应的全局初始化 w*,使得每个客户端能够快速微调以适应特定患者的数据。支持完整的二阶 MAML(Hessian 向量积)和 FOMAML(一阶近似),适用于资源受限的设备。 ### 🔐 差分隐私——正式的隐私保证 带有逐样本梯度裁剪和 RDP (Renyi Differential Privacy) 计算的高斯机制。可配置的噪声乘数 σ 控制着隐私与准确率的权衡——仪表盘通过交互式 σ 滑块实时对其进行可视化。 ``` # 每轮 privacy pipeline clipped = dp.clip_gradients(model_updates) # L2 norm bound noisy = dp.add_noise(clipped, sample_rate=q) # N(0, σ²) injection epsilon = dp.get_epsilon() # Cumulative ε tracking ``` ### 📦 梯度量化——通信效率 k-bit 编码在传输之前将 float32 模型权重压缩为 k-bit 整数,从而节省高达 75% (8-bit) 或 93.75% (2-bit) 的带宽。在服务器聚合之前进行反量化。 ### 🎯 自适应客户端选择——优化的参与度 在第 1 轮之后,服务器会跟踪每个客户端的梯度范数。选择概率与梯度幅度成正比——需要学习更多内容的客户端会更频繁地参与,从而减少了已收敛客户端的无效通信。 ### 📡 FL 策略——集五种方法于一体 所有五种方法共享相同的 Flower 客户端基础设施,只是替换了策略逻辑: | 策略 | 关键机制 | 复杂度 | |---|---|---| | **FedAvg** | 加权参数平均 | 基准 | | **FedProx** | + 近端项 `(μ/2)‖w - w_global‖²` | + 1 个正则化器 | | **Per-FedAvg** | +AML 内/外循环 | + 元学习 | | **pFedMe** | + Moreau 包络 `(λ/2)‖w - θᵢ‖²` | + 个性化参数 | | **PFL-HCare** | + MAML + DP + 量化 + 自适应选择 | 完整管道 | ## 7. 🚀 设置与使用 ### ⚙️ 硬件要求 | 组件 | 本原型 | 论文复现 | |---|---|---| | 🎮 GPU | 可选(CPU 即可) | NVIDIA A100 · 80 GB VRAM | | 🧠 RAM | 4 GB+ | 16 GB+ | | 💾 硬盘 | ~2 GB(依赖 + 数据) | ~5 GB | | 📡 FL 客户端 | 5–10 | 100 | | 🔄 FL 轮数 | 30–50 | 200 | ### 📦 快速开始 ``` # 安装 Python 依赖 pip install -r requirements.txt # 安装 dashboard 依赖 cd client && npm install && cd .. # 启动 API + Dashboard uvicorn server.main:app --port 8000 & cd client && npm run dev & # 运行 FL simulation python3 scripts/run_local.py --method pfl_hcare --rounds 30 --clients 5 --dataset mimic ``` 打开 **http://localhost:5173** 以查看实时仪表盘。 ### 📂 数据集 | 数据集 | 用途 | 规模 | 访问 | |---|---|---|---| | **UCI HAR** | 活动识别(可穿戴 IoT) | 10,299 个样本 · 561 项特征 · 6 个类别 | https://archive.ics.uci.edu/ml/datasets/human+activity+recognition+using+smartphones | | **MIMIC-III** | ICU 死亡率预测 | 48,520 条记录 · 17 项特征 | https://physionet.org/content/mimiciii/ | | **Heart Disease UCI** | 心脏风险分类 | 303 个样本 · 13 项特征 | https://archive.ics.uci.edu/ml/datasets/heart+diseas | | **Synthetic Medical** | 备用生命体征数据 | 可配置 · 13 项特征 · 3 个集群 | data/synthetic_generator.py | ## 8. 📊 实现结果 以下结果是在消费级硬件(Apple M 系列,CPU)上运行原型,并采用缩小的参数(5 个客户端,30 轮)得出的。 ### 📈 FedAvg 收敛 ``` Round 1 — accuracy: 88.4% loss: 0.4515 Round 5 — accuracy: 87.3% loss: 0.3754 Round 10 — accuracy: 88.0% loss: 0.4148 Round 20 — accuracy: 88.9% loss: 0.4095 Round 30 — accuracy: 88.8% loss: 0.4045 ← stable convergence ``` ### 🔒 带有差分隐私的 PFL-HCare ``` Round 1 — accuracy: 60.0% loss: 84.04 (cold start with DP noise) Round 5 — accuracy: 72.7% loss: 54.58 (learning despite noise) Round 13 — accuracy: 81.9% loss: 2.56 (convergence through noise) Round 24 — accuracy: 87.6% loss: 3.48 ← peak with DP enabled ``` PFL-HCare 中的方差是**符合预期的**——它直接展示了仪表盘的隐私面板旨在可视化的隐私与准确率的权衡。σ 越高 = 隐私性越好 + 方差越大。 ### 🧪 测试套件 ``` 49 passed in 1.73s (excluding HAR download tests) ``` ## 9. ⚠️ 实现限制 | # | 📄 论文规范 | 💻 原型现实 | 🔧 修复路径 | |---|---|---|---| | L1 | A100 GPU 上的 100 个 FL 客户端 | CPU 上的 5–10 个客户端 | 使用 GPU 硬件扩展 `num_clients` | | L2 | 真实 MIMIC-III(4.8万条 ICU 记录) | 合成备用数据(2千个样本) | 获取 PhysioNet 凭证 → 替换数据路径 | | L3 | 同态加密 | 通过延迟 + 状态跟踪模拟 | 替换为 TenSEAL 或 Paillier 库 | | L4 | 完整的 MAML 二阶梯度 | 带有正则化器的个性化训练 | 为小型模型启用 `second_order=True` | | L5 | 200 轮 FL | 30 轮(时间限制) | 在配置中增加 `num_rounds` | | L6 | D3 网络拓扑可视化 | 概述面板中的占位符 | 实现力导向图 | | L7 | 从仪表盘导出 PDF/LaTeX | 未实现 | 添加基于 reportlab 的 export_report.py | | L8 | UCI HAR 9 通道 CNN 输入 | 561 维扁平特征输入(自动重塑) | 使用原始惯性信号文件 |
## 👥 团队 Tisha Runwal · Vaishnavi Shetty · Chandana A N · Gagana V ENG23CY0042 · ENG23CY0045 · ENG23CY0056 · ENG23CY0016 tisharunwal@gmail.com · vaishnaviapshetty@gmail.com · chandananatesh1@gmail.com · gaganagaganav2702@gmail.com **计算机科学与工程系(网络安全)** Dayananda Sagar 大学工程学院 ## 🧑‍🏫 导师 Dr. Prajwalasimha S N, Ph.D., Postdoc. (NewRIIS) 副教授 **计算机科学与工程系(网络安全)** Dayananda Sagar 大学工程学院 ## 🔬实验室 **Dayananda Sagar 大学工程学院 · TTEH 实验室** *印度卡纳塔克邦班加罗尔 – 562112*   [![IEEE 论文](https://img.shields.io/badge/Read_the_Paper-IEEE_Xplore-00629b?style=flat-square&logo=ieee)](https://doi.org/10.1109/ICICI65870.2025.11069877)
标签:AV绕过, CISA项目, FastAPI, Flower, MAML, Python, PyTorch, React, Rényi差分隐私, Syscalls, 个性化联邦学习, 元学习, 凭据扫描, 分布式机器学习, 医疗数据分析, 医疗物联网, 可扩展性, 差分隐私, 数据隐私, 无后门, 智慧医疗, 机器学习框架, 梯度量化, 深度学习, 网络安全, 联邦学习, 请求拦截, 边缘计算, 逆向工具, 隐私保护