MEDfl 0.2.1__py3-none-any.whl → 2.0.1__py3-none-any.whl
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.
- MEDfl/LearningManager/__init__.py +13 -13
- MEDfl/LearningManager/client.py +150 -181
- MEDfl/LearningManager/dynamicModal.py +287 -287
- MEDfl/LearningManager/federated_dataset.py +60 -60
- MEDfl/LearningManager/flpipeline.py +192 -192
- MEDfl/LearningManager/model.py +223 -223
- MEDfl/LearningManager/params.yaml +14 -14
- MEDfl/LearningManager/params_optimiser.py +442 -442
- MEDfl/LearningManager/plot.py +229 -229
- MEDfl/LearningManager/server.py +181 -189
- MEDfl/LearningManager/strategy.py +82 -138
- MEDfl/LearningManager/utils.py +331 -331
- MEDfl/NetManager/__init__.py +10 -10
- MEDfl/NetManager/database_connector.py +43 -43
- MEDfl/NetManager/dataset.py +92 -92
- MEDfl/NetManager/flsetup.py +320 -320
- MEDfl/NetManager/net_helper.py +254 -254
- MEDfl/NetManager/net_manager_queries.py +142 -142
- MEDfl/NetManager/network.py +194 -194
- MEDfl/NetManager/node.py +184 -184
- MEDfl/__init__.py +4 -3
- MEDfl/scripts/__init__.py +1 -1
- MEDfl/scripts/base.py +29 -29
- MEDfl/scripts/create_db.py +126 -126
- Medfl/LearningManager/__init__.py +13 -0
- Medfl/LearningManager/client.py +150 -0
- Medfl/LearningManager/dynamicModal.py +287 -0
- Medfl/LearningManager/federated_dataset.py +60 -0
- Medfl/LearningManager/flpipeline.py +192 -0
- Medfl/LearningManager/model.py +223 -0
- Medfl/LearningManager/params.yaml +14 -0
- Medfl/LearningManager/params_optimiser.py +442 -0
- Medfl/LearningManager/plot.py +229 -0
- Medfl/LearningManager/server.py +181 -0
- Medfl/LearningManager/strategy.py +82 -0
- Medfl/LearningManager/utils.py +331 -0
- Medfl/NetManager/__init__.py +10 -0
- Medfl/NetManager/database_connector.py +43 -0
- Medfl/NetManager/dataset.py +92 -0
- Medfl/NetManager/flsetup.py +320 -0
- Medfl/NetManager/net_helper.py +254 -0
- Medfl/NetManager/net_manager_queries.py +142 -0
- Medfl/NetManager/network.py +194 -0
- Medfl/NetManager/node.py +184 -0
- Medfl/__init__.py +3 -0
- Medfl/scripts/__init__.py +2 -0
- Medfl/scripts/base.py +30 -0
- Medfl/scripts/create_db.py +126 -0
- alembic/env.py +61 -61
- {MEDfl-0.2.1.dist-info → medfl-2.0.1.dist-info}/METADATA +120 -108
- medfl-2.0.1.dist-info/RECORD +55 -0
- {MEDfl-0.2.1.dist-info → medfl-2.0.1.dist-info}/WHEEL +1 -1
- {MEDfl-0.2.1.dist-info → medfl-2.0.1.dist-info/licenses}/LICENSE +674 -674
- MEDfl-0.2.1.dist-info/RECORD +0 -31
- {MEDfl-0.2.1.dist-info → medfl-2.0.1.dist-info}/top_level.txt +0 -0
@@ -1,13 +1,13 @@
|
|
1
|
-
# MEDfl/LearningManager/__init__.py
|
2
|
-
|
3
|
-
# Import modules from this package
|
4
|
-
# from .client import *
|
5
|
-
# from .dynamicModal import *
|
6
|
-
# from .flpipeline import *
|
7
|
-
# from .federated_dataset import *
|
8
|
-
# from .model import *
|
9
|
-
# from .params_optimiser import *
|
10
|
-
# from .plot import *
|
11
|
-
# from .server import *
|
12
|
-
# from .strategy import *
|
13
|
-
# from .utils import *
|
1
|
+
# MEDfl/LearningManager/__init__.py
|
2
|
+
|
3
|
+
# Import modules from this package
|
4
|
+
# from .client import *
|
5
|
+
# from .dynamicModal import *
|
6
|
+
# from .flpipeline import *
|
7
|
+
# from .federated_dataset import *
|
8
|
+
# from .model import *
|
9
|
+
# from .params_optimiser import *
|
10
|
+
# from .plot import *
|
11
|
+
# from .server import *
|
12
|
+
# from .strategy import *
|
13
|
+
# from .utils import *
|
MEDfl/LearningManager/client.py
CHANGED
@@ -1,181 +1,150 @@
|
|
1
|
-
#!/usr/bin/env python3
|
2
|
-
import flwr as fl
|
3
|
-
from opacus import PrivacyEngine
|
4
|
-
from torch.utils.data import DataLoader
|
5
|
-
|
6
|
-
from .model import Model
|
7
|
-
from .utils import params
|
8
|
-
import torch
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
class
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
self.
|
35
|
-
self.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
self.
|
53
|
-
self.
|
54
|
-
self.
|
55
|
-
self.
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
)
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
""
|
112
|
-
|
113
|
-
print('\n -------------------------------- \n
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
self.
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
self.
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
self.
|
151
|
-
self.accuracies.append(accuracy)
|
152
|
-
|
153
|
-
print(f"[ ============== From evaluate ==== Client {self.cid}] fit, config: {config}")
|
154
|
-
|
155
|
-
|
156
|
-
# if('study' in config):
|
157
|
-
# if 0 < config['server_round'] <= config['HPO_factor']*config['server_rounds'] and (config['server_round'] -1) %(config['HPO_RATE'] )==0:
|
158
|
-
# print("==================== this is th optimisations info ===================")
|
159
|
-
# print(auc)
|
160
|
-
# print(config['trail'])
|
161
|
-
# print('---------')
|
162
|
-
# print(config['study'].trials[0].state)
|
163
|
-
# try:
|
164
|
-
# # Call tell() method to report the result
|
165
|
-
# config['study'].tell(config['trail'], auc)
|
166
|
-
|
167
|
-
# # Fetch the updated trial from the study
|
168
|
-
# updated_trial = config['trail']
|
169
|
-
|
170
|
-
# # Check the state of the trial
|
171
|
-
# if updated_trial.state == optuna.trial.TrialState.COMPLETE:
|
172
|
-
# print(f"Trial {updated_trial.number} completed successfully with value {updated_trial.value}")
|
173
|
-
# else:
|
174
|
-
# print(f"Trial {updated_trial.number} is in state {updated_trial.state}")
|
175
|
-
|
176
|
-
# except Exception as e:
|
177
|
-
# # Handle and log any errors
|
178
|
-
# print(f"Error during tell(): {e}")
|
179
|
-
|
180
|
-
|
181
|
-
return float(loss), len(self.valloader), {"accuracy": float(accuracy)}
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
import flwr as fl
|
3
|
+
from opacus import PrivacyEngine
|
4
|
+
from torch.utils.data import DataLoader
|
5
|
+
|
6
|
+
from .model import Model
|
7
|
+
from .utils import params
|
8
|
+
import torch
|
9
|
+
|
10
|
+
class FlowerClient(fl.client.NumPyClient):
|
11
|
+
"""
|
12
|
+
FlowerClient class for creating MEDfl clients.
|
13
|
+
|
14
|
+
Attributes:
|
15
|
+
cid (str): Client ID.
|
16
|
+
local_model (Model): Local model of the federated learning network.
|
17
|
+
trainloader (DataLoader): DataLoader for training data.
|
18
|
+
valloader (DataLoader): DataLoader for validation data.
|
19
|
+
diff_priv (bool): Flag indicating whether to use differential privacy.
|
20
|
+
"""
|
21
|
+
def __init__(self, cid: str, local_model: Model, trainloader: DataLoader, valloader: DataLoader, diff_priv: bool = params["diff_privacy"]):
|
22
|
+
"""
|
23
|
+
Initializes the FlowerClient instance.
|
24
|
+
|
25
|
+
Args:
|
26
|
+
cid (str): Client ID.
|
27
|
+
local_model (Model): Local model of the federated learning network.
|
28
|
+
trainloader (DataLoader): DataLoader for training data.
|
29
|
+
valloader (DataLoader): DataLoader for validation data.
|
30
|
+
diff_priv (bool): Flag indicating whether to use differential privacy.
|
31
|
+
"""
|
32
|
+
self.cid = cid
|
33
|
+
self.local_model = local_model
|
34
|
+
self.trainloader = trainloader
|
35
|
+
self.valloader = valloader
|
36
|
+
if torch.cuda.is_available():
|
37
|
+
num_cuda_devices = torch.cuda.device_count()
|
38
|
+
if num_cuda_devices > 0:
|
39
|
+
device_idx = int(self.cid) % num_cuda_devices
|
40
|
+
self.device = torch.device(f"cuda:{device_idx}")
|
41
|
+
self.local_model.model.to(self.device)
|
42
|
+
else:
|
43
|
+
# Handle case where CUDA is available but no CUDA devices are found
|
44
|
+
raise RuntimeError("CUDA is available, but no CUDA devices are found.")
|
45
|
+
else:
|
46
|
+
# Handle case where CUDA is not available
|
47
|
+
self.device = torch.device("cpu")
|
48
|
+
self.local_model.model.to(self.device)
|
49
|
+
|
50
|
+
self.privacy_engine = PrivacyEngine(secure_mode=False)
|
51
|
+
self.diff_priv = diff_priv
|
52
|
+
self.epsilons = []
|
53
|
+
self.accuracies = []
|
54
|
+
self.losses = []
|
55
|
+
if self.diff_priv:
|
56
|
+
model, optimizer, self.trainloader = self.privacy_engine.make_private_with_epsilon(
|
57
|
+
module=self.local_model.model.train(),
|
58
|
+
optimizer=self.local_model.optimizer,
|
59
|
+
data_loader=self.trainloader,
|
60
|
+
epochs=params["train_epochs"],
|
61
|
+
target_epsilon=float(params["EPSILON"]),
|
62
|
+
target_delta= float(params["DELTA"]),
|
63
|
+
max_grad_norm=params["MAX_GRAD_NORM"],
|
64
|
+
)
|
65
|
+
setattr(self.local_model, "model", model)
|
66
|
+
setattr(self.local_model, "optimizer", optimizer)
|
67
|
+
self.validate()
|
68
|
+
|
69
|
+
def validate(self):
|
70
|
+
"""Validates cid, local_model, trainloader, valloader."""
|
71
|
+
if not isinstance(self.cid, str):
|
72
|
+
raise TypeError("cid argument must be a string")
|
73
|
+
|
74
|
+
if not isinstance(self.local_model, Model):
|
75
|
+
raise TypeError("local_model argument must be a MEDfl.LearningManager.model.Model")
|
76
|
+
|
77
|
+
if not isinstance(self.trainloader, DataLoader):
|
78
|
+
raise TypeError("trainloader argument must be a torch.utils.data.dataloader")
|
79
|
+
|
80
|
+
if not isinstance(self.valloader, DataLoader):
|
81
|
+
raise TypeError("valloader argument must be a torch.utils.data.dataloader")
|
82
|
+
|
83
|
+
if not isinstance(self.diff_priv, bool):
|
84
|
+
raise TypeError("diff_priv argument must be a bool")
|
85
|
+
|
86
|
+
def get_parameters(self, config):
|
87
|
+
"""
|
88
|
+
Returns the current parameters of the local model.
|
89
|
+
|
90
|
+
Args:
|
91
|
+
config: Configuration information.
|
92
|
+
|
93
|
+
Returns:
|
94
|
+
Numpy array: Parameters of the local model.
|
95
|
+
"""
|
96
|
+
print(f"[Client {self.cid}] get_parameters")
|
97
|
+
return self.local_model.get_parameters()
|
98
|
+
|
99
|
+
def fit(self, parameters, config):
|
100
|
+
"""
|
101
|
+
Fits the local model to the received parameters using federated learning.
|
102
|
+
|
103
|
+
Args:
|
104
|
+
parameters: Parameters received from the server.
|
105
|
+
config: Configuration information.
|
106
|
+
|
107
|
+
Returns:
|
108
|
+
Tuple: Parameters of the local model, number of training examples, and privacy information.
|
109
|
+
"""
|
110
|
+
print('\n -------------------------------- \n this is the config of the client')
|
111
|
+
print(f"[Client {self.cid}] fit, config: {config}")
|
112
|
+
# print(config['epochs'])
|
113
|
+
print('\n -------------------------------- \n ')
|
114
|
+
self.local_model.set_parameters(parameters)
|
115
|
+
for _ in range(params["train_epochs"]):
|
116
|
+
epsilon = self.local_model.train(
|
117
|
+
self.trainloader,
|
118
|
+
epoch=_,
|
119
|
+
device=self.device,
|
120
|
+
privacy_engine=self.privacy_engine,
|
121
|
+
diff_priv=self.diff_priv,
|
122
|
+
)
|
123
|
+
self.epsilons.append(epsilon)
|
124
|
+
print(f"epsilon of client {self.cid} : eps = {epsilon}")
|
125
|
+
return (
|
126
|
+
self.local_model.get_parameters(),
|
127
|
+
len(self.trainloader),
|
128
|
+
{"epsilon": epsilon},
|
129
|
+
)
|
130
|
+
|
131
|
+
def evaluate(self, parameters, config):
|
132
|
+
"""
|
133
|
+
Evaluates the local model on the validation data and returns the loss and accuracy.
|
134
|
+
|
135
|
+
Args:
|
136
|
+
parameters: Parameters received from the server.
|
137
|
+
config: Configuration information.
|
138
|
+
|
139
|
+
Returns:
|
140
|
+
Tuple: Loss, number of validation examples, and accuracy information.
|
141
|
+
"""
|
142
|
+
print(f"[Client {self.cid}] evaluate, config: {config}")
|
143
|
+
self.local_model.set_parameters(parameters)
|
144
|
+
loss, accuracy , auc = self.local_model.evaluate(
|
145
|
+
self.valloader, device=self.device
|
146
|
+
)
|
147
|
+
self.losses.append(loss)
|
148
|
+
self.accuracies.append(accuracy)
|
149
|
+
|
150
|
+
return float(loss), len(self.valloader), {"accuracy": float(accuracy)}
|