-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
executable file
·236 lines (184 loc) · 9.21 KB
/
dataloader.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: winston
"""
import numpy as np
from torch.utils.data import Dataset
from scipy.io import loadmat
from utils import getPaths_attri, getPaths_unlabel_Podcast
import random
# Ignore warnings & Fix random seed
import warnings
warnings.filterwarnings("ignore")
random.seed(999)
###############################################################################
# For the building of SSL-DeepEmoCluster Dataloaders #
###############################################################################
class UnlabelDataset(Dataset):
"""Unlabeled Dataset"""
def __init__(self, unlabel_podcast_dir):
# init parameters
self.unlabel_podcast_dir = unlabel_podcast_dir
# norm-parameters
self.Feat_mean = loadmat('./NormTerm/feat_norm_means.mat')['normal_para']
self.Feat_std = loadmat('./NormTerm/feat_norm_stds.mat')['normal_para']
# unlabeled data paths
self._paths = getPaths_unlabel_Podcast(unlabel_podcast_dir)
# data path of each utterance
self.imgs = []
repeat_paths = self._paths.tolist()
for i in range(len(repeat_paths)):
self.imgs.extend([unlabel_podcast_dir+repeat_paths[i]])
def __len__(self):
return len(self._paths)
def __getitem__(self, idx):
# Loading Data MSP-Podcast Unlabeled set
data = loadmat(self.unlabel_podcast_dir + self._paths[idx].replace('.wav','.mat'))['Audio_data']
# Z-normalization
data = (data - self.Feat_mean) / self.Feat_std
data = data.reshape(-1)
# Bounded NormFeat Range -3~3 and assign NaN to 0
data[np.isnan(data)]=0
data[data>3]=3
data[data<-3]= -3
return data
class MspPodcastEmoDataset(Dataset):
"""MSP-Podcast Dataset (labeled set)"""
def __init__(self, root_dir, label_dir, split_set, emo_attr):
# init parameters
self.root_dir = root_dir
self.label_dir = label_dir
self.split_set = split_set
# label data paths
self._paths, self._labels = getPaths_attri(label_dir, split_set, emo_attr)
# norm-parameters
self.Feat_mean = loadmat('./NormTerm/feat_norm_means.mat')['normal_para']
self.Feat_std = loadmat('./NormTerm/feat_norm_stds.mat')['normal_para']
if emo_attr == 'Act':
self.Label_mean = loadmat('./NormTerm/act_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/act_norm_stds.mat')['normal_para'][0][0]
elif emo_attr == 'Dom':
self.Label_mean = loadmat('./NormTerm/dom_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/dom_norm_stds.mat')['normal_para'][0][0]
elif emo_attr == 'Val':
self.Label_mean = loadmat('./NormTerm/val_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/val_norm_stds.mat')['normal_para'][0][0]
# data path/label of each utterance
self.imgs = []
repeat_paths = self._paths.tolist()
repeat_labels = ((self._labels-self.Label_mean)/self.Label_std).tolist()
for i in range(len(repeat_paths)):
self.imgs.extend([(root_dir+repeat_paths[i], repeat_labels[i])])
def __len__(self):
return len(self._paths)
def __getitem__(self, idx):
# Loading Labeled Data
data = loadmat(self.root_dir + self._paths[idx].replace('.wav','.mat'))['Audio_data']
# Z-normalization
data = (data - self.Feat_mean) / self.Feat_std
data = data.reshape(-1)
# Bounded NormFeat Range -3~3 and assign NaN to 0
data[np.isnan(data)]=0
data[data>3]=3
data[data<-3]=-3
# Loading Label & Normalization
label = self._labels[idx]
label = (label - self.Label_mean) / self.Label_std
return data, label
###############################################################################
# For the building of Curriculum Learning Dataloaders #
###############################################################################
def parse_dict_to_arry(metric_dict: dict):
"""This function parses the saved DeepMI curriculum metric to perform curriculum learning."""
FileName = np.array(list(metric_dict.keys()))
Metric_Value = np.array(list(metric_dict.values()))
# sorted by DeepMI-values
sort_idx = Metric_Value.argsort()
sort_FileName = FileName[sort_idx[::-1]]
sort_Metric_Value = Metric_Value[sort_idx[::-1]]
return sort_FileName, sort_Metric_Value
class MspPodcast_CurriculumTrain(Dataset):
"""MSP-Podcast dataset (for curriculum learning)"""
def __init__(self, root_dir, label_dir, emo_attr, curriculum_metric, difficulty_level):
# init parameters
self.root_dir = root_dir
##############################################################################
# Dynamically define curriculum dataset based on given difficulty metric #
##############################################################################
# loading all labeled data paths (training set)
_paths_all, _labels_all = getPaths_attri(label_dir, split_set='Train', emo_attr=emo_attr)
# sorting by DeepMI curriculum_metric
_metric_sort_files, _ = parse_dict_to_arry(curriculum_metric)
bin_length = int((difficulty_level / 10) * len(_paths_all)) # divide 10-bins of difficulty levels in total
training_files = _metric_sort_files[:bin_length]
training_labels = []
for f in training_files:
training_labels.append(_labels_all[f==_paths_all][0])
training_labels = np.array(training_labels)
self._paths = training_files
self._labels = training_labels
###############################################################################
# norm-parameters
self.Feat_mean = loadmat('./NormTerm/feat_norm_means.mat')['normal_para']
self.Feat_std = loadmat('./NormTerm/feat_norm_stds.mat')['normal_para']
if emo_attr == 'Act':
self.Label_mean = loadmat('./NormTerm/act_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/act_norm_stds.mat')['normal_para'][0][0]
elif emo_attr == 'Dom':
self.Label_mean = loadmat('./NormTerm/dom_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/dom_norm_stds.mat')['normal_para'][0][0]
elif emo_attr == 'Val':
self.Label_mean = loadmat('./NormTerm/val_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/val_norm_stds.mat')['normal_para'][0][0]
def __len__(self):
return len(self._paths)
def __getitem__(self, idx):
# Loading Data
data = loadmat(self.root_dir + self._paths[idx].replace('.wav','.mat'))['Audio_data']
# Z-normalization
data = (data - self.Feat_mean) / self.Feat_std
data = data.reshape(-1)
# Bounded NormFeat Range -3~3 and assign NaN to 0
data[np.isnan(data)]=0
data[data>3]=3
data[data<-3]=-3
# Loading Label & Normalization
label = self._labels[idx]
label = (label - self.Label_mean) / self.Label_std
return data, label
class MspPodcast_Validation(Dataset):
"""MSP-Podcast dataset (for validation)"""
def __init__(self, root_dir, label_dir, emo_attr):
# init parameters
self.root_dir = root_dir
# labeled data paths (validation/development set)
self._paths, self._labels = getPaths_attri(label_dir, split_set='Validation', emo_attr=emo_attr)
# norm-parameters
self.Feat_mean = loadmat('./NormTerm/feat_norm_means.mat')['normal_para']
self.Feat_std = loadmat('./NormTerm/feat_norm_stds.mat')['normal_para']
if emo_attr == 'Act':
self.Label_mean = loadmat('./NormTerm/act_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/act_norm_stds.mat')['normal_para'][0][0]
elif emo_attr == 'Dom':
self.Label_mean = loadmat('./NormTerm/dom_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/dom_norm_stds.mat')['normal_para'][0][0]
elif emo_attr == 'Val':
self.Label_mean = loadmat('./NormTerm/val_norm_means.mat')['normal_para'][0][0]
self.Label_std = loadmat('./NormTerm/val_norm_stds.mat')['normal_para'][0][0]
def __len__(self):
return len(self._paths)
def __getitem__(self, idx):
# Loading Data
data = loadmat(self.root_dir + self._paths[idx].replace('.wav','.mat'))['Audio_data']
# Z-normalization
data = (data - self.Feat_mean) / self.Feat_std
data = data.reshape(-1)
# Bounded NormFeat Range -3~3 and assign NaN to 0
data[np.isnan(data)]=0
data[data>3]=3
data[data<-3]=-3
# Loading Label & Normalization
label = self._labels[idx]
label = (label-self.Label_mean)/self.Label_std
return data, label