-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
480 lines (416 loc) · 12.6 KB
/
utils.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import numpy as np
import torch
import random
from torchvision import datasets, transforms
import os
from loguru import logger
from dataset.custom_dataset import CustomDataset
def load_data(
client_id: int = 0,
dataset_name: str = "mnist",
num_clients: int = 10,
batch_size: int = 64,
num_workers: int = 0,
data_path=os.path.join(".", "data"),
iid=True,
alpha=0.1,
train=True,
):
logger.info(
f"loading {dataset_name} dataset with iid={iid} and alpha={alpha} for client {client_id}"
)
if dataset_name == "mnist":
# Load MNIST dataset
transform = transforms.Compose(
[
transforms.ToTensor(),
]
)
data = datasets.MNIST(
data_path, train=train, download=True, transform=transform
)
elif dataset_name == "cifar10":
# Load CIFAR-10 dataset
transform = transforms.Compose(
[
transforms.ToTensor(),
]
)
data = datasets.CIFAR10(
data_path, train=train, download=True, transform=transform
)
elif dataset_name == "cifar100":
# Load CIFAR-100 dataset
transform = transforms.Compose(
[
transforms.ToTensor(),
]
)
data = datasets.CIFAR100(
data_path, train=train, download=True, transform=transform
)
elif dataset_name == "emnist":
# Load EMNIST dataset
transform = transforms.Compose([transforms.ToTensor()])
data = datasets.EMNIST(
data_path, train=train, download=True, transform=transform, split="digits"
)
elif dataset_name == "fashionmnist":
# Load FashionMNIST dataset
transform = transforms.Compose([transforms.ToTensor()])
data = datasets.FashionMNIST(
data_path, train=train, download=True, transform=transform
)
elif dataset_name == "amd" or dataset_name == "palm" or dataset_name == "uwf":
# Load AMD dataset
transform = transforms.Compose([transforms.ToTensor()])
data = CustomDataset(
os.path.join(data_path, dataset_name.upper()), train=train, transform=transform
)
else:
raise ValueError(
"Invalid dataset name. Allowed values are: mnist, cifar10, cifar100, fashionmnist, emnist, amd, palm, uwf"
)
if not train:
# Return test data
return torch.utils.data.DataLoader(
data, batch_size=batch_size, shuffle=False, num_workers=num_workers
)
# Partition data among clients using non-iid strategy
num_data = len(data)
data_indices = list(range(num_data))
if iid:
# Partition data using an IID strategy
random.shuffle(data_indices)
data_indices_per_client = num_data // num_clients
start = client_id * data_indices_per_client
end = (client_id + 1) * data_indices_per_client
data = torch.utils.data.Subset(data, data_indices[start:end])
else:
# Partition data using a non-IID strategy for size imbalance
n_classes = 10
if dataset_name == "cifar100":
n_classes = 100
elif dataset_name == "amd":
n_classes = 2
elif dataset_name == "palm":
n_classes = 3
elif dataset_name == "uwf":
n_classes = 5
label_distribution = np.random.dirichlet([alpha] * num_clients, n_classes)
class_idcs = [
np.argwhere(np.array(data.targets) == i).flatten() for i in range(n_classes)
]
client_idcs = [[] for _ in range(num_clients)]
for c, fracs in zip(class_idcs, label_distribution):
for i, idcs in enumerate(
np.split(c, (np.cumsum(fracs)[:-1] * len(c)).astype(int))
):
client_idcs[i] += [idcs]
client_idcs = [np.concatenate(idcs) for idcs in client_idcs]
data = torch.utils.data.Subset(data, client_idcs[client_id])
return torch.utils.data.DataLoader(
data,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers,
drop_last=True,
)
def set_seed(seed: int):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def parse_args():
import argparse
import json
parser = argparse.ArgumentParser(description="AFL-CS")
parser.add_argument(
"--no-cuda", action="store_true", default=False, help="disables CUDA training"
)
parser.add_argument(
"--dataset",
type=str,
default="mnist",
metavar="DATASET",
help="dataset for training (options: mnist, cifar10, cifar100)",
)
parser.add_argument(
"--gpu",
type=int,
default=0,
metavar="N",
help="gpu id used for training (default: 0)",
)
parser.add_argument(
"--verbose",
action="store_true",
default=False,
help="print status messages during training",
)
parser.add_argument(
"--algor",
type=str,
default="fedasync",
help="federated learning algorithm (options: fedasync, fedbuff, aflcs)",
)
parser.add_argument(
"--sync",
action="store_true",
default=False,
help="synchronous or asynchronous training",
)
parser.add_argument(
"--cfg",
type=str,
default="",
help="config file to use (overrides algor and dataset)",
)
# federated learning parameters
parser.add_argument(
"--num_clients",
type=int,
default=10,
metavar="N",
help="number of clients (default: 10)",
)
parser.add_argument(
"--num_select_clients_per_round",
type=int,
default=10,
metavar="N",
help="number of selected clients per round (default: 10)",
)
parser.add_argument(
"--num_rounds",
type=int,
default=100,
metavar="N",
help="number of rounds of training (default: 100)",
)
parser.add_argument(
"--epochs",
type=int,
default=1,
metavar="N",
help="number of local epochs per round (default: 1)",
)
parser.add_argument(
"--batch_size",
type=int,
default=32,
metavar="N",
help="input batch size for training (default: 64)",
)
parser.add_argument(
"--num_workers",
type=int,
default=0,
metavar="N",
help="number of workers for data loading (default: 0)",
)
parser.add_argument(
"--test_batch_size",
type=int,
default=64,
metavar="N",
help="input batch size for testing (default: 64)",
)
parser.add_argument(
"--seed",
type=int,
default=3407,
metavar="S",
help="random seed (default: 3407)",
)
# model parameters
parser.add_argument(
"--model_name",
type=str,
default="cnn",
help="model name (options: cnn, cnn4, resnet18, etc.)",
)
parser.add_argument(
"--in_channels",
type=int,
default=1,
metavar="N",
help="number of input channels (default: 1)",
)
parser.add_argument(
"--num_classes",
type=int,
default=10,
metavar="N",
help="number of classes (default: 10)",
)
parser.add_argument(
"--img_size",
type=int,
default=28,
metavar="N",
help="image size (default: 28)",
)
# optimizer parameters
parser.add_argument(
"--optimizer_name",
type=str,
default="sgd",
help="optimizer name (options: sgd, adam, etc.)",
)
# dictionary of optimizer parameters
parser.add_argument(
"--optimizer_hyperparams",
type=json.loads,
default='{"lr": 0.01, "momentum": 0.5, "weight_decay": 0.0}',
help="optimizer parameters (default: {})",
)
parser.add_argument(
"--lr",
type=float,
default=0.01,
metavar="LR",
help="learning rate (default: 0.01)",
)
parser.add_argument(
"--momentum",
type=float,
default=0.5,
metavar="M",
help="SGD momentum (default: 0.5)",
)
parser.add_argument(
"--weight_decay",
type=float,
default=0.0,
metavar="M",
help="weight decay (default: 0.0)",
)
parser.add_argument(
"--lr_scheduler_name",
type=str,
default="step_lr",
help="lr scheduler name (options: step_lr, exp_lr, etc.)",
)
parser.add_argument(
"--lr_scheduler_hyperparams",
type=json.loads,
default='{"step_size": 10, "gamma": 0.9}',
help="lr scheduler parameters (default: {})",
)
parser.add_argument(
"--loss_name",
type=str,
default="cross_entropy",
help="loss name (options: cross_entropy, mse, etc.)",
)
# data settings
parser.add_argument(
"--data_path",
type=str,
default="./data",
metavar="PATH",
help="path to datasets location (default: ./data)",
)
parser.add_argument(
"--iid",
action="store_true",
default=False,
help="sample iid data from clients",
)
parser.add_argument(
"--alpha",
type=float,
default=0.1,
metavar="A",
help="IID smoothing parameter (default: 0.1)",
)
# logging settings
parser.add_argument(
"--log_interval",
type=int,
default=100,
metavar="N",
help="how many batches to wait before logging training status",
)
parser.add_argument(
"--log_dir",
type=str,
default="./logs",
metavar="PATH",
help="path to logging directory (default: ./logs)",
)
parser.add_argument(
"--log_path",
type=str,
default="",
metavar="PATH",
help="path to logging file",
)
# save settings
parser.add_argument(
"--save_interval",
type=int,
default=10,
metavar="N",
help="how many rounds to wait before saving model",
)
args = parser.parse_args()
args.cfg = (
f"config/config_{args.algor}_{args.dataset}.yaml"
if args.cfg == ""
else args.cfg
)
return args
def get_cfg(args=None):
import ezkfg as ez
import shutil
args_ = parse_args() if args is None else args
cfg = ez.load(args.cfg)
cfg.merge(vars(args_), overwrite=False)
cfg.log_path = f"{'iid' if cfg.iid else 'niid_{}'.format(str(cfg.alpha).replace('.', ''))}_le{cfg.epochs}_r{cfg.num_rounds}_c{cfg.num_clients}_b{cfg.batch_size}/{cfg.dataset}/{cfg.algor}"
# copy the config file to the log directory
if not os.path.exists(f"{cfg.log_dir}/{cfg.log_path}"):
os.makedirs(f"{cfg.log_dir}/{cfg.log_path}", exist_ok=True)
shutil.copyfile(
cfg.cfg,
f"{cfg.log_dir}/{cfg.log_path}/config.yaml",
)
ez.save(cfg, f"{cfg.log_dir}/{cfg.log_path}/config_final.yaml")
return cfg
if __name__ == "__main__":
print("This is a utility file.")
# Test the load_data function
for dataset_name in [
"mnist",
"cifar10",
"cifar100",
"fashionmnist",
"emnist",
]:
print(f"Testing load_data function for {dataset_name} dataset")
train_loader = load_data(
client_id=0,
dataset_name=dataset_name,
num_clients=10,
batch_size=64,
num_workers=0,
data_path=os.path.join(".", "data"),
iid=True,
alpha=0.1,
train=True,
)
test_loader = load_data(
client_id=0,
dataset_name=dataset_name,
num_clients=10,
batch_size=64,
num_workers=0,
data_path=os.path.join(".", "data"),
iid=True,
alpha=0.1,
train=False,
)