-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
1013 lines (828 loc) · 37.5 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
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 os
import logging
import numpy as np
import torch
import torchvision.transforms as transforms
import torch.utils.data as data
from torch.autograd import Variable
import torch.nn.functional as F
import random
from sklearn.metrics import confusion_matrix
from torch.utils.data import DataLoader
from model import *
from datasets import MNIST_truncated, CIFAR10_truncated, CIFAR100_truncated, ImageFolder_custom, SVHN_custom, FashionMNIST_truncated, CustomTensorDataset, CelebA_custom, FEMNIST, Generated, genData, Criteo
from math import sqrt
import torch.nn as nn
import torch.optim as optim
import torchvision.utils as vutils
import time
import random
from models.mnist_model import Generator, Discriminator, DHead, QHead
from config import params
import sklearn.datasets as sk
from sklearn.datasets import load_svmlight_file
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def mkdirs(dirpath):
try:
os.makedirs(dirpath)
except Exception as _:
pass
def load_mnist_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
mnist_train_ds = MNIST_truncated(datadir, train=True, download=True, transform=transform)
mnist_test_ds = MNIST_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = mnist_train_ds.data, mnist_train_ds.target
X_test, y_test = mnist_test_ds.data, mnist_test_ds.target
X_train = X_train.data.numpy()
y_train = y_train.data.numpy()
X_test = X_test.data.numpy()
y_test = y_test.data.numpy()
return (X_train, y_train, X_test, y_test)
def load_fmnist_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
mnist_train_ds = FashionMNIST_truncated(datadir, train=True, download=True, transform=transform)
mnist_test_ds = FashionMNIST_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = mnist_train_ds.data, mnist_train_ds.target
X_test, y_test = mnist_test_ds.data, mnist_test_ds.target
X_train = X_train.data.numpy()
y_train = y_train.data.numpy()
X_test = X_test.data.numpy()
y_test = y_test.data.numpy()
return (X_train, y_train, X_test, y_test)
def load_svhn_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
svhn_train_ds = SVHN_custom(datadir, train=True, download=True, transform=transform)
svhn_test_ds = SVHN_custom(datadir, train=False, download=True, transform=transform)
X_train, y_train = svhn_train_ds.data, svhn_train_ds.target
X_test, y_test = svhn_test_ds.data, svhn_test_ds.target
# X_train = X_train.data.numpy()
# y_train = y_train.data.numpy()
# X_test = X_test.data.numpy()
# y_test = y_test.data.numpy()
return (X_train, y_train, X_test, y_test)
def load_cifar10_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
cifar10_train_ds = CIFAR10_truncated(datadir, train=True, download=True, transform=transform)
cifar10_test_ds = CIFAR10_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = cifar10_train_ds.data, cifar10_train_ds.target
X_test, y_test = cifar10_test_ds.data, cifar10_test_ds.target
# y_train = y_train.numpy()
# y_test = y_test.numpy()
return (X_train, y_train, X_test, y_test)
def load_celeba_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
celeba_train_ds = CelebA_custom(datadir, split='train', target_type="attr", download=True, transform=transform)
celeba_test_ds = CelebA_custom(datadir, split='test', target_type="attr", download=True, transform=transform)
gender_index = celeba_train_ds.attr_names.index('Male')
y_train = celeba_train_ds.attr[:,gender_index:gender_index+1].reshape(-1)
y_test = celeba_test_ds.attr[:,gender_index:gender_index+1].reshape(-1)
# y_train = y_train.numpy()
# y_test = y_test.numpy()
return (None, y_train, None, y_test)
def load_femnist_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
mnist_train_ds = FEMNIST(datadir, train=True, transform=transform, download=True)
mnist_test_ds = FEMNIST(datadir, train=False, transform=transform, download=True)
X_train, y_train, u_train = mnist_train_ds.data, mnist_train_ds.targets, mnist_train_ds.users_index
X_test, y_test, u_test = mnist_test_ds.data, mnist_test_ds.targets, mnist_test_ds.users_index
X_train = X_train.data.numpy()
y_train = y_train.data.numpy()
u_train = np.array(u_train)
X_test = X_test.data.numpy()
y_test = y_test.data.numpy()
u_test = np.array(u_test)
return (X_train, y_train, u_train, X_test, y_test, u_test)
def record_net_data_stats(y_train, net_dataidx_map, logdir):
net_cls_counts = {}
for net_i, dataidx in net_dataidx_map.items():
unq, unq_cnt = np.unique(y_train[dataidx], return_counts=True)
tmp = {unq[i]: unq_cnt[i] for i in range(len(unq))}
net_cls_counts[net_i] = tmp
logger.info('Data statistics: %s' % str(net_cls_counts))
return net_cls_counts
def load_cifar100_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
cifar100_train_ds = CIFAR100_truncated(datadir, train=True, download=True, transform=transform)
cifar100_test_ds = CIFAR100_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = cifar100_train_ds.data, cifar100_train_ds.target
X_test, y_test = cifar100_test_ds.data, cifar100_test_ds.target
# y_train = y_train.numpy()
# y_test = y_test.numpy()
return (X_train, y_train, X_test, y_test)
def load_tinyimagenet_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
xray_train_ds = ImageFolder_custom(datadir+'./train/', transform=transform)
xray_test_ds = ImageFolder_custom(datadir+'./val/', transform=transform)
X_train, y_train = np.array([s[0] for s in xray_train_ds.samples]), np.array([int(s[1]) for s in xray_train_ds.samples])
X_test, y_test = np.array([s[0] for s in xray_test_ds.samples]), np.array([int(s[1]) for s in xray_test_ds.samples])
return (X_train, y_train, X_test, y_test)
def partition_data(dataset, datadir, logdir, partition, n_parties, beta=0.4):
#np.random.seed(2020)
#torch.manual_seed(2020)
if dataset == 'mnist':
X_train, y_train, X_test, y_test = load_mnist_data(datadir)
elif dataset == 'fmnist':
X_train, y_train, X_test, y_test = load_fmnist_data(datadir)
elif dataset == 'cifar10':
X_train, y_train, X_test, y_test = load_cifar10_data(datadir)
elif dataset == 'svhn':
X_train, y_train, X_test, y_test = load_svhn_data(datadir)
elif dataset == 'celeba':
X_train, y_train, X_test, y_test = load_celeba_data(datadir)
elif dataset == 'femnist':
X_train, y_train, u_train, X_test, y_test, u_test = load_femnist_data(datadir)
elif dataset == 'cifar100':
X_train, y_train, X_test, y_test = load_cifar100_data(datadir)
elif dataset == 'tinyimagenet':
X_train, y_train, X_test, y_test = load_tinyimagenet_data(datadir)
elif dataset == 'generated':
X_train, y_train = [], []
for loc in range(4):
for i in range(1000):
p1 = random.random()
p2 = random.random()
p3 = random.random()
if loc > 1:
p2 = -p2
if loc % 2 ==1:
p3 = -p3
if i % 2 == 0:
X_train.append([p1, p2, p3])
y_train.append(0)
else:
X_train.append([-p1, -p2, -p3])
y_train.append(1)
X_test, y_test = [], []
for i in range(1000):
p1 = random.random() * 2 - 1
p2 = random.random() * 2 - 1
p3 = random.random() * 2 - 1
X_test.append([p1, p2, p3])
if p1>0:
y_test.append(0)
else:
y_test.append(1)
X_train = np.array(X_train, dtype=np.float32)
X_test = np.array(X_test, dtype=np.float32)
y_train = np.array(y_train, dtype=np.int32)
y_test = np.array(y_test, dtype=np.int64)
idxs = np.linspace(0,3999,4000,dtype=np.int64)
batch_idxs = np.array_split(idxs, n_parties)
net_dataidx_map = {i: batch_idxs[i] for i in range(n_parties)}
mkdirs("data/generated/")
np.save("data/generated/X_train.npy",X_train)
np.save("data/generated/X_test.npy",X_test)
np.save("data/generated/y_train.npy",y_train)
np.save("data/generated/y_test.npy",y_test)
#elif dataset == 'covtype':
# cov_type = sk.fetch_covtype('./data')
# num_train = int(581012 * 0.75)
# idxs = np.random.permutation(581012)
# X_train = np.array(cov_type['data'][idxs[:num_train]], dtype=np.float32)
# y_train = np.array(cov_type['target'][idxs[:num_train]], dtype=np.int32) - 1
# X_test = np.array(cov_type['data'][idxs[num_train:]], dtype=np.float32)
# y_test = np.array(cov_type['target'][idxs[num_train:]], dtype=np.int32) - 1
# mkdirs("data/generated/")
# np.save("data/generated/X_train.npy",X_train)
# np.save("data/generated/X_test.npy",X_test)
# np.save("data/generated/y_train.npy",y_train)
# np.save("data/generated/y_test.npy",y_test)
elif dataset in ('rcv1', 'SUSY', 'covtype'):
X_train, y_train = load_svmlight_file(datadir+dataset)
X_train = X_train.todense()
num_train = int(X_train.shape[0] * 0.75)
if dataset == 'covtype':
y_train = y_train-1
else:
y_train = (y_train+1)/2
idxs = np.random.permutation(X_train.shape[0])
X_test = np.array(X_train[idxs[num_train:]], dtype=np.float32)
y_test = np.array(y_train[idxs[num_train:]], dtype=np.int32)
X_train = np.array(X_train[idxs[:num_train]], dtype=np.float32)
y_train = np.array(y_train[idxs[:num_train]], dtype=np.int32)
mkdirs("data/generated/")
np.save("data/generated/X_train.npy",X_train)
np.save("data/generated/X_test.npy",X_test)
np.save("data/generated/y_train.npy",y_train)
np.save("data/generated/y_test.npy",y_test)
elif dataset in ('a9a'):
X_train, y_train = load_svmlight_file(datadir+"a9a")
X_test, y_test = load_svmlight_file(datadir+"a9a.t")
X_train = X_train.todense()
X_test = X_test.todense()
X_test = np.c_[X_test, np.zeros((len(y_test), X_train.shape[1] - np.size(X_test[0, :])))]
X_train = np.array(X_train, dtype=np.float32)
X_test = np.array(X_test, dtype=np.float32)
y_train = (y_train+1)/2
y_test = (y_test+1)/2
y_train = np.array(y_train, dtype=np.int32)
y_test = np.array(y_test, dtype=np.int32)
mkdirs("data/generated/")
np.save("data/generated/X_train.npy",X_train)
np.save("data/generated/X_test.npy",X_test)
np.save("data/generated/y_train.npy",y_train)
np.save("data/generated/y_test.npy",y_test)
elif dataset == 'criteo':
path = "./data/criteo/"
if not os.path.exists(path):
os.makedirs(path)
if not os.path.exists("{}criteo_attribution_dataset.tsv".format(path)):
os.chdir(path)
if not os.path.exists("criteo-research-attribution-dataset.zip"):
os.system("wget http://go.criteo.net/criteo-research-attribution-dataset.zip")
os.system("unzip -u criteo-research-attribution-dataset.zip")
os.system("gzip -d criteo_attribution_dataset.tsv.gz")
os.chdir("../..")
f = open("{}criteo_attribution_dataset.tsv".format(path), "r")
all_data = f.readlines()
used_data = []
max_value = [0 for i in range(12)]
for i in range(1, len(all_data)):
current = all_data[i].split()
item = []
for j in range(10,22):
item.append(float(current[j]))
for j in range(12):
if item[j] > max_value[j]:
max_value[j] = item[j]
item.append(int(current[7])) # label
item.append(int(current[1])) # user id
used_data.append(item)
for item in used_data:
for j in range(12):
if item[j] > 0:
item[j] /= max_value[j]
used_data = np.array(used_data, dtype=np.float32)
ordered_data = used_data[np.lexsort(used_data.T)]
net_dataidx_map = {}
party_id = 0
current = np.array([0])
n_train = len(all_data)
for i in range(1, n_train):
if ordered_data[i][-1] == ordered_data[i-1][-1]:
current = np.append(current, [i])
else:
net_dataidx_map[party_id] = current
party_id += 1
current = np.array([i])
if party_id == n_parties:
n_train = i
break
test_id = np.array([])
for i in range(n_train+1, n_train+1000000):
if ordered_data[i][-1] == ordered_data[i-1][-1]:
current = np.append(current, [i])
else:
#if (len(current)>8):
test_id = np.append(test_id, current)
if len(test_id)>=10000:
break
current = np.array([i])
test_id = test_id.astype(np.int32)
X_train = np.array(ordered_data[:n_train,:-2], dtype=np.float32)
y_train = np.array(ordered_data[:n_train,-2], dtype=np.int32)
X_test = np.array(ordered_data[test_id,:-2], dtype=np.float32)
y_test = np.array(ordered_data[test_id,-2], dtype=np.int32)
mkdirs("data/generated/client/")
np.save("data/generated/X_train.npy",X_train)
np.save("data/generated/X_test.npy",X_test)
np.save("data/generated/y_train.npy",y_train)
np.save("data/generated/y_test.npy",y_test)
for i in range(party_id):
np.save("data/generated/client/X_train{}.npy".format(i), X_train[net_dataidx_map[i][0]:net_dataidx_map[i][-1]+1])
np.save("data/generated/client/y_train{}.npy".format(i), y_train[net_dataidx_map[i][0]:net_dataidx_map[i][-1]+1])
n_train = y_train.shape[0]
if dataset == 'criteo':
pass
if partition == "homo":
idxs = np.random.permutation(n_train)
batch_idxs = np.array_split(idxs, n_parties)
net_dataidx_map = {i: batch_idxs[i] for i in range(n_parties)}
elif partition == "noniid-labeldir":
min_size = 0
min_require_size = 10
K = 10
if dataset in ('celeba', 'covtype', 'a9a', 'rcv1', 'SUSY'):
K = 2
# min_require_size = 100
if dataset == 'cifar100':
K = 100
elif dataset == 'tinyimagenet':
K = 200
N = y_train.shape[0]
#np.random.seed(2020)
net_dataidx_map = {}
while min_size < min_require_size:
idx_batch = [[] for _ in range(n_parties)]
for k in range(K):
idx_k = np.where(y_train == k)[0]
np.random.shuffle(idx_k)
proportions = np.random.dirichlet(np.repeat(beta, n_parties))
# logger.info("proportions1: ", proportions)
# logger.info("sum pro1:", np.sum(proportions))
## Balance
proportions = np.array([p * (len(idx_j) < N / n_parties) for p, idx_j in zip(proportions, idx_batch)])
# logger.info("proportions2: ", proportions)
proportions = proportions / proportions.sum()
# logger.info("proportions3: ", proportions)
proportions = (np.cumsum(proportions) * len(idx_k)).astype(int)[:-1]
# logger.info("proportions4: ", proportions)
idx_batch = [idx_j + idx.tolist() for idx_j, idx in zip(idx_batch, np.split(idx_k, proportions))]
min_size = min([len(idx_j) for idx_j in idx_batch])
# if K == 2 and n_parties <= 10:
# if np.min(proportions) < 200:
# min_size = 0
# break
for j in range(n_parties):
np.random.shuffle(idx_batch[j])
net_dataidx_map[j] = idx_batch[j]
elif partition > "noniid-#label0" and partition <= "noniid-#label9":
num = eval(partition[13:])
if dataset in ('celeba', 'covtype', 'a9a', 'rcv1', 'SUSY'):
num = 1
K = 2
else:
K = 10
if dataset == "cifar100":
K = 100
elif dataset == "tinyimagenet":
K = 200
if num == 10:
net_dataidx_map ={i:np.ndarray(0,dtype=np.int64) for i in range(n_parties)}
for i in range(10):
idx_k = np.where(y_train==i)[0]
np.random.shuffle(idx_k)
split = np.array_split(idx_k,n_parties)
for j in range(n_parties):
net_dataidx_map[j]=np.append(net_dataidx_map[j],split[j])
else:
times=[0 for i in range(K)]
contain=[]
for i in range(n_parties):
current=[i%K]
times[i%K]+=1
j=1
while (j<num):
ind=random.randint(0,K-1)
if (ind not in current):
j=j+1
current.append(ind)
times[ind]+=1
contain.append(current)
net_dataidx_map ={i:np.ndarray(0,dtype=np.int64) for i in range(n_parties)}
for i in range(K):
idx_k = np.where(y_train==i)[0]
np.random.shuffle(idx_k)
split = np.array_split(idx_k,times[i])
ids=0
for j in range(n_parties):
if i in contain[j]:
net_dataidx_map[j]=np.append(net_dataidx_map[j],split[ids])
ids+=1
elif partition == "iid-diff-quantity":
idxs = np.random.permutation(n_train)
min_size = 0
while min_size < 10:
proportions = np.random.dirichlet(np.repeat(beta, n_parties))
proportions = proportions/proportions.sum()
min_size = np.min(proportions*len(idxs))
proportions = (np.cumsum(proportions)*len(idxs)).astype(int)[:-1]
batch_idxs = np.split(idxs,proportions)
net_dataidx_map = {i: batch_idxs[i] for i in range(n_parties)}
elif partition == "mixed":
min_size = 0
min_require_size = 10
K = 10
if dataset in ('celeba', 'covtype', 'a9a', 'rcv1', 'SUSY'):
K = 2
# min_require_size = 100
N = y_train.shape[0]
net_dataidx_map = {}
times=[1 for i in range(10)]
contain=[]
for i in range(n_parties):
current=[i%K]
j=1
while (j<2):
ind=random.randint(0,K-1)
if (ind not in current and times[ind]<2):
j=j+1
current.append(ind)
times[ind]+=1
contain.append(current)
net_dataidx_map ={i:np.ndarray(0,dtype=np.int64) for i in range(n_parties)}
min_size = 0
while min_size < 10:
proportions = np.random.dirichlet(np.repeat(beta, n_parties))
proportions = proportions/proportions.sum()
min_size = np.min(proportions*n_train)
for i in range(K):
idx_k = np.where(y_train==i)[0]
np.random.shuffle(idx_k)
proportions_k = np.random.dirichlet(np.repeat(beta, 2))
#proportions_k = np.ndarray(0,dtype=np.float64)
#for j in range(n_parties):
# if i in contain[j]:
# proportions_k=np.append(proportions_k ,proportions[j])
proportions_k = (np.cumsum(proportions_k)*len(idx_k)).astype(int)[:-1]
split = np.split(idx_k, proportions_k)
ids=0
for j in range(n_parties):
if i in contain[j]:
net_dataidx_map[j]=np.append(net_dataidx_map[j],split[ids])
ids+=1
elif partition == "real" and dataset == "femnist":
num_user = u_train.shape[0]
user = np.zeros(num_user+1,dtype=np.int32)
for i in range(1,num_user+1):
user[i] = user[i-1] + u_train[i-1]
no = np.random.permutation(num_user)
batch_idxs = np.array_split(no, n_parties)
net_dataidx_map = {i:np.zeros(0,dtype=np.int32) for i in range(n_parties)}
for i in range(n_parties):
for j in batch_idxs[i]:
net_dataidx_map[i]=np.append(net_dataidx_map[i], np.arange(user[j], user[j+1]))
elif partition == "transfer-from-femnist":
stat = np.load("femnist-dis.npy")
n_total = stat.shape[0]
chosen = np.random.permutation(n_total)[:n_parties]
stat = stat[chosen,:]
if dataset in ('celeba', 'covtype', 'a9a', 'rcv1', 'SUSY'):
K = 2
else:
K = 10
N = y_train.shape[0]
#np.random.seed(2020)
net_dataidx_map = {}
idx_batch = [[] for _ in range(n_parties)]
for k in range(K):
idx_k = np.where(y_train == k)[0]
np.random.shuffle(idx_k)
proportions = stat[:,k]
# logger.info("proportions2: ", proportions)
proportions = proportions / proportions.sum()
# logger.info("proportions3: ", proportions)
proportions = (np.cumsum(proportions) * len(idx_k)).astype(int)[:-1]
# logger.info("proportions4: ", proportions)
idx_batch = [idx_j + idx.tolist() for idx_j, idx in zip(idx_batch, np.split(idx_k, proportions))]
for j in range(n_parties):
np.random.shuffle(idx_batch[j])
net_dataidx_map[j] = idx_batch[j]
elif partition == "transfer-from-criteo":
stat0 = np.load("criteo-dis.npy")
n_total = stat0.shape[0]
flag=True
while (flag):
chosen = np.random.permutation(n_total)[:n_parties]
stat = stat0[chosen,:]
check = [0 for i in range(10)]
for ele in stat:
for j in range(10):
if ele[j]>0:
check[j]=1
flag=False
for i in range(10):
if check[i]==0:
flag=True
break
if dataset in ('celeba', 'covtype', 'a9a', 'rcv1', 'SUSY'):
K = 2
stat[:,0]=np.sum(stat[:,:5],axis=1)
stat[:,1]=np.sum(stat[:,5:],axis=1)
else:
K = 10
N = y_train.shape[0]
#np.random.seed(2020)
net_dataidx_map = {}
idx_batch = [[] for _ in range(n_parties)]
for k in range(K):
idx_k = np.where(y_train == k)[0]
np.random.shuffle(idx_k)
proportions = stat[:,k]
# logger.info("proportions2: ", proportions)
proportions = proportions / proportions.sum()
# logger.info("proportions3: ", proportions)
proportions = (np.cumsum(proportions) * len(idx_k)).astype(int)[:-1]
# logger.info("proportions4: ", proportions)
idx_batch = [idx_j + idx.tolist() for idx_j, idx in zip(idx_batch, np.split(idx_k, proportions))]
for j in range(n_parties):
np.random.shuffle(idx_batch[j])
net_dataidx_map[j] = idx_batch[j]
traindata_cls_counts = record_net_data_stats(y_train, net_dataidx_map, logdir)
return (X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts)
def get_trainable_parameters(net):
'return trainable parameter values as a vector (only the first parameter set)'
trainable=filter(lambda p: p.requires_grad, net.parameters())
# logger.info("net.parameter.data:", list(net.parameters()))
paramlist=list(trainable)
N=0
for params in paramlist:
N+=params.numel()
# logger.info("params.data:", params.data)
X=torch.empty(N,dtype=torch.float64)
X.fill_(0.0)
offset=0
for params in paramlist:
numel=params.numel()
with torch.no_grad():
X[offset:offset+numel].copy_(params.data.view_as(X[offset:offset+numel].data))
offset+=numel
# logger.info("get trainable x:", X)
return X
def put_trainable_parameters(net,X):
'replace trainable parameter values by the given vector (only the first parameter set)'
trainable=filter(lambda p: p.requires_grad, net.parameters())
paramlist=list(trainable)
offset=0
for params in paramlist:
numel=params.numel()
with torch.no_grad():
params.data.copy_(X[offset:offset+numel].data.view_as(params.data))
offset+=numel
def compute_accuracy(model, dataloader, get_confusion_matrix=False, device="cpu"):
was_training = False
if model.training:
model.eval()
was_training = True
true_labels_list, pred_labels_list = np.array([]), np.array([])
if type(dataloader) == type([1]):
pass
else:
dataloader = [dataloader]
model.to(device)
correct, total = 0, 0
with torch.no_grad():
for tmp in dataloader:
for batch_idx, (x, target) in enumerate(tmp):
x, target = x.to(device), target.to(device,dtype=torch.int64)
out = model(x)
_, pred_label = torch.max(out.data, 1)
total += x.data.size()[0]
correct += (pred_label == target.data).sum().item()
if device == "cpu":
pred_labels_list = np.append(pred_labels_list, pred_label.numpy())
true_labels_list = np.append(true_labels_list, target.data.numpy())
else:
pred_labels_list = np.append(pred_labels_list, pred_label.cpu().numpy())
true_labels_list = np.append(true_labels_list, target.data.cpu().numpy())
if get_confusion_matrix:
conf_matrix = confusion_matrix(true_labels_list, pred_labels_list)
if was_training:
model.train()
if get_confusion_matrix:
return correct/float(total), conf_matrix
return correct/float(total)
def compute_accuracy_ae(model_list, dataloader, cate_loss, get_confusion_matrix=False, device="cpu"):
was_training = False
for i in range(10):
model = model_list[i]
if model.training:
model.eval()
was_training = True
true_labels_list, pred_labels_list = np.array([]), np.array([])
if type(dataloader) == type([1]):
pass
else:
dataloader = [dataloader]
loss_function = nn.MSELoss(reduce = False)
correct, total = 0, 0
with torch.no_grad():
for tmp in dataloader:
for batch_idx, (x, target) in enumerate(tmp):
x, target = x.to(device), target.to(device,dtype=torch.int64)
out = []
for i in range(10):
model = model_list[i]
model.to(device)
re = model(x).reshape(x.shape)
loss = loss_function(re, x)
loss = torch.mean(loss, dim = (1, 2, 3)) / cate_loss[i]
out.append(loss.tolist())
out = torch.Tensor(out)
logger.info("out: "+str(out))
pred_label = torch.min(out, dim = 0)[1]
#logger.info(pred_label)
total += x.data.size()[0]
correct += (pred_label == target.data).sum().item()
logger.info("predict: "+str(pred_label))
logger.info("correct: "+str(target.data))
if device == "cpu":
pred_labels_list = np.append(pred_labels_list, pred_label.numpy())
true_labels_list = np.append(true_labels_list, target.data.numpy())
else:
pred_labels_list = np.append(pred_labels_list, pred_label.cpu().numpy())
true_labels_list = np.append(true_labels_list, target.data.cpu().numpy())
if get_confusion_matrix:
conf_matrix = confusion_matrix(true_labels_list, pred_labels_list)
if was_training:
for i in range(10):
model = model_list[i]
model.train()
if get_confusion_matrix:
return correct/float(total), conf_matrix
return correct/float(total)
def save_model(model, model_index, args):
logger.info("saving local model-{}".format(model_index))
with open(args.modeldir+"trained_local_model"+str(model_index), "wb") as f_:
torch.save(model.state_dict(), f_)
return
def load_model(model, model_index, device="cpu"):
#
with open("trained_local_model"+str(model_index), "rb") as f_:
model.load_state_dict(torch.load(f_))
model.to(device)
return model
class AddGaussianNoise(object):
def __init__(self, mean=0., std=1., net_id=None, total=0):
self.std = std
self.mean = mean
self.net_id = net_id
self.num = int(sqrt(total))
if self.num * self.num < total:
self.num = self.num + 1
def __call__(self, tensor):
if self.net_id is None:
return tensor + torch.randn(tensor.size()) * self.std + self.mean
else:
tmp = torch.randn(tensor.size())
filt = torch.zeros(tensor.size())
size = int(28 / self.num)
row = int(self.net_id / size)
col = self.net_id % size
for i in range(size):
for j in range(size):
filt[:,row*size+i,col*size+j] = 1
tmp = tmp * filt
return tensor + tmp * self.std + self.mean
def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)
def get_dataloader(dataset, datadir, train_bs, test_bs, dataidxs=None, noise_level=0, net_id=None, need_test=True, total=0):
if dataset in ('mnist', 'femnist', 'fmnist', 'cifar10', 'svhn', 'generated', 'covtype', 'a9a', 'rcv1', 'SUSY', 'cifar100', 'tinyimagenet', 'criteo'):
if dataset == 'mnist':
dl_obj = MNIST_truncated
transform_train = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
transform_test = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
elif dataset == 'femnist':
dl_obj = FEMNIST
transform_train = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
transform_test = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
elif dataset == 'fmnist':
dl_obj = FashionMNIST_truncated
transform_train = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
transform_test = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
elif dataset == 'svhn':
dl_obj = SVHN_custom
transform_train = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
transform_test = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
elif dataset == 'cifar10':
dl_obj = CIFAR10_truncated
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: F.pad(
Variable(x.unsqueeze(0), requires_grad=False),
(4, 4, 4, 4), mode='reflect').data.squeeze()),
transforms.ToPILImage(),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)
])
# data prep for test set
transform_test = transforms.Compose([
transforms.ToTensor(),
AddGaussianNoise(0., noise_level, net_id, total)])
elif dataset == 'cifar100':
dl_obj = CIFAR100_truncated
normalize = transforms.Normalize(mean=[0.5070751592371323, 0.48654887331495095, 0.4409178433670343],
std=[0.2673342858792401, 0.2564384629170883, 0.27615047132568404])
# transform_train = transforms.Compose([
# transforms.RandomCrop(32),
# transforms.RandomHorizontalFlip(),
# transforms.ToTensor(),
# normalize
# ])
transform_train = transforms.Compose([
# transforms.ToPILImage(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ToTensor(),
normalize
])
# data prep for test set
transform_test = transforms.Compose([
transforms.ToTensor(),
normalize])
elif dataset == 'tinyimagenet':
dl_obj = ImageFolder_custom
transform_train = transforms.Compose([
transforms.Resize(32),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
transform_test = transforms.Compose([
transforms.Resize(32),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
elif dataset == 'criteo':
dl_obj = Criteo
transform_train = None
transform_test = None
else:
dl_obj = Generated
transform_train = None
transform_test = None
if dataset == "tinyimagenet":
train_ds = dl_obj(datadir+'./train/', dataidxs=dataidxs, transform=transform_train)
test_ds = dl_obj(datadir+'./val/', transform=transform_test)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, shuffle=True, drop_last=False)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False, drop_last=False)
return train_dl, test_dl, train_ds, test_ds
if need_test:
train_ds = dl_obj(datadir, dataidxs=dataidxs, train=True, transform=transform_train, download=True)
test_ds = dl_obj(datadir, train=False, transform=transform_test, download=True)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, shuffle=True, drop_last=False)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False, drop_last=False)
return train_dl, test_dl, train_ds, test_ds
elif dataset == 'criteo':
train_ds = dl_obj(datadir, net_id=net_id, train=True, transform=transform_train, download=True)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, shuffle=True, drop_last=False)
return train_dl
else:
train_ds = dl_obj(datadir, dataidxs=dataidxs, train=True, transform=transform_train, download=True)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, shuffle=True, drop_last=False)
return train_dl
def weights_init(m):
"""
Initialise weights of the model.
"""
if(type(m) == nn.ConvTranspose2d or type(m) == nn.Conv2d):
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif(type(m) == nn.BatchNorm2d):
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
class NormalNLLLoss:
"""
Calculate the negative log likelihood
of normal distribution.
This needs to be minimised.
Treating Q(cj | x) as a factored Gaussian.
"""
def __call__(self, x, mu, var):
logli = -0.5 * (var.mul(2 * np.pi) + 1e-6).log() - (x - mu).pow(2).div(var.mul(2.0) + 1e-6)
nll = -(logli.sum(1).mean())
return nll
def noise_sample(choice, n_dis_c, dis_c_dim, n_con_c, n_z, batch_size, device):
"""
Sample random noise vector for training.
INPUT
--------
n_dis_c : Number of discrete latent code.
dis_c_dim : Dimension of discrete latent code.
n_con_c : Number of continuous latent code.
n_z : Dimension of iicompressible noise.
batch_size : Batch Size
device : GPU/CPU
"""
z = torch.randn(batch_size, n_z, 1, 1, device=device)
idx = np.zeros((n_dis_c, batch_size))
if(n_dis_c != 0):
dis_c = torch.zeros(batch_size, n_dis_c, dis_c_dim, device=device)
c_tmp = np.array(choice)
for i in range(n_dis_c):
idx[i] = np.random.randint(len(choice), size=batch_size)
for j in range(batch_size):
idx[i][j] = c_tmp[int(idx[i][j])]
dis_c[torch.arange(0, batch_size), i, idx[i]] = 1.0
dis_c = dis_c.view(batch_size, -1, 1, 1)