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