MEDfl 2.0.4.dev1__py3-none-any.whl → 2.0.4.dev3__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/rw/client.py +98 -29
- MEDfl/rw/model.py +28 -0
- MEDfl/rw/server.py +71 -18
- MEDfl/rw/strategy.py +72 -78
- {MEDfl-2.0.4.dev1.dist-info → MEDfl-2.0.4.dev3.dist-info}/METADATA +1 -1
- MEDfl-2.0.4.dev3.dist-info/RECORD +36 -0
- MEDfl/rw/rwConfig.py +0 -21
- MEDfl/rw/verbose_server.py +0 -21
- MEDfl-2.0.4.dev1.dist-info/RECORD +0 -62
- Medfl/LearningManager/__init__.py +0 -13
- Medfl/LearningManager/client.py +0 -150
- Medfl/LearningManager/dynamicModal.py +0 -287
- Medfl/LearningManager/federated_dataset.py +0 -60
- Medfl/LearningManager/flpipeline.py +0 -192
- Medfl/LearningManager/model.py +0 -223
- Medfl/LearningManager/params.yaml +0 -14
- Medfl/LearningManager/params_optimiser.py +0 -442
- Medfl/LearningManager/plot.py +0 -229
- Medfl/LearningManager/server.py +0 -181
- Medfl/LearningManager/strategy.py +0 -82
- Medfl/LearningManager/utils.py +0 -331
- Medfl/NetManager/__init__.py +0 -10
- Medfl/NetManager/database_connector.py +0 -43
- Medfl/NetManager/dataset.py +0 -92
- Medfl/NetManager/flsetup.py +0 -320
- Medfl/NetManager/net_helper.py +0 -254
- Medfl/NetManager/net_manager_queries.py +0 -142
- Medfl/NetManager/network.py +0 -194
- Medfl/NetManager/node.py +0 -184
- Medfl/__init__.py +0 -3
- Medfl/scripts/__init__.py +0 -2
- Medfl/scripts/base.py +0 -30
- Medfl/scripts/create_db.py +0 -126
- {MEDfl-2.0.4.dev1.dist-info → MEDfl-2.0.4.dev3.dist-info}/LICENSE +0 -0
- {MEDfl-2.0.4.dev1.dist-info → MEDfl-2.0.4.dev3.dist-info}/WHEEL +0 -0
- {MEDfl-2.0.4.dev1.dist-info → MEDfl-2.0.4.dev3.dist-info}/top_level.txt +0 -0
@@ -1,62 +0,0 @@
|
|
1
|
-
MEDfl/__init__.py,sha256=70DmtU4C3A-1XYoaYm0moXBe-YGJ2FhEe3ga5SQVTts,97
|
2
|
-
MEDfl/LearningManager/__init__.py,sha256=IMHJVeyx5ew0U_90LNMNCd4QISzWv3XCCri7fQRvcsM,341
|
3
|
-
MEDfl/LearningManager/client.py,sha256=9Y_Zb0yxvCxx3dVCPQ1bXS5mCKasylSBnoVj-RDN270,5933
|
4
|
-
MEDfl/LearningManager/dynamicModal.py,sha256=q8u7xPpj_TdZnSr8kYj0Xx7Sdz-diXsKBAfVce8-qSU,10534
|
5
|
-
MEDfl/LearningManager/federated_dataset.py,sha256=InsZ5Rys2dgqaPxVyP5G3TrJMwiCNHOoTd3tCpUwUVM,2081
|
6
|
-
MEDfl/LearningManager/flpipeline.py,sha256=5lT2uod5EqnkRQ04cgm0gYyZz0djumfIYipCrzX1fdo,7111
|
7
|
-
MEDfl/LearningManager/model.py,sha256=vp8FIMxBdz3FTF5wJaea2IO_WGeANLZgBxTKVe3gW3Q,7456
|
8
|
-
MEDfl/LearningManager/params.yaml,sha256=Ix1cNtlWr3vDC0te6pipl5w8iLADO6dZvwm633-VaIA,436
|
9
|
-
MEDfl/LearningManager/params_optimiser.py,sha256=8e0gCt4imwQHlNSJ3A2EAuc3wSr6yfSI6JDghohfmZQ,17618
|
10
|
-
MEDfl/LearningManager/plot.py,sha256=A6Z8wC8J-H-OmWBPKqwK5eiTB9vzOBGMaFv1SaNA9Js,7698
|
11
|
-
MEDfl/LearningManager/server.py,sha256=oTgW3K1UT6m4SQBk23FIf23km_BDq9vvjeC6OgY8DNw,7077
|
12
|
-
MEDfl/LearningManager/strategy.py,sha256=BHXpwmt7jx07y45YLUs8FZry2gYQbpiV4vNbHhsksQ4,3435
|
13
|
-
MEDfl/LearningManager/utils.py,sha256=B4RULJp-puJr724O6teI0PxnUyPV8NG-uPC6jqaiDKI,9605
|
14
|
-
MEDfl/NetManager/__init__.py,sha256=OpgsIiBg7UA6Bfnu_kqGfEPxU8JfpPxSFU98TOeDTP0,273
|
15
|
-
MEDfl/NetManager/database_connector.py,sha256=G8DAsD_pAIK1U67x3Q8gmSJGW7iJyxQ_NE5lWpT-P0Q,1474
|
16
|
-
MEDfl/NetManager/dataset.py,sha256=HTV0jrJ4Qlhl2aSJzdFU1lkxGBKtmJ390eBpwfKf_4o,2777
|
17
|
-
MEDfl/NetManager/flsetup.py,sha256=CVu_TIU7l3G6DDnwtY6JURbhIZk7gKC3unqWnU-YtlM,11434
|
18
|
-
MEDfl/NetManager/net_helper.py,sha256=tyfxmpbleSdfPfo2ezKT0VOvZu660v9nhBuHCpl8pG4,6812
|
19
|
-
MEDfl/NetManager/net_manager_queries.py,sha256=j-CLQPjtTLyZuFPhIcwJStD7L7xtZpkmkhe_h3pDuTs,4086
|
20
|
-
MEDfl/NetManager/network.py,sha256=5t705fzWc-BRg-QPAbAcDv5ckDGzsPwj_Q5V0iTgkx0,6829
|
21
|
-
MEDfl/NetManager/node.py,sha256=t90QuYZ8M1X_AG1bwTta0CnlOuodqkmpVda2K7NOgHc,6542
|
22
|
-
MEDfl/rw/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
23
|
-
MEDfl/rw/client.py,sha256=k8y8Wxh2KNe2oy5gRD-KXpTEGCYzp7X2oF5-Z6Rk1_E,1329
|
24
|
-
MEDfl/rw/model.py,sha256=TKCfE4nYx75uQdgABwEMkb_ynT-xS_MxNPbGAzJ3EcQ,629
|
25
|
-
MEDfl/rw/rwConfig.py,sha256=nK3Inv7v7Dm9gZnUnK5EqA4DmQ7TqiH4UoCZ8MlgFjA,823
|
26
|
-
MEDfl/rw/server.py,sha256=PiCrUTlnx7rVcO9DcT-vnJF5WkOCe4eEzWXeRSUBh10,3286
|
27
|
-
MEDfl/rw/strategy.py,sha256=sUwu0aAq6q3sKnfRimCRfps3be8s2iepGoD9NfcyjXI,6233
|
28
|
-
MEDfl/rw/verbose_server.py,sha256=B_abnpCy43e3YrjotLFOm7cLiuiB5PSTeXD5sMP0CxA,851
|
29
|
-
MEDfl/scripts/__init__.py,sha256=Pq1weevsPaU7MRMHfBYeyT0EOFeWLeVM6Y1DVz6jw1A,48
|
30
|
-
MEDfl/scripts/base.py,sha256=QrmG7gkiPYkAy-5tXxJgJmOSLGAKeIVH6i0jq7G9xnA,752
|
31
|
-
MEDfl/scripts/create_db.py,sha256=MnFtZkTueRZ-3qXPNX4JsXjOKj-4mlkxoRhSFdRcvJw,3817
|
32
|
-
Medfl/__init__.py,sha256=-BV6VpkX931dhU_qLqRJyhhRP9ftIrlHBvTgQVC-jK0,79
|
33
|
-
Medfl/LearningManager/__init__.py,sha256=IMHJVeyx5ew0U_90LNMNCd4QISzWv3XCCri7fQRvcsM,341
|
34
|
-
Medfl/LearningManager/client.py,sha256=9Y_Zb0yxvCxx3dVCPQ1bXS5mCKasylSBnoVj-RDN270,5933
|
35
|
-
Medfl/LearningManager/dynamicModal.py,sha256=q8u7xPpj_TdZnSr8kYj0Xx7Sdz-diXsKBAfVce8-qSU,10534
|
36
|
-
Medfl/LearningManager/federated_dataset.py,sha256=InsZ5Rys2dgqaPxVyP5G3TrJMwiCNHOoTd3tCpUwUVM,2081
|
37
|
-
Medfl/LearningManager/flpipeline.py,sha256=5lT2uod5EqnkRQ04cgm0gYyZz0djumfIYipCrzX1fdo,7111
|
38
|
-
Medfl/LearningManager/model.py,sha256=vp8FIMxBdz3FTF5wJaea2IO_WGeANLZgBxTKVe3gW3Q,7456
|
39
|
-
Medfl/LearningManager/params.yaml,sha256=_yAdYBtxNqKRWIhs_XebG_w1NGyq4-3NzVwWb8xiU5o,436
|
40
|
-
Medfl/LearningManager/params_optimiser.py,sha256=8e0gCt4imwQHlNSJ3A2EAuc3wSr6yfSI6JDghohfmZQ,17618
|
41
|
-
Medfl/LearningManager/plot.py,sha256=A6Z8wC8J-H-OmWBPKqwK5eiTB9vzOBGMaFv1SaNA9Js,7698
|
42
|
-
Medfl/LearningManager/server.py,sha256=oTgW3K1UT6m4SQBk23FIf23km_BDq9vvjeC6OgY8DNw,7077
|
43
|
-
Medfl/LearningManager/strategy.py,sha256=BHXpwmt7jx07y45YLUs8FZry2gYQbpiV4vNbHhsksQ4,3435
|
44
|
-
Medfl/LearningManager/utils.py,sha256=B4RULJp-puJr724O6teI0PxnUyPV8NG-uPC6jqaiDKI,9605
|
45
|
-
Medfl/NetManager/__init__.py,sha256=OpgsIiBg7UA6Bfnu_kqGfEPxU8JfpPxSFU98TOeDTP0,273
|
46
|
-
Medfl/NetManager/database_connector.py,sha256=Yh3GxI0NmbftM7YUkqQBjsXAe3i1ucF9q5OyR9DOhDQ,1473
|
47
|
-
Medfl/NetManager/dataset.py,sha256=HTV0jrJ4Qlhl2aSJzdFU1lkxGBKtmJ390eBpwfKf_4o,2777
|
48
|
-
Medfl/NetManager/flsetup.py,sha256=CVu_TIU7l3G6DDnwtY6JURbhIZk7gKC3unqWnU-YtlM,11434
|
49
|
-
Medfl/NetManager/net_helper.py,sha256=tyfxmpbleSdfPfo2ezKT0VOvZu660v9nhBuHCpl8pG4,6812
|
50
|
-
Medfl/NetManager/net_manager_queries.py,sha256=j-CLQPjtTLyZuFPhIcwJStD7L7xtZpkmkhe_h3pDuTs,4086
|
51
|
-
Medfl/NetManager/network.py,sha256=5t705fzWc-BRg-QPAbAcDv5ckDGzsPwj_Q5V0iTgkx0,6829
|
52
|
-
Medfl/NetManager/node.py,sha256=t90QuYZ8M1X_AG1bwTta0CnlOuodqkmpVda2K7NOgHc,6542
|
53
|
-
Medfl/scripts/__init__.py,sha256=Pq1weevsPaU7MRMHfBYeyT0EOFeWLeVM6Y1DVz6jw1A,48
|
54
|
-
Medfl/scripts/base.py,sha256=QrmG7gkiPYkAy-5tXxJgJmOSLGAKeIVH6i0jq7G9xnA,752
|
55
|
-
Medfl/scripts/create_db.py,sha256=MnFtZkTueRZ-3qXPNX4JsXjOKj-4mlkxoRhSFdRcvJw,3817
|
56
|
-
alembic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
57
|
-
alembic/env.py,sha256=-aSZ6SlJeK1ZeqHgM-54hOi9LhJRFP0SZGjut-JnY-4,1588
|
58
|
-
MEDfl-2.0.4.dev1.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
59
|
-
MEDfl-2.0.4.dev1.dist-info/METADATA,sha256=rvSMS_MZCyEDMkL54d4tRwWtSCa3AIQ3wuj6yZbszOc,4326
|
60
|
-
MEDfl-2.0.4.dev1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
61
|
-
MEDfl-2.0.4.dev1.dist-info/top_level.txt,sha256=dIL9X8HOFuaVSzpg40DVveDPrymWRoShHtspH7kkjdI,14
|
62
|
-
MEDfl-2.0.4.dev1.dist-info/RECORD,,
|
@@ -1,13 +0,0 @@
|
|
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
DELETED
@@ -1,150 +0,0 @@
|
|
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)}
|
@@ -1,287 +0,0 @@
|
|
1
|
-
import torch
|
2
|
-
import torch.nn as nn
|
3
|
-
from sklearn.svm import SVC
|
4
|
-
|
5
|
-
class DynamicModel:
|
6
|
-
"""DynamicModel class for creating various types of neural network models."""
|
7
|
-
|
8
|
-
# Create a binary classifier model
|
9
|
-
@staticmethod
|
10
|
-
def create_binary_classifier(input_dim, hidden_dims, output_dim, activation='relu', dropout_rate=0.0,
|
11
|
-
batch_norm=False, use_gpu=False):
|
12
|
-
"""
|
13
|
-
Creates a binary classifier neural network model with customizable architecture.
|
14
|
-
|
15
|
-
Args:
|
16
|
-
input_dim (int): Dimension of the input data.
|
17
|
-
hidden_dims (List[int]): List of dimensions for hidden layers.
|
18
|
-
output_dim (int): Dimension of the output (number of classes).
|
19
|
-
activation (str, optional): Activation function for hidden layers. Default is 'relu'.
|
20
|
-
dropout_rate (float, optional): Dropout rate for regularization. Default is 0.0 (no dropout).
|
21
|
-
batch_norm (bool, optional): Whether to apply batch normalization. Default is False.
|
22
|
-
use_gpu (bool, optional): Whether to use GPU acceleration. Default is False.
|
23
|
-
|
24
|
-
Returns:
|
25
|
-
torch.nn.Module: Created PyTorch model.
|
26
|
-
"""
|
27
|
-
|
28
|
-
layers = []
|
29
|
-
|
30
|
-
for i in range(len(hidden_dims)):
|
31
|
-
if i == 0:
|
32
|
-
layers.append(nn.Linear(input_dim, hidden_dims[0]))
|
33
|
-
else:
|
34
|
-
layers.append(nn.Linear(hidden_dims[i - 1], hidden_dims[i]))
|
35
|
-
|
36
|
-
if batch_norm:
|
37
|
-
layers.append(nn.BatchNorm1d(hidden_dims[i]))
|
38
|
-
|
39
|
-
activation_layer = nn.ReLU() if activation == 'relu' else nn.Sigmoid()
|
40
|
-
layers.append(activation_layer)
|
41
|
-
|
42
|
-
if dropout_rate > 0.0:
|
43
|
-
layers.append(nn.Dropout(dropout_rate))
|
44
|
-
|
45
|
-
layers.append(nn.Linear(hidden_dims[-1], output_dim))
|
46
|
-
layers.append(nn.Sigmoid())
|
47
|
-
|
48
|
-
model = nn.Sequential(*layers)
|
49
|
-
|
50
|
-
if use_gpu:
|
51
|
-
model = model.cuda()
|
52
|
-
|
53
|
-
return model
|
54
|
-
|
55
|
-
# Create a multi-class classifier model
|
56
|
-
@staticmethod
|
57
|
-
def create_multiclass_classifier(input_dim, hidden_dims, output_dim, activation='relu', dropout_rate=0.0,
|
58
|
-
batch_norm=False, use_gpu=False):
|
59
|
-
"""
|
60
|
-
Creates a multiclass classifier neural network model with customizable architecture.
|
61
|
-
|
62
|
-
Args:
|
63
|
-
input_dim (int): Dimension of the input data.
|
64
|
-
hidden_dims (List[int]): List of dimensions for hidden layers.
|
65
|
-
output_dim (int): Dimension of the output (number of classes).
|
66
|
-
activation (str, optional): Activation function for hidden layers. Default is 'relu'.
|
67
|
-
dropout_rate (float, optional): Dropout rate for regularization. Default is 0.0 (no dropout).
|
68
|
-
batch_norm (bool, optional): Whether to apply batch normalization. Default is False.
|
69
|
-
use_gpu (bool, optional): Whether to use GPU acceleration. Default is False.
|
70
|
-
|
71
|
-
Returns:
|
72
|
-
torch.nn.Module: Created PyTorch model.
|
73
|
-
"""
|
74
|
-
layers = []
|
75
|
-
|
76
|
-
for i in range(len(hidden_dims)):
|
77
|
-
if i == 0:
|
78
|
-
layers.append(nn.Linear(input_dim, hidden_dims[0]))
|
79
|
-
else:
|
80
|
-
layers.append(nn.Linear(hidden_dims[i - 1], hidden_dims[i]))
|
81
|
-
|
82
|
-
if batch_norm:
|
83
|
-
layers.append(nn.BatchNorm1d(hidden_dims[i]))
|
84
|
-
|
85
|
-
activation_layer = nn.ReLU() if activation == 'relu' else nn.Sigmoid()
|
86
|
-
layers.append(activation_layer)
|
87
|
-
|
88
|
-
if dropout_rate > 0.0:
|
89
|
-
layers.append(nn.Dropout(dropout_rate))
|
90
|
-
|
91
|
-
layers.append(nn.Linear(hidden_dims[-1], output_dim))
|
92
|
-
layers.append(nn.LogSoftmax(dim=1))
|
93
|
-
|
94
|
-
model = nn.Sequential(*layers)
|
95
|
-
|
96
|
-
if use_gpu:
|
97
|
-
model = model.cuda()
|
98
|
-
|
99
|
-
return model
|
100
|
-
|
101
|
-
# Create a linear regressor model
|
102
|
-
@staticmethod
|
103
|
-
def create_linear_regressor(input_dim, output_dim, use_gpu=False):
|
104
|
-
"""
|
105
|
-
Creates a linear regressor neural network model.
|
106
|
-
|
107
|
-
Args:
|
108
|
-
input_dim (int): Dimension of the input data.
|
109
|
-
output_dim (int): Dimension of the output.
|
110
|
-
|
111
|
-
Returns:
|
112
|
-
torch.nn.Module: Created PyTorch model.
|
113
|
-
"""
|
114
|
-
class LinearRegressionModel(nn.Module):
|
115
|
-
def __init__(self):
|
116
|
-
super(LinearRegressionModel, self).__init__()
|
117
|
-
self.linear = nn.Linear(input_dim, output_dim)
|
118
|
-
|
119
|
-
def forward(self, x):
|
120
|
-
return self.linear(x)
|
121
|
-
|
122
|
-
model = LinearRegressionModel()
|
123
|
-
|
124
|
-
if use_gpu:
|
125
|
-
model = model.cuda()
|
126
|
-
|
127
|
-
return model
|
128
|
-
|
129
|
-
# Create a logistic regressor model
|
130
|
-
@staticmethod
|
131
|
-
def create_logistic_regressor(input_dim, use_gpu=False):
|
132
|
-
"""
|
133
|
-
Creates a logistic regressor neural network model.
|
134
|
-
|
135
|
-
Args:
|
136
|
-
input_dim (int): Dimension of the input data.
|
137
|
-
|
138
|
-
Returns:
|
139
|
-
torch.nn.Module: Created PyTorch model.
|
140
|
-
"""
|
141
|
-
class LogisticRegressionModel(nn.Module):
|
142
|
-
def __init__(self):
|
143
|
-
super(LogisticRegressionModel, self).__init__()
|
144
|
-
self.linear = nn.Linear(input_dim, 1)
|
145
|
-
|
146
|
-
def forward(self, x):
|
147
|
-
return torch.sigmoid(self.linear(x))
|
148
|
-
|
149
|
-
model = LogisticRegressionModel()
|
150
|
-
|
151
|
-
if use_gpu:
|
152
|
-
model = model.cuda()
|
153
|
-
|
154
|
-
return model
|
155
|
-
|
156
|
-
@staticmethod
|
157
|
-
def create_convolutional_neural_network(input_channels, output_channels, kernel_size, use_gpu=False):
|
158
|
-
"""
|
159
|
-
Creates a convolutional neural network (CNN) model.
|
160
|
-
|
161
|
-
Args:
|
162
|
-
input_channels (int): Number of input channels.
|
163
|
-
output_channels (int): Number of output channels.
|
164
|
-
kernel_size (int): Size of the convolutional kernel.
|
165
|
-
|
166
|
-
Returns:
|
167
|
-
torch.nn.Module: Created PyTorch model.
|
168
|
-
"""
|
169
|
-
|
170
|
-
model = nn.Sequential(
|
171
|
-
nn.Conv2d(input_channels, output_channels, kernel_size),
|
172
|
-
nn.ReLU(),
|
173
|
-
nn.MaxPool2d(2)
|
174
|
-
)
|
175
|
-
|
176
|
-
if use_gpu:
|
177
|
-
model = model.cuda()
|
178
|
-
|
179
|
-
return model
|
180
|
-
|
181
|
-
@staticmethod
|
182
|
-
def create_recurrent_neural_network(input_size, hidden_size, use_gpu=False):
|
183
|
-
"""
|
184
|
-
Creates a recurrent neural network (RNN) model.
|
185
|
-
|
186
|
-
Args:
|
187
|
-
input_size (int): Size of the input.
|
188
|
-
hidden_size (int): Size of the hidden layer.
|
189
|
-
|
190
|
-
Returns:
|
191
|
-
torch.nn.Module: Created PyTorch model.
|
192
|
-
"""
|
193
|
-
|
194
|
-
model = nn.RNN(input_size, hidden_size, batch_first=True)
|
195
|
-
|
196
|
-
if use_gpu:
|
197
|
-
model = model.cuda()
|
198
|
-
|
199
|
-
return model
|
200
|
-
|
201
|
-
@staticmethod
|
202
|
-
def create_lstm_network(input_size, hidden_size, use_gpu=False):
|
203
|
-
"""
|
204
|
-
Creates a Long Short-Term Memory (LSTM) network model.
|
205
|
-
|
206
|
-
Args:
|
207
|
-
input_size (int): Size of the input layer.
|
208
|
-
hidden_size (int): Size of the hidden layer.
|
209
|
-
|
210
|
-
Returns:
|
211
|
-
torch.nn.Module: Created PyTorch model.
|
212
|
-
"""
|
213
|
-
|
214
|
-
model = nn.LSTM(input_size, hidden_size, batch_first=True)
|
215
|
-
|
216
|
-
if use_gpu:
|
217
|
-
model = model.cuda()
|
218
|
-
|
219
|
-
return model
|
220
|
-
|
221
|
-
# Create the dynamic model
|
222
|
-
def create_model(self, model_type: str, params_dict={}) -> torch.nn.Module:
|
223
|
-
"""
|
224
|
-
Create a specific type of model dynamically based on the given parameters.
|
225
|
-
|
226
|
-
Args:
|
227
|
-
model_type (str): Type of the model to create ('Binary Classifier', 'Multiclass Classifier', 'Linear Regressor', 'Logistic Regressor', 'SVM', 'Neural Network Classifier', 'Convolutional Neural Network', 'Recurrent Neural Network', 'LSTM Network', 'Autoencoder').
|
228
|
-
params_dict (dict): Dictionary containing parameters for model creation.
|
229
|
-
|
230
|
-
Returns:
|
231
|
-
torch.nn.Module: Created PyTorch model.
|
232
|
-
"""
|
233
|
-
if model_type == 'Binary Classifier':
|
234
|
-
return self.create_binary_classifier(
|
235
|
-
params_dict['input_dim'], params_dict['hidden_dims'],
|
236
|
-
params_dict['output_dim'], params_dict.get('activation', 'relu'),
|
237
|
-
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
238
|
-
params_dict.get('use_gpu', False)
|
239
|
-
)
|
240
|
-
elif model_type == 'Multiclass Classifier':
|
241
|
-
return self.create_multiclass_classifier(
|
242
|
-
params_dict['input_dim'], params_dict['hidden_dims'],
|
243
|
-
params_dict['output_dim'], params_dict.get('activation', 'relu'),
|
244
|
-
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
245
|
-
params_dict.get('use_gpu', False)
|
246
|
-
)
|
247
|
-
elif model_type == 'Linear Regressor':
|
248
|
-
return self.create_linear_regressor(
|
249
|
-
params_dict['input_dim'], params_dict['output_dim'],
|
250
|
-
params_dict.get('use_gpu', False)
|
251
|
-
)
|
252
|
-
elif model_type == 'Logistic Regressor':
|
253
|
-
return self.create_logistic_regressor(
|
254
|
-
params_dict['input_dim'], params_dict.get('use_gpu', False)
|
255
|
-
)
|
256
|
-
elif model_type == 'Neural Network Classifier':
|
257
|
-
return self.create_neural_network_classifier(
|
258
|
-
params_dict['input_dim'], params_dict['output_dim'],
|
259
|
-
params_dict['hidden_dims'], params_dict.get('activation', 'relu'),
|
260
|
-
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
261
|
-
params_dict.get('num_layers', 2), params_dict.get('use_gpu', False)
|
262
|
-
)
|
263
|
-
elif model_type == 'Convolutional Neural Network':
|
264
|
-
return self.create_convolutional_neural_network(
|
265
|
-
params_dict['input_channels'], params_dict['output_channels'],
|
266
|
-
params_dict['kernel_size'], params_dict.get('use_gpu', False)
|
267
|
-
)
|
268
|
-
elif model_type == 'Recurrent Neural Network':
|
269
|
-
return self.create_recurrent_neural_network(
|
270
|
-
params_dict['input_size'], params_dict['hidden_size'],
|
271
|
-
params_dict.get('use_gpu', False)
|
272
|
-
)
|
273
|
-
elif model_type == 'LSTM Network':
|
274
|
-
return self.create_lstm_network(
|
275
|
-
params_dict['input_size'], params_dict['hidden_size'],
|
276
|
-
params_dict.get('use_gpu', False)
|
277
|
-
)
|
278
|
-
elif model_type == 'Autoencoder':
|
279
|
-
return self.create_autoencoder(
|
280
|
-
params_dict['input_size'], params_dict['encoder_hidden_size'],
|
281
|
-
params_dict.get('use_gpu', False)
|
282
|
-
)
|
283
|
-
else:
|
284
|
-
raise ValueError("Invalid model type provided")
|
285
|
-
|
286
|
-
|
287
|
-
|
@@ -1,60 +0,0 @@
|
|
1
|
-
from MEDfl.NetManager.net_helper import *
|
2
|
-
from MEDfl.NetManager.net_manager_queries import *
|
3
|
-
from MEDfl.NetManager.database_connector import DatabaseManager
|
4
|
-
|
5
|
-
class FederatedDataset:
|
6
|
-
def __init__(
|
7
|
-
self,
|
8
|
-
name: str,
|
9
|
-
train_nodes: list,
|
10
|
-
test_nodes: list,
|
11
|
-
trainloaders: list,
|
12
|
-
valloaders: list,
|
13
|
-
testloaders: list,
|
14
|
-
):
|
15
|
-
"""
|
16
|
-
Represents a Federated Dataset.
|
17
|
-
|
18
|
-
:param name: Name of the Federated Dataset.
|
19
|
-
:param train_nodes: List of train nodes.
|
20
|
-
:param test_nodes: List of test nodes.
|
21
|
-
:param trainloaders: List of train data loaders.
|
22
|
-
:param valloaders: List of validation data loaders.
|
23
|
-
:param testloaders: List of test data loaders.
|
24
|
-
"""
|
25
|
-
self.name = name
|
26
|
-
self.train_nodes = train_nodes
|
27
|
-
self.test_nodes = test_nodes
|
28
|
-
self.trainloaders = trainloaders
|
29
|
-
self.valloaders = valloaders
|
30
|
-
self.testloaders = testloaders
|
31
|
-
self.size = len(self.trainloaders[0].dataset[0][0])
|
32
|
-
|
33
|
-
db_manager = DatabaseManager()
|
34
|
-
db_manager.connect()
|
35
|
-
self.eng = db_manager.get_connection()
|
36
|
-
|
37
|
-
def create(self, FLsetupId: int):
|
38
|
-
"""
|
39
|
-
Create a new Federated Dataset in the database.
|
40
|
-
|
41
|
-
:param FLsetupId: The FLsetup ID associated with the Federated Dataset.
|
42
|
-
"""
|
43
|
-
query_params = {"name": self.name, "FLsetupId": FLsetupId}
|
44
|
-
fedDataId = get_feddataset_id_from_name(self.name)
|
45
|
-
if fedDataId :
|
46
|
-
self.id = fedDataId
|
47
|
-
else:
|
48
|
-
self.eng.execute(text(INSERT_FLDATASET_QUERY), query_params)
|
49
|
-
self.id = get_feddataset_id_from_name(self.name)
|
50
|
-
|
51
|
-
|
52
|
-
def update(self, FLpipeId: int, FedId: int):
|
53
|
-
"""
|
54
|
-
Update the FLpipe ID associated with the Federated Dataset in the database.
|
55
|
-
|
56
|
-
:param FLpipeId: The new FLpipe ID to be updated.
|
57
|
-
:param FedId: The Federated Dataset ID.
|
58
|
-
"""
|
59
|
-
query_params = {"FLpipeId": FLpipeId, "FedId": FedId}
|
60
|
-
self.eng.execute(text(UPDATE_FLDATASET_QUERY), **query_params)
|