forked from haozhaowang/DaFKD2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
846 lines (706 loc) · 31.8 KB
/
data_loader.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
import json
import logging
import os
import random
import h5py
import numpy as np
import torch
import pandas as pd
from datasets import load_dataset
from transformers import AutoTokenizer
from datasets import Dataset
from transformers import DataCollatorWithPadding
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
import logging
import torch.utils.data as data
def read_data(train_data_dir, test_data_dir):
clients = []
groups = []
train_data = {}
test_data = {}
train_files = os.listdir(train_data_dir)
train_files = [f for f in train_files if f.endswith('.json')]
for f in train_files:
file_path = os.path.join(train_data_dir, f)
with open(file_path, 'r') as inf:
cdata = json.load(inf)
clients.extend(cdata['users'])
if 'hierarchies' in cdata:
groups.extend(cdata['hierarchies'])
train_data.update(cdata['user_data'])
test_files = os.listdir(test_data_dir)
test_files = [f for f in test_files if f.endswith('.json')]
for f in test_files:
file_path = os.path.join(test_data_dir, f)
with open(file_path, 'r') as inf:
cdata = json.load(inf)
test_data.update(cdata['user_data'])
clients = sorted(cdata['users'])
return clients, groups, train_data, test_data
def batch_data(data, batch_size, model_name):
data_x = data['x']
data_y = data['y']
if model_name != "lr":
data_x = np.array(data_x).reshape((-1, 1, 28, 28))
# randomly shuffle data
np.random.seed(100)
rng_state = np.random.get_state()
np.random.shuffle(data_x)
np.random.set_state(rng_state)
np.random.shuffle(data_y)
data_x = np.where(data_x > 0, 1, 0)
# loop through mini-batches
batch_data = list()
for i in range(0, len(data_x), batch_size):
batched_x = data_x[i:i + batch_size]
batched_y = data_y[i:i + batch_size]
batched_x = torch.from_numpy(np.asarray(batched_x)).float()
batched_y = torch.from_numpy(np.asarray(batched_y)).long()
batch_data.append((batched_x, batched_y))
return batch_data
def non_iid_partition_with_dirichlet_distribution(label_list,
client_num,
classes,
alpha,
task='classification'):
net_dataidx_map = {}
K = classes
# For multiclass labels, the list is ragged and not a numpy array
N = len(label_list)
# guarantee the minimum number of sample in each client
min_size = 0
while min_size < 100:
# logging.debug("min_size = {}".format(min_size))
idx_batch = [[] for _ in range(client_num)]
for k in range(K):
# get a list of batch indexes which are belong to label k
idx_k = np.where(label_list == k)[0]
idx_batch, min_size = partition_class_samples_with_dirichlet_distribution(N, alpha, client_num,
idx_batch, idx_k)
for i in range(client_num):
np.random.shuffle(idx_batch[i])
net_dataidx_map[i] = idx_batch[i]
return net_dataidx_map
def partition_class_samples_with_dirichlet_distribution(N, alpha, client_num, idx_batch, idx_k):
np.random.shuffle(idx_k)
# using dirichlet distribution to determine the unbalanced proportion for each client (client_num in total)
# e.g., when client_num = 4, proportions = [0.29543505 0.38414498 0.31998781 0.00043216], sum(proportions) = 1
proportions = np.random.dirichlet(np.repeat(alpha, client_num))
# get the index in idx_k according to the dirichlet distribution
proportions = np.array([p * (len(idx_j) < N / client_num) for p, idx_j in zip(proportions, idx_batch)])
proportions = proportions / proportions.sum()
proportions = (np.cumsum(proportions) * len(idx_k)).astype(int)[:-1]
# generate the batch list for each client
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])
return idx_batch, min_size
def noniid_merge_data_with_dirichlet_distribution(client_num_in_total, train_data, test_data, alpha, class_num=10):
new_users = []
new_train_data = {}
new_test_data = {}
all_distillation_data = {"x": [], "y": []}
new_distillation_data = {}
length_train = len(train_data)
length_test = len(test_data)
# alpha = 1
for i in range(client_num_in_total):
if i < 10:
new_users.append("f_0000" + str(i))
else:
new_users.append("f_000" + str(i))
count1 = 0
all_train_data = {"x": [], "y": []}
for (_, value) in train_data.items():
count1 += 1
if count1 / length_train < 0.5:
all_train_data["x"] += value["x"]
all_train_data["y"] += value["y"]
else:
all_distillation_data["x"] += value["x"]
all_distillation_data["y"] += value["y"]
train_label_list = np.asarray(all_train_data["y"])
train_idx_map = non_iid_partition_with_dirichlet_distribution(train_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in train_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_train_data["x"][i] for i in idx_list],
"y": [all_train_data["y"][i] for i in idx_list]}
new_train_data[key] = temp_data
count2 = 0
all_test_data = {"x": [], "y": []}
for (_, value) in test_data.items():
count2 += 1
if count2 / length_test < 1:
all_test_data["x"] += value["x"]
all_test_data["y"] += value["y"]
else:
all_distillation_data["x"] += value["x"]
all_distillation_data["y"] += value["y"]
test_label_list = np.asarray(all_test_data["y"])
test_idx_map = non_iid_partition_with_dirichlet_distribution(test_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in test_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_test_data["x"][i] for i in idx_list],
"y": [all_test_data["y"][i] for i in idx_list]}
new_test_data[key] = temp_data
return new_users, new_train_data, new_test_data
def load_partition_data_mnist(batch_size,
client_num_in_total,
model_name,
alpha,
data_dir="data/MNIST/",
):
class_num = 10
def extract_data(filename, num_data, head_size, data_size):
with gzip.open(filename) as bytestream:
bytestream.read(head_size)
buf = bytestream.read(data_size * num_data)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
return data
new_users = []
groups=[]
new_train_data = {}
all_train_data = {"x": [], "y": []}
new_test_data = {}
all_test_data = {"x": [], "y": []}
for i in range(client_num_in_total):
if i < class_num:
new_users.append("f_0000" + str(i))
else:
new_users.append("f_000" + str(i))
data = extract_data(data_dir + 'train-images-idx3-ubyte.gz', 60000, 16, 28 * 28)
trX = data.reshape((60000, 28, 28, 1))
data = extract_data(data_dir + 'train-labels-idx1-ubyte.gz', 60000, 8, 1)
trY = data.reshape((60000))
data = extract_data(data_dir + 't10k-images-idx3-ubyte.gz', 10000, 16, 28 * 28)
teX = data.reshape((10000, 28, 28, 1))
data = extract_data(data_dir + 't10k-labels-idx1-ubyte.gz', 10000, 8, 1)
teY = data.reshape((10000))
trX = np.asarray(trX).tolist()
teX = np.asarray(teX).tolist()
trY = np.asarray(trY).tolist()
teY = np.asarray(teY).tolist()
for i in range(len(trX)):
all_train_data['x'].append(remake_fashion_mnist(trX[i]))
all_train_data['y'].append(trY[i])
train_label_list = np.asarray(all_train_data["y"])
train_idx_map = non_iid_partition_with_dirichlet_distribution(train_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in train_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_train_data["x"][i] for i in idx_list],
"y": [all_train_data["y"][i] for i in idx_list]}
new_train_data[key] = temp_data
for i in range(len(teX)):
all_test_data['x'].append(remake_fashion_mnist(teX[i]))
all_test_data['y'].append(teY[i])
test_label_list = np.asarray(all_test_data["y"])
test_idx_map = non_iid_partition_with_dirichlet_distribution(test_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in test_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_test_data["x"][i] for i in idx_list],
"y": [all_test_data["y"][i] for i in idx_list]}
new_test_data[key] = temp_data
if len(groups) == 0:
groups = [None for _ in new_users]
train_data_num = 0
test_data_num = 0
train_data_local_dict = dict()
test_data_local_dict = dict()
train_data_local_num_dict = dict()
train_data_global = list()
test_data_global = list()
distillation_data_global = list()
client_idx = 0
logging.info("loading data...")
for u, g in zip(new_users, groups):
user_train_data_num = len(new_train_data[u]['x'])
user_test_data_num = len(new_test_data[u]['x'])
train_data_num += user_train_data_num
test_data_num += user_test_data_num
train_data_local_num_dict[client_idx] = user_train_data_num
# transform to batches
train_batch = batch_data(new_train_data[u], batch_size, model_name)
test_batch = batch_data(new_test_data[u], batch_size, model_name)
# index using client index
train_data_local_dict[client_idx] = train_batch
test_data_local_dict[client_idx] = test_batch
train_data_global += train_batch
test_data_global += test_batch
logging.info("client_idx = %d, batch_num_train_local = %d, batch_num_test_local = %d" % (
client_idx, len(train_batch), len(test_batch)))
client_idx += 1
logging.info("finished the loading data")
client_num = client_idx
class_num = 10
return client_num, train_data_num, test_data_num, train_data_global, test_data_global, \
train_data_local_num_dict, train_data_local_dict, test_data_local_dict, class_num
def load_partition_distillation_data_mnist(batch_size,
client_num_in_total,
model_name,
alpha,
data_dir="data/MNIST/",
):
class_num = 10
def extract_data(filename, num_data, head_size, data_size):
with gzip.open(filename) as bytestream:
bytestream.read(head_size)
buf = bytestream.read(data_size * num_data)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
return data
new_users = []
groups=[]
new_train_data = {}
all_train_data = {"x": [], "y": []}
new_test_data = {}
all_test_data = {"x": [], "y": []}
for i in range(client_num_in_total):
if i < class_num:
new_users.append("f_0000" + str(i))
else:
new_users.append("f_000" + str(i))
data = extract_data(data_dir + 'train-images-idx3-ubyte.gz', 60000, 16, 28 * 28)
trX = data.reshape((60000, 28, 28, 1))
data = extract_data(data_dir + 'train-labels-idx1-ubyte.gz', 60000, 8, 1)
trY = data.reshape((60000))
data = extract_data(data_dir + 't10k-images-idx3-ubyte.gz', 10000, 16, 28 * 28)
teX = data.reshape((10000, 28, 28, 1))
data = extract_data(data_dir + 't10k-labels-idx1-ubyte.gz', 10000, 8, 1)
teY = data.reshape((10000))
trX = np.asarray(trX).tolist()
teX = np.asarray(teX).tolist()
trY = np.asarray(trY).tolist()
teY = np.asarray(teY).tolist()
for i in range(len(trX)):
all_train_data['x'].append(remake_fashion_mnist(trX[i]))
all_train_data['y'].append(trY[i])
train_label_list = np.asarray(all_train_data["y"])
train_idx_map = non_iid_partition_with_dirichlet_distribution(train_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in train_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_train_data["x"][i] for i in idx_list],
"y": [all_train_data["y"][i] for i in idx_list]}
new_train_data[key] = temp_data
for i in range(len(teX)):
all_test_data['x'].append(remake_fashion_mnist(teX[i]))
all_test_data['y'].append(teY[i])
test_label_list = np.asarray(all_test_data["y"])
test_idx_map = non_iid_partition_with_dirichlet_distribution(test_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in test_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_test_data["x"][i] for i in idx_list],
"y": [all_test_data["y"][i] for i in idx_list]}
new_test_data[key] = temp_data
if len(groups) == 0:
groups = [None for _ in new_users]
train_data_num = 0
test_data_num = 0
train_data_local_dict = dict()
test_data_local_dict = dict()
train_data_local_num_dict = dict()
train_data_global = list()
test_data_global = list()
client_idx = 0
logging.info("loading data...")
for u, g in zip(new_users, groups):
user_train_data_num = len(new_train_data[u]['x'])
user_test_data_num = len(new_test_data[u]['x'])
train_data_num += user_train_data_num
test_data_num += user_test_data_num
train_data_local_num_dict[client_idx] = user_train_data_num
# transform to batches
train_batch = batch_data(new_train_data[u], batch_size, model_name)
test_batch = batch_data(new_test_data[u], batch_size, model_name)
# index using client index
train_data_local_dict[client_idx] = train_batch
test_data_local_dict[client_idx] = test_batch
train_data_global += train_batch
test_data_global += test_batch
client_idx += 1
logging.info("finished the loading distillation data")
class_num = 10
return test_data_global
def remake(pic,size=28):
new = [(1-i)*255 for i in pic]
new_pic = np.array(new).reshape(size,size)
mu = np.mean(new_pic.astype(np.float32),0)
sigma = np.std(new_pic.astype(np.float32),0)
new_pic2 = (new_pic.astype(np.float32)-mu)/(sigma+0.001)
return new_pic2.flatten().tolist()
import gzip
def remake_fashion_mnist(pic,size=28):
new_pic = []
for i in range(len(pic)):
for j in range(size):
new_pic.append(pic[i][j][0])
return remake(new_pic)
def load_partition_data_fashion_mnist(batch_size,
client_num_in_total,
model_name,
alpha,
data_dir="data/FASHION_MNIST/",
):
class_num = 10
def extract_data(filename, num_data, head_size, data_size):
with gzip.open(filename) as bytestream:
bytestream.read(head_size)
buf = bytestream.read(data_size * num_data)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
return data
new_users = []
groups=[]
new_train_data = {}
all_train_data = {"x": [], "y": []}
new_test_data = {}
all_test_data = {"x": [], "y": []}
for i in range(client_num_in_total):
if i < class_num:
new_users.append("f_0000" + str(i))
else:
new_users.append("f_000" + str(i))
data = extract_data(data_dir + 'train-images-idx3-ubyte.gz', 60000, 16, 28 * 28)
trX = data.reshape((60000, 28, 28, 1))
data = extract_data(data_dir + 'train-labels-idx1-ubyte.gz', 60000, 8, 1)
trY = data.reshape((60000))
data = extract_data(data_dir + 't10k-images-idx3-ubyte.gz', 10000, 16, 28 * 28)
teX = data.reshape((10000, 28, 28, 1))
data = extract_data(data_dir + 't10k-labels-idx1-ubyte.gz', 10000, 8, 1)
teY = data.reshape((10000))
trX = np.asarray(trX).tolist()
teX = np.asarray(teX).tolist()
trY = np.asarray(trY).tolist()
teY = np.asarray(teY).tolist()
for i in range(len(trX)):
all_train_data['x'].append(remake_fashion_mnist(trX[i]))
all_train_data['y'].append(trY[i])
train_label_list = np.asarray(all_train_data["y"])
train_idx_map = non_iid_partition_with_dirichlet_distribution(train_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in train_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_train_data["x"][i] for i in idx_list],
"y": [all_train_data["y"][i] for i in idx_list]}
new_train_data[key] = temp_data
for i in range(len(teX)):
all_test_data['x'].append(remake_fashion_mnist(teX[i]))
all_test_data['y'].append(teY[i])
test_label_list = np.asarray(all_test_data["y"])
test_idx_map = non_iid_partition_with_dirichlet_distribution(test_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in test_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_test_data["x"][i] for i in idx_list],
"y": [all_test_data["y"][i] for i in idx_list]}
new_test_data[key] = temp_data
if len(groups) == 0:
groups = [None for _ in new_users]
train_data_num = 0
test_data_num = 0
train_data_local_dict = dict()
test_data_local_dict = dict()
train_data_local_num_dict = dict()
train_data_global = list()
test_data_global = list()
distillation_data_global = list()
client_idx = 0
logging.info("loading data...")
for u, g in zip(new_users, groups):
user_train_data_num = len(new_train_data[u]['x'])
user_test_data_num = len(new_test_data[u]['x'])
train_data_num += user_train_data_num
test_data_num += user_test_data_num
train_data_local_num_dict[client_idx] = user_train_data_num
# transform to batches
train_batch = batch_data(new_train_data[u], batch_size, model_name)
test_batch = batch_data(new_test_data[u], batch_size, model_name)
# index using client index
train_data_local_dict[client_idx] = train_batch
test_data_local_dict[client_idx] = test_batch
train_data_global += train_batch
test_data_global += test_batch
logging.info("client_idx = %d, batch_num_train_local = %d, batch_num_test_local = %d" % (
client_idx, len(train_batch), len(test_batch)))
client_idx += 1
logging.info("finished the loading data")
client_num = client_idx
class_num = 10
return client_num, train_data_num, test_data_num, train_data_global, test_data_global, \
train_data_local_num_dict, train_data_local_dict, test_data_local_dict, class_num
def load_partition_data_emnist(batch_size,
client_num_in_total,
model_name,
alpha,
train_path="data/EMINIST/datasets/fed_emnist_train.h5",
test_path="data/EMINIST/datasets/fed_emnist_test.h5"):
_EXAMPLE = 'examples'
_IMGAE = 'pixels'
_LABEL = 'label'
client_idx = None
class_num = 62
new_users = []
groups=[]
for i in range(client_num_in_total):
if i < 62:
new_users.append("f_0000" + str(i))
else:
new_users.append("f_000" + str(i))
train_h5 = h5py.File(train_path, 'r')
new_train_data = {}
all_train_data = {"x": [], "y": []}
client_ids_train = list(train_h5[_EXAMPLE].keys())
train_data_global = list()
if client_idx is None:
# get ids of all clients
train_ids = client_ids_train[:]
else:
# get ids of single client
train_ids = [client_ids_train[client_idx]]
for client_id in train_ids:
temp = train_h5[_EXAMPLE][client_id][_LABEL][()]
for index, label in enumerate(temp):
if label < 100:
x = np.array(train_h5[_EXAMPLE][client_id][_IMGAE][index]).flatten().tolist()
y = train_h5[_EXAMPLE][client_id][_LABEL][index].tolist()
all_train_data['x'].append(remake(x))
all_train_data['y'].append(y)
train_label_list = np.asarray(all_train_data["y"])
train_idx_map = non_iid_partition_with_dirichlet_distribution(train_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in train_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_train_data["x"][i] for i in idx_list],
"y": [all_train_data["y"][i] for i in idx_list]}
new_train_data[key] = temp_data
test_h5 = h5py.File(test_path, 'r')
new_test_data = {}
all_test_data = {"x": [], "y": []}
client_ids_test = list(test_h5[_EXAMPLE].keys())
test_data_global = list()
# load data
if client_idx is None:
# get ids of all clients
test_ids = client_ids_test[:]
else:
# get ids of single client
test_ids = [client_ids_test[client_idx]]
# load data in numpy format from h5 file
for client_id in test_ids:
temp = test_h5[_EXAMPLE][client_id][_LABEL][()]
for index, label in enumerate(temp):
if label < 100:
x = np.array(test_h5[_EXAMPLE][client_id][_IMGAE][index]).flatten().tolist()
y = test_h5[_EXAMPLE][client_id][_LABEL][index].tolist()
all_test_data['x'].append(remake(x))
all_test_data['y'].append(y)
test_label_list = np.asarray(all_test_data["y"])
test_idx_map = non_iid_partition_with_dirichlet_distribution(test_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in test_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_test_data["x"][i] for i in idx_list],
"y": [all_test_data["y"][i] for i in idx_list]}
new_test_data[key] = temp_data
if len(groups) == 0:
groups = [None for _ in new_users]
train_data_num = 0
test_data_num = 0
train_data_local_dict = dict()
test_data_local_dict = dict()
train_data_local_num_dict = dict()
train_data_global = list()
test_data_global = list()
distillation_data_global = list()
client_idx = 0
logging.info("loading data...")
for u, g in zip(new_users, groups):
user_train_data_num = len(new_train_data[u]['x'])
user_test_data_num = len(new_test_data[u]['x'])
train_data_num += user_train_data_num
test_data_num += user_test_data_num
train_data_local_num_dict[client_idx] = user_train_data_num
# transform to batches
train_batch = batch_data(new_train_data[u], batch_size, model_name)
test_batch = batch_data(new_test_data[u], batch_size, model_name)
# index using client index
train_data_local_dict[client_idx] = train_batch
test_data_local_dict[client_idx] = test_batch
train_data_global += train_batch
test_data_global += test_batch
logging.info("client_idx = %d, batch_num_train_local = %d, batch_num_test_local = %d" % (
client_idx, len(train_batch), len(test_batch)))
client_idx += 1
logging.info("finished the loading data")
client_num = client_idx
class_num = 62
return client_num, train_data_num, test_data_num, train_data_global, test_data_global, \
train_data_local_num_dict, train_data_local_dict, test_data_local_dict, class_num
def load_partition_distillation_data_emnist(batch_size,
client_num_in_total,
model_name,
alpha,
test_path="data/EMINIST/datasets/fed_emnist_test.h5"):
_EXAMPLE = 'examples'
_IMGAE = 'pixels'
_LABEL = 'label'
client_idx = None
class_num = 10
test_h5 = h5py.File(test_path, 'r')
new_users = []
new_test_data = {}
all_test_data = {"x": [], "y": []}
client_ids_test = list(test_h5[_EXAMPLE].keys())
test_data_global = list()
# load data
if client_idx is None:
# get ids of all clients
test_ids = client_ids_test[:]
else:
# get ids of single client
test_ids = [client_ids_test[client_idx]]
# load data in numpy format from h5 file
for client_id in test_ids:
temp = test_h5[_EXAMPLE][client_id][_LABEL][()]
for index, label in enumerate(temp):
if label < 10:
x = np.array(test_h5[_EXAMPLE][client_id][_IMGAE][index]).flatten().tolist()
y = test_h5[_EXAMPLE][client_id][_LABEL][index].tolist()
all_test_data['x'].append(remake(x))
all_test_data['y'].append(y)
for i in range(client_num_in_total):
if i < 10:
new_users.append("f_0000" + str(i))
else:
new_users.append("f_000" + str(i))
test_label_list = np.asarray(all_test_data["y"])
test_idx_map = non_iid_partition_with_dirichlet_distribution(test_label_list, client_num_in_total, class_num,
alpha)
for index, idx_list in test_idx_map.items():
key = new_users[index]
temp_data = {"x": [all_test_data["x"][i] for i in idx_list],
"y": [all_test_data["y"][i] for i in idx_list]}
new_test_data[key] = temp_data
logging.info("loading distillation_data...")
for u in new_users:
test_batch = batch_data(new_test_data[u], batch_size, model_name)
test_data_global += test_batch
logging.info("finish loading distillation_data...")
return test_data_global
class ToxicComments(data.Dataset):
def __init__(self,
root="vmalperovich/toxic_comments",
dataidxs=None,
max_seq_length=128,
tokenizer=None,
split="train"):
self.root = root
self.split = split
self.dataset = load_dataset(self.root)
self.dataset = self.dataset.rename_column("label", "labels")
self.label_names = self.dataset["train"].features["labels"].feature.names
self.max_seq_length = max_seq_length
self.get_ids2label = lambda ids: [self.label_names[t] for t in ids]
self.num_labels = len(self.label_names)
self.tokenizer = tokenizer
self.tokenize = lambda x: self.tokenizer(
x["text"], truncation=True, max_length=self.max_seq_length
)
self.labeled_size = 400
self.unlabeled_size = 3000
self.full_size = self.labeled_size + self.unlabeled_size
multiplier = int(np.log2(self.full_size / self.labeled_size)) - 1
self.multiplier = max(1, multiplier)
self.tokenized_data = self.gen_tokenized_data()
def get_bool_labels(self, labels, num_classes):
new_labels = np.zeros(num_classes, dtype=bool)
for i in labels:
new_labels[i] = True
return {"labels": new_labels}
def gen_tokenized_data(self):
tokenize = lambda x: self.tokenizer(
x["text"], truncation=True, max_length=self.max_seq_length
)
tokenized_dataset = self.dataset.map(tokenize, batched=True)
tokenized_dataset = tokenized_dataset.map(
lambda x: self.get_bool_labels(x["labels"], self.num_labels)
)
tokenized_dataset = tokenized_dataset.select_columns(
["input_ids", "attention_mask", "labels"]
)
print(tokenized_dataset)
tokenized_train_df = tokenized_dataset["train"].to_pandas()
tokenized_train_df_labeled = tokenized_train_df.sample(self.labeled_size)
tokenized_train_df_labeled["labeled_mask"] = True
tokenized_train_df = tokenized_train_df.sample(self.unlabeled_size)
tokenized_train_df["labeled_mask"] = False
tokenized_train_df["labels"] = tokenized_train_df["labels"].apply(
lambda x: np.ones(self.num_labels, np.int64) * -100
)
for _ in range(self.multiplier):
tokenized_train_df = pd.concat([tokenized_train_df, tokenized_train_df_labeled])
tokenized_dataset["train"] = Dataset.from_pandas(
tokenized_train_df_labeled, preserve_index=False
)
return tokenized_dataset
def __len__(self):
return len(self.tokenized_data[self.split])
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
return self.tokenized_data[self.split][idx]
def get_dataloader_ToxicComments(datadir, train_bs, test_bs, dataidxs1=None, dataidxs2=None):
dl_obj = ToxicComments
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
np.random.seed(42)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
train_tokenized_dataset = dl_obj(datadir, dataidxs=dataidxs1, tokenizer=tokenizer, split='train')
validation_tokenized_dataset = dl_obj(datadir, dataidxs=dataidxs1, tokenizer=tokenizer, split='validation')
train_dl = DataLoader(
train_tokenized_dataset,
batch_size=train_bs,
sampler=RandomSampler(train_tokenized_dataset),
collate_fn=data_collator,
pin_memory=True,
)
test_dl = DataLoader(
validation_tokenized_dataset,
batch_size=test_bs,
sampler=SequentialSampler(validation_tokenized_dataset),
collate_fn=data_collator,
pin_memory=True,
)
train_data = list()
for batch_idx, (batched_data) in enumerate(train_dl):
train_data.append(batched_data)
test_data = list()
for batch_idx, (batched_data) in enumerate(test_dl):
test_data.append(batched_data)
return train_data, test_data
def load_partition_data_ToxCom(data_dir, batch_size, client_num_in_total, partition_alpha):
class_num = 7
new_users = []
for client_idx in range(client_num_in_total):
new_users.append(client_idx)
train_data_num = 0
test_data_num = 0
train_data_local_dict = dict()
test_data_local_dict = dict()
data_local_num_dict = dict()
train_data_global, test_data_global = get_dataloader_ToxicComments(data_dir, batch_size, batch_size)
new_users, new_train_data, new_test_data = noniid_merge_data_with_dirichlet_distribution(client_num_in_total, train_data_global, test_data_global, partition_alpha, 7)
for client_idx in range(client_num_in_total):
data_local_num_dict[client_idx] = len(new_train_data[client_idx])
train_data_local_dict[client_idx] = new_train_data[client_idx]
test_data_local_dict[client_idx] = new_test_data[client_idx]
train_data_num += len(new_train_data[client_idx])
test_data_num += len(new_test_data[client_idx])
return client_num_in_total, train_data_num, test_data_num, train_data_global, test_data_global, \
data_local_num_dict, train_data_local_dict, test_data_local_dict, class_num
if __name__ == '__main__':
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
print("finish")