Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A flexible Federated Learning Framework based on PyTorch, simplifying your Federated Learning research.

License

NotificationsYou must be signed in to change notification settings

SMILELab-FL/FedLab

Repository files navigation

FedLab: A Flexible Federated Learning Framework

GH Actions TestsDocumentation StatusLicensecodecovarXivPyversions

Federated learning (FL), proposed by Google at the very beginning, is recently a burgeoning research area of machine learning, which aims to protect individual data privacy in the distributed machine learning processes, especially in finance, smart healthcare, and edge computing. Different from traditional data-centered distributed machine learning, participants in the FL setting utilize localized data to train local models, then leverages specific strategies with other participants to acquire the final model collaboratively, avoiding direct data-sharing behavior.

To relieve the burden of researchers in implementing FL algorithms and emancipate FL scientists from the repetitive implementation of basic FL settings, we introduce a highly customizable frameworkFedLab in this work.FedLab provides the necessary modules for FL simulation, includingcommunication,compression,model optimization,data partition and otherfunctional modules. Users can build an FL simulation environment with custom modules like playing with LEGO bricks. For better understanding and easy usage, the FL baseline algorithms implemented viaFedLab are also presented.

Quick start

Install

  • Install the latest version from source code:
$ git clone git@github.com:SMILELab-FL/FedLab.git$ cd FedLab$ pip install -r requirements.txt
  • Install the stable version (old version) via pip:
# assign the version fedlab==1.3.0$ pip install fedlab

Learning materials

We provide tutorials in jupyter notebook format for FedLab beginners in FedLab\tutorials. These tutorials include data partition, customized algorithms, and pipeline demos. For the FedLab or FL beginners, we recommend thisnotebook. Furthermore, we provide reproductions of federated algorithms via FedLab, which are stored in fedlab.contirb.algorithm. We think they are good examples for users to further explore FedLab.

Website Documentations are available:

Run Examples

  • Run our quick start examples of different scenarios with a partitioned MNIST dataset.
# example of standalone$ cd ./examples/standalone/$ python standalone.py --total_clients 100 --com_round 3 --sample_ratio 0.1 --batch_size 100 --epochs 5 --lr 0.02

Architecture

Files architecture of FedLab. These contents may be helpful for users to understand our repo.

├── fedlab│   ├── contrib│   ├── core│   ├── models│   └── utils├── datasets│   └── ...├── examples│   ├── asynchronous-cross-process-mnist│   ├── cross-process-mnist│   ├── hierarchical-hybrid-mnist│   ├── network-connection-checker│   ├── scale-mnist│   └── standalone-mnist└── tutorials    ├── communication_tutorial.ipynb    ├── customize_tutorial.ipynb    ├── pipeline_tutorial.ipynb    └── ...

Baselines

We provide the reproduction of baseline federated algorthms for users in this repo.

MethodTypePaperPublicationOfficial code
FedAvgOptim.Communication-Efficient Learning of Deep Networks from Decentralized DataAISTATS'2017
FedProxOptim.Federated Optimization in Heterogeneous NetworksMLSys' 2020Code
FedDynOptim.Federated Learning Based on Dynamic RegularizationICLR' 2021Code
q-FFLOptim.Fair Resource Allocation in Federated LearningICLR' 2020Code
FedNovaOptim.Tackling the Objective Inconsistency Problem in Heterogeneous Federated OptimizationNeurIPS'2020Code
IFCAOptim.An Efficient Framework for Clustered Federated LearningNeurIPS'2020Code
DittoOptim.Ditto: Fair and Robust Federated Learning Through PersonalizationICML'2021Code
SCAFFOLDOptim.SCAFFOLD: Stochastic Controlled Averaging for Federated LearningICML'2020
Personalized-FedAvgOptim.Improving Federated Learning Personalization via Model Agnostic Meta LearningPre-print
CFLOptim.Clustered Federated Learning: Model-Agnostic Distributed Multi-Task Optimization under Privacy ConstraintsIEEE'2020Code
Power-of-choiceMisc.Client Selection in Federated Learning: Convergence Analysis and Power-of-Choice Selection StrategiesAISTATS'2021
QSGDCom.QSGD: Communication-Efficient SGD via Gradient Quantization and EncodingNeurIPS'2017
NIID-BenchData.Federated Learning on Non-IID Data Silos: An Experimental StudyICDE' 2022Code
LEAFData.LEAF: A Benchmark for Federated SettingsPre-printCode
...

Datasets & Data Partition

Sophisticated in the real world, FL needs to handle various kind of data distribution scenarios, including iid and non-iid scenarios. Though there already exists some datasets and partition schemes for published data benchmark, it still can be very messy and hard for researchers to partition datasets according to their specific research problems, and maintain partition results during simulation.FedLab providesfedlab.utils.dataset.partition.DataPartitioner that allows you to use pre-partitioned datasets as well as your own data.DataPartitioner stores sample indices for each client given a data partition scheme. Also, FedLab provides some extra datasets that are used in current FL researches while not provided by official PyTorchtorchvision.datasets yet.

Data Partition

We provide multiple data partition schemes used in recent FL papers[1][2][3]. Here we show the data partition visualization of several common used datasets as the examples.

1. Balanced IID partition

Each client has same number of samples, and same distribution for all class samples.

Given 100 clients and CIFAR10, the data samples assigned to the first 10 clients could be:

2. Unbalanced IID partition

Assign different sample number for each client using Log-Normal distribution$\text{Log-N}(0, \sigma^2)$, while keep same distribution for different class samples.

Given$\sigma=0.3$, 100 clients and CIFAR10, the data samples assigned to the first 10 clients is showed left below. And distribution of sample number for clients is showed right below.

   

3. Hetero Dirichlet partition

Non-iid partition used in[3] and[6]. Number of data points and class proportions are unbalanced. Samples will be partitioned into$J$ clients by sampling $p_k∼\text{Dir}J(\alpha)$ and allocating a $p{k,j}$ proportion of the samples of class$k$ to local client$j$.

Given 100 clients,$\alpha=0.3$ and CIFAR10, the data samples assigned to the first 10 clients is showed left below. And distribution of sample number for clients is showed right below.

   

4. Shards partition

Non-iid partition based on shards, used in[4].

Givenshard_number=200, 100 clients and CIFAR10, the data samples assigned to the first 10 clients could be:

5. Balanced Dirichlet partition

Non-iid partition used in[5]. Each client has same number of samples, while class distribution in each client follows Dirichlet distribution$\text{Dir}{(\alpha)}$.

Given$\alpha=0.3$, 100 clients and CIFAR10, the data samples assigned to the first 10 clients could be:

6. Unbalanced Dirichlet partition

Non-iid partition used in[5]. Sample numbers of clients are drawn from Log-normal distribution$\text{Log-N}(0, \sigma^2)$, while class distribution in each client follows Dirichlet distribution$\text{Dir}{(\alpha)}$.

Given$\sigma=0.3$,$\alpha=0.3$, 100 clients and CIFAR10, the data samples assigned to the first 10 clients is showed left below. And distribution of sample number for clients is showed right below.

   

7. Quantity-based Label Distribution Skew partition

Non-iid partition used in[1]. Each client has only specific number of sample class.

Given class number for each client as$3$, 10 clients and FashionMNIST, the data samples assigned to each client could be:

8. Noise-based Feature Distribution Skew partition

Non-iid partition used in[1]. Different client's sample feature has different levels of Gaussian noise. Data example for 10 clients could be:

9. FCUBE Synthetic partition

Non-iid partition used in[1]. Data example for 4 clients could be shown as:

Datasets supported

Data TypeData Name#Training Samples#Test Samples#Label Classes
Vision dataCIFAR1050K 10K10
CIFAR10050K10K 100
FashionMNIST60K10K 10
MNIST60K10K 10
SVHN73K26K 10
CelebA200, 288 2
FEMNIST805, 263 62
Text dataShakespeare4, 226, 158 -
Sent141, 600, 498 3
Reddit56, 587, 343 -
Tabular dataAdult32, 561 16, 2812
Covtype 581, 012 2
RCV1 binary20, 242 677, 3992
Synthetic dataFCUBE - -2
LEAF-Synthetic - - -

Partition Visualization

For data distribution visualization in data partition, we providefedlab.utils.dataset.functional.feddata_scatterplot() for users' convenience.

Visualization for synthetic partition code below:

importnumpyasnpfrommatplotlibimportpyplotaspltfromfedlab.utils.dataset.functionalimportfeddata_scatterplotsample_num=15class_num=4clients_num=3num_per_client=int(sample_num/clients_num)labels=np.random.randint(class_num,size=sample_num)# generate 15 labels, each label is 0 to 3rand_per=np.random.permutation(sample_num)# partition synthetic data into 3 clientsdata_indices= {0:rand_per[0:num_per_client],1:rand_per[num_per_client:num_per_client*2],2:rand_per[num_per_client*2:num_per_client*3]}title='Data Distribution over Clients for Each Class'fig=feddata_scatterplot(labels.tolist(),data_indices,clients_num,class_num,figsize=(6,4),max_size=200,title=title)plt.show(fig)fig.savefig(f'imgs/feddata-scatterplot-vis.png')

Visualization result for CIFAR-10 Dirichlet Non-IID with$\alpha=0.6$ on 5 clients:

Performance & Insights

We provide the performance report of several reproduced federated learning algorithms to illustrate the correctness of FedLab in simulation. Furthermore, we describe several insights FedLab could provide for federated learning research. Without loss of generality, this section's experiments are conducted on partitioned MNIST datasets. The conclusions and observations in this section should still be valid in other data sets and scenarios.

Federated Optimization on Non-IID Data

We choose$\alpha = [0.1, 0.3, 0.5, 0.7]$ in label Dirichlet partitioned MNIST with 100 clients. We run 200 rounds of FedAvg with 5 local batches with full batch, learning rate 0.1, and sample ratio 0.1 (10 clients for each FL round). The test accuracy over the communication round is shown below. The results reveal the most vital challenge in federated learning.

We use the same partitioned MNIST dataset in FedAvg[4] to evaluate the corectness of FedLab. The rounds for FedAvg to achieve 97% test accuracy on MNIST using 2NN with E=5 reported in[4] / FedLab:

Sample ratioIIDNon-IID
B=FULLB=10B=FULLB=10
0.01455 / 1293316 / 774278 / 18153275 / 1056
0.11474 / 123087 / 431796 / 2778664 / 439
0.21658 / 123477 / 371528 / 2805619 / 427
0.5-- / 122975 / 36-- / 3034443 / 474
1.0-- / 128470 / 35-- / 3154380 / 507

The results are obtained by running thetutorial with random seed 0.

Simulation Efficiency

Time cost in 100 rounds (50 clients are sampled per round) under different acceleration settings. 1M-10P stands for the simulation runs on 1 machine with 4 GPUs and 10 processes. 2M-10P stands for the simulation runs on 2 machines with 4 GPUs and 10 processes (5 processes on each machine).

Hardware platform: Intel(R) Xeon(R) Gold 6240L CPU @ 2.60GHz + Tesla V100 * 4.

StandaloneCross-process 1M-10PCross-process 2M-10P
45.6 Min2.9 Min4.23 Min

The results are obtained by running thetutorial and an example of cross-process scenario. Besides, the results reveal the simulation efficiency of FedLab under different simulation modes. Cross-process with 2 machines could be slower in this setting due to communication bottleneck.

Communication Efficiency

We provide a few performance baselines in communication-efficient federated learning including QSGD and top-k. In the experiment setting, we choose$\alpha = 0.5$ in the label Dirichlet partitioned MNIST with 100 clients. We run 200 rounds with a sample ratio of 0.1 (10 clients for each FL round) of FedAvg, where each client performs 5 local epochs of SGD with a full batch and learning rate of 0.1. We report the top-1 test accuracy and its communication volume during the training.

SettingBaselineQSGD-4bitQSGD-8bitQSGD-16bitTop-5%Top-10%Top-20%
Test Accuracy (%)93.1493.0393.2793.1111.3561.2589.96
Communication (MB)302.4545.5985.06160.670.941.893.79

The above results are obtained by running thetutorial.

Citation

Please citeFedLab in your publications if it helps your research:

@article{JMLR:v24:22-0440,author  ={Dun Zeng and Siqi Liang and Xiangjing Hu and Hui Wang and Zenglin Xu},title   ={FedLab: A Flexible Federated Learning Framework},journal ={Journal of Machine Learning Research},year    ={2023},volume  ={24},number  ={100},pages   ={1--7},url     ={http://jmlr.org/papers/v24/22-0440.html}}

or

@article{zeng2021fedlab,  title={Fedlab: A flexible federated learning framework},  author={Zeng, Dun and Liang, Siqi and Hu, Xiangjing and Wang, Hui and Xu, Zenglin},  journal={arXiv preprint arXiv:2107.11621},  year={2021}}

Contact

Project Investigator:Prof. Zenglin Xu (xuzenglin@hit.edu.cn).

For technical issues related toFedLab development, please contact our development team through Github issues or email:

References

[1] Li, Q., Diao, Y., Chen, Q., & He, B. (2022, May). Federated learning on non-iid data silos: An experimental study. In2022 IEEE 38th International Conference on Data Engineering (ICDE) (pp. 965-978). IEEE.

[2] Caldas, S., Duddu, S. M. K., Wu, P., Li, T., Konečný, J., McMahan, H. B., ... & Talwalkar, A. (2018). Leaf: A benchmark for federated settings.arXiv preprint arXiv:1812.01097.

[3] Yurochkin, M., Agarwal, M., Ghosh, S., Greenewald, K., Hoang, N., & Khazaeni, Y. (2019, May). Bayesian nonparametric federated learning of neural networks. InInternational Conference on Machine Learning (pp. 7252-7261). PMLR.

[4] McMahan, B., Moore, E., Ramage, D., Hampson, S., & y Arcas, B. A. (2017, April). Communication-efficient learning of deep networks from decentralized data. InArtificial intelligence and statistics (pp. 1273-1282). PMLR.

[5] Acar, D. A. E., Zhao, Y., Navarro, R. M., Mattina, M., Whatmough, P. N., & Saligrama, V. (2021). Federated learning based on dynamic regularization.arXiv preprint arXiv:2111.04263.

[6] Wang, H., Yurochkin, M., Sun, Y., Papailiopoulos, D., & Khazaeni, Y. (2020). Federated learning with matched averaging.arXiv preprint arXiv:2002.06440.


[8]ページ先頭

©2009-2025 Movatter.jp