FedModelKit 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fedmodelkit-0.5.0/.gitignore +174 -0
- fedmodelkit-0.5.0/.python-version +1 -0
- fedmodelkit-0.5.0/LICENSE +23 -0
- fedmodelkit-0.5.0/PKG-INFO +283 -0
- fedmodelkit-0.5.0/README.md +263 -0
- fedmodelkit-0.5.0/examples/simulation-scikit-model/.gitignore +6 -0
- fedmodelkit-0.5.0/examples/simulation-scikit-model/AML_preprocessed_dataset.xlsx +0 -0
- fedmodelkit-0.5.0/examples/simulation-scikit-model/README.md +29 -0
- fedmodelkit-0.5.0/examples/simulation-scikit-model/simulation_example.ipynb +666 -0
- fedmodelkit-0.5.0/images/aggregator.png +0 -0
- fedmodelkit-0.5.0/images/federated_learning_model.png +0 -0
- fedmodelkit-0.5.0/images/local_learner.png +0 -0
- fedmodelkit-0.5.0/pyproject.toml +32 -0
- fedmodelkit-0.5.0/src/FedModelKit/README.md +25 -0
- fedmodelkit-0.5.0/src/FedModelKit/__init__.py +17 -0
- fedmodelkit-0.5.0/src/FedModelKit/aggregator.py +41 -0
- fedmodelkit-0.5.0/src/FedModelKit/cli.py +97 -0
- fedmodelkit-0.5.0/src/FedModelKit/default_create_functions.py +114 -0
- fedmodelkit-0.5.0/src/FedModelKit/interface.py +130 -0
- fedmodelkit-0.5.0/src/FedModelKit/local_learner.py +61 -0
- fedmodelkit-0.5.0/src/FedModelKit/py.typed +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/src/utils.py +65 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/__init__template.py +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/client_app_template.py +118 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/ds_template.ipynb +332 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/extern_pyproject_template.toml +17 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/images/doSendModels.png +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/images/doWaitsForJobs.png +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/images/dsAggregateModels.png +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/images/dsDoneSubmittingJobs.png +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/images/dsSendsJobs.png +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/images/overview.png +0 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/main_template.py +27 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/pyproject_template.toml +53 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/readme_template.md +48 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/server_app_template.py +204 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/task_template.py +140 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates/uv_template.lock +2812 -0
- fedmodelkit-0.5.0/src/FedModelKit/templates.py +76 -0
- fedmodelkit-0.5.0/uv.lock +2817 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
169
|
+
|
|
170
|
+
# PyPI configuration file
|
|
171
|
+
.pypirc
|
|
172
|
+
|
|
173
|
+
# VSCode ignore
|
|
174
|
+
.vscode/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.13
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Grupo de Aplicación de Telecomunicaciones Visuales (GATV),
|
|
4
|
+
Escuela Técnica Superior de Ingenieros de Telecomunicación, Universidad
|
|
5
|
+
Politécnica de Madrid
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: FedModelKit
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: This package contains the core components and protocols for creating, managing, and registering federated learning models using MLflow. It provides utilities for defining local learners, aggregation strategies, and integrating them with MLflow for tracking and deployment.
|
|
5
|
+
Author-email: ceresale <alessandro.ceresi@upm.es>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.13
|
|
8
|
+
Requires-Dist: flwr-datasets>=0.5.0
|
|
9
|
+
Requires-Dist: flwr[simulation]==1.17.0
|
|
10
|
+
Requires-Dist: imblearn>=0.0
|
|
11
|
+
Requires-Dist: jupyterlab>=4.4.4
|
|
12
|
+
Requires-Dist: loguru>=0.7.3
|
|
13
|
+
Requires-Dist: pandas>=2.3.0
|
|
14
|
+
Requires-Dist: scikit-learn==1.6.1
|
|
15
|
+
Requires-Dist: syft-flwr>=0.1.5
|
|
16
|
+
Requires-Dist: torch==2.7.0
|
|
17
|
+
Provides-Extra: edge
|
|
18
|
+
Requires-Dist: torch==2.7.0; extra == 'edge'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# FedModelKit
|
|
22
|
+
|
|
23
|
+
## Table of Contents
|
|
24
|
+
1. [Upload a Federated Learning Model to the Federated Platform](#upload-a-federated-learning-model-to-the-federated-platform)
|
|
25
|
+
2. [Features provided by the package](#features-provided-by-the-package)
|
|
26
|
+
3. [Directory Structure and File Descriptions](#directory-structure-and-file-descriptions)
|
|
27
|
+
4. [Prerequisites](#prerequisites)
|
|
28
|
+
5. [Installation](#installation)
|
|
29
|
+
6. [Walkthrough: How to Implement a Federated Model](#walkthrough-how-to-implement-a-federated-model)
|
|
30
|
+
- [Step 1: Define Your Local Learner](#step-1-define-your-local-learner)
|
|
31
|
+
- [Step 2: Incapsulate local learner into a function](#step-2-incapsulate-local-learner-into-a-function)
|
|
32
|
+
- [Step 3: Define Your Aggregation Strategy](#step-3-define-your-aggregation-strategy)
|
|
33
|
+
- [Step 4: Incapsulate local learner into function](#step-4-incapsulate-local-learner-into-function)
|
|
34
|
+
- [Step 5: Create the FederatedModel to include both local learner and aggregator](#step-5-create-the-federatedmodel-to-include-both-local-learner-and-aggregator)
|
|
35
|
+
- [Step 6: Submit the model to the Platform Model Catalogue](#step-6-submit-the-model-to-the-platform-model-catalogue)
|
|
36
|
+
7. [Example notebooks](#example-notebooks)
|
|
37
|
+
8. [Deployment of the package](#deployment-of-the-package)
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
# **Upload a Federated Learning Model to the Federated Platform**
|
|
42
|
+
|
|
43
|
+
<img src="images/federated_learning_model.png" alt="Federated Learning Model" width="800" height="400">
|
|
44
|
+
|
|
45
|
+
This library provides utilities for creating, managing, and registering federated learning models to the Federated Platform. It encapsulates a local learner and an aggregator into a single FederatedModel object and provides a function to register the model in a Federated Platform Model Catalogue with appropriate credentials and metadata.
|
|
46
|
+
|
|
47
|
+
The package uses the following libraries internally:
|
|
48
|
+
- `MLflow`: For model tracking and registry. More information can be found [here](https://mlflow.org/).
|
|
49
|
+
- `Flower`: For federated learning framework. More information can be found [here](https://flower.dev/).
|
|
50
|
+
The user needs do work with objects originating from these libraries to upload a Federated Model.
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## **Features provided by the package**
|
|
55
|
+
|
|
56
|
+
- **Create a custom FederatedModel**:
|
|
57
|
+
- Create a local learner, a ML model that will be executed on edge nodes with custom training, evaluation, and parameter management methods.
|
|
58
|
+
- Define and integrate your custom aggregation strategy, with a default implementation of plain averaging (DefaultAggregator) for both parameters and metrics.
|
|
59
|
+
|
|
60
|
+
- **Upload it to the Model Catalogue of the Federated Platform**:
|
|
61
|
+
- Log the FL Model and its metadata to the Federated Platform using the `submit_fl_model` method.
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## **Directory Structure and File Descriptions**
|
|
67
|
+
|
|
68
|
+
This repository contains the core components and protocols for the upload of a model to the Federated Platform. They are stored in `src/FedModelKit` directory and you can find a detailed description here [README.md](src/FedModelKit/README.md).
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Prerequisites
|
|
73
|
+
|
|
74
|
+
- `mypy`: For static type checking and ensuring compatibility with the protocols.
|
|
75
|
+
- `uv`: For developing purposes. You can install it by following the instructions [here](https://github.com/ultraviolet/uv).
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Installation
|
|
80
|
+
|
|
81
|
+
Follow the steps below to install and set up the uploading environment:
|
|
82
|
+
|
|
83
|
+
1. Create a virtual environment with python verion 3.11.* and install in it the package with the following command:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pip install --index-url https://pypi.synthema.rid-intrasoft.eu/simple FedModelKit[edge]
|
|
87
|
+
```
|
|
88
|
+
will be asked to furnish username and password that will be provided to users with access permission. (for now use the dev user to access the private PyPI server)
|
|
89
|
+
|
|
90
|
+
Here is a table listing all the packages that are installed together with the installation of the main package:
|
|
91
|
+
|
|
92
|
+
| Package | Version installed | Versions compatibles |
|
|
93
|
+
|-----------------|---------------------|------------------------|
|
|
94
|
+
| flwr | 1.9 | ==1.9 |
|
|
95
|
+
| mlflow-skinny | 2.19.0 | >=2.19.0 |
|
|
96
|
+
| pandas | 2.2.3 | >=2.2.3 |
|
|
97
|
+
| torch | 2.5.1 | >=2.5.1 |
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
2. Go to the directory from where you want to upload the model and run this command to initialize it (pay attention to files that might get overwritten):
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
msi init
|
|
105
|
+
```
|
|
106
|
+
A `src` folder will be created where the dependencies files must be stored, an `example.py` script will be created in the main directory with an example of how to upload the model and a `README.md` file with a description of how to use the package functionalities. They are provided with clear documentation on how to define your local learner and aggregation strategy, and how to log the model to the Platform Model Catalogue
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
# **Walkthrough: How to Implement a Federated Model**
|
|
112
|
+
|
|
113
|
+
### **Step 1: Define Your Local Learner**
|
|
114
|
+
|
|
115
|
+
<img src="images/local_learner.png" alt="Local Learner" width="800" height="400">
|
|
116
|
+
|
|
117
|
+
To create a custom local learner, implement your model according to the `LLProtocol`. Your model class should have the following structure:
|
|
118
|
+
|
|
119
|
+
#### **Methods**
|
|
120
|
+
- **`prepare_data(data: pd.DataFrame) -> None`**:
|
|
121
|
+
- **Purpose**: Prepares the input data for training or evaluation.
|
|
122
|
+
- **Arguments**:
|
|
123
|
+
- `data`: A pandas DataFrame containing the input data.
|
|
124
|
+
- **Returns**: None.
|
|
125
|
+
|
|
126
|
+
- **`train_round() -> flwr.common.MetricsRecord`**:
|
|
127
|
+
- **Purpose**: Performs the training process for the local learner.
|
|
128
|
+
- **Arguments**: None.
|
|
129
|
+
- **Returns**: A `flwr.common.MetricsRecord` containing metrics collected during training.
|
|
130
|
+
|
|
131
|
+
- **`get_parameters() -> flwr.common.ParametersRecord`**:
|
|
132
|
+
- **Purpose**: Retrieves the model's current parameters for aggregation.
|
|
133
|
+
- **Arguments**: None.
|
|
134
|
+
- **Returns**: A `flwr.common.ParametersRecord` representing the current model parameters.
|
|
135
|
+
|
|
136
|
+
- **`set_parameters(parameters: flwr.common.ParametersRecord) -> None`**:
|
|
137
|
+
- **Purpose**: Updates the model's parameters with the provided values.
|
|
138
|
+
- **Arguments**:
|
|
139
|
+
- `parameters`: A `flwr.common.ParametersRecord` containing the parameters to be set.
|
|
140
|
+
- **Returns**: None.
|
|
141
|
+
|
|
142
|
+
- **`evaluate() -> flwr.common.MetricsRecord`**:
|
|
143
|
+
- **Purpose**: Evaluates the model's performance on validation or test data.
|
|
144
|
+
- **Arguments**: None.
|
|
145
|
+
- **Returns**: A `flwr.common.MetricsRecord` containing metrics from the evaluation.
|
|
146
|
+
|
|
147
|
+
**NB:** Any dependency needed alongside the model must be stored inside the `src/` directory and referenced from there.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
### **Step 2: Incapsulate local learner into a function**
|
|
152
|
+
|
|
153
|
+
A function must be created according to the `LLFactoryProtocol`. The function must contain the definition of the model class and it must return an instance of the model itself. Also the function must import all the packages necessary to the local learner with the `Lazy Imports` strategy. Here is an example:
|
|
154
|
+
```python
|
|
155
|
+
# Incapsulating function
|
|
156
|
+
def create_aggregator():
|
|
157
|
+
import torch # import all the packages necessary to the local learner
|
|
158
|
+
|
|
159
|
+
# Definition of the local learner as in step 1
|
|
160
|
+
class CustomLocalLearner(nn.Module):
|
|
161
|
+
'''Local learner according LLProtocol'''
|
|
162
|
+
...
|
|
163
|
+
|
|
164
|
+
return CustomLocalLearner()
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
### **Step 3: Define Your Aggregation Strategy**
|
|
169
|
+
|
|
170
|
+
<img src="images/aggregator.png" alt="Aggregator" width="800" height="400">
|
|
171
|
+
|
|
172
|
+
To implement a custom aggregation strategy, follow the `AggProtocol`. The strategy class should have the following structure:
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
#### **Methods**
|
|
176
|
+
- **`aggregate_parameters(results: list[flwr.common.ParametersRecord], config: Optional[flwr.common.ConfigsRecord]=None) -> flwr.common.ParametersRecord`**:
|
|
177
|
+
- **Purpose**: Aggregates a list of parameter records from multiple clients into a single set of parameters.
|
|
178
|
+
- **Arguments**:
|
|
179
|
+
- `results`: A list of `flwr.common.ParametersRecord` objects, each representing the parameters from a client.
|
|
180
|
+
- **Returns**: A `flwr.common.ParametersRecord` containing the aggregated parameters.
|
|
181
|
+
|
|
182
|
+
- **`aggregate_metrics(results: list[flwr.common.MetricsRecord], config: Optional[flwr.common.ConfigsRecord]=None) -> flwr.common.MetricsRecord`**:
|
|
183
|
+
- **Purpose**: Aggregates a list of metrics records from multiple clients into a single set of metrics.
|
|
184
|
+
- **Arguments**:
|
|
185
|
+
- `results`: A list of `flwr.common.MetricsRecord` objects, each representing the metrics from a client.
|
|
186
|
+
- **Returns**: A `flwr.common.MetricsRecord` containing the aggregated metrics.
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
### **Step 4: Incapsulate local learner into function**
|
|
191
|
+
|
|
192
|
+
As for the local learner,a function must be created according to the `AggFactoryProtocol`. The function must contain the definition of the aggregator class and it must return an instance of the aggregator itself. Also the function must import all the packages necessary to the class with the `Lazy Imports` strategy. Here is an example:
|
|
193
|
+
```python
|
|
194
|
+
# Incapsulating function
|
|
195
|
+
def create_aggregator():
|
|
196
|
+
import numpy as np # import all the packages necessary to the aggregator
|
|
197
|
+
|
|
198
|
+
# Definition of the aggregator as in step 3
|
|
199
|
+
class CustomAggregator():
|
|
200
|
+
'''Aggregator according AggProtocol'''
|
|
201
|
+
...
|
|
202
|
+
|
|
203
|
+
return CustomAggregator()
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### **Step 5: Create the FederatedModel to include both local learner and aggregator**
|
|
207
|
+
The local learner and the aggregator must be included in the same FederatedModel class. The FedModelKit package provides the FederatedModelclass which recieves as arguments the function creating the local learner and the function creating the aggregator with their respective names. If no aggregation strategy is provided, the model will by default use a plain averaging strategy for both parameters and metrics, and if also the local learner is not provided the model will get a default local learner which is shown in `example.py`. Here is an example of how to set up the FederatedModel:
|
|
208
|
+
```python
|
|
209
|
+
from FedModelKit import FederatedModel
|
|
210
|
+
|
|
211
|
+
# Define your local learner and aggregator
|
|
212
|
+
def create_local_learner():
|
|
213
|
+
'''Create local learner according to LLProtocol'''
|
|
214
|
+
return CustomLocalLearner()
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def create_aggregator():
|
|
218
|
+
'''Create aggregator according to AggProtocol'''
|
|
219
|
+
return CustomAggregator()
|
|
220
|
+
|
|
221
|
+
# Create the FederatedModel
|
|
222
|
+
federated_model = FederatedModel(create_local_learner=create_local_learner,
|
|
223
|
+
model_name="your_model_name",
|
|
224
|
+
create_saggregator=create_aggregator,
|
|
225
|
+
aggregator_name="your_aggregator_name")
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**NB:** Make sure your model and aggregation strategy are compatible with static type checking tools like MyPy. This will help catch any issues related to the implementation of the protocols.
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
### **Step 6: Submit the model to the Platform Model Catalogue**
|
|
232
|
+
Upload the model created with the submit_fl_model function provided by the package. To successfully upload the model the user must provide to the function the platform url to which upload the model, a valid set of username and password, the name of the experiment (if it doesn't exist already a new experiment is created with that name) and some tags related to the model the user is uploading. Here is an example:
|
|
233
|
+
|
|
234
|
+
```python
|
|
235
|
+
from FedModelKit import submit_fl_model
|
|
236
|
+
|
|
237
|
+
# Submit the FederatedModel to the Platform
|
|
238
|
+
submit_fl_model(federated_model,
|
|
239
|
+
platform_url="platform_model_registry_url"
|
|
240
|
+
username="your_username",
|
|
241
|
+
password="your_password",
|
|
242
|
+
experiment_name="your_experiment_name",
|
|
243
|
+
disease="your_disease", # The use case the model is used for ("AML" or "SCD")
|
|
244
|
+
trained=False) # Whether the local learner is trained or not
|
|
245
|
+
```
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
# Example notebooks
|
|
249
|
+
The example notebooks in the `examples` directory demonstrate how to simulate a federated learning model using the functionalities provided by this package. These notebooks serve as a guide to:
|
|
250
|
+
|
|
251
|
+
- Perform simulations with an example model or customize the setup to use your own model.
|
|
252
|
+
- Load the model into the Federated Platform Model Catalogue, showcasing the process of registering a federated learning model with appropriate metadata and credentials.
|
|
253
|
+
|
|
254
|
+
Each notebook is designed to provide step-by-step instructions, making it easier for users to understand and implement the package's features in their own projects.
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
# Deployment of the package
|
|
260
|
+
|
|
261
|
+
To deploy and make changes to the package functionalities, you need to have `uv` installed. You can install it by following the instructions [here](https://github.com/ultraviolet/uv).
|
|
262
|
+
|
|
263
|
+
Once you have `uv` installed, follow these steps:
|
|
264
|
+
|
|
265
|
+
1. Clone the repository to your local machine:
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
git clone https://github.com/synthema-project/app-model_store-interface.git
|
|
269
|
+
cd FedModelKit
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
2. Sync the repository using `uv sync`:
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
uv sync
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
This command will synchronize your local repository with the remote repository, allowing you to make changes to the package functionalities. After making your changes, you can propose them by creating a pull request on the GitHub repository.
|
|
279
|
+
|
|
280
|
+
By using `uv sync`, you can ensure that your local changes are in sync with the latest version of the repository, making it easier to collaborate with other developers and contribute to the project.
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|