-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset.py
217 lines (184 loc) · 6.52 KB
/
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
import numpy as np
import torch
from torchvision import datasets
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from PIL import Image
import pdb
def get_dataset(name):
if name == 'MNIST':
return get_MNIST()
elif name == 'FashionMNIST':
return get_FashionMNIST()
elif name == 'SVHN':
return get_SVHN()
elif name == 'CIFAR10':
return get_CIFAR10()
elif name == 'CIFAR100':
return get_CIFAR100()
elif name == 'CALTECH256':
return get_CALTECH256()
elif name == 'TINY_IMAGENET':
return get_TINY_IMAGENET()
def get_MNIST():
raw_tr = datasets.MNIST('./MNIST', train=True, download=True)
raw_te = datasets.MNIST('./MNIST', train=False, download=True)
X_tr = raw_tr.train_data
Y_tr = raw_tr.train_labels
X_te = raw_te.test_data
Y_te = raw_te.test_labels
return X_tr, Y_tr, X_te, Y_te
def get_FashionMNIST():
raw_tr = datasets.FashionMNIST('./FashionMNIST', train=True, download=True)
raw_te = datasets.FashionMNIST('./FashionMNIST', train=False, download=True)
X_tr = raw_tr.train_data
Y_tr = raw_tr.train_labels
X_te = raw_te.test_data
Y_te = raw_te.test_labels
return X_tr, Y_tr, X_te, Y_te
def get_SVHN():
data_tr = datasets.SVHN('./SVHN', split='train', download=True)
data_te = datasets.SVHN('./SVHN', split='test', download=True)
X_tr = data_tr.data
Y_tr = torch.from_numpy(data_tr.labels)
X_te = data_te.data
Y_te = torch.from_numpy(data_te.labels)
return X_tr, Y_tr, X_te, Y_te
def get_CIFAR10():
data_tr = datasets.CIFAR10('./CIFAR10', train=True, download=True)
data_te = datasets.CIFAR10('./CIFAR10', train=False, download=True)
X_tr = data_tr.train_data
Y_tr = torch.from_numpy(np.array(data_tr.train_labels))
X_te = data_te.test_data
Y_te = torch.from_numpy(np.array(data_te.test_labels))
return X_tr, Y_tr, X_te, Y_te
def get_CIFAR100():
data_tr = datasets.CIFAR100('./CIFAR100', train=True, download=True)
data_te = datasets.CIFAR100('./CIFAR100', train=False, download=True)
X_tr = data_tr.train_data
Y_tr = torch.from_numpy(np.array(data_tr.train_labels))
X_te = data_te.test_data
Y_te = torch.from_numpy(np.array(data_te.test_labels))
return X_tr, Y_tr, X_te, Y_te
def get_CALTECH256():
data_tr = datasets.Caltech256('./CALTECH256', train=True, download=True)
data_te = datasets.Caltech256('./CALTECH256', train=False, download=True)
X_tr = data_tr.train_data
Y_tr = torch.from_numpy(np.array(data_tr.train_labels))
X_te = data_te.test_data
Y_te = torch.from_numpy(np.array(data_te.test_labels))
return X_tr, Y_tr, X_te, Y_te
def get_TINY_IMAGENET():
data_tr = datasets.ImageFolder('./tiny-imagenet-200/train/')
data_te = datasets.ImageFolder('./tiny-imagenet-200/val/')
fp = open('./tiny-imagenet-200/val/val_annotations.txt', 'r')
data = fp.readlines()
val_img_dict = {}
for line in data:
words = line.split('\t')
val_img_dict[words[0]] = words[1]
fp.close()
train_data = []
train_labels = []
for i in range(len(data_tr)):
img = data_tr.__getitem__(i)[0]
train_data.append(np.asarray(img))
train_labels.append(data_tr.__getitem__(i)[1])
train_data = np.concatenate(train_data)
train_data = train_data.reshape((len(data_tr), 3, 64, 64))
train_data = train_data.transpose((0, 2, 3, 1)) # convert to HWC
test_data = []
test_labels = []
for i in range(len(data_te)):
img = data_te.__getitem__(i)[0]
test_data.append(np.asarray(img))
img_name=(data_te.samples[i][0]).split('/')[-1]
test_labels.append(data_tr.classes.index(val_img_dict[img_name]))
test_data = np.concatenate(test_data)
test_data = test_data.reshape((len(data_te), 3, 64, 64))
test_data = test_data.transpose((0, 2, 3, 1)) # convert to HWC
X_tr = train_data
Y_tr = torch.from_numpy(np.array(train_labels))
X_te = test_data
Y_te = torch.from_numpy(np.array(test_labels))
return X_tr, Y_tr, X_te, Y_te
def get_handler(name):
if name == 'MNIST':
return DataHandler1
elif name == 'FashionMNIST':
return DataHandler1
elif name == 'SVHN':
return DataHandler2
elif name == 'CIFAR10':
return DataHandler3
elif name == 'CIFAR100':
return DataHandler3
elif name == 'CALTECH256':
return DataHandler4
elif name == 'TINY_IMAGENET':
return DataHandler5
class DataHandler1(Dataset):
def __init__(self, X, Y, transform=None):
self.X = X
self.Y = Y
self.transform = transform
def __getitem__(self, index):
x, y = self.X[index], self.Y[index]
if self.transform is not None:
x = Image.fromarray(x.numpy(), mode='L')
x = self.transform(x)
return x, y, index
def __len__(self):
return len(self.X)
class DataHandler2(Dataset):
def __init__(self, X, Y, transform=None):
self.X = X
self.Y = Y
self.transform = transform
def __getitem__(self, index):
x, y = self.X[index], self.Y[index]
if self.transform is not None:
x = Image.fromarray(np.transpose(x, (1, 2, 0)))
x = self.transform(x)
return x, y, index
def __len__(self):
return len(self.X)
class DataHandler3(Dataset):
def __init__(self, X, Y, transform=None):
self.X = X
self.Y = Y
self.transform = transform
def __getitem__(self, index):
x, y = self.X[index], self.Y[index]
if self.transform is not None:
x = Image.fromarray(x)
x = self.transform(x)
return x, y, index
def __len__(self):
return len(self.X)
class DataHandler4(Dataset):
def __init__(self, X, Y, transform=None):
self.X = X
self.Y = Y
self.transform = transform
def __getitem__(self, index):
x, y = self.X[index], self.Y[index]
if self.transform is not None:
x = Image.fromarray(x)
x = self.transform(x)
return x, y, index
def __len__(self):
return len(self.X)
class DataHandler5(Dataset):
def __init__(self, X, Y, transform=None):
self.X = X
self.Y = Y
self.transform = transform
def __getitem__(self, index):
x, y = self.X[index], self.Y[index]
if self.transform is not None:
x = Image.fromarray(x)
x = self.transform(x)
return x, y, index
def __len__(self):
return len(self.X)