-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
539 lines (424 loc) · 17.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import argparse
import copy
import json
import logging
import os
import random
import sys
import time
import warnings
from contextlib import contextmanager
from pathlib import Path
from typing import Dict
import numpy as np
import pandas as pd
import torch
import wandb
from sklearn import metrics
def set_seed(seed, cudnn_enabled=True):
"""for reproducibility
:param seed:
:return:
"""
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.enabled = cudnn_enabled
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def set_logger(args):
logger = logging.getLogger(args.log_name)
logger.setLevel(args.log_level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setLevel(args.log_level)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
log_dir = Path(args.log_dir)
log_dir.mkdir(exist_ok=True)
file_handler = logging.FileHandler(log_dir / f'{args.log_name}_{time.asctime()}.log')
file_handler.setLevel(args.log_level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
def get_device(cuda=True, gpus='0'):
# return torch.device("cuda:" + gpus if torch.cuda.is_available() and cuda else "cpu")
return torch.device("cuda" if torch.cuda.is_available() and cuda else "cpu")
def detach_to_numpy(tensor):
return tensor.detach().cpu().numpy()
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def take(X, Y, classes):
indices = np.isin(Y, classes)
return X[indices], Y[indices]
def pytorch_take(X, Y, classes):
indices = torch.stack([y_ == Y for y_ in classes]).sum(0).bool()
return X[indices], Y[indices]
def lbls1_to_lbls2(Y, l2l):
for (lbls1_class, lbls2_class) in l2l.items():
if isinstance(lbls2_class, list):
for c in lbls2_class:
Y[Y == lbls1_class] = c + 1000
elif isinstance(lbls2_class, int):
Y[Y == lbls1_class] = lbls2_class + 1000
else:
raise NotImplementedError("not a valid type")
return Y - 1000
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = devnull
sys.stderr = devnull
try:
yield
finally:
sys.stdout = old_stdout
sys.stderr = old_stderr
# create folders for saving models and logs
def _init_(out_path, exp_name):
script_path = os.path.dirname(__file__)
script_path = '.' if script_path == '' else script_path
if not os.path.exists(out_path + '/' + exp_name):
os.makedirs(out_path + '/' + exp_name)
# save configurations
os.system('cp -r ' + script_path + '/*.py ' + out_path + '/' + exp_name)
def get_art_dir(args):
art_dir = Path(args.out_dir)
art_dir.mkdir(exist_ok=True, parents=True)
curr = 0
existing = [
int(x.as_posix().split('_')[-1])
for x in art_dir.iterdir() if x.is_dir()
]
if len(existing) > 0:
curr = max(existing) + 1
out_dir = art_dir / f"version_{curr}"
out_dir.mkdir()
return out_dir
def save_experiment(args, results, return_out_dir=False, save_results=True):
out_dir = get_art_dir(args)
json.dump(
vars(args),
open(out_dir / "meta.experiment", "w")
)
# loss curve
if save_results:
json.dump(results, open(out_dir / "results.experiment", "w"))
if return_out_dir:
return out_dir
def topk(true, pred, k):
max_pred = np.argsort(pred, axis=1)[:, -k:] # take top k
two_d_true = np.expand_dims(true, 1) # 1d -> 2d
two_d_true = np.repeat(two_d_true, k, axis=1) # repeat along second axis
return (two_d_true == max_pred).sum() / true.shape[0]
def to_one_hot(y, dtype=torch.double):
# convert a single label into a one-hot vector
y_output_onehot = torch.zeros((y.shape[0], y.max().type(torch.IntTensor) + 1), dtype=dtype, device=y.device)
return y_output_onehot.scatter_(1, y.unsqueeze(1), 1)
def CE_loss(y, y_hat, num_classes, reduction='mean'):
# convert a single label into a one-hot vector
y_output_onehot = torch.zeros((y.shape[0], num_classes), dtype=y_hat.dtype, device=y.device)
y_output_onehot.scatter_(1, y.unsqueeze(1), 1)
if reduction == 'mean':
return - torch.sum(y_output_onehot * torch.log(y_hat + 1e-12), dim=1).mean()
return - torch.sum(y_output_onehot * torch.log(y_hat + 1e-12))
def permute_data_lbls(data, labels):
perm = np.random.permutation(data.shape[0])
return data[perm], labels[perm]
def N_vec(y):
"""
Compute the count vector for PG Multinomial inference
:param x:
:return:
"""
if y.dim() == 1:
N = torch.sum(y)
reminder = N - torch.cumsum(y)[:-2]
return torch.cat((torch.tensor([N]).to(y.device), reminder))
elif y.dim() == 2:
N = torch.sum(y, dim=1, keepdim=True)
reminder = N - torch.cumsum(y, dim=1)[:, :-2]
return torch.cat((N, reminder), dim=1)
else:
raise ValueError("x must be 1 or 2D")
def kappa_vec(y):
"""
Compute the kappa vector for PG Multinomial inference
:param x:
:return:
"""
if y.dim() == 1:
return y[:-1] - N_vec(y) / 2.0
elif y.dim() == 2:
return y[:, :-1] - N_vec(y) / 2.0
else:
raise ValueError("x must be 1 or 2D")
# modified from:
# https://github.com/cornellius-gp/gpytorch/blob/master/gpytorch/utils/cholesky.py
def psd_safe_cholesky(A, upper=False, out=None, jitter=None):
"""Compute the Cholesky decomposition of A. If A is only p.s.d, add a small jitter to the diagonal.
Args:
:attr:`A` (Tensor):
The tensor to compute the Cholesky decomposition of
:attr:`upper` (bool, optional):
See torch.cholesky
:attr:`out` (Tensor, optional):
See torch.cholesky
:attr:`jitter` (float, optional):
The jitter to add to the diagonal of A in case A is only p.s.d. If omitted, chosen
as 1e-6 (float) or 1e-8 (double)
"""
try:
L = torch.cholesky(A, upper=upper, out=out)
return L
except RuntimeError as e:
isnan = torch.isnan(A)
if isnan.any():
raise ValueError(
f"cholesky_cpu: {isnan.sum().item()} of {A.numel()} elements of the {A.shape} tensor are NaN."
)
if jitter is None:
jitter = 1e-6 if A.dtype == torch.float32 else 1e-8
Aprime = A.clone()
jitter_prev = 0
for i in range(5):
jitter_new = jitter * (10 ** i)
Aprime.diagonal(dim1=-2, dim2=-1).add_(jitter_new - jitter_prev)
jitter_prev = jitter_new
try:
L = torch.cholesky(Aprime, upper=upper, out=out)
warnings.warn(
f"A not p.d., added jitter of {jitter_new} to the diagonal",
RuntimeWarning,
)
return L
except RuntimeError:
continue
raise e
def print_calibration(ECE_module, out_dir, lbls_vs_target, file_name, color, temp=1.0):
lbls_preds = torch.tensor(lbls_vs_target)
probs = lbls_preds[:, 1:]
targets = lbls_preds[:, 0]
ece_metrics = ECE_module.forward(probs, targets, (out_dir / file_name).as_posix(),
color=color, temp=temp)
logging.info(f"{file_name}, "
f"ECE: {ece_metrics[0].item():.3f}, "
f"MCE: {ece_metrics[1].item():.3f}, "
f"BRI: {ece_metrics[2].item():.3f}")
def calibration_search(ECE_module, out_dir, lbls_vs_target, color, file_name):
lbls_preds = torch.tensor(lbls_vs_target)
probs = lbls_preds[:, 1:]
targets = lbls_preds[:, 0]
temps = torch.tensor([0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0])
eces = [ECE_module.forward(probs, targets, None, color=color, temp=t)[0].item() for t in temps]
best_temp = round(temps[np.argmin(eces)].item(), 2)
ece_metrics = ECE_module.forward(probs, targets, (out_dir / file_name).as_posix(),
color=color, temp=best_temp)
logging.info(f"{file_name}, "
f"Best Temperature: {best_temp:.3f}, "
f"ECE: {ece_metrics[0].item():.3f}, "
f"MCE: {ece_metrics[1].item():.3f}, "
f"BRI: {ece_metrics[2].item():.3f}")
return best_temp
def offset_client_classes(loader, device):
for i, batch in enumerate(loader):
img, label = tuple(t.to(device) for t in batch)
all_labels = label if i == 0 else torch.cat((all_labels, label))
client_labels, client_indices = torch.sort(torch.unique(all_labels))
label_map = {client_labels[i].item(): client_indices[i].item() for i in range(client_labels.shape[0])}
return label_map
def calc_metrics(results):
total_correct = sum([val['correct'] for val in results.values()])
total_samples = sum([val['total'] for val in results.values()])
avg_loss = np.mean([val['loss'] for val in results.values()])
avg_acc = total_correct / total_samples
return avg_loss, avg_acc
def local_train(args, net, train_loader, pbar, pbar_dict: Dict):
local_net = copy.deepcopy(net)
local_net.train()
optimizer = get_optimizer(args, local_net)
criteria = torch.nn.CrossEntropyLoss()
device = get_device()
train_avg_loss = 0.0
for i in range(args.inner_steps):
for k, batch in enumerate(train_loader):
x, Y = tuple(t.to(device) for t in batch)
optimizer.zero_grad()
# forward prop
pred = local_net(x)
loss = criteria(pred, Y)
# back prop
loss.backward()
# # clip gradients
# torch.nn.utils.clip_grad_norm_(local_net.parameters(), args.clip)
# update local parameters
optimizer.step()
# aggregate losses
train_avg_loss += (loss.item() / Y.shape[0])
pbar_dict.update({"Inner Step": f'{(i + 1)}'.zfill(3),
"Batch": f'{(k + 1)}'.zfill(3),
"Train Current Loss": f'{loss.item():5.2f}'})
pbar.set_postfix(pbar_dict)
# end of for k, batch in enumerate(train_loader):
# end of for i in range(args.inner_steps):
return local_net, train_avg_loss
def get_optimizer(args, network):
return torch.optim.SGD(network.parameters(), lr=args.lr, weight_decay=args.wd, momentum=0.9) \
if args.optimizer == 'sgd' else torch.optim.Adam(network.parameters(), lr=args.lr, weight_decay=args.wd)
@torch.no_grad()
def eval_model(args, global_model, client_ids, loaders):
device = get_device()
# device = get_device(cuda=int(args.gpus) >= 0, gpus=args.gpus)
loss_dict: Dict[str, float] = {}
acc_dict: Dict[str, float] = {}
acc_score_dict: Dict[str, float] = {}
f1s_dict: Dict[str, float] = {}
criteria = torch.nn.CrossEntropyLoss()
y_true_all, y_pred_all, loss_all = None, None, 0.
global_model.eval()
num_clients = len(client_ids)
for i, client_id in enumerate(client_ids):
running_loss, running_correct, running_samples = 0., 0., 0.
test_loader = loaders[client_id]
all_targets = []
all_preds = []
for batch_count, batch in enumerate(test_loader):
X_test, Y_test = tuple(t.to(device) for t in batch)
pred = global_model(X_test)
loss = criteria(pred, Y_test)
predicted = torch.max(pred, dim=1)[1].cpu().numpy()
running_loss += (loss.item() * Y_test.size(0))
running_correct += pred.argmax(1).eq(Y_test).sum().item()
running_samples += Y_test.size(0)
target = Y_test.cpu().numpy().reshape(predicted.shape)
all_targets += target.tolist()
all_preds += predicted.tolist()
# calculate confusion matrix
y_true = np.array(all_targets)
y_pred = np.array(all_preds)
running_loss /= running_samples
y_true_all = y_true if y_true_all is None else np.concatenate((y_true_all, y_true), axis=0)
y_pred_all = y_pred if y_pred_all is None else np.concatenate((y_pred_all, y_pred), axis=0)
loss_all += (running_loss / num_clients)
eval_accuracy = (y_true == y_pred).sum().item() / running_samples
acc_score = metrics.accuracy_score(y_true, y_pred)
f1 = metrics.f1_score(y_true, y_pred, average='micro')
acc_dict[f"P{client_id}"] = eval_accuracy
loss_dict[f"P{client_id}"] = running_loss
acc_score_dict[f"P{client_id}"] = acc_score
f1s_dict[f"P{client_id}"] = f1
avg_acc = (y_true_all == y_pred_all).mean().item()
avg_loss = loss_all
avg_acc_score = metrics.accuracy_score(y_true_all, y_pred_all)
avg_f1 = metrics.f1_score(y_true_all, y_pred_all, average='micro')
return acc_dict, loss_dict, acc_score_dict, f1s_dict, avg_acc, avg_loss, avg_acc_score, avg_f1
def flatten_tensor(tensor_list) -> torch.Tensor:
"""
Taken from https://github.com/dayu11/Gradient-Embedding-Perturbation
"""
for i in range(len(tensor_list)):
tensor_list[i] = tensor_list[i].reshape([tensor_list[i].shape[0], -1])
# tensor_list[i] = tensor_list[i].reshape(1, -1)
flatten_param = torch.cat(tensor_list, dim=1)
del tensor_list
return flatten_param
def get_clients(args):
if args.data_name == 'keypressemg':
import keypressemg_utils
return keypressemg_utils.get_clients(args)
num_clients = args.num_clients
num_private_clients = args.num_private_clients
num_public_clients = args.num_public_clients
assert num_clients >= (num_private_clients + num_public_clients), \
f'num clients should be more than sum of all participating clients. Got {num_clients} clients'
num_dummy_clients = num_clients - (num_private_clients + num_public_clients)
i = 0
public_clients = list(range(i, i + num_public_clients))
i += num_public_clients
private_clients = list(range(i, i + num_private_clients))
i += num_private_clients
dummy_clients = list(range(i, i + num_dummy_clients))
i += num_dummy_clients
return public_clients, private_clients, dummy_clients
def update_frame(args, dp_method, epoch_of_best_val, best_val_acc, test_avg_acc, reconstruction_similarity=0.0):
csv_path = Path(args.csv_path)
csv_path.mkdir(exist_ok=True)
csv_file_path = csv_path / args.csv_name
new_row_dict = {
'timestamp': pd.Timestamp.now(),
'data_name': args.data_name,
'num-steps': args.num_steps,
'optimizer': args.optimizer,
'lr': args.lr,
'num-client-agg': args.num_client_agg,
'clip': args.clip,
'noise-multiplier': args.noise_multiplier,
'seed': args.seed,
'history_size': args.gradients_history_size if dp_method in ['GEP_PUBLIC', 'GEP_PRIVATE'] else 1,
'basis_size': args.basis_size if dp_method in ['GEP_PUBLIC', 'GEP_PRIVATE'] else 1,
'dp_method': dp_method,
'epoch_of_best_val': epoch_of_best_val,
'best_val_acc': best_val_acc,
'test_avg_acc': test_avg_acc,
'reconstruction_similarity': reconstruction_similarity
}
new_row = pd.Series(new_row_dict)
new_row_df = pd.DataFrame([new_row])
if csv_file_path.exists():
df = pd.read_csv(csv_file_path)
df = df[new_row_df.columns]
df = pd.concat([df, new_row_df], ignore_index=True)
else:
df = new_row_df
df.to_csv(csv_file_path, index=False)
def log2wandb(best_acc, best_acc_score, best_epoch, best_f1, best_loss, step, train_avg_loss, val_acc_dict,
val_acc_score_dict, val_avg_acc, val_avg_acc_score, val_avg_f1, val_avg_loss, val_f1s_dict,
val_loss_dict):
log_dict = {}
log_dict.update(
{
'custom_step': step,
'train_loss': train_avg_loss,
'test_avg_loss': val_avg_loss,
'test_avg_acc': val_avg_acc,
'test_avg_acc_score': val_avg_acc_score,
'test_avg_f1': val_avg_f1,
'test_best_loss': best_loss,
'test_best_acc': best_acc,
'test_best_acc_score': best_acc_score,
'test_best_f1': best_f1,
'test_best_epoch': best_epoch
}
)
log_dict.update({f"test_acc_{l}": m for (l, m) in val_acc_dict.items()})
log_dict.update({f"test_loss_{l}": m for (l, m) in val_loss_dict.items()})
log_dict.update({f"test_acc_score_{l}": m for (l, m) in val_acc_score_dict.items()})
log_dict.update({f"test_f1_{l}": m for (l, m) in val_f1s_dict.items()})
wandb.log(log_dict)
@torch.no_grad()
def load_aggregated_grads_to_global_net(aggregated_grads, net, prev_params, global_lr):
# update old parameters using private aggregated grads
params = {}
offset = 0
for n, p in prev_params.items():
num_layer_elements = p.numel()
params[n] = p + global_lr * aggregated_grads[offset: offset + num_layer_elements].reshape(p.shape)
offset += num_layer_elements
# update new parameters of global net
net.load_state_dict(params)
return net