-
-
Notifications
You must be signed in to change notification settings - Fork 788
/
data_loader.py
269 lines (209 loc) · 11.5 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
import logging
import numpy as np
import torch
import torch.utils.data as data
import torchvision.transforms as transforms
from .datasets import CIFAR100_truncated
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# generate the non-IID distribution for all methods
def read_data_distribution(filename='./data_preprocessing/non-iid-distribution/CIFAR10/distribution.txt'):
distribution = {}
with open(filename, 'r') as data:
for x in data.readlines():
if '{' != x[0] and '}' != x[0]:
tmp = x.split(':')
if '{' == tmp[1].strip():
first_level_key = int(tmp[0])
distribution[first_level_key] = {}
else:
second_level_key = int(tmp[0])
distribution[first_level_key][second_level_key] = int(tmp[1].strip().replace(',', ''))
return distribution
def read_net_dataidx_map(filename='./data_preprocessing/non-iid-distribution/CIFAR10/net_dataidx_map.txt'):
net_dataidx_map = {}
with open(filename, 'r') as data:
for x in data.readlines():
if '{' != x[0] and '}' != x[0] and ']' != x[0]:
tmp = x.split(':')
if '[' == tmp[-1].strip():
key = int(tmp[0])
net_dataidx_map[key] = []
else:
tmp_array = x.split(',')
net_dataidx_map[key] = [int(i.strip()) for i in tmp_array]
return net_dataidx_map
def record_net_data_stats(y_train, net_dataidx_map):
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
logging.debug('Data statistics: %s' % str(net_cls_counts))
return net_cls_counts
class Cutout(object):
def __init__(self, length):
self.length = length
def __call__(self, img):
h, w = img.size(1), img.size(2)
mask = np.ones((h, w), np.float32)
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
mask[y1: y2, x1: x2] = 0.
mask = torch.from_numpy(mask)
mask = mask.expand_as(img)
img *= mask
return img
def _data_transforms_cifar100():
CIFAR_MEAN = [0.5071, 0.4865, 0.4409]
CIFAR_STD = [0.2673, 0.2564, 0.2762]
train_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
train_transform.transforms.append(Cutout(16))
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
return train_transform, valid_transform
def load_cifar100_data(datadir):
train_transform, test_transform = _data_transforms_cifar100()
cifar10_train_ds = CIFAR100_truncated(datadir, train=True, download=True, transform=train_transform)
cifar10_test_ds = CIFAR100_truncated(datadir, train=False, download=True, transform=test_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
return (X_train, y_train, X_test, y_test)
def partition_data(dataset, datadir, partition, n_nets, alpha):
logging.info("*********partition data***************")
X_train, y_train, X_test, y_test = load_cifar100_data(datadir)
n_train = X_train.shape[0]
# n_test = X_test.shape[0]
if partition == "homo":
total_num = n_train
idxs = np.random.permutation(total_num)
batch_idxs = np.array_split(idxs, n_nets)
net_dataidx_map = {i: batch_idxs[i] for i in range(n_nets)}
elif partition == "hetero":
min_size = 0
K = 100
N = y_train.shape[0]
logging.info("N = " + str(N))
net_dataidx_map = {}
while min_size < 10:
idx_batch = [[] for _ in range(n_nets)]
# for each class in the dataset
for k in range(K):
idx_k = np.where(y_train == k)[0]
np.random.shuffle(idx_k)
proportions = np.random.dirichlet(np.repeat(alpha, n_nets))
## Balance
proportions = np.array([p * (len(idx_j) < N / n_nets) for p, idx_j in zip(proportions, idx_batch)])
proportions = proportions / proportions.sum()
proportions = (np.cumsum(proportions) * len(idx_k)).astype(int)[:-1]
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])
for j in range(n_nets):
np.random.shuffle(idx_batch[j])
net_dataidx_map[j] = idx_batch[j]
elif partition == "hetero-fix":
dataidx_map_file_path = './data_preprocessing/non-iid-distribution/CIFAR100/net_dataidx_map.txt'
net_dataidx_map = read_net_dataidx_map(dataidx_map_file_path)
if partition == "hetero-fix":
distribution_file_path = './data_preprocessing/non-iid-distribution/CIFAR100/distribution.txt'
traindata_cls_counts = read_data_distribution(distribution_file_path)
else:
traindata_cls_counts = record_net_data_stats(y_train, net_dataidx_map)
return X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts
# for centralized training
def get_dataloader(dataset, datadir, train_bs, test_bs, dataidxs=None):
return get_dataloader_CIFAR100(datadir, train_bs, test_bs, dataidxs)
# for local devices
def get_dataloader_test(dataset, datadir, train_bs, test_bs, dataidxs_train, dataidxs_test):
return get_dataloader_test_CIFAR100(datadir, train_bs, test_bs, dataidxs_train, dataidxs_test)
def get_dataloader_CIFAR100(datadir, train_bs, test_bs, dataidxs=None):
dl_obj = CIFAR100_truncated
transform_train, transform_test = _data_transforms_cifar100()
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=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False, drop_last=True)
return train_dl, test_dl
def get_dataloader_test_CIFAR100(datadir, train_bs, test_bs, dataidxs_train=None, dataidxs_test=None):
dl_obj = CIFAR100_truncated
transform_train, transform_test = _data_transforms_cifar100()
train_ds = dl_obj(datadir, dataidxs=dataidxs_train, train=True, transform=transform_train, download=True)
test_ds = dl_obj(datadir, dataidxs=dataidxs_test, train=False, transform=transform_test, download=True)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, shuffle=True, drop_last=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False, drop_last=True)
return train_dl, test_dl
def load_partition_data_distributed_cifar100(process_id, dataset, data_dir, partition_method, partition_alpha,
client_number, batch_size):
X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts = partition_data(dataset,
data_dir,
partition_method,
client_number,
partition_alpha)
class_num = len(np.unique(y_train))
logging.info("traindata_cls_counts = " + str(traindata_cls_counts))
train_data_num = sum([len(net_dataidx_map[r]) for r in range(client_number)])
# get global test data
if process_id == 0:
train_data_global, test_data_global = get_dataloader(dataset, data_dir, batch_size, batch_size)
logging.info("train_dl_global number = " + str(len(train_data_global)))
logging.info("test_dl_global number = " + str(len(train_data_global)))
train_data_local = None
test_data_local = None
local_data_num = 0
else:
# get local dataset
dataidxs = net_dataidx_map[process_id - 1]
local_data_num = len(dataidxs)
logging.info("rank = %d, local_sample_number = %d" % (process_id, local_data_num))
# training batch size = 64; algorithms batch size = 32
train_data_local, test_data_local = get_dataloader(dataset, data_dir, batch_size, batch_size,
dataidxs)
logging.info("process_id = %d, batch_num_train_local = %d, batch_num_test_local = %d" % (
process_id, len(train_data_local), len(test_data_local)))
train_data_global = None
test_data_global = None
return train_data_num, train_data_global, test_data_global, local_data_num, train_data_local, test_data_local, class_num
def load_partition_data_cifar100(dataset, data_dir, partition_method, partition_alpha, client_number, batch_size):
X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts = partition_data(dataset,
data_dir,
partition_method,
client_number,
partition_alpha)
class_num = len(np.unique(y_train))
logging.info("traindata_cls_counts = " + str(traindata_cls_counts))
train_data_num = sum([len(net_dataidx_map[r]) for r in range(client_number)])
train_data_global, test_data_global = get_dataloader(dataset, data_dir, batch_size, batch_size)
logging.info("train_dl_global number = " + str(len(train_data_global)))
logging.info("test_dl_global number = " + str(len(train_data_global)))
test_data_num = len(test_data_global)
# get local dataset
data_local_num_dict = dict()
train_data_local_dict = dict()
test_data_local_dict = dict()
for client_idx in range(client_number):
dataidxs = net_dataidx_map[client_idx]
local_data_num = len(dataidxs)
data_local_num_dict[client_idx] = local_data_num
logging.info("client_idx = %d, local_sample_number = %d" % (client_idx, local_data_num))
# training batch size = 64; algorithms batch size = 32
train_data_local, test_data_local = get_dataloader(dataset, data_dir, batch_size, batch_size,
dataidxs)
logging.info("client_idx = %d, batch_num_train_local = %d, batch_num_test_local = %d" % (
client_idx, len(train_data_local), len(test_data_local)))
train_data_local_dict[client_idx] = train_data_local
test_data_local_dict[client_idx] = test_data_local
return 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