-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_loading.py
209 lines (163 loc) · 8.44 KB
/
data_loading.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
"""
Data loading functions for TimeSformer and DenseNet
Put imports in #comments that are not needed for the model (use either tensorflow or pytorch imports)
"""
import numpy as np
import torch
from torch.utils.data import TensorDataset
# import tensorflow as tf
def load_dataset_train_valid(size, classes):
"""
Loading training and validation dataset for TimeSformer (pytorch)
Args:
size (str): the dataset size - '160x128x32', '80x64x16-2nd' or '80x64x16-mid'
classes ([str]): a list of classes that should be considered out of ['CP', 'NCP', 'Normal']
Returns:
train_dataset (torch.TensorDataset): the dataset for training
val_dataset (torch.TensorDataset): the dataset for validation
"""
data_train_list = []
data_valid_list = []
label_train_list = []
label_valid_list = []
for i, type in enumerate(classes):
loader_train = np.load('Data/dataset_'+type+'_train_'+size+'.npz')
loader_valid = np.load('Data/dataset_'+type+'_valid_'+size+'.npz')
dataset_train = loader_train['arr_0']
dataset_valid = loader_valid['arr_0']
if size in ['160x128x32']:
dataset_train = dataset_train.reshape(-1, 160, 128, 32)
dataset_train = dataset_train[:, :, :, :, np.newaxis]
dataset_train = dataset_train.reshape(-1, 1, 32, 128, 160)
dataset_valid = dataset_valid.reshape(-1, 160, 128, 32)
dataset_valid = dataset_valid[:, :, :, :, np.newaxis]
dataset_valid = dataset_valid.reshape(-1, 1, 32, 128, 160)
elif size in ['80x64x16-2nd', '80x64x16-mid']:
dataset_train = dataset_train.reshape(-1, 80, 64, 16)
dataset_train = dataset_train[:, :, :, :, np.newaxis]
dataset_train = dataset_train.reshape(-1, 1, 16, 64, 80)
dataset_valid = dataset_valid.reshape(-1, 80, 64, 16)
dataset_valid = dataset_valid[:, :, :, :, np.newaxis]
dataset_valid = dataset_valid.reshape(-1, 1, 16, 64, 80)
labels_train = np.array([i for _ in range(len(dataset_train))])
labels_valid = np.array([i for _ in range(len(dataset_valid))])
data_train_list.append(dataset_train)
data_valid_list.append(dataset_valid)
label_train_list.append(labels_train)
label_valid_list.append(labels_valid)
x_train = np.concatenate(data_train_list, axis=0)
y_train = np.concatenate(label_train_list, axis=0)
x_val = np.concatenate(data_valid_list, axis=0)
y_val = np.concatenate(label_valid_list, axis=0)
tensor_x_train = torch.Tensor(x_train)
tensor_y_train = torch.Tensor(y_train)
tensor_x_val = torch.Tensor(x_val)
tensor_y_val = torch.Tensor(y_val)
train_dataset = TensorDataset(tensor_x_train,tensor_y_train)
val_dataset = TensorDataset(tensor_x_val,tensor_y_val)
return train_dataset, val_dataset
def load_dataset_test(size, classes):
"""
Loading test dataset for TimeSformer (pytorch)
Args:
size (str): the dataset size - '160x128x32', '80x64x16-2nd' or '80x64x16-mid'
classes ([str]): a list of classes that should be considered out of ['CP', 'NCP', 'Normal']
Returns:
test_dataset (torch.TensorDataset): the dataset for testing
"""
data_test_list = []
label_test_list = []
for i, type in enumerate(classes):
loader_test = np.load('Data/dataset_'+type+'_test_'+size+'.npz')
dataset_test = loader_test['arr_0']
if size in ['160x128x32']:
dataset_test = dataset_test.reshape(-1, 160, 128, 32)
dataset_test = dataset_test[:, :, :, :, np.newaxis]
dataset_test = dataset_test.reshape(-1, 1, 32, 128, 160)
dataset_test = dataset_test.reshape(-1, 1, 1, 32, 128, 160)
elif size in ['80x64x16-2nd', '80x64x16-mid']:
dataset_test = dataset_test.reshape(-1, 80, 64, 16)
dataset_test = dataset_test[:, :, :, :, np.newaxis]
dataset_test = dataset_test.reshape(-1, 1, 16, 64, 80)
dataset_test = dataset_test.reshape(-1, 1, 1, 16, 64, 80)
labels_test = np.array([i for _ in range(len(dataset_test))])
data_test_list.append(dataset_test)
label_test_list.append(labels_test)
x_test = np.concatenate(data_test_list, axis=0)
y_test = np.concatenate(label_test_list, axis=0)
tensor_x_test = torch.Tensor(x_test)
tensor_y_test = torch.Tensor(y_test)
test_dataset = TensorDataset(tensor_x_test,tensor_y_test)
return test_dataset
def load_dataset_train_valid_test(size, classes):
"""
Loading training, validation and test dataset for DenseNet (tf/keras)
Args:
size (str): the dataset size - '160x128x32', '80x64x16-2nd' or '80x64x16-mid'
classes ([str]): a list of classes that should be considered out of ['CP', 'NCP', 'Normal']
Returns:
train_dataset (tf.data.Dataset): the dataset for training
val_dataset (tf.data.Dataset): the dataset for validation
test_dataset (tf.data.Dataset): the dataset for testing
x_train (np.array): array containing x values for training
x_val (np.array): array containing x values for validation
x_test (np.array): array containing x values for testing
y_train (np.array): array containing y values for training
y_val (np.array): array containing y values for validation
y_test (np.array): array containing y values for testing
"""
data_train_list = []
data_valid_list = []
data_test_list = []
label_train_list = []
label_valid_list = []
label_test_list = []
label2class = [[1,0], [0,1]]
label3class = [[1,0,0], [0,1,0], [0,0,1]]
for i, type in enumerate(classes):
loader_train = np.load('Data/dataset_'+type+'_train_'+size+'.npz')
loader_valid = np.load('Data/dataset_'+type+'_valid_'+size+'.npz')
loader_test = np.load('Data/dataset_'+type+'_test_'+size+'.npz')
dataset_train = loader_train['arr_0']
dataset_valid = loader_valid['arr_0']
dataset_test = loader_test['arr_0']
if size in ['160x128x32']:
dataset_train = dataset_train.reshape(-1, 160, 128, 32)
dataset_valid = dataset_valid.reshape(-1, 160, 128, 32)
dataset_test = dataset_test.reshape(-1, 160, 128, 32)
if len(classes) == 3:
dataset_train = dataset_train[:, :, :, :, np.newaxis]
dataset_valid = dataset_valid[:, :, :, :, np.newaxis]
dataset_test = dataset_test[:, :, :, :, np.newaxis]
elif size in ['80x64x16-2nd', '80x64x16-mid']:
dataset_train = dataset_train.reshape(-1, 80, 64, 16)
dataset_valid = dataset_valid.reshape(-1, 80, 64, 16)
dataset_test = dataset_test.reshape(-1, 80, 64, 16)
if len(classes) == 3:
dataset_train = dataset_train[:, :, :, :, np.newaxis]
dataset_valid = dataset_valid[:, :, :, :, np.newaxis]
dataset_test = dataset_test[:, :, :, :, np.newaxis]
if len(classes) == 3:
labels_train = np.array([label3class[i] for _ in range(len(dataset_train))])
labels_valid = np.array([label3class[i] for _ in range(len(dataset_valid))])
labels_test = np.array([label3class[i] for _ in range(len(dataset_test))])
else:
labels_train = np.array([label2class[i] for _ in range(len(dataset_train))])
labels_valid = np.array([label2class[i] for _ in range(len(dataset_valid))])
labels_test = np.array([label2class[i] for _ in range(len(dataset_test))])
data_train_list.append(dataset_train)
data_valid_list.append(dataset_valid)
label_train_list.append(labels_train)
label_valid_list.append(labels_valid)
data_test_list.append(dataset_test)
label_test_list.append(labels_test)
x_train = np.concatenate(data_train_list, axis=0)
y_train = np.concatenate(label_train_list, axis=0)
x_val = np.concatenate(data_valid_list, axis=0)
y_val = np.concatenate(label_valid_list, axis=0)
x_test = np.concatenate(data_test_list, axis=0)
y_test = np.concatenate(label_test_list, axis=0)
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))
test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))
return train_dataset, val_dataset, test_dataset, x_train, x_val, x_test, y_train, y_val, y_test