-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataloader.py
216 lines (186 loc) · 7.86 KB
/
dataloader.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
# https://github.com/AshwinRJ/Federated-Learning-PyTorch
import torch
from torchvision import datasets
import torchvision.transforms as transforms
import numpy as np
# torch.manual_seed(0)
#
# g = torch.Generator()
# g.manual_seed(0)
#
# np.random.seed(0)
# TODO correct and use this or utils not both
def cifar_iid(dataset, num_users):
"""
Sample I.I.D. client data from CIFAR10 dataset
:param dataset:
:param num_users:
:return: dict of image index
"""
num_items = int(len(dataset) / num_users)
dict_users = {}
all_idxs = [i for i in range(len(dataset))]
for i in range(num_users):
dict_users[i] = set(np.random.choice(all_idxs, num_items,
replace=False)) # i.i.d. selection from dataset
all_idxs = list(set(all_idxs) - dict_users[i])
return dict_users
def cifar_noniid(dataset, num_users):
"""
Sample non-I.I.D client data from CIFAR10 dataset
:param dataset:
:param num_users:
:return: dict of image index
"""
num_shards, num_imgs = 200, 250
idx_shard = [i for i in range(num_shards)]
dict_users = {i: np.array([]) for i in range(num_users)}
idxs = np.arange(num_shards * num_imgs) # 250*200=50000 -> train dataset dimension
# labels = dataset.targets.numpy()
labels = np.array(dataset.targets)
# sort labels
idxs_labels = np.vstack((idxs, labels))
idxs_labels = idxs_labels[:, idxs_labels[1, :].argsort()]
idxs = idxs_labels[0, :]
# divide and assign
for i in range(num_users):
rand_set = set(np.random.choice(idx_shard, 2, replace=False))
idx_shard = list(set(idx_shard) - rand_set)
for rand in rand_set:
dict_users[i] = np.concatenate(
(dict_users[i], idxs[rand * num_imgs:(rand + 1) * num_imgs]), axis=0)
return dict_users
# TODO change this function in order to have clients with access to only some specific data
def cifar_noniid_unbalanced(dataset, num_users):
"""
Sample non-I.I.D client data from CIFAR10 dataset such that
clients have unequal amount of data
:param dataset:
:param num_users:
:returns a dict of clients with each clients assigned certain
number of training imgs
"""
# 50,000 training imgs --> 50 imgs/shard X 1000 shards
num_shards, num_imgs = 1000, 50
idx_shard = [i for i in range(num_shards)]
dict_users = {i: np.array([]) for i in range(num_users)}
idxs = np.arange(num_shards*num_imgs)
# labels = dataset.targets.numpy()
labels = np.array(dataset.targets)
# sort labels
idxs_labels = np.vstack((idxs, labels))
idxs_labels = idxs_labels[:, idxs_labels[1, :].argsort()]
idxs = idxs_labels[0, :]
# Minimum and maximum shards assigned per client:
min_shard = 1 # at least 50 images for a client
max_shard = 30 # at most 1500 images for a client
# Divide the shards into random chunks for every client
# such that the sum of these chunks = num_shards
random_shard_size = np.random.randint(min_shard, max_shard+1,
size=num_users)
random_shard_size = np.around(random_shard_size /
sum(random_shard_size) * num_shards)
random_shard_size = random_shard_size.astype(int)
# Assign the shards randomly to each client
if sum(random_shard_size) > num_shards:
for i in range(num_users):
# First assign each client 1 shard to ensure every client has
# atleast one shard of data
rand_set = set(np.random.choice(idx_shard, 1, replace=False))
idx_shard = list(set(idx_shard) - rand_set)
for rand in rand_set:
dict_users[i] = np.concatenate(
(dict_users[i], idxs[rand*num_imgs:(rand+1)*num_imgs]),
axis=0)
random_shard_size = random_shard_size-1
# Next, randomly assign the remaining shards
for i in range(num_users):
if len(idx_shard) == 0:
continue
shard_size = random_shard_size[i]
if shard_size > len(idx_shard):
shard_size = len(idx_shard)
rand_set = set(np.random.choice(idx_shard, shard_size,
replace=False))
idx_shard = list(set(idx_shard) - rand_set)
for rand in rand_set:
dict_users[i] = np.concatenate(
(dict_users[i], idxs[rand*num_imgs:(rand+1)*num_imgs]),
axis=0)
else:
for i in range(num_users):
shard_size = random_shard_size[i]
rand_set = set(np.random.choice(idx_shard, shard_size,
replace=False))
idx_shard = list(set(idx_shard) - rand_set)
for rand in rand_set:
dict_users[i] = np.concatenate(
(dict_users[i], idxs[rand*num_imgs:(rand+1)*num_imgs]),
axis=0)
if len(idx_shard) > 0:
# Add the leftover shards to the client with minimum images:
shard_size = len(idx_shard)
# Add the remaining shard to the client with lowest data
k = min(dict_users, key=lambda x: len(dict_users.get(x)))
rand_set = set(np.random.choice(idx_shard, shard_size,
replace=False))
idx_shard = list(set(idx_shard) - rand_set)
for rand in rand_set:
dict_users[k] = np.concatenate(
(dict_users[k], idxs[rand*num_imgs:(rand+1)*num_imgs]),
axis=0)
return dict_users
def get_dataset(iid=1, unbalanced=0, num_users=100):
"""
Returns train and test datasets and a user group which is a dict where
the keys are the user index and the values are the corresponding data for
each of those users.
"""
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
# mean and std of the CIFAR-10 dataset
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
# mean and std of the CIFAR-10 dataset
])
# choose the training and test datasets
train_dataset = datasets.CIFAR10('data', train=True,
download=True, transform=transform_train)
test_dataset = datasets.CIFAR10('data', train=False,
download=True, transform=transform_test)
if iid:
# Sample IID user data from Mnist
user_groups = cifar_iid(train_dataset, num_users)
else:
# Sample Non-IID user data from Mnist
if unbalanced:
# Chose unequal splits for every user
user_groups = cifar_noniid_unbalanced(train_dataset, num_users)
else:
# Chose equal splits for every user
user_groups = cifar_noniid(train_dataset, num_users)
return train_dataset, test_dataset, user_groups
def exp_details(model, optimizer, lr, norm, epochs, iid, frac, local_bs, local_ep, unbalanced, num_users):
print('\nExperimental details:')
print(f' Model : {model}')
print(f' Optimizer : {optimizer}')
print(f' Learning : {lr}')
print(f' Normalization : {norm}')
print(f' Global Rounds : {epochs}\n')
print(' Federated parameters:')
if iid:
print(' IID')
elif unbalanced:
print(' Non-IID - unbalanced')
else:
print(' Non-IID - balanced')
print(f' NUmber of users : {num_users}')
print(f' Fraction of users : {frac}')
print(f' Local Batch size : {local_bs}')
print(f' Local Epochs : {local_ep}\n')
return