{"id":25319487,"url":"https://github.com/prawater/federated_learning","last_synced_at":"2025-04-07T18:57:00.345Z","repository":{"id":274668130,"uuid":"923355288","full_name":"PraWater/Federated_Learning","owner":"PraWater","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-07T15:29:51.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-07T16:30:55.079Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PraWater.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-28T04:47:43.000Z","updated_at":"2025-02-07T15:29:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"c8d26c19-576a-487d-b20b-158a9241425a","html_url":"https://github.com/PraWater/Federated_Learning","commit_stats":null,"previous_names":["prawater/federated_learning"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PraWater%2FFederated_Learning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PraWater%2FFederated_Learning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PraWater%2FFederated_Learning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PraWater%2FFederated_Learning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PraWater","download_url":"https://codeload.github.com/PraWater/Federated_Learning/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247713310,"owners_count":20983683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-13T20:40:40.832Z","updated_at":"2025-04-07T18:57:00.119Z","avatar_url":"https://github.com/PraWater.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: Leveraging Flower and Docker for Device Heterogeneity Management in FL\ntags: [deployment, vision, tutorial]\ndataset: [CIFAR-10]\nframework: [Docker, tensorflow]\n---\n\n# Leveraging Flower and Docker for Device Heterogeneity Management in Federated Learning\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://flower.ai/_next/image/?url=%2F_next%2Fstatic%2Fmedia%2Fflower_white_border.c2012e70.png\u0026w=640\u0026q=75\" width=\"140px\" alt=\"Flower Website\" /\u003e\n  \u003cimg src=\"https://github.com/ChoosyDevs/Choosy/assets/59146613/73d15990-453b-4da6-b8d6-df0f956a127c\" width=\"140px\" alt=\"Docker Logo\" /\u003e\n\u003c/p\u003e\n\n## Introduction\n\nIn this example, we tackle device heterogeneity in federated learning, arising from differences in memory and CPU capabilities across devices. This diversity affects training efficiency and inclusivity. Our strategy includes simulating this heterogeneity by setting CPU and memory limits in a Docker setup, using a custom Docker compose generator script. This approach creates a varied training environment and enables us to develop strategies to manage these disparities effectively.\n\n## Handling Device Heterogeneity\n\n1. **System Metrics Access**:\n\n   - Effective management of device heterogeneity begins with monitoring system metrics of each container. We integrate the following services to achieve this:\n     - **Cadvisor**: Collects comprehensive metrics from each Docker container.\n     - **Prometheus**: Using `prometheus.yaml` for configuration, it scrapes data from Cadvisor at scheduled intervals, serving as a robust time-series database. Users can access the Prometheus UI at `http://localhost:9090` to create and run queries using PromQL, allowing for detailed insight into container performance.\n\n2. **Mitigating Heterogeneity**:\n\n   - In this basic use case, we address device heterogeneity by establishing rules tailored to each container's system capabilities. This involves modifying training parameters, such as batch sizes and learning rates, based on each device's memory capacity and CPU availability. These settings are specified in the `client_configs` array in the `create_docker_compose` script. For example:\n\n     ```python\n     client_configs = [\n           {\"mem_limit\": \"3g\", \"batch_size\": 32, \"cpus\": 4, \"learning_rate\": 0.001},\n           {\"mem_limit\": \"6g\", \"batch_size\": 256, \"cpus\": 1, \"learning_rate\": 0.05},\n           {\"mem_limit\": \"4g\", \"batch_size\": 64, \"cpus\": 3, \"learning_rate\": 0.02},\n           {\"mem_limit\": \"5g\", \"batch_size\": 128, \"cpus\": 2.5, \"learning_rate\": 0.09},\n     ]\n     ```\n\n## Prerequisites\n\nDocker must be installed and the Docker daemon running on your server. If you don't already have Docker installed, you can get [installation instructions for your specific Linux distribution or macOS from Docker](https://docs.docker.com/engine/install/). Besides Docker, the only extra requirement is having Python installed. You don't need to create a new environment for this example since all dependencies will be installed inside Docker containers automatically.\n\n## Running the Example\n\nRunning this example is easy. For a more detailed step-by-step guide, including more useful material, refer to the detailed guide in the following section.\n\n```bash\n\n# Generate docker compose file\npython helpers/generate_docker_compose.py # by default will configure to use 2 clients for 100 rounds\n\n# Build docker images\ndocker-compose build\n\n# Launch everything\ndocker-compose up\n```\n\nOn your favourite browser, go to `http://localhost:3000` to see the Graphana dashboard showing system-level and application-level metrics.\n\nTo stop all containers, open a new terminal and `cd` into this directory, then run `docker-compose down`. Alternatively, you can do `ctrl+c` on the same terminal and then run `docker-compose down` to ensure everything is terminated.\n\n## Running the Example (detailed)\n\n### Step 1: Configure Docker Compose\n\nExecute the following command to run the `helpers/generate_docker_compose.py` script. This script creates the docker-compose configuration needed to set up the environment.\n\n```bash\npython helpers/generate_docker_compose.py\n```\n\nWithin the script, specify the number of clients (`total_clients`) and resource limitations for each client in the `client_configs` array. You can adjust the number of rounds by passing `--num_rounds` to the above command.\n\n### Step 2: Build and Launch Containers\n\n1. **Execute Initialization Script**:\n\n   - To build the Docker images and start the containers, use the following command:\n\n     ```bash\n     # this is the only command you need to execute to run the entire example\n     docker-compose up\n     ```\n\n   - If you make any changes to the Dockerfile or other configuration files, you should rebuild the images to reflect these changes. This can be done by adding the `--build` flag to the command:\n\n     ```bash\n     docker-compose up --build\n     ```\n\n   - The `--build` flag instructs Docker Compose to rebuild the images before starting the containers, ensuring that any code or configuration changes are included.\n\n   - To stop all services, you have two options:\n\n     - Run `docker-compose down` in another terminal if you are in the same directory. This command will stop and remove the containers, networks, and volumes created by `docker-compose up`.\n     - Press `Ctrl+C` once in the terminal where `docker-compose up` is running. This will stop the containers but won't remove them or the networks and volumes they use.\n\n2. **Services Startup**:\n\n   - Several services will automatically launch as defined in your `docker-compose.yml` file:\n\n     - **Monitoring Services**: Prometheus for metrics collection, Cadvisor for container monitoring, and Grafana for data visualization.\n     - **Flower Federated Learning Environment**: The Flower server and client containers are initialized and start running.\n\n   - After launching the services, verify that all Docker containers are running correctly by executing the `docker ps` command. Here's an example output:\n\n     ```bash\n     ➜  ~ docker ps\n     CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS                 PORTS                                                              NAMES\n     9f05820eba45   flower-via-docker-compose-client2   \"python client.py --…\"   50 seconds ago   Up 48 seconds   0.0.0.0:6002-\u003e6002/tcp                                                   client2\n     a0333715d504   flower-via-docker-compose-client1   \"python client.py --…\"   50 seconds ago   Up 48 seconds   0.0.0.0:6001-\u003e6001/tcp                                                   client1\n     0da2bf735965   flower-via-docker-compose-server    \"python server.py --…\"   50 seconds ago   Up 48 seconds   0.0.0.0:6000-\u003e6000/tcp, 0.0.0.0:8000-\u003e8000/tcp, 0.0.0.0:8265-\u003e8265/tcp   server\n     c57ef50657ae   grafana/grafana:latest              \"/run.sh --config=/e…\"   50 seconds ago   Up 49 seconds   0.0.0.0:3000-\u003e3000/tcp                                                   grafana\n     4f274c2083dc   prom/prometheus:latest              \"/bin/prometheus --c…\"   50 seconds ago   Up 49 seconds   0.0.0.0:9090-\u003e9090/tcp                                                   prometheus\n     e9f4c9644a1c   gcr.io/cadvisor/cadvisor:v0.47.0    \"/usr/bin/cadvisor -…\"   50 seconds ago   Up 49 seconds   0.0.0.0:8080-\u003e8080/tcp                                                   cadvisor\n     ```\n\n   - To monitor the resource utilization of your containers in real-time and see the limits imposed in the Docker Compose file, you can use the `docker stats` command. This command provides a live stream of container CPU, memory, and network usage statistics.\n\n     ```bash\n     ➜  ~ docker stats\n     CONTAINER ID   NAME         CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS\n     9f05820eba45   client2      104.44%   1.968GiB / 6GiB       32.80%    148MB / 3.22MB    0B / 284MB        82\n     a0333715d504   client1      184.69%   1.498GiB / 3GiB       49.92%    149MB / 2.81MB    1.37MB / 284MB    82\n     0da2bf735965   server       0.12%     218.5MiB / 15.61GiB   1.37%     1.47MB / 2.89MB   2.56MB / 2.81MB   45\n     c57ef50657ae   grafana      0.24%     96.19MiB / 400MiB     24.05%    18.9kB / 3.79kB   77.8kB / 152kB    20\n     4f274c2083dc   prometheus   1.14%     52.73MiB / 500MiB     10.55%    6.79MB / 211kB    1.02MB / 1.31MB   15\n     e9f4c9644a1c   cadvisor     7.31%     32.14MiB / 500MiB     6.43%     139kB / 6.66MB    500kB / 0B        18\n     ```\n\n3. **Automated Grafana Configuration**:\n\n   - Grafana is configured to load pre-defined data sources and dashboards for immediate monitoring, facilitated by provisioning files. The provisioning files include `prometheus-datasource.yml` for data sources, located in the `./config/provisioning/datasources` directory, and `dashboard_index.json` for dashboards, in the `./config/provisioning/dashboards` directory. The `grafana.ini` file is also tailored to enhance user experience:\n     - **Admin Credentials**: We provide default admin credentials in the `grafana.ini` configuration, which simplifies access by eliminating the need for users to go through the initial login process.\n     - **Default Dashboard Path**: A default dashboard path is set in `grafana.ini` to ensure that the dashboard with all the necessary panels is rendered when Grafana is accessed.\n\n   These files and settings are directly mounted into the Grafana container via Docker Compose volume mappings. This setup guarantees that upon startup, Grafana is pre-configured for monitoring, requiring no additional manual setup.\n\n4. **Begin Training Process**:\n\n   - The federated learning training automatically begins once all client containers are successfully connected to the Flower server. This synchronizes the learning process across all participating clients.\n\nBy following these steps, you will have a fully functional federated learning environment with device heterogeneity and monitoring capabilities.\n\n## Model Training and Dataset Integration\n\n### Data Pipeline with FLWR-Datasets\n\nWe have integrated [`flwr-datasets`](https://flower.ai/docs/datasets/) into our data pipeline, which is managed within the `load_data.py` file in the `helpers/` directory. This script facilitates standardized access to datasets across the federated network and incorporates a `data_sampling_percentage` argument. This argument allows users to specify the percentage of the dataset to be used for training and evaluation, accommodating devices with lower memory capabilities to prevent Out-of-Memory (OOM) errors.\n\n### Model Selection and Dataset\n\nFor the federated learning system, we have selected the MobileNet model due to its efficiency in image classification tasks. The model is trained and evaluated on the CIFAR-10 dataset. The combination of MobileNet and CIFAR-10 is ideal for demonstrating the capabilities of our federated learning solution in a heterogeneous device environment.\n\n- **MobileNet**: A streamlined architecture for mobile and embedded devices that balances performance and computational cost.\n- **CIFAR-10 Dataset**: A standard benchmark dataset for image classification, containing various object classes that pose a comprehensive challenge for the learning model.\n\nBy integrating these components, our framework is well-prepared to handle the intricacies of training over a distributed network with varying device capabilities and data availability.\n\n## Visualizing with Grafana\n\n### Access Grafana Dashboard\n\nVisit `http://localhost:3000` to enter Grafana. The automated setup ensures that you're greeted with a series of pre-configured dashboards, including the default screen with a comprehensive set of graphs. These dashboards are ready for immediate monitoring and can be customized to suit your specific requirements.\n\n### Dashboard Configuration\n\nThe `dashboard_index.json` file, located in the `./config/provisioning/dashboards` directory, serves as the backbone of our Grafana dashboard's configuration. It defines the structure and settings of the dashboard panels, which are rendered when you access Grafana. This JSON file contains the specifications for various panels such as model accuracy, CPU usage, memory utilization, and network traffic. Each panel's configuration includes the data source, queries, visualization type, and other display settings like thresholds and colors.\n\nFor instance, in our project setup, the `dashboard_index.json` configures a panel to display the model's accuracy over time using a time-series graph, and another panel to show the CPU usage across clients using a graph that plots data points as they are received. This file is fundamental for creating a customized and informative dashboard that provides a snapshot of the federated learning system's health and performance metrics.\n\nBy modifying the `dashboard_index.json` file, users can tailor the Grafana dashboard to include additional metrics or change the appearance and behavior of existing panels to better fit their monitoring requirements.\n\n### Grafana Default Dashboard\n\nBelow is the default Grafana dashboard that users will see upon accessing Grafana:\n\n\u003cimg width=\"1440\" alt=\"grafana_home_screen\" src=\"https://github.com/ChoosyDevs/Choosy/assets/59146613/46c1016d-2376-4fdc-ae5f-68c550fc8e46\"\u003e\n\nThis comprehensive dashboard provides insights into various system metrics across client-server containers. It includes visualizations such as:\n\n- **Application Metrics**: The \"Model Accuracy\" graph shows an upward trend as rounds of training progress, which is a positive indicator of the model learning and improving over time. Conversely, the \"Model Loss\" graph trends downward, suggesting that the model is becoming more precise and making fewer mistakes as it trains.\n\n- **CPU Usage**: The sharp spikes in the red graph, representing \"client1\", indicate peak CPU usage, which is considerably higher than that of \"client2\" (blue graph). This difference is due to \"client1\" being allocated more computing resources (up to 4 CPU cores) compared to \"client2\", which is limited to just 1 CPU core, hence the more subdued CPU usage pattern.\n\n- **Memory Utilization**: Both clients are allocated a similar amount of memory, reflected in the nearly same lines for memory usage. This uniform allocation allows for a straightforward comparison of how each client manages memory under similar conditions.\n\n- **Network Traffic**: Monitor incoming and outgoing network traffic to each client, which is crucial for understanding data exchange volumes during federated learning cycles.\n\nTogether, these metrics paint a detailed picture of the federated learning operation, showcasing resource usage and model performance. Such insights are invaluable for system optimization, ensuring balanced load distribution and efficient model training.\n\n## Comprehensive Monitoring System Integration\n\n### Capturing Container Metrics with cAdvisor\n\ncAdvisor is seamlessly integrated into our monitoring setup to capture a variety of system and container metrics, such as CPU, memory, and network usage. These metrics are vital for analyzing the performance and resource consumption of the containers in the federated learning environment.\n\n### Custom Metrics: Setup and Monitoring via Prometheus\n\nIn addition to the standard metrics captured by cAdvisor, we have implemented a process to track custom metrics like model's accuracy and loss within Grafana, using Prometheus as the backbone for metric collection.\n\n1. **Prometheus Client Installation**:\n\n   - We began by installing the `prometheus_client` library in our Python environment, enabling us to define and expose custom metrics that Prometheus can scrape.\n\n2. **Defining Metrics in Server Script**:\n\n   - Within our `server.py` script, we have established two key Prometheus Gauge metrics, specifically tailored for monitoring our federated learning model: `model_accuracy` and `model_loss`. These custom gauges are instrumental in capturing the most recent values of the model's accuracy and loss, which are essential metrics for evaluating the model's performance. The gauges are defined as follows:\n\n     ```python\n     from prometheus_client import Gauge\n\n     accuracy_gauge = Gauge('model_accuracy', 'Current accuracy of the global model')\n     loss_gauge = Gauge('model_loss', 'Current loss of the global model')\n     ```\n\n3. **Exposing Metrics via HTTP Endpoint**:\n\n   - We leveraged the `start_http_server` function from the `prometheus_client` library to launch an HTTP server on port 8000. This server provides the `/metrics` endpoint, where the custom metrics are accessible for Prometheus scraping. The function is called at the end of the `main` method in `server.py`:\n\n     ```python\n     start_http_server(8000)\n     ```\n\n4. **Updating Metrics Recording Strategy**:\n\n   - The core of our metrics tracking lies in the `strategy.py` file, particularly within the `aggregate_evaluate` method. This method is crucial as it's where the federated learning model's accuracy and loss values are computed after each round of training with the aggregated data from all clients.\n\n     ```python\n        self.accuracy_gauge.set(accuracy_aggregated)\n        self.loss_gauge.set(loss_aggregated)\n     ```\n\n5. **Configuring Prometheus Scraping**:\n\n   - In the `prometheus.yml` file, under `scrape_configs`, we configured a new job to scrape the custom metrics from the HTTP server. This setup includes the job's name, the scraping interval, and the target server's URL.\n\n### Visualizing the Monitoring Architecture\n\nThe image below depicts the Prometheus scraping process as it is configured in our monitoring setup. Within this architecture:\n\n- The \"Prometheus server\" is the central component that retrieves and stores metrics.\n- \"cAdvisor\" and the \"HTTP server\" we set up to expose our custom metrics are represented as \"Prometheus targets\" in the diagram. cAdvisor captures container metrics, while the HTTP server serves our custom `model_accuracy` and `model_loss` metrics at the `/metrics` endpoint.\n- These targets are periodically scraped by the Prometheus server, aggregating data from both system-level and custom performance metrics.\n- The aggregated data is then made available to the \"Prometheus web UI\" and \"Grafana,\" as shown, enabling detailed visualization and analysis through the Grafana dashboard.\n\n\u003cimg width=\"791\" alt=\"prometheus-architecture\" src=\"https://github.com/ChoosyDevs/Choosy/assets/59146613/3b915e04-f12c-4aef-99ff-d75853234728\"\u003e\n\nBy incorporating these steps, we have enriched our monitoring capabilities to not only include system-level metrics but also critical performance indicators of our federated learning model. This approach is pivotal for understanding and improving the learning process. Similarly, you can apply this methodology to track any other metric that you find interesting or relevant to your specific needs. This flexibility allows for a comprehensive and customized monitoring environment, tailored to the unique aspects and requirements of your federated learning system.\n\n## Additional Resources\n\n- **Grafana Tutorials**: Explore a variety of tutorials on Grafana at [Grafana Tutorials](https://grafana.com/tutorials/).\n- **Prometheus Overview**: Learn more about Prometheus at their [official documentation](https://prometheus.io/docs/introduction/overview/).\n- **cAdvisor Guide**: For information on monitoring Docker containers with cAdvisor, see this [Prometheus guide](https://prometheus.io/docs/guides/cadvisor/).\n\n## Conclusion\n\nThis project serves as a foundational example of managing device heterogeneity within the federated learning context, employing the Flower framework alongside Docker, Prometheus, and Grafana. It's designed to be a starting point for users to explore and further adapt to the complexities of device heterogeneity in federated learning environments.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprawater%2Ffederated_learning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprawater%2Ffederated_learning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprawater%2Ffederated_learning/lists"}