-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
295 lines (239 loc) · 11 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
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
import random
from sklearn.preprocessing import StandardScaler
import torch
import numpy as np
from torch.utils.data import Dataset
# seed to initializa the random engine
#seed = 1234
def data_split(data_dict,
semi=False,
split_type='random',
positive_patient_num=20,
negative_patient_num=20,
seed=1234
):
random.seed(seed)
type_lists = {"positive": [], "negative": [], "untested": []}
for patient, info in data_dict.items():
result = info["pcr_test_result"]
type_lists[result].append(patient)
random.shuffle(type_lists["positive"])
random.shuffle(type_lists["negative"])
random.shuffle(type_lists["untested"])
if positive_patient_num < 0 or positive_patient_num > len(type_lists["positive"]):
positive_patient_num = len(type_lists["positive"])
if negative_patient_num < 0 or negative_patient_num > len(type_lists["negative"]):
negative_patient_num = len(type_lists["negative"])
# resize positive and negative datasets according to the specified number
type_lists["positive"] = type_lists["positive"][:positive_patient_num]
type_lists["negative"] = type_lists["negative"][:negative_patient_num]
train_list = []
valid_list = []
test_list = []
if split_type == "speaker":
# Split the dataset according to speaker (split by a 7speaker:1speaker:1speaker manner)
positive_index1, positive_index2 = (positive_patient_num//9)*7, (positive_patient_num//9)*8
for i, patient in enumerate(type_lists["positive"]):
info = data_dict[patient]
for feature_path in info["feature_paths"]:
if i < positive_index1:
train_list.append([feature_path, 1]) # the label for positive is 1
elif i >= positive_index1 and i < positive_index2:
valid_list.append([feature_path, 1])
else:
test_list.append([feature_path, 1])
negative_index1, negative_index2 = (negative_patient_num//9)*7, (negative_patient_num//9)*8
for i, patient in enumerate(type_lists["negative"]):
info = data_dict[patient]
for feature_path in info["feature_paths"]:
if i < negative_index1:
train_list.append([feature_path, 0]) # the label for negative is 0
elif i >= negative_index1 and i < negative_index2:
valid_list.append([feature_path, 0])
else:
test_list.append([feature_path, 0])
elif split_type == "7-1-1":
# Each patient/speaker has 9 audio file. We randomly pick 1 for valid, 1 for test
for patient in type_lists["positive"]:
info = data_dict[patient]
random.shuffle(info["feature_paths"])
for i, feature_path in enumerate(info["feature_paths"]):
if i == 1:
valid_list.append([feature_path, 1])
elif i == 2:
test_list.append([feature_path, 1])
else:
train_list.append([feature_path, 1])
for patient in type_lists["negative"]:
info = data_dict[patient]
random.shuffle(info["feature_paths"])
for i, feature_path in enumerate(info["feature_paths"]):
if i == 1:
valid_list.append([feature_path, 0])
elif i == 2:
test_list.append([feature_path, 0])
else:
train_list.append([feature_path, 0])
elif split_type == "random":
total_list = []
for patient in type_lists["positive"]:
info = data_dict[patient]
for feature_path in info["feature_paths"]:
total_list.append([feature_path, 1])
for patient in type_lists["negative"]:
info = data_dict[patient]
for feature_path in info["feature_paths"]:
total_list.append([feature_path, 0])
random.shuffle(total_list)
index1, index2 = (len(total_list)//9)*7, (len(total_list)//9)*8
train_list = total_list[:index1]
valid_list = total_list[index1:index2]
test_list = total_list[index2:]
if semi:
unlabeled_list = []
for patient in type_lists["untested"]:
info = data_dict[patient]
for feature_path in info["feature_paths"]:
unlabeled_list.append([feature_path, -1])
train_set = supervised_dataset(train_list)
else:
train_set = supervised_dataset(train_list)
valid_set = supervised_dataset(valid_list)
test_set = supervised_dataset(test_list)
return train_set, valid_set, test_set
def data_split2(data_dict,
semi=False,
split_type='random',
positive_patient_num=20,
negative_patient_num=20,
seed=1234
):
random.seed(seed)
type_lists = {"positive": [], "negative": [], "untested": []}
for patient, info in data_dict.items():
result = info["pcr_test_result"]
type_lists[result].append(patient)
random.shuffle(type_lists["positive"])
random.shuffle(type_lists["negative"])
random.shuffle(type_lists["untested"])
if positive_patient_num < 0 or positive_patient_num > len(type_lists["positive"]):
positive_patient_num = len(type_lists["positive"])
if negative_patient_num < 0 or negative_patient_num > len(type_lists["negative"]):
negative_patient_num = len(type_lists["negative"])
# resize positive and negative datasets according to the specified number
type_lists["positive"] = type_lists["positive"][:positive_patient_num]
type_lists["negative"] = type_lists["negative"][:negative_patient_num]
seen_speaker_train_list = []
unseen_speaker_train_list = []
test_list = []
## First we selected a half of speakers. Each of the speakers will contribute
## exactly one testing data. The remaining data will be our seen-speaker training data
for i, patient in enumerate(type_lists["positive"][:(positive_patient_num//2)]):
info = data_dict[patient]
random.shuffle(info['feature_paths'])
for i, feature_path in enumerate(info["feature_paths"]):
if i == 0:
test_list.append([feature_path, 1])
else:
seen_speaker_train_list.append([feature_path, 1])
for i, patient in enumerate(type_lists["negative"][:(negative_patient_num//2)]):
info = data_dict[patient]
random.shuffle(info['feature_paths'])
for i, feature_path in enumerate(info["feature_paths"]):
if i == 0:
test_list.append([feature_path, 0])
else:
seen_speaker_train_list.append([feature_path, 0])
# Gather training data from unseen speakers
for i, patient in enumerate(type_lists["positive"][(positive_patient_num//2):]):
info = data_dict[patient]
for feature_path in info["feature_paths"]:
unseen_speaker_train_list.append([feature_path, 1]) # the label for positive is 1
for i, patient in enumerate(type_lists["negative"][(negative_patient_num//2):]):
info = data_dict[patient]
for feature_path in info["feature_paths"]:
unseen_speaker_train_list.append([feature_path, 0]) # the label for negative is 0
# Make sure all three type of data spliting method
# will produce exactly the same number of training data
num_of_valid_data = len(seen_speaker_train_list)//8
num_of_train_data = len(seen_speaker_train_list) - num_of_valid_data
if split_type == "speaker":
random.shuffle(unseen_speaker_train_list)
valid_list = unseen_speaker_train_list[:num_of_valid_data]
train_list = unseen_speaker_train_list[num_of_valid_data:num_of_valid_data+num_of_train_data]
elif split_type == "7-1-1":
random.shuffle(seen_speaker_train_list)
valid_list = seen_speaker_train_list[:num_of_valid_data]
train_list = seen_speaker_train_list[num_of_valid_data:]
elif split_type == "random":
combine_list = seen_speaker_train_list + unseen_speaker_train_list
random.shuffle(combine_list)
valid_list = combine_list[:num_of_valid_data]
train_list = combine_list[num_of_valid_data:num_of_valid_data+num_of_train_data]
if semi:
unlabeled_list = []
for patient in type_lists["untested"]:
info = data_dict[patient]
for feature_path in info["feature_paths"]:
unlabeled_list.append([feature_path, -1])
train_set = supervised_dataset(train_list)
else:
train_set = supervised_dataset(train_list)
valid_set = supervised_dataset(valid_list)
test_set = supervised_dataset(test_list)
return train_set, valid_set, test_set
class supervised_dataset(Dataset):
def __init__(self, datalist):
self.features = []
self.labels = []
self.scaler = StandardScaler()
for path, label in datalist:
feature = np.load(path)
self.features.append(np.load(path))
self.labels.append(label)
all_features = np.concatenate(self.features, axis=1)
self.mean = all_features.mean(axis=1, keepdims=True)
self.std = all_features.std(axis=1, keepdims=True)
#print(self.mean, self.std)
def __getitem__(self, idx):
D, T = self.features[idx].shape
mean = np.repeat(self.mean, T, axis=1)
std = np.repeat(self.std, T, axis=1)
return (self.features[idx]-mean)/std, self.labels[idx]
def __len__(self):
return (len(self.features))
class supervised_collate_fn():
def __init__(self, num_of_frame=150, add_noise=True):
self.num_of_frame = num_of_frame
self.add_noise = add_noise
def __call__(self, batch):
features = []
labels = []
for feature, label in batch:
_, length = feature.shape
if length < self.num_of_frame:
feature = np.concatenate(
[np.tile(feature, self.num_of_frame // length), feature[:, :self.num_of_frame % length]],
axis=1)
elif length > self.num_of_frame and length <= self.num_of_frame+32:
start = (length - self.num_of_frame)//2
feature = feature[:, start:start+self.num_of_frame]
elif length > self.num_of_frame+32:
start = random.choice(range(16, length-self.num_of_frame-16))
feature = feature[:, start:start+self.num_of_frame]
features.append(np.expand_dims(feature, axis=0))
labels.append(label)
features = torch.from_numpy(np.concatenate(features, axis=0))
if self.add_noise:
noise = torch.randn(features.size())/10
features = features + noise
labels = torch.FloatTensor(labels)
return features, labels
#class semi_supervised_dataset(Dataset):
# def __init__(self, labeled_datalist, unlabeled_datalist):
#
#
# def __getitem__(self, idx):
#
#
# def __len__(self):