-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
386 lines (348 loc) · 14.1 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
import torch
import torch.utils.data as data
from torch.utils.data import DataLoader
import torchvision
import torchvision.transforms as transforms
import numpy as np
import scipy.io as scio
import os
# -------- get data sets --------
def get_dataloader(args):
if args.dataset=='monks1' or args.dataset=='monks2' or args.dataset=='monks3' \
or args.dataset=='adult' or args.dataset=='ijcnn' or args.dataset=='synthetic':
if args.dataset=='monks1' or args.dataset=='monks2' or args.dataset=='monks3':
args.num_dim=6
trainset = Monks(args.data_folder, train=True,
transform=transforms.Compose([transforms.ToTensor()]))
testset = Monks(args.data_folder, train=False,
transform=transforms.Compose([transforms.ToTensor()]))
elif args.dataset=='adult':
args.num_dim=123
trainset = Adult(args.data_folder, train=True,
transform=transforms.Compose([transforms.ToTensor()]))
testset = Adult(args.data_folder, train=False,
transform=transforms.Compose([transforms.ToTensor()]))
elif args.dataset=='ijcnn':
args.num_dim=22
trainset = Ijcnn(args.data_folder, train=True,
transform=transforms.Compose([transforms.ToTensor()]))
testset = Ijcnn(args.data_folder, train=False,
transform=transforms.Compose([transforms.ToTensor()]))
elif args.dataset=='synthetic':
assert args.num_dim>0, "The dimension of the synthetic data should be specified."
trainset = Synthetic(args.data_folder, args.num_dim, train=True,
transform=transforms.Compose([transforms.ToTensor()]))
testset = Synthetic(args.data_folder, args.num_dim, train=False,
transform=transforms.Compose([transforms.ToTensor()]))
if args.val:
args.num_test = len(testset)
args.num_val = round(len(trainset)*args.ratio_val)
args.num_train = len(trainset)-args.num_val
train_val = data.random_split(trainset, (args.num_train, args.num_val))
trainset, valset = train_val[0], train_val[1]
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
testloader = DataLoader(testset, batch_size=args.batch_size, shuffle=False)
valloader = DataLoader(valset, batch_size=args.batch_size, shuffle=False)
else:
args.num_val = 0
args.num_test = len(testset)
args.num_train = len(trainset)
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
testloader = DataLoader(testset, batch_size=args.batch_size, shuffle=False)
valloader = None
elif args.dataset == 'austra' or args.dataset == 'climate' \
or args.dataset == 'diabetic' or args.dataset == 'sonar' or args.dataset == 'phishing':
if args.dataset == 'austra':
args.num_dim=14
alldataset = Australian(args.data_folder, transform=transforms.Compose([transforms.ToTensor()]))
elif args.dataset == 'climate':
args.num_dim=18
alldataset = Climate(args.data_folder)
elif args.dataset == 'diabetic':
args.num_dim=19
alldataset = Diabetic(args.data_folder)
elif args.dataset == 'sonar':
args.num_dim=60
alldataset = Sonar(args.data_folder)
elif args.dataset == 'phishing':
args.num_dim=68
alldataset = Phishing(args.data_folder)
if args.val:
args.num_test = round(len(alldataset)*args.ratio_test)
args.num_val = round((len(alldataset)-args.num_test)*args.ratio_val)
args.num_train = len(alldataset)-args.num_test-args.num_val
train_test_val = data.random_split(alldataset, (args.num_train, args.num_test, args.num_val))
trainset, testset, valset = train_test_val[0], train_test_val[1], train_test_val[2]
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
testloader = DataLoader(testset, batch_size=args.batch_size, shuffle=False)
valloader = DataLoader(valset, batch_size=args.batch_size, shuffle=False)
else:
args.num_val = 0
args.num_test = round(len(alldataset)*args.ratio_test)
args.num_train = len(alldataset)-args.num_test
train_test = data.random_split(alldataset, (args.num_train, args.num_test))
trainset, testset = train_test[0], train_test[1]
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
testloader = DataLoader(testset, batch_size=args.batch_size, shuffle=False)
valloader = None
elif args.dataset == 'mnist':
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
trainset = torchvision.datasets.MNIST(root=args.data_folder, train=True, download=True,
transform=transform)
testset = torchvision.datasets.MNIST(root=args.data_folder, train=False, download=True,
transform=transform)
if args.val:
args.num_test = len(testset)
args.num_val = round(len(trainset)*args.ratio_val)
args.num_train = len(trainset)-args.num_val
train_val = data.random_split(trainset, (args.num_train, args.num_val))
trainset, valset = train_val[0], train_val[1]
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True, pin_memory=True, num_workers=8)
testloader = DataLoader(testset, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=8)
valloader = DataLoader(valset, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=8)
else:
args.num_val = 0
args.num_test = len(testset)
args.num_train = len(trainset)
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True, pin_memory=True, num_workers=8)
testloader = DataLoader(testset, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=8)
valloader = None
else:
assert False, "Unknown dataset : {}".format(args.dataset)
return trainloader, testloader, valloader
"""
Load UCI-Monks1,2,3 Data Set
ATTRIBUTE:
6 dimensions
432 instances total
2 labels (0, 1)
FLOAT32
"""
class Monks(data.Dataset):
def __init__(self, data_folder, train=True, transform=None):
if train == True:
data_path = os.path.join(data_folder, 'train.mat')
X = scio.loadmat(data_path)['Xtrain']
y = scio.loadmat(data_path)['ytrain']
else:
data_path = os.path.join(data_folder, 'test.mat')
X = scio.loadmat(data_path)['Xtest']
y = scio.loadmat(data_path)['ytest']
X = X.astype(np.float32)
y[y!=1] = 0
y = y.astype(np.int64).squeeze()
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load UCI-Australian Data Set
ATTRIBUTE:
14 dimensions
690 instances total
2 labels (0, 1)
FLOAT32
"""
class Australian(data.Dataset):
def __init__(self, data_folder, transform=None):
data_path = os.path.join(data_folder, 'australian.dat')
read_data = np.loadtxt(data_path, delimiter=" ")
y = np.array(read_data[:,-1])
X = np.array(read_data[:,0:-1])
X = X.astype(np.float32)
y = y.astype(np.int64)
# normalize
for i in range(X.shape[1]):
X[:,i] = (X[:,i]-X[:,i].min()) / (X[:,i].max()-X[:,i].min())
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load UCI-Climate Data Set
ATTRIBUTE:
18 dimensions
540 instances total
2 labels (0, 1)
FLOAT32
"""
class Climate(data.Dataset):
def __init__(self, data_folder, transform=None):
data_path = os.path.join(data_folder, 'pop_failures.dat')
read_data = np.loadtxt(data_path,delimiter=None,dtype=str,skiprows=1,usecols=range(2,21))
y = np.array(read_data[:,-1])
X = np.array(read_data[:,0:-1])
X = X.astype(np.float32)
y = y.astype(np.int64)
# normalize
for i in range(X.shape[1]):
X[:,i] = (X[:,i]-X[:,i].min()) / (X[:,i].max()-X[:,i].min())
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load UCI-Diabetic Data Set
ATTRIBUTE:
19 dimensions
1151 instances total
2 labels (0, 1)
FLOAT32
"""
class Diabetic(data.Dataset):
def __init__(self, data_folder, transform=None):
data_path = os.path.join(data_folder, 'messidor_features1.arff')
read_data = np.loadtxt(data_path, delimiter=",")
y = np.array(read_data[:,-1])
X = np.array(read_data[:,0:-1])
X = X.astype(np.float32)
y = y.astype(np.int64)
# normalize
for i in range(X.shape[1]):
X[:,i] = (X[:,i]-X[:,i].min()) / (X[:,i].max()-X[:,i].min())
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load UCI-Sonar Data Set
ATTRIBUTE:
60 dimensions
208 instances total
2 labels (0, 1)
FLOAT32
"""
class Sonar(data.Dataset):
def __init__(self, data_folder, transform=None):
data_path = os.path.join(data_folder, 'sonar.dat')
read_data = np.loadtxt(data_path, delimiter=",", dtype=str)
y = np.array(read_data[:,-1])
X = np.array(read_data[:,0:-1])
X = X.astype(np.float32)
y[y=='R']=1
y[y=='M']=0
y = y.astype(np.int64)
# normalize
for i in range(X.shape[1]):
X[:,i] = (X[:,i]-X[:,i].min()) / (X[:,i].max()-X[:,i].min())
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load UCI-Adult Data Set
ATTRIBUTE:
123 binary dimensions
48842 instances total
2 labels (0, 1)
FLOAT32
"""
class Adult(data.Dataset):
def __init__(self, data_folder, train=True, transform=None):
if train == True:
data_path = os.path.join(data_folder, 'train.mat')
read_data = scio.loadmat(data_path)['train_data']
else:
data_path = os.path.join(data_folder, 'test.mat')
read_data = scio.loadmat(data_path)['test_data']
y = np.array(read_data[:,-1])
X = np.array(read_data[:,0:-1])
X = X.astype(np.float32)
y = y.astype(np.int64)
y[y!=1] = 0
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load UCI-Ijcnn Data Set
ATTRIBUTE:
22 dimensions
48842 instances total
2 labels (0, 1)
FLOAT32
"""
class Ijcnn(data.Dataset):
def __init__(self, data_folder, train=True, transform=None):
if train:
data_path = os.path.join(data_folder, 'train.mat')
read_data1 = scio.loadmat(data_path)['train_data']
data_path = os.path.join(data_folder, 'val.mat')
read_data2 = scio.loadmat(data_path)['val_data']
read_data = np.vstack((read_data1, read_data2))
else:
data_path = os.path.join(data_folder, 'test.mat')
read_data = scio.loadmat(data_path)['test_data']
y = np.array(read_data[:,-1])
X = np.array(read_data[:,0:-1])
X = X.astype(np.float32)
y = y.astype(np.int64)
y[y!=1] = 0
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load UCI-Phishing Data Set
ATTRIBUTE:
68 dimensions
11055 instances total
2 labels (0, 1)
FLOAT32
"""
class Phishing(data.Dataset):
def __init__(self, data_folder, transform=None):
data_path = os.path.join(data_folder, 'phishing.mat')
read_data = scio.loadmat(data_path)['data']
y = np.array(read_data[:,-1])
X = np.array(read_data[:,0:-1])
X = X.astype(np.float32)
y = y.astype(np.int64)
y[y!=1] = 0
# normalize
for i in range(X.shape[1]):
X[:,i] = (X[:,i]-X[:,i].min()) / (X[:,i].max()-X[:,i].min())
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]
"""
Load synthetic data set
"""
class Synthetic(data.Dataset):
def __init__(self, data_folder, num_dim, train=True, transform=None):
if train == True:
data_path = os.path.join(data_folder, 'train_d%d.mat'%num_dim)
X = scio.loadmat(data_path)['Xtrain']
y = scio.loadmat(data_path)['ytrain']
else:
data_path = os.path.join(data_folder, 'test_d%d.mat'%num_dim)
X = scio.loadmat(data_path)['Xtest']
y = scio.loadmat(data_path)['ytest']
X = X.astype(np.float32)
y[y!=1] = 0
y = y.astype(np.int64).squeeze()
self.X = X
self.y = y
def __getitem__(self, index):
return self.X[index,:], self.y[index]
def __len__(self):
return self.X.shape[0]