-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataprocessing.py
136 lines (111 loc) · 5.26 KB
/
dataprocessing.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
import os, random, sys
import torch
import numpy as np
import scipy.io as sio
from sklearn.preprocessing import MinMaxScaler
from torch.utils.data import Dataset
# from torch.nn.functional import normalize
from utils import *
class MultiviewData(Dataset):
def __init__(self, db, device, path="datasets/"):
self.data_views = list()
if db == "MSRCv1":
mat = sio.loadmat(os.path.join(path, 'MSRCv1.mat'))
X_data = mat['X']
self.num_views = X_data.shape[1]
for idx in range(self.num_views):
self.data_views.append(X_data[0, idx].astype(np.float32))
scaler = MinMaxScaler()
for idx in range(self.num_views):
self.data_views[idx] = scaler.fit_transform(self.data_views[idx])
self.labels = np.array(np.squeeze(mat['Y'])).astype(np.int32)
elif db == "MNIST-USPS":
mat = sio.loadmat(os.path.join(path, 'MNIST_USPS.mat'))
X1 = mat['X1'].astype(np.float32)
X2 = mat['X2'].astype(np.float32)
self.data_views.append(X1.reshape(X1.shape[0], -1))
self.data_views.append(X2.reshape(X2.shape[0], -1))
self.num_views = len(self.data_views)
self.labels = np.array(np.squeeze(mat['Y'])).astype(np.int32)
elif db == "BDGP":
mat = sio.loadmat(os.path.join(path, 'BDGP.mat'))
X1 = mat['X1'].astype(np.float32)
X2 = mat['X2'].astype(np.float32)
self.data_views.append(X1)
self.data_views.append(X2)
self.num_views = len(self.data_views)
self.labels = np.array(np.squeeze(mat['Y'])).astype(np.int32)
elif db == "Fashion":
mat = sio.loadmat(os.path.join(path, 'Fashion.mat'))
X1 = mat['X1'].reshape(mat['X1'].shape[0], mat['X1'].shape[1] * mat['X1'].shape[2]).astype(np.float32)
X2 = mat['X2'].reshape(mat['X2'].shape[0], mat['X2'].shape[1] * mat['X2'].shape[2]).astype(np.float32)
X3 = mat['X3'].reshape(mat['X3'].shape[0], mat['X3'].shape[1] * mat['X3'].shape[2]).astype(np.float32)
self.data_views.append(X1)
self.data_views.append(X2)
self.data_views.append(X3)
self.num_views = len(self.data_views)
self.labels = np.array(np.squeeze(mat['Y'])).astype(np.int32)
elif db == "COIL20":
mat = sio.loadmat(os.path.join(path, 'COIL20.mat'))
X_data = mat['X']
self.num_views = X_data.shape[1]
for idx in range(self.num_views):
self.data_views.append(X_data[0, idx].astype(np.float32))
scaler = MinMaxScaler()
for idx in range(self.num_views):
self.data_views[idx] = scaler.fit_transform(self.data_views[idx])
self.labels = np.array(np.squeeze(mat['Y'])).astype(np.int32)
elif db == "hand":
mat = sio.loadmat(os.path.join(path, 'handwritten.mat'))
X_data = mat['X']
self.num_views = X_data.shape[1]
for idx in range(self.num_views):
self.data_views.append(X_data[0, idx].astype(np.float32))
scaler = MinMaxScaler()
for idx in range(self.num_views):
self.data_views[idx] = scaler.fit_transform(self.data_views[idx])
self.labels = np.array(np.squeeze(mat['Y'])+1).astype(np.int32)
elif db == "scene":
mat = sio.loadmat(os.path.join(path, 'Scene15.mat'))
X_data = mat['X']
self.num_views = X_data.shape[1]
for idx in range(self.num_views):
self.data_views.append(X_data[0, idx].astype(np.float32))
scaler = MinMaxScaler()
for idx in range(self.num_views):
self.data_views[idx] = scaler.fit_transform(self.data_views[idx])
self.labels = np.array(np.squeeze(mat['Y'])).astype(np.int32)
else:
raise NotImplementedError
for idx in range(self.num_views):
self.data_views[idx] = torch.from_numpy(self.data_views[idx]).to(device)
def __len__(self):
return len(self.labels)
def __getitem__(self, index):
sub_data_views = list()
for view_idx in range(self.num_views):
data_view = self.data_views[view_idx]
sub_data_views.append(data_view[index])
return sub_data_views, self.labels[index]
def get_multiview_data(mv_data, batch_size):
num_views = len(mv_data.data_views)
num_samples = len(mv_data.labels)
num_clusters = len(np.unique(mv_data.labels))
mv_data_loader = torch.utils.data.DataLoader(
mv_data,
batch_size=batch_size,
shuffle=True,
drop_last=True,
)
return mv_data_loader, num_views, num_samples, num_clusters
def get_all_multiview_data(mv_data):
num_views = len(mv_data.data_views)
num_samples = len(mv_data.labels)
num_clusters = len(np.unique(mv_data.labels))
mv_data_loader = torch.utils.data.DataLoader(
mv_data,
batch_size=num_samples,
shuffle=True,
drop_last=True,
)
return mv_data_loader, num_views, num_samples, num_clusters