-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_client.py
313 lines (250 loc) · 11.1 KB
/
dp_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import argparse
import copy
import random
import warnings
from collections import OrderedDict
import flwr as fl
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, random_split
from torchvision.datasets import CIFAR10
from torchvision.transforms import Compose, Normalize, ToTensor
from tqdm import tqdm
from opacus import PrivacyEngine
parser = argparse.ArgumentParser(description="Flower Embedded devices")
parser.add_argument(
"--server_address",
type=str,
default="192.168.0.116:49999", # replace with your server IP address
help=f"gRPC server address (default '192.168.0.112:49999')",
)
parser.add_argument(
"--cid",
type=int,
required=True,
help="Client id. Should be an integer between 0 and NUM_CLIENTS",
)
parser.add_argument(
"--iid",
action="store_true",
help="If you use iid use true else false",
)
warnings.filterwarnings("ignore", category=UserWarning)
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
NUM_CLIENTS = 10 # Number of clients = 10 for IID and 6 for non-IID
NUM_CLASSES = 10 # Number of classes in CIFAR-10
# Modify the privacy parameters based on your privacy requiremnts/budget.
PRIVACY_PARAMS = {
#test4- Best result so far
"target_epsilon": 7.0,
"target_delta": 1e-6,
# "noise_multiplier": 0.7,
"max_grad_norm": 0.7,
}
class Net(nn.Module):
"""Model (simple CNN adapted)."""
def __init__(self): # baseline 2
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(64, 64, 3, padding=1)
self.relu3 = nn.ReLU()
self.fc1 = nn.Linear(64 * 8 * 8, 64)
self.relu4 = nn.ReLU()
self.fc2 = nn.Linear(64, 10)
def forward(self, x):
x = self.pool1(self.relu1(self.conv1(x)))
x = self.pool2(self.relu2(self.conv2(x)))
x = self.relu3(self.conv3(x))
x = x.view(-1, 64 * 8 * 8)
x = self.relu4(self.fc1(x))
x = self.fc2(x)
return x
def train(net, trainloader,valloader, optimizer, epochs, privacy_engine,device, global_params, config):
"""Train the model on the training set."""
train_losses = []
train_accuracies = []
val_accuracies = []
correct, loss = 0, 0.0
running_loss = 0.0
total = 0
total_val = 0
val_correct = 0
proximal_term = 0.0
criterion = torch.nn.CrossEntropyLoss()
for epoch in range(epochs):
for images, labels in tqdm(trainloader):
optimizer.zero_grad()
outputs = net(images.to(DEVICE))
# In case where FedPRox strategy is used change the loss, uncomment the below commented part.
# for local_weights, global_weights in zip(net.parameters(), global_params):
# proximal_term += (local_weights - global_weights).norm(2)
# loss = criterion(outputs, labels.to(DEVICE)) + (config["proximal_mu"] / 2) * proximal_term
# loss.backward(retain_graph=True)
# Comment the below part related to loss when using FedProx.
loss = criterion(outputs, labels.to(DEVICE))
loss.backward()
optimizer.step()
running_loss += loss.item()
total += labels.size(0)
correct += (torch.max(outputs.data, 1)[1] == labels).sum().item()
epoch_loss = running_loss / len(trainloader)
epoch_accuracy = 100 * correct / total
train_losses.append(epoch_loss)
train_accuracies.append(epoch_accuracy)
print(f"Epoch {epoch + 1}/{epochs}: Train Loss: {epoch_loss:.4f}, Train Accuracy: {epoch_accuracy:.2f}%")
with torch.no_grad():
for images, labels in tqdm(valloader):
outputs = net(images.to(DEVICE))
labels = labels.to(DEVICE)
total_val += labels.size(0)
val_correct += (torch.max(outputs.data, 1)[1] == labels).sum().item()
epoch_val_accuracy = 100 * val_correct / total_val
val_accuracies.append(epoch_val_accuracy)
print(f"Epoch {epoch + 1}/{epochs}: Val Accuracy: {epoch_val_accuracy:.2f}%")
epsilon = privacy_engine.get_epsilon(delta=PRIVACY_PARAMS["target_delta"])
return train_accuracies, val_accuracies, epsilon
def test(net, testloader, device):
"""Validate the model on the test set."""
criterion = torch.nn.CrossEntropyLoss()
correct, loss = 0, 0.0
with torch.no_grad():
for images, labels in tqdm(testloader):
outputs = net(images.to(device))
labels = labels.to(device)
loss += criterion(outputs, labels).item()
correct += (torch.max(outputs.data, 1)[1] == labels).sum().item()
accuracy = correct / len(testloader.dataset)
test_acc = 100*accuracy
print(f"Test Accuracy: {test_acc:.2f}%")
return loss, accuracy
def prepare_dataset(iid=True):
"""Get CIFAR-10 and return client partitions and global testset."""
dataset = CIFAR10
norm = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
trf = Compose([ToTensor(), norm])
trainset = dataset("./data", train=True, download=True, transform=trf)
testset = dataset("./data", train=False, download=True, transform=trf)
print("Partitioning dataset (IID)..." if iid else "Partitioning dataset (non-IID)...")
if iid:
# Split trainset into `num_partitions` trainsets (IID)
num_images = len(trainset) // NUM_CLIENTS
partition_len = [num_images] * NUM_CLIENTS
trainsets = random_split(trainset, partition_len, torch.Generator().manual_seed(2023))
else:
# Define the non-IID partitioning strategy
NUM_SAMPLES_PER_CLASS = 1000 # samples per class
trainsets = []
for _ in range(NUM_CLIENTS):
# Sample non-IID data for each client
selected_classes = random.sample(range(NUM_CLASSES), 5) # Shuffle classes
# selected_classes = random.sample(range(NUM_CLASSES), num_selected_classes) pass num_selected_classes as argument to function
client_partition = []
for class_id in selected_classes:
# Select a subset of samples from each class
class_indices = [i for i, (_, label) in enumerate(trainset) if label == class_id]
# Randomly select a subset of samples from each class
selected_indices = random.sample(class_indices, NUM_SAMPLES_PER_CLASS)
# Add the selected samples to the client's partition
client_partition.extend([trainset[i] for i in selected_indices])
trainsets.append(client_partition)
val_ratio = 0.1
train_partitions = []
val_partitions = []
for trainset_ in trainsets:
num_total = len(trainset_)
num_val = int(val_ratio * num_total) # 10% for validation
num_train = num_total - num_val
for_train, for_val = random_split(trainset_, [num_train, num_val], torch.Generator().manual_seed(2023))
train_partitions.append(for_train)
val_partitions.append(for_val)
return train_partitions, val_partitions, testset
class FlowerClient(fl.client.NumPyClient):
"""A FlowerClient that trains a CNN model for CIFAR-10 """
def __init__(self, model, trainset, valset, testset):
super().__init__()
self.trainset = trainset
self.valset = valset
self.testset = testset
self.model = model
# dataLoader
trainloader = DataLoader(trainset, batch_size=32, shuffle=True)
self.valloader = DataLoader(valset, batch_size=32)
# Determine device
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.model.to(self.device) # send model to device
optimizer = torch.optim.Adam(self.model.parameters(), lr=0.001)
# Create a privacy engine which will add DP and keep track of the privacy budget.
self.privacy_engine = PrivacyEngine()
# self.model, self.dpoptimizer, self.trainloader = self.privacy_engine.make_private(
# module=model,
# optimizer=optimizer,
# data_loader=trainloader,
# max_grad_norm=PRIVACY_PARAMS["max_grad_norm"],
# noise_multiplier=PRIVACY_PARAMS["noise_multiplier"],
# )
self.model, self.dpoptimizer, self.trainloader = self.privacy_engine.make_private_with_epsilon(
module=model,
optimizer=optimizer,
data_loader=trainloader,
epochs=5,
max_grad_norm=PRIVACY_PARAMS["max_grad_norm"],
# noise_multiplier=PRIVACY_PARAMS["noise_multiplier"],
target_epsilon=PRIVACY_PARAMS.get("target_epsilon", 0.0),
target_delta = PRIVACY_PARAMS["target_delta"],
)
def set_parameters(self, params):
"""Set model weights from a list of NumPy ndarrays."""
params_dict = zip(self.model.state_dict().keys(), params)
state_dict = OrderedDict(
{
k: torch.Tensor(v) if v.shape != torch.Size([]) else torch.Tensor([0])
for k, v in params_dict
}
)
self.model.load_state_dict(state_dict, strict=True)
def get_parameters(self, config):
return [val.cpu().numpy() for _, val in self.model.state_dict().items()]
def fit(self, parameters, config):
print("Client sampled for fit()")
self.set_parameters(parameters)
# Read hyperparameters from config set by the server
epochs = 5
global_params = copy.deepcopy(self.model).parameters()
# Train
_,_,epsilon = train(self.model, self.trainloader, self.valloader, self.dpoptimizer, epochs=epochs,
privacy_engine=self.privacy_engine, device=self.device, global_params=global_params,
config=config)
# Return local model and statistics
print(f"epsilon = {epsilon:.2f}")
return self.get_parameters({}), len(self.trainloader.dataset), {"epsilon": epsilon}
def evaluate(self, parameters, config):
print("Client sampled for evaluate()")
self.set_parameters(parameters)
# Construct dataloader
testloader = DataLoader(self.testset, batch_size=32)
# Evaluate
loss, accuracy = test(self.model, testloader, device=self.device)
# Return statistics
return float(loss), len(testloader.dataset), {"accuracy": float(accuracy)}
def main():
args = parser.parse_args()
print(args)
use_iid = args.iid
assert args.cid < NUM_CLIENTS
# Instantiate model
model = Net()
# Download CIFAR-10 dataset and partition it
trainsets, valsets, testsets = prepare_dataset(use_iid)
# Start Flower client setting its associated data partition
fl.client.start_numpy_client(
server_address=args.server_address,
client=FlowerClient(model, trainset=trainsets[args.cid], valset=valsets[args.cid], testset = testsets
),
)
if __name__ == "__main__":
main()