-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdataloader.py
More file actions
247 lines (219 loc) · 9.24 KB
/
dataloader.py
File metadata and controls
247 lines (219 loc) · 9.24 KB
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
from sklearn.preprocessing import MinMaxScaler
import numpy as np
from torch.utils.data import Dataset
import scipy.io
import torch
class BDGP(Dataset):
def __init__(self, path):
data1 = scipy.io.loadmat(path+'BDGP.mat')['X1'].astype(np.float32)
data2 = scipy.io.loadmat(path+'BDGP.mat')['X2'].astype(np.float32)
labels = scipy.io.loadmat(path+'BDGP.mat')['Y'].transpose()
self.x1 = data1
self.x2 = data2
self.y = labels
def __len__(self):
return self.x1.shape[0]
def __getitem__(self, idx):
return [torch.from_numpy(self.x1[idx]), torch.from_numpy(
self.x2[idx])], torch.from_numpy(self.y[idx]), torch.from_numpy(np.array(idx)).long()
class CCV(Dataset):
def __init__(self, path):
self.data1 = np.load(path+'STIP.npy').astype(np.float32)
scaler = MinMaxScaler()
self.data1 = scaler.fit_transform(self.data1)
self.data2 = np.load(path+'SIFT.npy').astype(np.float32)
self.data3 = np.load(path+'MFCC.npy').astype(np.float32)
self.labels = np.load(path+'label.npy')
def __len__(self):
return 6773
def __getitem__(self, idx):
x1 = self.data1[idx]
x2 = self.data2[idx]
x3 = self.data3[idx]
return [torch.from_numpy(x1), torch.from_numpy(
x2), torch.from_numpy(x3)], torch.from_numpy(self.labels[idx]), torch.from_numpy(np.array(idx)).long()
class MNIST_USPS(Dataset):
def __init__(self, path):
self.Y = scipy.io.loadmat(path + 'MNIST_USPS.mat')['Y'].astype(np.int32).reshape(5000,)
self.V1 = scipy.io.loadmat(path + 'MNIST_USPS.mat')['X1'].astype(np.float32)
self.V2 = scipy.io.loadmat(path + 'MNIST_USPS.mat')['X2'].astype(np.float32)
def __len__(self):
return 5000
def __getitem__(self, idx):
x1 = self.V1[idx].reshape(784)
x2 = self.V2[idx].reshape(784)
return [torch.from_numpy(x1), torch.from_numpy(x2)], self.Y[idx], torch.from_numpy(np.array(idx)).long()
class Fashion(Dataset):
def __init__(self, path):
self.Y = scipy.io.loadmat(path + 'Fashion.mat')['Y'].astype(np.int32).reshape(10000,)
self.V1 = scipy.io.loadmat(path + 'Fashion.mat')['X1'].astype(np.float32)
self.V2 = scipy.io.loadmat(path + 'Fashion.mat')['X2'].astype(np.float32)
self.V3 = scipy.io.loadmat(path + 'Fashion.mat')['X3'].astype(np.float32)
def __len__(self):
return 10000
def __getitem__(self, idx):
x1 = self.V1[idx].reshape(784)
x2 = self.V2[idx].reshape(784)
x3 = self.V3[idx].reshape(784)
return [torch.from_numpy(x1), torch.from_numpy(x2), torch.from_numpy(x3)], self.Y[idx], torch.from_numpy(np.array(idx)).long()
class Caltech(Dataset):
def __init__(self, path, view):
data = scipy.io.loadmat(path)
scaler = MinMaxScaler()
self.view1 = scaler.fit_transform(data['X1'].astype(np.float32))
self.view2 = scaler.fit_transform(data['X2'].astype(np.float32))
self.view3 = scaler.fit_transform(data['X3'].astype(np.float32))
self.view4 = scaler.fit_transform(data['X4'].astype(np.float32))
self.view5 = scaler.fit_transform(data['X5'].astype(np.float32))
self.labels = scipy.io.loadmat(path)['Y'].transpose()
self.view = view
def __len__(self):
return 1400
def __getitem__(self, idx):
if self.view == 2:
return [torch.from_numpy(
self.view1[idx]), torch.from_numpy(self.view2[idx])], torch.from_numpy(self.labels[idx]), torch.from_numpy(np.array(idx)).long()
if self.view == 3:
return [torch.from_numpy(self.view1[idx]), torch.from_numpy(
self.view2[idx]), torch.from_numpy(self.view5[idx])], torch.from_numpy(self.labels[idx]), torch.from_numpy(np.array(idx)).long()
if self.view == 4:
return [torch.from_numpy(self.view1[idx]), torch.from_numpy(self.view2[idx]), torch.from_numpy(
self.view5[idx]), torch.from_numpy(self.view4[idx])], torch.from_numpy(self.labels[idx]), torch.from_numpy(np.array(idx)).long()
if self.view == 5:
return [torch.from_numpy(self.view1[idx]), torch.from_numpy(
self.view2[idx]), torch.from_numpy(self.view5[idx]), torch.from_numpy(
self.view4[idx]), torch.from_numpy(self.view3[idx])], torch.from_numpy(self.labels[idx]), torch.from_numpy(np.array(idx)).long()
class cifar_10():
def __init__(self, path):
data = scipy.io.loadmat(path + 'cifar10.mat')
self.Y = data['truelabel'][0][0].astype(np.int32).reshape(50000,)
self.V1 = data['data'][0][0].T.astype(np.float32)
self.V2 = data['data'][1][0].T.astype(np.float32)
self.V3 = data['data'][2][0].T.astype(np.float32)
def __len__(self):
return 50000
def __getitem__(self, idx):
x1 = self.V1[idx]
x2 = self.V2[idx]
x3 = self.V3[idx]
return [torch.from_numpy(x1), torch.from_numpy(x2), torch.from_numpy(x3)], self.Y[idx], torch.from_numpy(np.array(idx)).long()
class cifar_100():
def __init__(self, path):
data = scipy.io.loadmat(path + 'cifar100.mat')
self.Y = data['truelabel'][0][0].astype(np.int32).reshape(50000,)
self.V1 = data['data'][0][0].T.astype(np.float32)
self.V2 = data['data'][1][0].T.astype(np.float32)
self.V3 = data['data'][2][0].T.astype(np.float32)
def __len__(self):
return 50000
def __getitem__(self, idx):
x1 = self.V1[idx]
x2 = self.V2[idx]
x3 = self.V3[idx]
return [torch.from_numpy(x1), torch.from_numpy(x2), torch.from_numpy(x3)],self.Y[idx], torch.from_numpy(np.array(idx)).long()
class synthetic3d():
def __init__(self, path):
data = scipy.io.loadmat(path + 'synthetic3d.mat')
self.Y = data['Y'].astype(np.int32).reshape(600,)
self.V1 = data['X'][0][0].astype(np.float32)
self.V2 = data['X'][1][0].astype(np.float32)
self.V3 = data['X'][2][0].astype(np.float32)
def __len__(self):
return 600
def __getitem__(self, idx):
x1 = self.V1[idx]
x2 = self.V2[idx]
x3 = self.V3[idx]
return [torch.from_numpy(x1), torch.from_numpy(x2), torch.from_numpy(x3)], \
self.Y[idx], torch.from_numpy(np.array(idx)).long()
class prokaryotic():
def __init__(self, path):
data = scipy.io.loadmat(path + 'prokaryotic.mat')
self.Y = data['Y'].astype(np.int32).reshape(551,)
self.V1 = data['X'][0][0].astype(np.float32)
self.V2 = data['X'][1][0].astype(np.float32)
self.V3 = data['X'][2][0].astype(np.float32)
def __len__(self):
return 551
def __getitem__(self, idx):
x1 = self.V1[idx]
x2 = self.V2[idx]
x3 = self.V3[idx]
return [torch.from_numpy(x1), torch.from_numpy(x2), torch.from_numpy(x3)], \
self.Y[idx], torch.from_numpy(np.array(idx)).long()
def load_data(dataset):
if dataset == "BDGP":
dataset = BDGP('./data/')
dims = [1750, 79]
view = 2
data_size = 2500
class_num = 5
elif dataset == "MNIST-USPS":
dataset = MNIST_USPS('./data/')
dims = [784, 784]
view = 2
class_num = 10
data_size = 5000
elif dataset == "CCV":
dataset = CCV('./data/')
dims = [5000, 5000, 4000]
view = 3
data_size = 6773
class_num = 20
elif dataset == "Fashion":
dataset = Fashion('./data/')
dims = [784, 784, 784]
view = 3
data_size = 10000
class_num = 10
elif dataset == "Caltech-2V":
dataset = Caltech('data/Caltech-5V.mat', view=2)
dims = [40, 254]
view = 2
data_size = 1400
class_num = 7
elif dataset == "Caltech-3V":
dataset = Caltech('data/Caltech-5V.mat', view=3)
dims = [40, 254, 928]
view = 3
data_size = 1400
class_num = 7
elif dataset == "Caltech-4V":
dataset = Caltech('data/Caltech-5V.mat', view=4)
dims = [40, 254, 928, 512]
view = 4
data_size = 1400
class_num = 7
elif dataset == "Caltech-5V":
dataset = Caltech('data/Caltech-5V.mat', view=5)
dims = [40, 254, 928, 512, 1984]
view = 5
data_size = 1400
class_num = 7
elif dataset == "Synthetic3d":
dataset = synthetic3d('./data/')
dims = [3,3,3]
view = 3
data_size = 600
class_num = 3
elif dataset == "Prokaryotic":
dataset = prokaryotic('./data/')
dims = [438, 3, 393]
view = 3
data_size = 551
class_num = 4
elif dataset == "Cifar10":
dataset = cifar_10('./data/')
dims = [512, 2048, 1024]
view = 3
data_size = 50000
class_num = 10
elif dataset == "Cifar100":
dataset = cifar_100('./data/')
dims = [512, 2048, 1024]
view = 3
data_size = 50000
class_num = 100
else:
raise NotImplementedError
return dataset, dims, view, data_size, class_num