MEDfl 2.0.10.dev12__tar.gz → 2.0.10.dev14__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.
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/client.py +27 -4
- medfl-2.0.10.dev14/MEDfl/LearningManager/dp.py +115 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/model.py +2 -2
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/server.py +23 -1
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/PKG-INFO +1 -1
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/medfl.egg-info/PKG-INFO +1 -1
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/medfl.egg-info/SOURCES.txt +1 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/setup.py +1 -1
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/LICENSE +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/dynamicModal.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/federated_dataset.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/flpipeline.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/params.yaml +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/params_optimiser.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/plot.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/shap/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/shap/aggregation.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/shap/config.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/shap/explainer.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/shap/xgboost_explainer.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/strategy.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/utils.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/client.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/config.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/data.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/metrics.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/serialization.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/server.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/LearningManager/xgboost/strategy.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/database_connector.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/dataset.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/flsetup.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/net_helper.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/net_manager_queries.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/network.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/NetManager/node.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/client.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/holdout.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/identity.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/model.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/server.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/strategy.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/xgboost/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/xgboost/client.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/xgboost/config.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/xgboost/data.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/xgboost/metrics.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/xgboost/serialization.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/rw/xgboost/strategy.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/scripts/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/scripts/base.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/MEDfl/scripts/create_db.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/README.md +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/alembic/__init__.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/alembic/env.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/medfl.egg-info/dependency_links.txt +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/medfl.egg-info/requires.txt +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/medfl.egg-info/top_level.txt +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/setup.cfg +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/tests/test_dataset.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/tests/test_federated_dataset.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/tests/test_flpipeline.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/tests/test_flsetup.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/tests/test_network.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/tests/test_node.py +0 -0
- {medfl-2.0.10.dev12 → medfl-2.0.10.dev14}/tests/test_rw_holdout.py +0 -0
|
@@ -6,10 +6,12 @@ import flwr as fl
|
|
|
6
6
|
from opacus import PrivacyEngine
|
|
7
7
|
from torch.utils.data import DataLoader
|
|
8
8
|
|
|
9
|
+
from .dp import ACCOUNTANT, DPConfig
|
|
9
10
|
from .model import Model
|
|
10
11
|
from .utils import params
|
|
11
12
|
import torch
|
|
12
13
|
import numpy as np
|
|
14
|
+
from typing import Optional
|
|
13
15
|
|
|
14
16
|
|
|
15
17
|
# ---------------------------------------------------------------------------
|
|
@@ -47,8 +49,12 @@ class FlowerClient(fl.client.NumPyClient):
|
|
|
47
49
|
trainloader: DataLoader,
|
|
48
50
|
valloader: DataLoader,
|
|
49
51
|
diff_priv: bool = params["diff_privacy"],
|
|
52
|
+
dp_config: Optional[DPConfig] = None,
|
|
53
|
+
local_epochs: Optional[int] = None,
|
|
50
54
|
):
|
|
51
55
|
self.cid = cid
|
|
56
|
+
# number of local training epochs per round; None = train_epochs of params.yaml
|
|
57
|
+
self.local_epochs = int(local_epochs) if local_epochs is not None else int(params["train_epochs"])
|
|
52
58
|
self.local_model = local_model
|
|
53
59
|
self.trainloader = trainloader
|
|
54
60
|
self.valloader = valloader
|
|
@@ -57,20 +63,34 @@ class FlowerClient(fl.client.NumPyClient):
|
|
|
57
63
|
self.device = torch.device("cpu")
|
|
58
64
|
self.local_model.model.to(self.device)
|
|
59
65
|
|
|
60
|
-
self.privacy_engine = PrivacyEngine(secure_mode=False)
|
|
61
66
|
self.diff_priv = diff_priv
|
|
67
|
+
# set by the server (noise calibrated for the whole training); None = the settings of params.yaml
|
|
68
|
+
self.dp_config = dp_config if dp_config is not None and dp_config.noise_multiplier is not None else None
|
|
69
|
+
self.privacy_engine = (
|
|
70
|
+
PrivacyEngine(accountant=ACCOUNTANT, secure_mode=False) if self.dp_config else PrivacyEngine(secure_mode=False)
|
|
71
|
+
)
|
|
62
72
|
self.epsilons = []
|
|
63
73
|
self.accuracies = []
|
|
64
74
|
self.losses = []
|
|
65
75
|
self.train_round_losses = []
|
|
66
76
|
self.train_round_metrics = []
|
|
67
77
|
|
|
68
|
-
if self.diff_priv:
|
|
78
|
+
if self.diff_priv and self.dp_config:
|
|
79
|
+
model, optimizer, self.trainloader = self.privacy_engine.make_private(
|
|
80
|
+
module=self.local_model.model.train(),
|
|
81
|
+
optimizer=self.local_model.optimizer,
|
|
82
|
+
data_loader=self.trainloader,
|
|
83
|
+
noise_multiplier=float(self.dp_config.noise_multiplier),
|
|
84
|
+
max_grad_norm=float(self.dp_config.max_grad_norm),
|
|
85
|
+
)
|
|
86
|
+
setattr(self.local_model, "model", model)
|
|
87
|
+
setattr(self.local_model, "optimizer", optimizer)
|
|
88
|
+
elif self.diff_priv:
|
|
69
89
|
model, optimizer, self.trainloader = self.privacy_engine.make_private_with_epsilon(
|
|
70
90
|
module=self.local_model.model.train(),
|
|
71
91
|
optimizer=self.local_model.optimizer,
|
|
72
92
|
data_loader=self.trainloader,
|
|
73
|
-
epochs=
|
|
93
|
+
epochs=self.local_epochs,
|
|
74
94
|
target_epsilon=float(params["EPSILON"]),
|
|
75
95
|
target_delta=float(params["DELTA"]),
|
|
76
96
|
max_grad_norm=params["MAX_GRAD_NORM"],
|
|
@@ -92,6 +112,8 @@ class FlowerClient(fl.client.NumPyClient):
|
|
|
92
112
|
raise TypeError("valloader argument must be a torch.utils.data.dataloader")
|
|
93
113
|
if not isinstance(self.diff_priv, bool):
|
|
94
114
|
raise TypeError("diff_priv argument must be a bool")
|
|
115
|
+
if self.local_epochs <= 0:
|
|
116
|
+
raise ValueError("local_epochs argument must be greater than 0")
|
|
95
117
|
|
|
96
118
|
# ------------------------------------------------------------------
|
|
97
119
|
def get_parameters(self, config):
|
|
@@ -106,13 +128,14 @@ class FlowerClient(fl.client.NumPyClient):
|
|
|
106
128
|
|
|
107
129
|
epoch_losses, epoch_metrics = [], []
|
|
108
130
|
|
|
109
|
-
for ep in range(
|
|
131
|
+
for ep in range(self.local_epochs):
|
|
110
132
|
epsilon, tr_loss, tr_metric = self.local_model.train(
|
|
111
133
|
self.trainloader,
|
|
112
134
|
epoch=ep,
|
|
113
135
|
device=self.device,
|
|
114
136
|
privacy_engine=self.privacy_engine,
|
|
115
137
|
diff_priv=self.diff_priv,
|
|
138
|
+
delta=self.dp_config.delta if self.dp_config else None,
|
|
116
139
|
)
|
|
117
140
|
self.epsilons.append(epsilon)
|
|
118
141
|
epoch_losses.append(tr_loss)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Differential privacy settings of the simulation (Opacus, neural network models only).
|
|
3
|
+
|
|
4
|
+
The privacy budget (epsilon, delta) covers the WHOLE training: the noise is calibrated once, for every local step of
|
|
5
|
+
every round, so that a client that takes part in all the rounds spends exactly epsilon. Clients that take part in
|
|
6
|
+
fewer rounds spend less.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from dataclasses import asdict, dataclass, replace
|
|
10
|
+
from typing import Dict, List, Optional
|
|
11
|
+
|
|
12
|
+
from opacus.accountants import RDPAccountant
|
|
13
|
+
from opacus.accountants.utils import get_noise_multiplier
|
|
14
|
+
|
|
15
|
+
# The same accountant is used to calibrate the noise and to report the spent budget
|
|
16
|
+
ACCOUNTANT = "rdp"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class DPConfig:
|
|
21
|
+
"""
|
|
22
|
+
Attributes:
|
|
23
|
+
epsilon: total privacy budget of the training. Lower = stronger privacy, more noise, lower accuracy.
|
|
24
|
+
delta: probability that the epsilon guarantee does not hold. Keep it well below 1 / (training rows of a client).
|
|
25
|
+
max_grad_norm: each sample's gradient is clipped to this norm before the noise is added.
|
|
26
|
+
noise_multiplier: computed by the server for each client (from epsilon, delta, rounds, epochs and batch size).
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
epsilon: float = 5.0
|
|
30
|
+
delta: float = 1e-5
|
|
31
|
+
max_grad_norm: float = 1.0
|
|
32
|
+
noise_multiplier: Optional[float] = None
|
|
33
|
+
|
|
34
|
+
def validate(self) -> None:
|
|
35
|
+
if not self.epsilon or self.epsilon <= 0:
|
|
36
|
+
raise ValueError(f"Differential privacy: epsilon must be greater than 0 (got {self.epsilon}).")
|
|
37
|
+
if not self.delta or not 0 < self.delta < 1:
|
|
38
|
+
raise ValueError(f"Differential privacy: delta must be between 0 and 1 (got {self.delta}).")
|
|
39
|
+
if not self.max_grad_norm or self.max_grad_norm <= 0:
|
|
40
|
+
raise ValueError(f"Differential privacy: the max grad norm must be greater than 0 (got {self.max_grad_norm}).")
|
|
41
|
+
|
|
42
|
+
def to_dict(self) -> dict:
|
|
43
|
+
return asdict(self)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def epsilon_spent(noise_multiplier: float, sample_rate: float, steps: int, delta: float) -> float:
|
|
47
|
+
"""Epsilon spent after `steps` noisy steps"""
|
|
48
|
+
if steps <= 0:
|
|
49
|
+
return 0.0
|
|
50
|
+
accountant = RDPAccountant()
|
|
51
|
+
accountant.history = [(noise_multiplier, sample_rate, steps)]
|
|
52
|
+
return float(accountant.get_epsilon(delta))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def calibrate_clients(dp_config: DPConfig, num_batches: Dict[str, int], local_epochs: int, num_rounds: int) -> dict:
|
|
56
|
+
"""
|
|
57
|
+
Noise multiplier of each client so that all its local steps over all the rounds spend dp_config.epsilon.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
num_batches: number of batches of each client's train loader (Opacus samples each batch with rate 1 / num_batches)
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
{"clients": {cid: DPConfig with its noise_multiplier}, "report": summary for the results}
|
|
64
|
+
"""
|
|
65
|
+
dp_config.validate()
|
|
66
|
+
clients = {}
|
|
67
|
+
report_clients: List[dict] = []
|
|
68
|
+
|
|
69
|
+
for cid, batches in num_batches.items():
|
|
70
|
+
batches = max(int(batches), 1)
|
|
71
|
+
sample_rate = 1.0 / batches
|
|
72
|
+
steps_per_round = batches * int(local_epochs)
|
|
73
|
+
try:
|
|
74
|
+
noise_multiplier = float(
|
|
75
|
+
get_noise_multiplier(
|
|
76
|
+
target_epsilon=float(dp_config.epsilon),
|
|
77
|
+
target_delta=float(dp_config.delta),
|
|
78
|
+
sample_rate=sample_rate,
|
|
79
|
+
steps=steps_per_round * int(num_rounds),
|
|
80
|
+
accountant=ACCOUNTANT,
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
except ValueError as exc:
|
|
84
|
+
raise ValueError(
|
|
85
|
+
f"Differential privacy: no noise level reaches epsilon={dp_config.epsilon} with delta={dp_config.delta} "
|
|
86
|
+
f"for client {cid} ({exc}). Increase epsilon or delta."
|
|
87
|
+
) from exc
|
|
88
|
+
|
|
89
|
+
clients[cid] = replace(dp_config, noise_multiplier=noise_multiplier)
|
|
90
|
+
report_clients.append(
|
|
91
|
+
{
|
|
92
|
+
"cid": cid,
|
|
93
|
+
"noise_multiplier": noise_multiplier,
|
|
94
|
+
"sample_rate": sample_rate,
|
|
95
|
+
"steps_per_round": steps_per_round,
|
|
96
|
+
# budget spent by this client after each round, if it takes part in every round
|
|
97
|
+
"epsilon_by_round": [
|
|
98
|
+
epsilon_spent(noise_multiplier, sample_rate, steps_per_round * round_number, dp_config.delta)
|
|
99
|
+
for round_number in range(1, int(num_rounds) + 1)
|
|
100
|
+
],
|
|
101
|
+
}
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
"clients": clients,
|
|
106
|
+
"report": {
|
|
107
|
+
"epsilon": float(dp_config.epsilon),
|
|
108
|
+
"delta": float(dp_config.delta),
|
|
109
|
+
"max_grad_norm": float(dp_config.max_grad_norm),
|
|
110
|
+
"accountant": ACCOUNTANT,
|
|
111
|
+
"local_epochs": int(local_epochs),
|
|
112
|
+
"num_rounds": int(num_rounds),
|
|
113
|
+
"clients": report_clients,
|
|
114
|
+
},
|
|
115
|
+
}
|
|
@@ -76,7 +76,7 @@ class Model:
|
|
|
76
76
|
new_state_dict[k] = torch.as_tensor(v, device=device, dtype=ref.dtype)
|
|
77
77
|
self.model.load_state_dict(new_state_dict, strict=True)
|
|
78
78
|
|
|
79
|
-
def train(self, train_loader, epoch, device, privacy_engine, diff_priv=False) -> float:
|
|
79
|
+
def train(self, train_loader, epoch, device, privacy_engine, diff_priv=False, delta=None) -> float:
|
|
80
80
|
self.model.train()
|
|
81
81
|
epsilon = 0.0
|
|
82
82
|
losses = []
|
|
@@ -150,7 +150,7 @@ class Model:
|
|
|
150
150
|
metrics.append(float(rmse))
|
|
151
151
|
|
|
152
152
|
if diff_priv:
|
|
153
|
-
epsilon = float(privacy_engine.get_epsilon(float(params["DELTA"])))
|
|
153
|
+
epsilon = float(privacy_engine.get_epsilon(float(delta if delta is not None else params["DELTA"])))
|
|
154
154
|
|
|
155
155
|
if (i + 1) % 10 == 0 and len(losses) > 0:
|
|
156
156
|
if self.task_type == "binary" and len(metrics) > 0:
|
|
@@ -11,6 +11,8 @@ import torch
|
|
|
11
11
|
from torch.utils.data import ConcatDataset, DataLoader
|
|
12
12
|
|
|
13
13
|
from .client import FlowerClient
|
|
14
|
+
from .dp import DPConfig, calibrate_clients
|
|
15
|
+
from .utils import params as default_params
|
|
14
16
|
from .federated_dataset import FederatedDataset
|
|
15
17
|
from .model import Model
|
|
16
18
|
from .strategy import Strategy
|
|
@@ -74,6 +76,8 @@ class FlowerServer:
|
|
|
74
76
|
shap_config: Optional[
|
|
75
77
|
SHAPConfig
|
|
76
78
|
] = None,
|
|
79
|
+
dp_config: Optional[DPConfig] = None,
|
|
80
|
+
local_epochs: Optional[int] = None,
|
|
77
81
|
) -> None:
|
|
78
82
|
|
|
79
83
|
self.device = torch.device("cpu")
|
|
@@ -85,6 +89,8 @@ class FlowerServer:
|
|
|
85
89
|
self.fed_dataset = fed_dataset
|
|
86
90
|
self.strategy = strategy
|
|
87
91
|
self.client_resources = client_resources
|
|
92
|
+
# number of local training epochs per round; None = train_epochs of params.yaml
|
|
93
|
+
self.local_epochs = int(local_epochs) if local_epochs is not None else int(default_params["train_epochs"])
|
|
88
94
|
|
|
89
95
|
self.shap_config = (
|
|
90
96
|
shap_config
|
|
@@ -105,6 +111,20 @@ class FlowerServer:
|
|
|
105
111
|
|
|
106
112
|
self.diff_priv = diff_privacy
|
|
107
113
|
|
|
114
|
+
# Differential privacy: the noise of each client is calibrated once for the whole training (all rounds).
|
|
115
|
+
# Without dp_config, the clients keep the settings of params.yaml.
|
|
116
|
+
self.dp_clients = {}
|
|
117
|
+
self.dp_report = None
|
|
118
|
+
if self.diff_priv and dp_config is not None:
|
|
119
|
+
calibration = calibrate_clients(
|
|
120
|
+
dp_config,
|
|
121
|
+
num_batches={str(index): len(loader) for index, loader in enumerate(self.fed_dataset.trainloaders)},
|
|
122
|
+
local_epochs=self.local_epochs,
|
|
123
|
+
num_rounds=int(self.num_rounds),
|
|
124
|
+
)
|
|
125
|
+
self.dp_clients = calibration["clients"]
|
|
126
|
+
self.dp_report = calibration["report"]
|
|
127
|
+
|
|
108
128
|
# tracking
|
|
109
129
|
self.accuracies = []
|
|
110
130
|
self.losses = []
|
|
@@ -460,6 +480,8 @@ class FlowerServer:
|
|
|
460
480
|
raise TypeError("num_clients argument must be an int")
|
|
461
481
|
if not isinstance(self.num_rounds, int):
|
|
462
482
|
raise TypeError("num_rounds argument must be an int")
|
|
483
|
+
if self.local_epochs <= 0:
|
|
484
|
+
raise ValueError("local_epochs argument must be greater than 0")
|
|
463
485
|
if not isinstance(self.diff_priv, bool):
|
|
464
486
|
raise TypeError("diff_priv argument must be a bool")
|
|
465
487
|
|
|
@@ -478,7 +500,7 @@ class FlowerServer:
|
|
|
478
500
|
valloader = self.fed_dataset.valloaders[int(cid)]
|
|
479
501
|
|
|
480
502
|
client_model = copy.deepcopy(self.global_model)
|
|
481
|
-
client = FlowerClient(cid, client_model, trainloader, valloader, self.diff_priv)
|
|
503
|
+
client = FlowerClient(cid, client_model, trainloader, valloader, self.diff_priv, dp_config=self.dp_clients.get(str(cid)), local_epochs=self.local_epochs)
|
|
482
504
|
self.flower_clients.append(client)
|
|
483
505
|
return client
|
|
484
506
|
|
|
@@ -14,7 +14,7 @@ with open('requirements.txt') as f:
|
|
|
14
14
|
|
|
15
15
|
setup(
|
|
16
16
|
name="medfl",
|
|
17
|
-
version="2.0.10.
|
|
17
|
+
version="2.0.10.dev14",
|
|
18
18
|
author="MEDomics consortium",
|
|
19
19
|
author_email="medomics.info@gmail.com",
|
|
20
20
|
description="Python Open-source package for simulating federated learning and differential privacy",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|