forked from ubc-tea/DESA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_dirichlet_dataset.py
359 lines (281 loc) · 17.2 KB
/
make_dirichlet_dataset.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
from torchvision.datasets import ImageFolder
from PIL import Image
from cifar10c_dataset import CIFAR10C_preprocessed
import os
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import sys
class DatasetObject:
def __init__(self, dataset, n_client, rule, unbalanced_sgm=0, rule_arg=''):
self.dataset = dataset
self.n_client = n_client
self.rule = rule
self.rule_arg = rule_arg
rule_arg_str = rule_arg if isinstance(rule_arg, str) else '%.3f' % rule_arg
self.name = "%s_%d_%s_%s" %(self.dataset, self.n_client, self.rule, rule_arg_str)
self.name += '_%f' %unbalanced_sgm if unbalanced_sgm!=0 else ''
self.unbalanced_sgm = unbalanced_sgm
self.data_path = 'data'
self.set_data()
def set_data(self):
# Prepare data if not ready
if not os.path.exists('%s/%s' %(self.data_path, self.name)):
# if False:
# Get Raw data
if self.dataset == 'mnist':
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
trnset = torchvision.datasets.MNIST(root='%s/Raw' %self.data_path,
train=True , download=True, transform=transform)
tstset = torchvision.datasets.MNIST(root='%s/Raw' %self.data_path,
train=False, download=True, transform=transform)
trn_load = torch.utils.data.DataLoader(trnset, batch_size=60000, shuffle=False, num_workers=1)
tst_load = torch.utils.data.DataLoader(tstset, batch_size=10000, shuffle=False, num_workers=1)
self.channels = 1; self.width = 28; self.height = 28; self.n_cls = 10;
if self.dataset == 'CIFAR10':
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=[0.491, 0.482, 0.447], std=[0.247, 0.243, 0.262])])
trnset = torchvision.datasets.CIFAR10(root='%s/Raw' %self.data_path,
train=True , download=True, transform=transform)
tstset = torchvision.datasets.CIFAR10(root='%s/Raw' %self.data_path,
train=False, download=True, transform=transform)
trn_load = torch.utils.data.DataLoader(trnset, batch_size=50000, shuffle=False, num_workers=1)
tst_load = torch.utils.data.DataLoader(tstset, batch_size=10000, shuffle=False, num_workers=1)
self.channels = 3; self.width = 32; self.height = 32; self.n_cls = 10;
if self.dataset == 'CIFAR100':
print(self.dataset)
# mean and std are validated here: https://gist.github.com/weiaicunzai/e623931921efefd4c331622c344d8151
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=[0.5071, 0.4867, 0.4408],
std=[0.2675, 0.2565, 0.2761])])
trnset = torchvision.datasets.CIFAR100(root='%s/Raw' %self.data_path,
train=True , download=True, transform=transform)
tstset = torchvision.datasets.CIFAR100(root='%s/Raw' %self.data_path,
train=False, download=True, transform=transform)
trn_load = torch.utils.data.DataLoader(trnset, batch_size=50000, shuffle=False, num_workers=0)
tst_load = torch.utils.data.DataLoader(tstset, batch_size=10000, shuffle=False, num_workers=0)
self.channels = 3; self.width = 32; self.height = 32; self.n_cls = 100;
if self.dataset != 'emnist':
trn_itr = trn_load.__iter__(); tst_itr = tst_load.__iter__()
# labels are of shape (n_data,)
trn_x, trn_y = trn_itr.__next__()
tst_x, tst_y = tst_itr.__next__()
trn_x = trn_x.numpy(); trn_y = trn_y.numpy().reshape(-1,1)
tst_x = tst_x.numpy(); tst_y = tst_y.numpy().reshape(-1,1)
if self.dataset == 'emnist':
emnist = io.loadmat(self.data_path + "/Raw/matlab/emnist-letters.mat")
# load training dataset
x_train = emnist["dataset"][0][0][0][0][0][0]
x_train = x_train.astype(np.float32)
# load training labels
y_train = emnist["dataset"][0][0][0][0][0][1] - 1 # make first class 0
# take first 10 classes of letters
trn_idx = np.where(y_train < 10)[0]
y_train = y_train[trn_idx]
x_train = x_train[trn_idx]
mean_x = np.mean(x_train)
std_x = np.std(x_train)
# load test dataset
x_test = emnist["dataset"][0][0][1][0][0][0]
x_test = x_test.astype(np.float32)
# load test labels
y_test = emnist["dataset"][0][0][1][0][0][1] - 1 # make first class 0
tst_idx = np.where(y_test < 10)[0]
y_test = y_test[tst_idx]
x_test = x_test[tst_idx]
x_train = x_train.reshape((-1, 1, 28, 28))
x_test = x_test.reshape((-1, 1, 28, 28))
# normalise train and test features
trn_x = (x_train - mean_x) / std_x
trn_y = y_train
tst_x = (x_test - mean_x) / std_x
tst_y = y_test
self.channels = 1; self.width = 28; self.height = 28; self.n_cls = 10;
# Shuffle Data
rand_perm = np.random.permutation(len(trn_y))
trn_x = trn_x[rand_perm]
trn_y = trn_y[rand_perm]
self.trn_x = trn_x
self.trn_y = trn_y
self.tst_x = tst_x
self.tst_y = tst_y
###
n_data_per_clnt = int((len(trn_y)) / self.n_client)
if self.unbalanced_sgm != 0:
# Draw from lognormal distribution
clnt_data_list = (np.random.lognormal(mean=np.log(n_data_per_clnt), sigma=self.unbalanced_sgm, size=self.n_client))
clnt_data_list = (clnt_data_list/np.sum(clnt_data_list)*len(trn_y)).astype(int)
diff = np.sum(clnt_data_list) - len(trn_y)
# Add/Subtract the excess number starting from first client
if diff!= 0:
for clnt_i in range(self.n_client):
if clnt_data_list[clnt_i] > diff:
clnt_data_list[clnt_i] -= diff
break
else:
clnt_data_list = (np.ones(self.n_client) * n_data_per_clnt).astype(int)
###
if self.rule == 'Dirichlet':
cls_priors = np.random.dirichlet(alpha=[self.rule_arg]*self.n_cls,size=self.n_client)
prior_cumsum = np.cumsum(cls_priors, axis=1)
idx_list = [np.where(trn_y==i)[0] for i in range(self.n_cls)]
cls_amount = [len(idx_list[i]) for i in range(self.n_cls)]
clnt_x = [ np.zeros((clnt_data_list[clnt__], self.channels, self.height, self.width)).astype(np.float32) for clnt__ in range(self.n_client) ]
clnt_y = [ np.zeros((clnt_data_list[clnt__], 1)).astype(np.int64) for clnt__ in range(self.n_client) ]
while(np.sum(clnt_data_list)!=0):
curr_clnt = np.random.randint(self.n_client)
# If current node is full resample a client
print('Remaining Data: %d' %np.sum(clnt_data_list))
if clnt_data_list[curr_clnt] <= 0:
continue
clnt_data_list[curr_clnt] -= 1
curr_prior = prior_cumsum[curr_clnt]
while True:
cls_label = np.argmax(np.random.uniform() <= curr_prior)
# Redraw class label if trn_y is out of that class
if cls_amount[cls_label] <= 0:
continue
cls_amount[cls_label] -= 1
clnt_x[curr_clnt][clnt_data_list[curr_clnt]] = trn_x[idx_list[cls_label][cls_amount[cls_label]]]
clnt_y[curr_clnt][clnt_data_list[curr_clnt]] = trn_y[idx_list[cls_label][cls_amount[cls_label]]]
break
clnt_x = np.asarray(clnt_x)
clnt_y = np.asarray(clnt_y)
cls_means = np.zeros((self.n_client, self.n_cls))
for clnt in range(self.n_client):
for cls in range(self.n_cls):
cls_means[clnt,cls] = np.mean(clnt_y[clnt]==cls)
prior_real_diff = np.abs(cls_means-cls_priors)
print('--- Max deviation from prior: %.4f' %np.max(prior_real_diff))
print('--- Min deviation from prior: %.4f' %np.min(prior_real_diff))
elif self.rule == 'iid' and self.dataset == 'CIFAR100' and self.unbalanced_sgm==0:
assert len(trn_y)//100 % self.n_client == 0
# Only have the number clients if it divides 500
# Perfect IID partitions for cifar100 instead of shuffling
idx = np.argsort(trn_y[:, 0])
n_data_per_clnt = len(trn_y) // self.n_client
# clnt_x dtype needs to be float32, the same as weights
clnt_x = np.zeros((self.n_client, n_data_per_clnt, 3, 32, 32), dtype=np.float32)
clnt_y = np.zeros((self.n_client, n_data_per_clnt, 1), dtype=np.float32)
trn_x = trn_x[idx] # 50000*3*32*32
trn_y = trn_y[idx]
n_cls_sample_per_device = n_data_per_clnt // 100
for i in range(self.n_client): # devices
for j in range(100): # class
clnt_x[i, n_cls_sample_per_device*j:n_cls_sample_per_device*(j+1), :, :, :] = trn_x[500*j+n_cls_sample_per_device*i:500*j+n_cls_sample_per_device*(i+1), :, :, :]
clnt_y[i, n_cls_sample_per_device*j:n_cls_sample_per_device*(j+1), :] = trn_y[500*j+n_cls_sample_per_device*i:500*j+n_cls_sample_per_device*(i+1), :]
elif self.rule == 'iid':
clnt_x = [ np.zeros((clnt_data_list[clnt__], self.channels, self.height, self.width)).astype(np.float32) for clnt__ in range(self.n_client) ]
clnt_y = [ np.zeros((clnt_data_list[clnt__], 1)).astype(np.int64) for clnt__ in range(self.n_client) ]
clnt_data_list_cum_sum = np.concatenate(([0], np.cumsum(clnt_data_list)))
for clnt_idx_ in range(self.n_client):
clnt_x[clnt_idx_] = trn_x[clnt_data_list_cum_sum[clnt_idx_]:clnt_data_list_cum_sum[clnt_idx_+1]]
clnt_y[clnt_idx_] = trn_y[clnt_data_list_cum_sum[clnt_idx_]:clnt_data_list_cum_sum[clnt_idx_+1]]
clnt_x = np.asarray(clnt_x)
clnt_y = np.asarray(clnt_y)
self.clnt_x = clnt_x; self.clnt_y = clnt_y
self.tst_x = tst_x; self.tst_y = tst_y
# Save data
os.mkdir('%s/%s' %(self.data_path, self.name))
np.save('%s/%s/clnt_x.npy' %(self.data_path, self.name), clnt_x)
np.save('%s/%s/clnt_y.npy' %(self.data_path, self.name), clnt_y)
np.save('%s/%s/tst_x.npy' %(self.data_path, self.name), tst_x)
np.save('%s/%s/tst_y.npy' %(self.data_path, self.name), tst_y)
else:
print("Data is already downloaded in the folder.")
self.clnt_x = np.load('%s/%s/clnt_x.npy' %(self.data_path, self.name), allow_pickle=True)
self.clnt_y = np.load('%s/%s/clnt_y.npy' %(self.data_path, self.name), allow_pickle=True)
self.n_client = len(self.clnt_x)
tst_x = np.load('%s/%s/tst_x.npy' %(self.data_path, self.name), allow_pickle=True)
tst_y = np.load('%s/%s/tst_y.npy' %(self.data_path, self.name), allow_pickle=True)
print(tst_x.shape)
if self.dataset == 'mnist':
self.channels = 1; self.width = 28; self.height = 28; self.n_cls = 10;
if self.dataset == 'CIFAR10':
self.channels = 3; self.width = 32; self.height = 32; self.n_cls = 10;
if self.dataset == 'CIFAR100':
self.channels = 3; self.width = 32; self.height = 32; self.n_cls = 100;
if self.dataset == 'fashion_mnist':
self.channels = 1; self.width = 28; self.height = 28; self.n_cls = 10;
if self.dataset == 'emnist':
self.channels = 1; self.width = 28; self.height = 28; self.n_cls = 10;
self.tst_x = []
self.tst_y = []
num_test_per_client = int(len(tst_y)/self.n_cls)
for i in range(self.n_cls):
self.tst_x.append(tst_x[i*num_test_per_client:(i+1)*num_test_per_client])
self.tst_y.append(tst_y[i*num_test_per_client:(i+1)*num_test_per_client])
self.tst_x = np.array(self.tst_x)
self.tst_y = np.array(self.tst_y)
print('Class frequencies:')
count = 0
for clnt in range(self.n_client):
print("Client %3d: " %clnt +
', '.join(["%f" %np.mean(self.clnt_y[clnt]==cls) for cls in range(self.n_cls)]) +
', Amount:%d' %self.clnt_y[clnt].shape[0])
count += self.clnt_y[clnt].shape[0]
for clnt in range(self.n_client):
print("Client %3d: " %clnt +
', '.join(["%f" %np.mean(self.tst_y[clnt]==cls) for cls in range(self.n_cls)]) +
', Amount:%d' %self.tst_y[clnt].shape[0])
count += self.tst_y[clnt].shape[0]
print('Total Amount:%d' %count)
print('--------')
print(" Test: " +
', '.join(["%.3f" %np.mean(self.tst_y==cls) for cls in range(self.n_cls)]) +
', Amount:%d' %self.tst_y.shape[0])
# sys.exit()
class Dataset(torch.utils.data.Dataset):
def __init__(self, data_x, data_y=True, train=False, dataset_name=''):
self.name = dataset_name
if self.name == 'mnist' or self.name == 'synt' or self.name == 'emnist' or self.name == 'digits' or self.name == 'retina' or self.name == 'cifar10c':
self.X_data = torch.tensor(data_x).float()
self.y_data = data_y
if not isinstance(data_y, bool):
self.y_data = torch.tensor(data_y).float()
elif self.name == 'CIFAR10' or self.name == 'CIFAR100':
self.train = train
self.transform = transforms.Compose([transforms.ToTensor()])
self.X_data = data_x
self.y_data = data_y
if not isinstance(data_y, bool):
self.y_data = data_y.astype('int32')
elif self.name == 'shakespeare':
self.X_data = data_x
self.y_data = data_y
self.X_data = torch.tensor(self.X_data).long()
if not isinstance(data_y, bool):
self.y_data = torch.tensor(self.y_data).float()
def __len__(self):
return len(self.X_data)
def __getitem__(self, idx):
if self.name == 'mnist' or self.name == 'synt' or self.name == 'emnist' or self.name == 'digits' or self.name == 'retina' or self.name == 'cifar10c':
X = self.X_data[idx, :]
if isinstance(self.y_data, bool):
return X
else:
y = self.y_data[idx]
return X, y
elif self.name == 'CIFAR10' or self.name == 'CIFAR100':
img = self.X_data[idx]
if self.train:
img = np.flip(img, axis=2).copy() if (np.random.rand() > .5) else img # Horizontal flip
if (np.random.rand() > .5):
# Random cropping
pad = 4
extended_img = np.zeros((3,32 + pad *2, 32 + pad *2)).astype(np.float32)
extended_img[:,pad:-pad,pad:-pad] = img
dim_1, dim_2 = np.random.randint(pad * 2 + 1, size=2)
img = extended_img[:,dim_1:dim_1+32,dim_2:dim_2+32]
img = np.moveaxis(img, 0, -1)
img = self.transform(img)
if isinstance(self.y_data, bool):
return img
else:
y = torch.tensor(self.y_data[idx], dtype=torch.long)
return img, y
elif self.name == 'shakespeare':
x = self.X_data[idx]
y = self.y_data[idx]
return x, y