-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflower_weights.py
281 lines (228 loc) · 9.89 KB
/
flower_weights.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
from collections import OrderedDict
from typing import Callable, Dict, List, Optional, Tuple, Union
from logging import WARNING
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import torch
import torchvision.transforms as transforms
from torchvision.datasets import FashionMNIST
import flwr as fl
from flwr.common import Metrics
from flwr.common import (
EvaluateIns,
EvaluateRes,
FitIns,
FitRes,
MetricsAggregationFn,
NDArrays,
Parameters,
Scalar,
ndarrays_to_parameters,
parameters_to_ndarrays,
)
from flwr.common.logger import log
from flwr.server.client_manager import ClientManager
from flwr.server.client_proxy import ClientProxy
from flwr.server.strategy.aggregate import aggregate, weighted_loss_avg
from torch.utils.data import DataLoader, random_split
from utils.mnist_models import modelA
# User defined functions
from utils.partition import unbal_split
from utils.flower_detection import mal_agents_update_statistics
# Get cpu or gpu device for training.
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using {DEVICE} device")
NUM_CLIENTS = 10
BATCH_SIZE = 64
NUM_CLASSES = 10 #for fashionMNIST #classes= 10
NUM_FL_ROUNDS = 2
NUM_TRAIN_EPOCH = 1
IMBALANCE_RATIO = 0.1
KAPPA = 0.7 # Param for detection algo 2
def load_datasets():
# Define the transformation to Fashion MNIST
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]
)
# Load in the Fashion MNIST dataset
training_data = FashionMNIST(
root='/home/jihoon/data',
train=True,
download=True,
transform=transform
)
test_data = FashionMNIST(
root='/home/jihoon/data',
train=False,
download=True,
transform=transform
)
# Split the training data into NUM_CLIENTS clients
partition_size = len(training_data) // NUM_CLIENTS
lengths = [partition_size] * NUM_CLIENTS
# datasets = unbal_split(training_data, lengths, p=IMBALANCE_RATIO, generator=torch.Generator().manual_seed(42))
datasets = random_split(training_data, lengths, generator=torch.Generator().manual_seed(42))
# Split each partition into train.val and create DataLoader
train_loaders = []
val_loaders = []
i= 0
for ds in datasets:
shuffle_value= True
length_val = len(ds) // 10 # 10% of the partition is used for validation
length_train = len(ds) - length_val
lengths = [length_train, length_val]
ds_train, ds_val = random_split(ds, lengths, generator=torch.Generator().manual_seed(42))
train_loaders.append(DataLoader(ds_train, batch_size=BATCH_SIZE, shuffle=shuffle_value))
val_loaders.append(DataLoader(ds_val, batch_size=BATCH_SIZE))
i += 1
test_loader = DataLoader(test_data, batch_size=BATCH_SIZE)
return train_loaders, val_loaders, test_loader
# Get the data loaders
train_loaders, val_loaders, test_loader = load_datasets()
def train(cid, net, train_loader, epochs: int, verbose=False, global_params= None):
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
net.train()
for epoch in range(epochs):
correct, total, epoch_loss = 0, 0, 0.0
for images, labels in train_loader:
images, labels = images.to(DEVICE), labels.to(DEVICE)
optimizer.zero_grad()
outputs = net(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# Metrics
epoch_loss += loss
total += labels.size(0)
correct += (torch.max(outputs, 1)[1] == labels).sum().item()
epoch_loss /= len(train_loader.dataset)
epoch_acc = correct / total
if verbose:
print(f"Epoch {epoch+1} | Loss: {epoch_loss:.4f} | Acc: {epoch_acc:.4f}")
def test(net, test_loader):
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
criterion = torch.nn.CrossEntropyLoss()
correct, total, loss = 0, 0, 0.0
net.eval()
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(DEVICE), labels.to(DEVICE)
outputs = net(images)
loss += criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
loss /= len(test_loader.dataset)
accuracy = correct / total
return loss, accuracy
def get_parameters(net) -> List[np.ndarray]:
"""Get model parameters as a list of NumPy ndarrays."""
return [val.cpu().numpy() for _, val in net.state_dict().items()]
def set_parameters(net, parameters: List[np.ndarray]) -> None:
"""Set model parameters from a list of NumPy ndarrays."""
params_dict = zip(net.state_dict().keys(), parameters)
state_dict = OrderedDict({k: torch.Tensor(v) for k, v in params_dict})
net.load_state_dict(state_dict, strict=True)
class FlowerClient(fl.client.NumPyClient):
def __init__(self, net, train_loader, val_loader, cid):
self.net = net
self.train_loader = train_loader
self.val_loader = val_loader
self.cid= cid
def get_parameters(self, config):
return get_parameters(self.net)
def fit(self, parameters, config):
set_parameters(self.net, parameters)
train(self.cid, self.net, self.train_loader, epochs=NUM_TRAIN_EPOCH)
return get_parameters(self.net), len(self.train_loader), {}
def evaluate(self, parameters, config):
set_parameters(self.net, parameters)
loss, accuracy = test(self.net, self.val_loader)
return float(loss), len(self.val_loader), {"accuracy": float(accuracy)}
def client_fn(cid: str) -> FlowerClient:
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = modelA().to(DEVICE)
return FlowerClient(net, train_loaders[int(cid)], test_loader], cid)
def weighted_average(metrics: List[Tuple[int, Metrics]]) -> Metrics:
# Multiply accuracy of each client by number of examples used
accuracies = [num_examples * m["accuracy"] for num_examples, m in metrics]
examples = [num_examples for num_examples, _ in metrics]
# Aggregate and return custom metric (weighted average)
return {"accuracy": sum(accuracies) / sum(examples)}
class Custom_FedAvg(fl.server.strategy.FedAvg):
"""
Custom FedAvg strategy that will output each individual client's parameters during after training
It uses the same FedAvg logic, but it will output the parameters of each client after training
"""
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
def aggregate_fit(
self,
server_round: int,
results: List[Tuple[ClientProxy, FitRes]],
failures: List[Union[Tuple[ClientProxy, FitRes], BaseException]],
) -> Tuple[Optional[Parameters], Dict[str, Scalar]]:
"""Aggregate fit results using weighted average."""
if not results:
return None, {}
# Do not aggregate if there are failures and failures are not accepted
if not self.accept_failures and failures:
return None, {}
# Convert results
weights_results = [
(parameters_to_ndarrays(fit_res.parameters), fit_res.num_examples)
for _, fit_res in results
]
# Output the weights from all clients to the terminal
# print(f"Weights: {weights_results}")
# cid and weight mappings
cid_weights_dict = {}
# Get the client ids from the ray workers
client_ids = [client.cid for client, _ in results]
for client_id, weights in zip(client_ids, weights_results):
cid_weights_dict[client_id] = weights
# Detection Algo #2
global KAPPA
mal_agents_update = mal_agents_update_statistics(cid_weights_dict, kappa=KAPPA, debug=False)
print(f"Detection method #2, {KAPPA=}, agents' idx detected {mal_agents_update}")
parameters_aggregated = ndarrays_to_parameters(aggregate(weights_results))
# Aggregate custom metrics if aggregation fn was provided
metrics_aggregated = {}
if self.fit_metrics_aggregation_fn:
fit_metrics = [(res.num_examples, res.metrics) for _, res in results]
metrics_aggregated = self.fit_metrics_aggregation_fn(fit_metrics)
elif server_round == 1: # Only log this warning once
log(WARNING, "No fit_metrics_aggregation_fn provided")
return parameters_aggregated, metrics_aggregated
# This is the aggregration strategy that we are using for our experiments
# We are sampling from all clients and evaluating the model on all their evaluation data
stragegy = Custom_FedAvg(
fraction_fit=1.0, # Sample 100% of available clients for training
fraction_evaluate=1.0, # Sample 100% of available clients for evaluation
min_fit_clients=10, # Never sample less than 10 clients for training
min_evaluate_clients=10, # Never sample less than 5 clients for evaluation
min_available_clients=10, # Wait until all 10 clients are available
evaluate_metrics_aggregation_fn=weighted_average, # Custom aggregation function
)
# Set the number of cpus for how many ray instance you want to have running or being at idle
ray_init_args = {
"include_dashboard": True,
"num_cpus": 6,
}
# Set the number of gpus to be (num of gpus) / (number of running instances)
client_resources = {
"num_gpus": 0.2,
}
# This will start the simulations
hist = fl.simulation.start_simulation(
client_fn=client_fn,
num_clients=NUM_CLIENTS,
config=fl.server.ServerConfig(num_rounds=NUM_FL_ROUNDS),
strategy=stragegy,
ray_init_args=ray_init_args,
client_resources=client_resources,
)