--- title: "FedBN: Federated Learning on Non-IID Features via Local Batch Normalization" url: https://arxiv.org/abs/2102.07623 labels: [data heterogeneity, feature shift, cross-silo] dataset: [MNIST, MNIST-M, SVHN, USPS, SynthDigits] --- # FedBN: Federated Learning on Non-IID Features via Local Batch Normalization [View on GitHub](https://github.com/adap/flower/blob/main/baselines/fedbn) > Note: If you use this baseline in your work, please remember to cite the original authors of the paper as well as the Flower paper. **Paper:** [arxiv.org/abs/2102.07623](https://arxiv.org/abs/2102.07623) **Authors:** Xiaoxiao Li, Meirui Jiang, Xiaofei Zhang, Michael Kamp, Qi Dou **Abstract:** The emerging paradigm of federated learning (FL) strives to enable collaborative training of deep models on the network edge without centrally aggregating raw data and hence improving data privacy. In most cases, the assumption of independent and identically distributed samples across local clients does not hold for federated learning setups. Under this setting, neural network training performance may vary significantly according to the data distribution and even hurt training convergence. Most of the previous work has focused on a difference in the distribution of labels or client shifts. Unlike those settings, we address an important problem of FL, e.g., different scanners/sensors in medical imaging, different scenery distribution in autonomous driving (highway vs. city), where local clients store examples with different distributions compared to other clients, which we denote as feature shift non-iid. In this work, we propose an effective method that uses local batch normalization to alleviate the feature shift before averaging models. The resulting scheme, called FedBN, outperforms both classical FedAvg, as well as the state-of-the-art for non-iid data (FedProx) on our extensive experiments. These empirical results are supported by a convergence analysis that shows in a simplified setting that FedBN has a faster convergence rate than FedAvg. ## About this baseline **What’s implemented:** Figure 3 in the paper: convergence in training loss comparing `FedBN` to `FedAvg` for five datasets. **Datasets:** Vision datasets including digits 0-9. These datasets are: [MNIST](https://ieeexplore.ieee.org/document/726791), [MNIST-M](https://arxiv.org/pdf/1505.07818.pdf), [SVHN](http://ufldl.stanford.edu/housenumbers/nips2011_housenumbers.pdf), [USPS](https://ieeexplore.ieee.org/document/291440), and [SynthDigits](https://arxiv.org/pdf/1505.07818.pdf). **Hardware Setup:** Using the default configurations, any machine with 8 CPU cores should be capable to run 100 rounds of FedAvg or FedBN in under 5 minutes. Therefore a GPU is not needed if you stick to the small model used in the paper and you limit clients to use a 10% of the data in each dataset (these are the default settings) **Contributors:** Meirui Jiang, Maria Boerner, Javier Fernandez-Marques ## Experimental Setup **Task:** Image classification **Model:** A six-layer CNN with 14,219,210 parameters following the structure described in appendix D.2. **Dataset:** This baseline makes use of the pre-processed partitions created and open source by the authors of the FedBN paper. You can read more about how those were created [here](https://github.com/med-air/FedBN). Follow the steps below in the `Environment Setup` section to download them. A more detailed explanation of the datasets is given in the following table. | | MNIST | MNIST-M | SVHN | USPS | SynthDigits | | ------------------ | ------------------ | -------------------------------------------------------- | ------------------------- | ------------------------------------------------------------ | -------------------------------------------------------------------------------- | | data type | handwritten digits | MNIST modification randomly colored with colored patches | Street view house numbers | handwritten digits from envelopes by the U.S. Postal Service | Synthetic digits Windows TM font varying the orientation, blur and stroke colors | | color | greyscale | RGB | RGB | greyscale | RGB | | pixelsize | 28x28 | 28 x 28 | 32 x32 | 16 x16 | 32 x32 | | labels | 0-9 | 0-9 | 1-10 | 0-9 | 1-10 | | number of trainset | 60.000 | 60.000 | 73.257 | 9,298 | 50.000 | | number of testset | 10.000 | 10.000 | 26.032 | - | - | | image shape | (28,28) | (28,28,3) | (32,32,3) | (16,16) | (32,32,3) | **Training Hyperparameters:** By default (i.e. if you don't override anything in the config) these main hyperparameters used are shown in the table below. For a complete list of hyperparameters, please refer to the config files in `fedbn/conf`. | Description | Value | | --------------------------- | ------------------ | | rounds | 10 | | num_clients | 5 | | strategy_fraction_fit | 1.0 | | strategy.fraction_evaluate | 0.0 | | training samples per client | 743 | | client.l_r | 10E-2 | | local epochs | 1 | | loss | cross entropy loss | | optimizer | SGD | | client_resources.num_cpu | 2 | | client_resources.num_gpus | 0.0 | ## Environment Setup To construct the Python environment, simply run: ```bash # Set directory to use python 3.10 (install with `pyenv install ` if you don't have it) pyenv local 3.10.6 # Tell poetry to use python3.10 poetry env use 3.10.6 # Install poetry install ``` Before running the experiments you'll need to download the five datasets for this baseline. We'll be using the pre-processed datasets created by the `FedBN` authors. Download the dataset from [here](https://mycuhk-my.sharepoint.com/:u:/g/personal/1155149226_link_cuhk_edu_hk/EV1YgHfFC4RKjw06NL4JMdgBMU21CegM12SpXrSmGjt3XA?e=XK2rFs) and move the file into a new directory named `data`. ```bash mkdir data mv data/ # now uncompress the zipfile cd data && unzip digit_dataset.zip cd data .. ``` ## Running the Experiments First, activate your environment via `poetry shell`. The commands below show how to run the experiments and modify some of its key hyperparameters via the cli. Each time you run an experiment, the log and results will be stored inside `outputs//