-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1152 lines (925 loc) · 34 KB
/
Copy pathmain.py
File metadata and controls
1152 lines (925 loc) · 34 KB
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import math
import os
import pickle
import random
from functools import partial
from typing import Union
import hydra
import matplotlib.animation as anim
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
# from hydra.core.config_store import ConfigStore
from hydra.utils import instantiate
from omegaconf import DictConfig, OmegaConf
from torch.optim.lr_scheduler import CosineAnnealingLR
import CustomResNet
## [TODO]: Commenting and documentation
# region Neural Networks
class Init_NN:
# "kaiming_uniform_" is the default initialization method from PyTroch
__kwargs_scaling_factors = {
"kaiming_uniform_": {"a": math.sqrt(5)},
"kaiming_normal_": {"a": math.sqrt(5)},
"xavier_uniform_": {"gain": 1},
"xavier_normal_": {"gain": 1},
}
def __init_weights_apply(
self, module: nn.Module, init_method_class: any, kwargs: dict[str, any]
) -> None:
init_method_class(module.weight, **kwargs)
if module.bias is not None:
fan_in, _ = nn.init._calculate_fan_in_and_fan_out(module.weight)
bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0
nn.init.uniform_(module.bias, -bound, bound)
def _init_weights(self, module: nn.Module, init_method: str) -> None:
init_method_class = getattr(nn.init, init_method)
# Apply init_method to weight/bias on Linear and Conv2D Layer.
if isinstance(module, nn.Sequential):
for sub_module in module:
if isinstance(sub_module, (nn.Linear, nn.Conv2d)):
self.__init_weights_apply(
sub_module,
init_method_class,
self.__kwargs_scaling_factors[init_method],
)
elif isinstance(module, (nn.Linear, nn.Conv2d)):
self.__init_weights_apply(
module, init_method_class, self.__kwargs_scaling_factors[init_method]
)
class Custom_Network(nn.Module, Init_NN):
"""
Build custom Neural Network describe in "./configs/model/custom"
"""
def __init__(self, criterion, layers, init_method):
super().__init__()
self.criterion = getattr(nn, criterion)()
self.init = init_method
layers_ = []
# Create the layer based on the configuration
for layer_cfg in layers:
if layer_cfg.arguments is not None:
layer = getattr(nn, layer_cfg.type)(**layer_cfg.arguments)
else:
layer = getattr(nn, layer_cfg.type)()
layers_.append(layer)
if layer_cfg.activation is not None:
layers_.append(getattr(nn, layer_cfg.activation)())
self.sequential = nn.Sequential(*layers_)
init_weight_with_config = partial(self._init_weights, init_method=init_method)
self.apply(init_weight_with_config)
def forward(self, input):
x = self.sequential(input)
return x
class McMahan_2NN(nn.Module, Init_NN):
"""
Define a 2 hidden layer MLP for MNIST digit recognition.
Attributes
----------
flatten : nn.Flatten
Flatten images.
sequential : nn.Sequential
Define internal structure of the MLP.
softmax : nn.Softmax
Softmax the output of the MLP.
"""
def __init__(
self, data_size, label_size, hidden_layer_size, activation, init_method
) -> None:
super().__init__()
# loss(output, target), target's format logits
self.criterion = nn.CrossEntropyLoss()
self.init = init_method
# alias for verbal parameters
activation = getattr(nn, activation)
hl_size = hidden_layer_size
data = data_size
# input.shape => (batch_size, 1, 28, 28)
self.sequential = nn.Sequential(
# x.shape => (batch_size, 1*28*28)
nn.Flatten(),
# x.shape => (batch_size, 200)
nn.Linear(data.c * data.x * data.y, hl_size),
activation(),
# x.shape => (batch_size, 200)
nn.Linear(hl_size, hl_size),
activation(),
# x.shape => (batch_size, 10)
nn.Linear(hl_size, label_size),
nn.Softmax(dim=1),
)
init_weight_with_config = partial(self._init_weights, init_method=init_method)
self.apply(init_weight_with_config)
def forward(self, input):
x = self.sequential(input)
return x
class Custom_ResNet(nn.Module, Init_NN):
def __init__(self, data_size, label_size, layers, activation, init_method):
super().__init__()
block_type, layer_structure = CustomResNet.resnet_depths_to_config(layers)
self.resnet = CustomResNet.ResNet(
channels=data_size.c,
classes=label_size,
block=block_type,
layers=layer_structure,
nonlin=activation,
)
self.criterion = nn.CrossEntropyLoss()
self.init = init_method
init_weight_with_config = partial(self._init_weights, init_method=init_method)
self.apply(init_weight_with_config)
def forward(self, input):
x = self.resnet(input)
return x
class McMahan_CNN(nn.Module, Init_NN):
"""
Define a convolutional neural network for MNIST digit recognition.
Attributes
----------
sequential : nn.Sequential
Define internal structure of the CNN.
softmax : nn.Softmax
Softmax the output of the CNN.
"""
def __init__(self, data_size, label_size, layers, activation, init_method):
super().__init__()
self.criterion = nn.CrossEntropyLoss()
self.init = init_method
# alias for verbal parameters
activation = getattr(nn, activation)
hl_size = layers.hidden_layer_size
data = data_size
conv1 = layers.conv1
conv2 = layers.conv2
# Con2d -> (B, C', (H-kernel_size+1), (W-kernel_size+1))
# MaxPool2d -> (B, C, (H/kernel_size), (W/kernel_size))
size_x = (data.x - conv1.kernel_size + 3) / 2 + 1
size_x = (size_x - conv2.kernel_size + 3) / 2 + 1
size_x = int(size_x)
size_y = (data.y - conv1.kernel_size + 3) / 2 + 1
size_y = (size_y - conv2.kernel_size + 3) / 2 + 1
size_y = int(size_y)
# input.shape => (batch_size, 1, 28, 28)
self.sequential = nn.Sequential(
# (batch_size, 32, 26, 26)
nn.Conv2d(
data.c,
conv1.out_channels,
conv1.kernel_size,
padding=1,
),
nn.BatchNorm2d(conv1.out_channels),
activation(),
# (batch_size, 32, 14, 14)
nn.MaxPool2d(2, padding=1),
# (batch_size, 64, 12, 12)
nn.Conv2d(
conv1.out_channels,
conv2.out_channels,
conv2.kernel_size,
padding=1,
),
nn.BatchNorm2d(conv2.out_channels),
activation(),
# (batch_size, 64, 7, 7)
nn.MaxPool2d(2, padding=1),
# (batch_size, 64 * 7 * 7)
nn.Flatten(),
# (batch_size, 512)
nn.Linear(
conv2.out_channels * size_x * size_y,
hl_size,
),
activation(),
# (batch_size, 10)
nn.Linear(
hl_size,
label_size,
),
)
init_weight_with_config = partial(self._init_weights, init_method=init_method)
self.apply(init_weight_with_config)
def forward(self, input):
x = self.sequential(input)
return x
# endregion
# region Dataset
class Custom_Dataset:
"""
Load dataset from custom settings (Only dataset from pytorch)
"""
def __init__(
self,
name,
download,
shuffle,
mean,
var,
batch_size,
data_size: None,
label_size: None,
):
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, var)]
) # normalise dataset
self.train_dataset = getattr(torchvision.datasets, name)(
root="./data", download=download, train=True, transform=transform
)
self.custom_trainLoader = torch.utils.data.DataLoader(
self.train_dataset, batch_size=batch_size, shuffle=shuffle
)
def get_trainLoader(self):
return self.custom_trainLoader
class MnistDataset:
"""
MNIST Dataset loader.
Attributes
----------
mnist_train : MNIST
Load train data from MNIST dataset.
mnist_loader : DataLoader
Training data loader.
Methods
-------
get_trainloader()
Return the train loader.
"""
def __init__(
self,
download,
shuffle,
mean,
var,
batch_size,
data_size: None,
label_size: None,
):
mnist_transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(mean, var),
] # normalise MNIST dataset
)
self.mnist_train = torchvision.datasets.MNIST(
root="./data",
download=download,
train=True,
transform=mnist_transform,
)
self.mnist_trainLoader = torch.utils.data.DataLoader(
self.mnist_train, batch_size=batch_size, shuffle=shuffle
)
def get_trainLoader(self):
return self.mnist_trainLoader
# endregion
# region Client
class SimpleClient:
"""
Define a client for federated learning.
Attributes
----------
trainLoader : DataLoader
Training data loader.
model : any
Model of NN.
loss : nn.CrossEntropyLoss
Loss function used for the learning
Methods
-------
compute_true_gradient()
Compute the gradient of model parameters based on the loss.
"""
def __init__(
self,
prune,
model,
trainLoader,
label_size,
device="cpu",
) -> None:
"""
Parameters
----------
trainLoader : DataLoader
Training data loader.
model : any
Model of NN.
device : torch.device
Device on which model will run.
"""
self.trainLoader = iter(trainLoader)
self.model = model
self.device = device
self.label_class = label_size
self.prune = prune.type
self.percentage = prune.percentage
def compute_true_gradient(self):
"""
Compute the gradient of model parameters based on the loss.
Randomly set a percentage of gradients to zero.
"""
# Load data and label from the MNIST dataset and send them
# to the device memory.
data, label = next(self.trainLoader)
data, label = data.to(self.device), label.to(self.device)
# The data is normalized within the whole dataset.
# We re-normalize each image individually (maybe use Layer Norm or Instance Norm ?)
data = normalize_torch(data)
self.model.eval() # 在eval模式下均值和方差均为0
preds = self.model(data)
# loss = self.model.criterion(
# preds, F.one_hot(label, num_classes=self.label_class).to(torch.float32)
# )
loss = self.model.criterion(preds, label)
gradient = torch.autograd.grad(loss, self.model.parameters())
gradient = [layer.detach().clone() for layer in gradient]
print("CLIENT INFO: [ ##################### ] 100%\n")
## Why prunning an entire layer ??
## [TODO] Check how prunning works
if self.prune == "random":
print(
f"Pruning mode: {self.prune} | Pruning percentage: {self.percentage}\n"
)
# Randomly set a percentage of gradients to zero
for layer_grad in gradient:
if torch.rand(1).item() < self.percentage:
layer_grad.zero_()
if self.prune == "small":
print(
f"Pruning mode: {self.prune} | Pruning percentage: {self.percentage}\n"
)
# Prune the gradient by setting some elements to zero by magnitudes order.
gradient_magnitudes = [layer.norm().item() for layer in gradient]
sorted_gradients = sorted(
zip(gradient_magnitudes, gradient), key=lambda x: x[0]
)
retain_threshold = sorted_gradients[
int(len(sorted_gradients) * self.percentage)
][0]
# for i, (grad_mag, layer_grad) in enumerate(sorted_gradients):
# if grad_mag <= retain_threshold:
# gradient[i].zero_()
# Zero-out gradients below the threshold
[
gradient[i].zero_()
for i, (grad_mag, _) in enumerate(sorted_gradients)
if grad_mag <= retain_threshold
]
return gradient, data, label
# endregion
# region Attacker
# stop training when the distance continue to increase
class SimpleEarlyStopping:
LoopStopCrit = {
"iteration": 0,
"threshold": 1,
"variation": 2,
"best": 3,
}
def __init__(self, type: str, value: Union[int, float], max_iter: int = 500):
self.loop_criterion = self.LoopStopCrit[type]
self.loop_value = value
self.loop_max_iter = self.loop_value if self.loop_criterion == 0 else max_iter
self.reset()
def reset(self):
# init loop start and stop values
# NB: In mode "iteration", the first condition is alway true;
# the second condition is the stop condition.
self.early_stop = False
self.loop_current = self.loop_value + 1
self.loss_prec = 0
self.best_score = None
self.counter = 0
self.j = 0
def update(self, loss):
# Update loop current value
if self.loop_criterion == 1: # Threshold based distance
self.loop_current = loss
elif self.loop_criterion == 2: # Variation of distance
self.loop_current = abs(self.loss_prec - loss)
elif self.loop_criterion == 3: # Best
if self.best_score is None:
self.best_score = loss
elif loss > self.best_score:
self.counter += 1
else:
self.best_score = loss
self.counter = 0
self.loss_prec = loss
self.j += 1
self.early_stop = (
(self.loop_current < self.loop_value)
or (self.j >= self.loop_max_iter)
or (self.counter >= self.loop_value)
)
class GradAttacker:
"""
Attack on differentiable NN to reconstruct private data.
Attributes
----------
Methods
-------
"""
def __init__(
self,
attacker_criterion,
optimizer,
loss_function,
dummy_batch_size,
dummy_data_size,
dummy_label_size,
model,
device: str = "cpu",
) -> None:
self.device = device
self.model = model
self.optimizer = getattr(torch.optim, optimizer.type)
self.lr = optimizer.learning_rate
self.scheduler = optimizer.scheduler
self.batch_size = dummy_batch_size
self.cfg_data = dummy_data_size
self.cfg_label = dummy_label_size
self.early_stopping = SimpleEarlyStopping(
type=attacker_criterion.type,
value=attacker_criterion.value,
max_iter=attacker_criterion.max_iteration,
)
self.iter = attacker_criterion.value
# Status Check
print()
print("ATTACKER INFO [ ##################### ] 100%")
print(f"Early Stop Type: {attacker_criterion.type}")
print(f"Max Iteration: {self.iter}")
print(f"Optimizer Used: {optimizer.type}")
print(f"Initial Learning Rate: {self.lr}")
print(f"Scheduler Status: {self.scheduler}")
print(f"Loss Function: {loss_function}")
if loss_function == "cosine_similarity":
self.compute_distance_between_grad = self.cosine_similarity
elif loss_function == "MSE":
self.compute_distance_between_grad = self.MSE
else:
self.compute_distance_between_grad = self.cosine_similarity
print(
'WARN : Invalid Loss function. Should be one of "cosine_similarity" or "MSE"'
+ '\nDefault is set to "consine_similarity"'
)
def attack_gradient(self, true_gradient, shared_data={}):
# PyTorch automatically calculates the gradient of the loss with respect to dummy_data
# and stores these gradients in the .grad attribute of dummy_data.
# call the backward() method, PyTorch automatically calculates the gradient of the loss relative to
# all tensors with the requires_grad=True setting.
# init data
dummy_data, dummy_label = self.init_dummy_data()
# init optimizer
if "true_label" in shared_data:
optimizer = self.optimizer([dummy_data], lr=self.lr)
print("True Label is shared")
else:
optimizer = self.optimizer([dummy_data, dummy_label], lr=self.lr)
print("True Label is not shared")
# use scheduler
if self.scheduler:
scheduler = CosineAnnealingLR(optimizer, T_max=self.iter, eta_min=0.00001)
# init history data
history_loss = np.array([])
history_img = np.zeros(
(0, self.batch_size, self.cfg_data.c, self.cfg_data.x, self.cfg_data.y)
)
self.early_stopping.reset()
counter = 0
loss = None
print()
print("START TRAINING ATTACK GRADIENT...")
while not self.early_stopping.early_stop:
counter += 1
if counter % 5 == 0:
print(
f"Current Iteration: {counter} | Current Learning Rate: {optimizer.param_groups[0]['lr']:.3f} | Current Grad Loss: {loss:.4f}",
end="\r",
)
def closure():
optimizer.zero_grad()
# compute_fake
fake_grad = self.compute_fake_gradient(
dummy_data, dummy_label, shared_data=shared_data
)
# compute_distance
loss = self.compute_distance_between_grad(true_gradient, fake_grad)
# Compute backward gradient
loss.backward(retain_graph=True)
return loss
# Optimise dummy data & dummy label
loss = optimizer.step(closure).item()
# Update scheduler
if self.scheduler:
scheduler.step()
# Update history data
history_loss = np.append(history_loss, loss)
history_img = np.append(
history_img,
to_numpy_array(dummy_data.clone().unsqueeze(0)),
axis=0,
)
self.early_stopping.update(loss)
history = {
"distance": history_loss,
"data": history_img,
}
return dummy_data, dummy_label, history
def init_dummy_data(self):
# initialize random images (B,C,W,H) from a normal distribution
dummy_data = torch.randn(
self.batch_size,
self.cfg_data.c,
self.cfg_data.x,
self.cfg_data.y,
requires_grad=True,
device=self.device,
)
dummy_label = torch.randn(
self.batch_size,
self.cfg_label,
requires_grad=True,
device=self.device,
)
return dummy_data, dummy_label
def compute_fake_gradient(self, dummy_data, dummy_label, shared_data={}):
def total_variation_loss(c):
x = c[:, :, 1:, :] - c[:, :, :-1, :]
y = c[:, :, :, 1:] - c[:, :, :, :-1]
loss = torch.sum(torch.abs(x)) + torch.sum(torch.abs(y))
return loss
preds = self.model(dummy_data)
if "true_label" in shared_data:
loss = self.model.criterion(
preds,
F.one_hot(shared_data["true_label"], num_classes=self.cfg_label).to(
torch.float32
),
)
else:
loss = self.model.criterion(
preds,
F.softmax(dummy_label, dim=-1),
)
# Added total variation loss
loss += total_variation_loss(dummy_data)
gradient = torch.autograd.grad(
loss,
self.model.parameters(),
create_graph=True,
)
return gradient
def MSE(self, true_grad, fake_grad):
layer_distance = [
((fg - tg) ** 2).sum() for tg, fg in zip(true_grad, fake_grad)
]
loss = sum(layer_distance)
return loss
def cosine_similarity(self, gradient_data, gradient_rec):
scalar_product = gradient_rec[0].new_zeros(1)
rec_norm = gradient_rec[0].new_zeros(1)
data_norm = gradient_rec[0].new_zeros(1)
count = 0
for rec, data in zip(gradient_rec, gradient_data):
if data.norm().item() != 0: # Check if data gradient is non-zero
scalar_product += (rec * data).sum()
rec_norm += rec.pow(2).sum()
data_norm += data.pow(2).sum()
count += 1
if count == 0:
return 0 # If no non-zero gradients in gradient_data, return 0 similarity
objective = 1 - scalar_product / (rec_norm.sqrt() * data_norm.sqrt())
return objective
# endregion
# region Utils
def set_seed(seed=42):
random.seed(seed) # python
np.random.seed(seed) # numpy
torch.manual_seed(seed) # pytorch
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def device():
return "cuda" if torch.cuda.is_available() else "cpu"
def count_nb_parameters(model):
return sum([p.numel() for p in model.parameters() if p.requires_grad])
def to_numpy_array(var):
return var.detach().cpu().numpy()
def normalize_torch(data):
for i in range(data.shape[0]):
min_ = data[i].min()
max_ = data[i].max()
data[i] = (data[i] - min_) / (max_ - min_)
return data
def normalize(data):
min_ = data.min(axis=(1, 2, 3))
max_ = data.max(axis=(1, 2, 3))
data = np.array(
[
(data[i] - min_) / (max_ - min_)
for i, (min_, max_) in enumerate(zip(min_, max_))
]
)
return data
def PSNR(A, B, dynamic):
EQM = np.mean((A - B) ** 2, axis=(1, 2, 3))
return np.where(EQM == 0, -np.inf, 10 * np.log10(dynamic**2 / EQM))
def visualisation(cfg, fake_data, data, history=None, model=None):
# Copy fake_data to host memory if cuda is used and
# convert to a numpy array.
data = np.transpose(data, (0, 2, 3, 1))
fake_data = np.transpose(fake_data, (0, 2, 3, 1))
# difference = np.abs(np.sum(fake_data - data, axis=3))
difference = normalize(np.abs(fake_data - data))
if history is not None:
x = np.arange(1, history["distance"].shape[0] + 1)
yscale = "log" if cfg.yscale_log else "linear"
if cfg.total_distance:
plt.figure()
plt.plot(x, history["distance"])
plt.title("Difference between True Grad and Fake Grad")
plt.xlabel("Iteration")
plt.xlim(1, history["distance"].shape[0])
plt.ylabel(yscale)
plt.yscale(yscale)
plt.grid()
if cfg.display_images:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
if cfg.normalize:
data = normalize(data)
fake_data = normalize(fake_data)
history["data"] = np.array(
[normalize(elem) for elem in history["data"]]
)
square_size = int(math.sqrt(data.shape[0]))
data_as_grid = (
torchvision.utils.make_grid(
torch.from_numpy(data.transpose(0, 3, 1, 2)), nrow=square_size
)
.numpy()
.transpose(1, 2, 0)
)
ax1.imshow(data_as_grid, cmap="gray", vmin=0, vmax=1)
ax1.set_title("Original data")
ax1.axis("off")
image_diff_as_grid = (
torchvision.utils.make_grid(
torch.from_numpy(difference.transpose(0, 3, 1, 2)),
# torch.from_numpy(difference[..., np.newaxis].transpose(0, 3, 1, 2)),
nrow=square_size,
)
.numpy()
.transpose(1, 2, 0)
)
image_diff = ax2.imshow(image_diff_as_grid, cmap="gray")
fig.colorbar(image_diff, ax=ax2)
ax2.set_title("Difference")
ax2.axis("off")
fake_data_as_grid = (
torchvision.utils.make_grid(
torch.from_numpy(fake_data.transpose(0, 3, 1, 2)), nrow=square_size
)
.numpy()
.transpose(1, 2, 0)
)
ax3.imshow(fake_data_as_grid, cmap="gray", vmin=0, vmax=1)
ax3.set_title("Recovered data")
ax3.axis("off")
first_history_data_as_grid = (
torchvision.utils.make_grid(
torch.from_numpy(history["data"][0]), nrow=square_size
)
.numpy()
.transpose(1, 2, 0)
)
img = ax4.imshow(first_history_data_as_grid, cmap="gray", animated=True)
ax4.axis("off")
def animation(frame):
frame_hist_data_as_grid = (
torchvision.utils.make_grid(
torch.from_numpy(history["data"][frame]), nrow=square_size
)
.numpy()
.transpose(1, 2, 0)
)
img.set_array(frame_hist_data_as_grid) # Update the image data
ax4.set_title(
f"Update: {frame*1 + 1}"
) # Update the title with the frame number
return (img,)
# Animation must be kept in memory
ani = anim.FuncAnimation( # noqa: F841
fig,
animation,
frames=history["data"].shape[0],
interval=cfg.duration // history["data"].shape[0],
)
fig.tight_layout()
else:
if cfg.display_images:
fig = plt.figure()
if cfg.normalize:
data = normalize(data)
fake_data = normalize(fake_data)
plt.subplot(2, 2, 1)
plt.imshow(data[0], cmap="gray", vmin=0, vmax=1)
plt.title("Original data")
plt.axis("off")
plt.subplot(2, 2, (2, 4))
image_diff = plt.imshow(difference, cmap="gray")
plt.colorbar()
plt.title("Difference")
plt.axis("off")
plt.subplot(2, 2, 3)
plt.imshow(fake_data[0], cmap="gray", vmin=0, vmax=1)
plt.title("Recovered data")
plt.axis("off")
fig.tight_layout()
plt.show()
def zero_percentage(gradient, conv=False):
if conv:
print(gradient[4])
zero_count = gradient[4].eq(0).sum().item()
total_elements = gradient[4].numel()
zero_percentage = (zero_count / total_elements) * 100
else:
zero_count = sum(layer.eq(0).sum().item() for layer in gradient)
total_elements = sum(layer.numel() for layer in gradient)
zero_percentage = (zero_count / total_elements) * 100
return zero_percentage
def write_to_csv(data, file_path, fieldnames):
try:
file_exists = os.path.exists(file_path)
with open(file_path, "a", newline="") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames, delimiter=';')
# Write header only if the file is created
if not file_exists:
writer.writeheader()
# Write data
writer.writerow(data)
except Exception as e:
print(f"Error: {e}")
def iDLG(true_gradient):
last_tensor = true_gradient[-1]
negative_indices = [index for index, value in enumerate(last_tensor) if value < 0]
return torch.tensor(negative_indices)
def load_previous_gradients():
try:
with open("./Client_Server/previous_gradients.pkl", "rb") as f:
return pickle.load(f)
except FileNotFoundError:
return {}
def save_gradients(gradients):
with open("previous_gradients.pkl", "wb") as f:
pickle.dump(gradients, f)
f.close()
# endregion
# region Main
@hydra.main(version_base=None, config_path="configs", config_name="config")
def main(cfg: DictConfig):
seed = cfg.seed if "seed" in cfg else 42
set_seed(seed)
visualisation_enabled = "visualisation" in cfg and cfg.visualisation is not None
if visualisation_enabled:
print(f"\nConfiguration used :\n\n{OmegaConf.to_yaml(cfg)}")
device_name = torch.device(device())
if visualisation_enabled:
print(f"Using {device_name} device\n")