-
Notifications
You must be signed in to change notification settings - Fork 23
/
dataio.py
206 lines (128 loc) · 6.7 KB
/
dataio.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
import os
import tensorflow as tf
import numpy as np
import hyperparameters
from filter_dataset import *
from models import MFCCExtractor
#os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
AUTOTUNE = tf.data.experimental.AUTOTUNE
linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(hyperparameters.NUM_MEL_BINS,
hyperparameters.NUM_SPECTROGRAM_BINS,
hyperparameters.SAMPLE_RATE,
hyperparameters.LOWER_EDGE_HERTZ,
hyperparameters.UPPER_EDGE_HERTZ)
def decode_audio(audio_binary):
audio, _ = tf.audio.decode_wav(audio_binary)
return tf.squeeze(audio, axis=-1)
def get_label(file_path):
parts = tf.strings.split(file_path, os.path.sep)
return parts[-2]
def get_waveform_and_label(file_path):
label = get_label(file_path)
audio_binary = tf.io.read_file(file_path)
waveform = decode_audio(audio_binary)
return waveform, label
def get_spectrogram(waveform):
waveform = tf.cast(waveform, tf.float32)
spectrogram = tf.signal.stft(
waveform, frame_length=hyperparameters.FRAME_LENGTH, frame_step=hyperparameters.FRAME_STEP, fft_length=hyperparameters.FFT_LENGTH)
spectrogram = tf.abs(spectrogram)
return spectrogram
def get_mel_spectrogram(spectrogram):
mel_spectrogram = tf.tensordot(spectrogram, linear_to_mel_weight_matrix, 1)
mel_spectrogram.set_shape(spectrogram.shape[:-1].concatenate(linear_to_mel_weight_matrix.shape[-1:]))
# Compute a stabilized log to get log-magnitude mel-scale spectrograms.
log_mel_spectrogram = tf.math.log(mel_spectrogram + 1e-6)
return log_mel_spectrogram
def get_mfcc(log_mel_spectrograms, clip_value=10):
mfcc = mfccs = tf.signal.mfccs_from_log_mel_spectrograms(log_mel_spectrograms)[..., :hyperparameters.N_MFCC]
return tf.clip_by_value(mfcc, -clip_value, clip_value)
def get_label_id(label, labels_list):
label_id = tf.argmax(tf.cast(label == labels_list, tf.int64))
return label_id
def get_input_and_label_id(audio, label, labels_list, input_type="mfcc", merge_tflite=False):
label_id = get_label_id(label, labels_list)
if input_type == "spectrogram":
spectrogram = get_spectrogram(audio)
spectrogram = tf.expand_dims(spectrogram, -1)
return spectrogram, label_id
elif input_type == "mel_spectrogram":
spectrogram = get_spectrogram(audio)
mel_spectrogram = get_mel_spectrogram(spectrogram)
mel_spectrogram = tf.expand_dims(mel_spectrogram, -1)
return mel_spectrogram, label_id
elif input_type == "mfcc":
if merge_tflite:
mfcc = MFCCExtractor(hyperparameters.NUM_MEL_BINS,
hyperparameters.SAMPLE_RATE,
hyperparameters.LOWER_EDGE_HERTZ,
hyperparameters.UPPER_EDGE_HERTZ,
hyperparameters.FRAME_LENGTH,
hyperparameters.FRAME_STEP,
hyperparameters.N_MFCC)(audio[..., None])[0]
else:
spectrogram = get_spectrogram(audio)
mel_spectrogram = get_mel_spectrogram(spectrogram)
mfcc = get_mfcc(mel_spectrogram)
mfcc = tf.expand_dims(mfcc, -1)
return mfcc, label_id
else:
raise ValueError('input_type not valid!')
def preprocess_dataset(files, labels_list, input_type="mfcc", merge_tflite=False):
files_ds = tf.data.Dataset.from_tensor_slices(files)
output_ds = files_ds.map(get_waveform_and_label, num_parallel_calls=AUTOTUNE)
output_ds = output_ds.map(lambda x, y :
get_input_and_label_id(x, y, labels_list, input_type, merge_tflite), num_parallel_calls=AUTOTUNE)
return output_ds
def split_dataset(dataset_name, audio_type="all"):
dataset_name = os.path.join(hyperparameters.BASE_DIRECTORY, dataset_name)
if "IEMOCAP" in dataset_name:
labels_list = np.array(tf.io.gfile.listdir(str(dataset_name)))
if len(labels_list) != 4:
seperate_iemocap_class(dataset_name,
target_classes=['angry', 'neutral', 'sadness'],
merge_classes=['happiness', 'excited'])
filenames = tf.io.gfile.glob(str(dataset_name) + '/*/*')
if "IEMOCAP" in dataset_name:
filenames = filter_iemocap(filenames, audio_type=audio_type)
filenames = tf.random.shuffle(filenames)
num_samples = len(filenames)
labels_list = np.array(tf.io.gfile.listdir(str(dataset_name)))
splited_index = seperate_speaker_id_iemocap(filenames)
else:
filenames = tf.random.shuffle(filenames)
num_samples = len(filenames)
labels_list = np.array(tf.io.gfile.listdir(str(dataset_name)))
splited_index = seperate_speaker_id_emodb(filenames)
return filenames, splited_index, labels_list
def make_dataset(dataset_name, filenames, splited_index, labels_list, index_selection_fold, cache="disk", merge_tflite=False, input_type="mfcc", maker=True):
if cache == "disk":
cache_directory = f"{hyperparameters.BASE_DIRECTORY}/Cache/{dataset_name}"
os.system(f"rm -rf {cache_directory}")
train_cache_directory = os.path.join(cache_directory, "train")
test_cache_directory = os.path.join(cache_directory, "test")
os.makedirs(train_cache_directory, exist_ok=True)
os.makedirs(test_cache_directory, exist_ok=True)
test_index = splited_index[index_selection_fold]
train_index = np.setdiff1d(np.arange(len(filenames)), test_index)
train_files = tf.gather(filenames, train_index)
test_files = tf.gather(filenames, test_index)
train_dataset = preprocess_dataset(train_files, labels_list, input_type, merge_tflite)
test_dataset = preprocess_dataset(test_files, labels_list, input_type, merge_tflite)
if cache == "disk":
train_dataset = train_dataset.cache(train_cache_directory + "/file")
test_dataset = test_dataset.cache(test_cache_directory+ "/file")
elif cache == "ram":
train_dataset = train_dataset.cache()
test_dataset = test_dataset.cache()
elif cache== "None":
pass
else:
raise ValueError('cache method not valid!')
train_dataset = train_dataset.shuffle(len(train_files))
train_dataset = train_dataset.batch(hyperparameters.BATCH_SIZE).prefetch(AUTOTUNE)
test_dataset = test_dataset.batch(hyperparameters.BATCH_SIZE).prefetch(AUTOTUNE)
if maker:
list(test_dataset.as_numpy_iterator())
list(train_dataset.as_numpy_iterator())
return train_dataset, test_dataset