Step-by-Step Guide: Implementing FedAvg vs FedProx on Non-IID CIFAR-10 Using NVIDIA FLARE
This tutorial walks through building a federated learning experiment comparing FedAvg and FedProx algorithms on a non-IID CIFAR-10 dataset using NVIDIA FLARE. By simulating label imbalance with a Dirichlet distribution, we demonstrate realistic federated scenarios. The NVFlare Job API handles job submission, while the Client API manages local training and model exchange. Finally, we compare accuracy across rounds.
Setup and Dependencies
Install required packages and define experiment parameters:
pip install -q "nvflare>=2.5" torch torchvision matplotlib
Set constants for 3 clients, 5 rounds, 1 local epoch, Dirichlet alpha 0.3, batch size 64, learning rate 0.01, and a maximum of 4000 samples per client.
Data Partitioning
CIFAR-10 is split using a Dirichlet distribution to create non-IID label skew. Each client receives a unique subset with imbalanced class proportions.
Client Model
A simple CNN with two convolutional layers and two fully connected layers is used for local training.
Federated Workflow
Using the NVFlare Client API, each client initializes the model, receives the global model from the server, trains locally, and sends updates back. FedProx adds a proximal term to the loss to stabilize training under heterogeneity.
Results
Run both algorithms and plot global model accuracy per round. FedProx often outperforms FedAvg in non-IID settings due to its regularization.
Key Takeaways
- Non-IID data distribution significantly impacts federated learning performance.
- FedProx's proximal term helps mitigate client drift.
- NVIDIA FLARE simplifies deployment and comparison of FL algorithms.
For a full code listing and detailed instructions, refer to the original article.