-
Notifications
You must be signed in to change notification settings - Fork 39
/
data_utils.py
252 lines (193 loc) · 8.94 KB
/
data_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
import os
import collections
import logging
import glob
import re
import torch, torchvision
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data.dataset import Dataset
import itertools as it
import copy
#-------------------------------------------------------------------------------------------------------
# DATASETS
#-------------------------------------------------------------------------------------------------------
DATA_PATH = os.path.join(os.environ['TRAINING_DATA'], 'PyTorch')
def get_kws():
'''Return MNIST train/test data and labels as numpy arrays'''
data = np.load(os.path.join(DATA_PATH, "Speech_Commands/data_noaug.npz"))
x_train, y_train = data["x_train"], data["y_train"].astype("int").flatten()
x_test, y_test = data["x_test"], data["y_test"].astype("int").flatten()
x_train, x_test = (x_train-0.5412386)/0.2746128, (x_test-0.5412386)/0.2746128
return x_train, y_train, x_test, y_test
def get_mnist():
'''Return MNIST train/test data and labels as numpy arrays'''
data_train = torchvision.datasets.MNIST(root=os.path.join(DATA_PATH, "MNIST"), train=True, download=True)
data_test = torchvision.datasets.MNIST(root=os.path.join(DATA_PATH, "MNIST"), train=False, download=True)
x_train, y_train = data_train.train_data.numpy().reshape(-1,1,28,28)/255, np.array(data_train.train_labels)
x_test, y_test = data_test.test_data.numpy().reshape(-1,1,28,28)/255, np.array(data_test.test_labels)
return x_train, y_train, x_test, y_test
def get_fashionmnist():
'''Return MNIST train/test data and labels as numpy arrays'''
data_train = torchvision.datasets.FashionMNIST(root=os.path.join(DATA_PATH, "FashionMNIST"), train=True, download=True)
data_test = torchvision.datasets.FashionMNIST(root=os.path.join(DATA_PATH, "FashionMNIST"), train=False, download=True)
x_train, y_train = data_train.train_data.numpy().reshape(-1,1,28,28)/255, np.array(data_train.train_labels)
x_test, y_test = data_test.test_data.numpy().reshape(-1,1,28,28)/255, np.array(data_test.test_labels)
return x_train, y_train, x_test, y_test
def get_cifar10():
'''Return CIFAR10 train/test data and labels as numpy arrays'''
data_train = torchvision.datasets.CIFAR10(root=os.path.join(DATA_PATH, "CIFAR10"), train=True, download=True)
data_test = torchvision.datasets.CIFAR10(root=os.path.join(DATA_PATH, "CIFAR10"), train=False, download=True)
x_train, y_train = data_train.train_data.transpose((0,3,1,2)), np.array(data_train.train_labels)
x_test, y_test = data_test.test_data.transpose((0,3,1,2)), np.array(data_test.test_labels)
return x_train, y_train, x_test, y_test
def print_image_data_stats(data_train, labels_train, data_test, labels_test):
print("\nData: ")
print(" - Train Set: ({},{}), Range: [{:.3f}, {:.3f}], Labels: {},..,{}".format(
data_train.shape, labels_train.shape, np.min(data_train), np.max(data_train),
np.min(labels_train), np.max(labels_train)))
print(" - Test Set: ({},{}), Range: [{:.3f}, {:.3f}], Labels: {},..,{}".format(
data_test.shape, labels_test.shape, np.min(data_train), np.max(data_train),
np.min(labels_test), np.max(labels_test)))
#-------------------------------------------------------------------------------------------------------
# SPLIT DATA AMONG CLIENTS
#-------------------------------------------------------------------------------------------------------
def split_image_data(data, labels, n_clients=10, classes_per_client=10, shuffle=True, verbose=True, balancedness=None):
'''
Splits (data, labels) evenly among 'n_clients s.t. every client holds 'classes_per_client
different labels
data : [n_data x shape]
labels : [n_data (x 1)] from 0 to n_labels
'''
# constants
n_data = data.shape[0]
n_labels = np.max(labels) + 1
if balancedness >= 1.0:
data_per_client = [n_data // n_clients]*n_clients
data_per_client_per_class = [data_per_client[0] // classes_per_client]*n_clients
else:
fracs = balancedness**np.linspace(0,n_clients-1, n_clients)
fracs /= np.sum(fracs)
fracs = 0.1/n_clients + (1-0.1)*fracs
data_per_client = [np.floor(frac*n_data).astype('int') for frac in fracs]
data_per_client = data_per_client[::-1]
data_per_client_per_class = [np.maximum(1,nd // classes_per_client) for nd in data_per_client]
if sum(data_per_client) > n_data:
print("Impossible Split")
exit()
# sort for labels
data_idcs = [[] for i in range(n_labels)]
for j, label in enumerate(labels):
data_idcs[label] += [j]
if shuffle:
for idcs in data_idcs:
np.random.shuffle(idcs)
# split data among clients
clients_split = []
c = 0
for i in range(n_clients):
client_idcs = []
budget = data_per_client[i]
c = np.random.randint(n_labels)
while budget > 0:
take = min(data_per_client_per_class[i], len(data_idcs[c]), budget)
client_idcs += data_idcs[c][:take]
data_idcs[c] = data_idcs[c][take:]
budget -= take
c = (c + 1) % n_labels
clients_split += [(data[client_idcs], labels[client_idcs])]
def print_split(clients_split):
print("Data split:")
for i, client in enumerate(clients_split):
split = np.sum(client[1].reshape(1,-1)==np.arange(n_labels).reshape(-1,1), axis=1)
print(" - Client {}: {}".format(i,split))
print()
if verbose:
print_split(clients_split)
return clients_split
#-------------------------------------------------------------------------------------------------------
# IMAGE DATASET CLASS
#-------------------------------------------------------------------------------------------------------
class CustomImageDataset(Dataset):
'''
A custom Dataset class for images
inputs : numpy array [n_data x shape]
labels : numpy array [n_data (x 1)]
'''
def __init__(self, inputs, labels, transforms=None):
assert inputs.shape[0] == labels.shape[0]
self.inputs = torch.Tensor(inputs)
self.labels = torch.Tensor(labels).long()
self.transforms = transforms
def __getitem__(self, index):
img, label = self.inputs[index], self.labels[index]
if self.transforms is not None:
img = self.transforms(img)
return (img, label)
def __len__(self):
return self.inputs.shape[0]
def get_default_data_transforms(name, train=True, verbose=True):
transforms_train = {
'mnist' : transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((32, 32)),
#transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize((0.06078,),(0.1957,))
]),
'fashionmnist' : transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((32, 32)),
#transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]),
'cifar10' : transforms.Compose([
transforms.ToPILImage(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))]),#(0.24703223, 0.24348513, 0.26158784)
'kws' : None
}
transforms_eval = {
'mnist' : transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.06078,),(0.1957,))
]),
'fashionmnist' : transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]),
'cifar10' : transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))]),#
'kws' : None
}
if verbose:
print("\nData preprocessing: ")
for transformation in transforms_train[name].transforms:
print(' -', transformation)
print()
return (transforms_train[name], transforms_eval[name])
def get_data_loaders(hp, verbose=True):
x_train, y_train, x_test, y_test = globals()['get_'+hp['dataset']]()
if verbose:
print_image_data_stats(x_train, y_train, x_test, y_test)
transforms_train, transforms_eval = get_default_data_transforms(hp['dataset'], verbose=False)
split = split_image_data(x_train, y_train, n_clients=hp['n_clients'],
classes_per_client=hp['classes_per_client'], balancedness=hp['balancedness'], verbose=verbose)
client_loaders = [torch.utils.data.DataLoader(CustomImageDataset(x, y, transforms_train),
batch_size=hp['batch_size'], shuffle=True) for x, y in split]
train_loader = torch.utils.data.DataLoader(CustomImageDataset(x_train, y_train, transforms_eval), batch_size=100, shuffle=False)
test_loader = torch.utils.data.DataLoader(CustomImageDataset(x_test, y_test, transforms_eval), batch_size=100, shuffle=False)
stats = {"split" : [x.shape[0] for x, y in split]}
return client_loaders, train_loader, test_loader, stats