Repository navigation
Expand file tree
/
Copy pathdataset_generation.py
More file actions
299 lines (243 loc) · 15.6 KB
/
Copy pathdataset_generation.py
File metadata and controls
299 lines (243 loc) · 15.6 KB
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
296
297
298
299
import pandas as pd
import numpy as np
from IPython.display import Audio
import librosa
import soundfile
import json
import math
import os
import shutil
from os import listdir
from os.path import isfile, join
import json
import re
import random
from pathlib import Path
import sys
with open('your_config.json', 'r') as archivo_json:
config_datos = json.load(archivo_json)
common_voice_datapath = config_datos['common_voice_datapath']
wake_words = config_datos['wake_words']
wake_words_sequence = []
for indice, elemento in enumerate(wake_words):
wake_words_sequence.append(str(indice))
wake_word_seq_map = dict(zip(wake_words, wake_words_sequence))
sr = 16000
train_data = pd.read_csv(common_voice_datapath + 'train.tsv', sep='\t')
dev_data = pd.read_csv(common_voice_datapath + 'dev.tsv', sep='\t')
test_data = pd.read_csv(common_voice_datapath + 'test.tsv', sep='\t')
train_data_woCustom = train_data
dev_data_woCustom = dev_data
test_data_woCustom = test_data
custom_train_data_only = False
custom_dev_data_only = False
custom_test_data_only = False
if len(config_datos['custom_dataset_path']) > 0:
if os.path.isdir(config_datos['custom_dataset_path'] + 'clips'):
if os.path.exists(config_datos['custom_dataset_path'] + 'train.csv'):
custom_train_data = pd.read_csv(config_datos['custom_dataset_path'] + 'train.csv')
for customAudioPath in custom_train_data['path']:
shutil.copy(config_datos['custom_dataset_path'] + 'clips/' + customAudioPath, common_voice_datapath + '/clips/')
custom_train_data_only = custom_train_data
train_data = pd.concat([custom_train_data, train_data], ignore_index=True)
if os.path.exists(config_datos['custom_dataset_path'] + 'dev.csv'):
custom_dev_data = pd.read_csv(config_datos['custom_dataset_path'] + 'dev.csv')
for customAudioPath in custom_dev_data['path']:
shutil.copy(config_datos['custom_dataset_path'] + 'clips/' + customAudioPath, common_voice_datapath + '/clips/')
custom_dev_data_only = custom_dev_data
dev_data = pd.concat([custom_dev_data, dev_data], ignore_index=True)
if os.path.exists(config_datos['custom_dataset_path'] + 'test.csv'):
custom_test_data = pd.read_csv(config_datos['custom_dataset_path'] + 'test.csv')
for customAudioPath in custom_test_data['path']:
shutil.copy(config_datos['custom_dataset_path'] + 'clips/' + customAudioPath, common_voice_datapath + '/clips/')
custom_test_data_only = custom_test_data
test_data = pd.concat([custom_test_data, test_data], ignore_index=True)
print(f"Total clips available in Train {train_data.shape[0]}")
print(f"Total clips available in Dev {dev_data.shape[0]}")
print(f"Total clips available in Test {test_data.shape[0]}")
# print(train_data.columns)
# print(train_data.head(20))
# sys.exit()
regex_pattern = r'\b(?:{})\b'.format('|'.join(map(re.escape, wake_words)))
pattern = re.compile(regex_pattern, flags=re.IGNORECASE)
def wake_words_search(pattern, word):
try:
return bool(pattern.search(word))
except TypeError:
return False
positive_train_data = train_data[[wake_words_search(pattern, sentence) for sentence in train_data['sentence']]]
positive_dev_data = dev_data[[wake_words_search(pattern, sentence) for sentence in dev_data['sentence']]]
positive_test_data = test_data[[wake_words_search(pattern, sentence) for sentence in test_data['sentence']]]
positive_train_data['sentence'] = positive_train_data['sentence'].apply(
lambda x: x if isinstance(x, str) else ""
)
positive_dev_data['sentence'] = positive_dev_data['sentence'].apply(
lambda x: x if isinstance(x, str) else ""
)
positive_test_data['sentence'] = positive_test_data['sentence'].apply(
lambda x: x if isinstance(x, str) else ""
)
print(f"Total clips available in Train with wake words {positive_train_data.shape[0]}")
print(f"Total clips available in Dev with wake words {positive_dev_data.shape[0]}")
print(f"Total clips available in Test with wake words {positive_test_data.shape[0]}")
print(positive_train_data.head(20))
print(positive_train_data.tail(20))
negative_train_data = train_data_woCustom[[not wake_words_search(pattern, sentence) for sentence in train_data_woCustom['sentence']]]
negative_dev_data = dev_data_woCustom[[not wake_words_search(pattern, sentence) for sentence in dev_data_woCustom['sentence']]]
negative_test_data = test_data_woCustom[[not wake_words_search(pattern, sentence) for sentence in test_data_woCustom['sentence']]]
# negative data size
print(f"Total clips available in Train without wake words {negative_train_data.shape[0]}")
print(f"Total clips available in Dev without wake words {negative_dev_data.shape[0]}")
print(f"Total clips available in Test without wake words {negative_test_data.shape[0]}")
# negative_data_percent = 0.012
#
# negative_train_data = negative_train_data.sample(frac=negative_data_percent)
# negative_dev_data = negative_dev_data.sample(frac=negative_data_percent)
# negative_test_data = negative_test_data.sample(frac=negative_data_percent)
positive_train_amount = positive_train_data.shape[0] + config_datos['tts_generated_amounts']['train']
positive_dev_amount = positive_dev_data.shape[0] + config_datos['tts_generated_amounts']['dev']
positive_test_amount = positive_test_data.shape[0] + config_datos['tts_generated_amounts']['test']
negative_train_data = negative_train_data.sample(int(positive_train_amount / 100 * config_datos['negative_data_proportion']) + config_datos['hnm_pool_size'])
negative_dev_data = negative_dev_data.sample(int(positive_dev_amount / 100 * config_datos['negative_data_proportion']))
negative_test_data = negative_test_data.sample(int(positive_test_amount / 100 * config_datos['negative_data_proportion']))
negative_pool_data = negative_train_data.tail(config_datos['hnm_pool_size']).copy()
negative_train_data = negative_train_data[:-config_datos['hnm_pool_size']]
if isinstance(custom_train_data_only, pd.DataFrame) and not custom_train_data_only.empty:
custom_negative_train_data = custom_train_data_only[[not wake_words_search(pattern, sentence) for sentence in custom_train_data_only['sentence']]]
rowsToDeleteAmount = custom_negative_train_data.shape[0]
negative_train_data = negative_train_data[:-rowsToDeleteAmount]
negative_train_data = pd.concat([custom_negative_train_data, negative_train_data], ignore_index=True)
if isinstance(custom_dev_data_only, pd.DataFrame) and not custom_dev_data_only.empty:
custom_negative_dev_data = custom_dev_data_only[[not wake_words_search(pattern, sentence) for sentence in custom_dev_data_only['sentence']]]
rowsToDeleteAmount = custom_negative_dev_data.shape[0]
negative_dev_data = negative_dev_data[:-rowsToDeleteAmount]
negative_dev_data = pd.concat([custom_negative_dev_data, negative_dev_data], ignore_index=True)
if isinstance(custom_test_data_only, pd.DataFrame) and not custom_test_data_only.empty:
custom_negative_test_data = custom_test_data_only[[not wake_words_search(pattern, sentence) for sentence in custom_test_data_only['sentence']]]
rowsToDeleteAmount = custom_negative_test_data.shape[0]
negative_test_data = negative_test_data[:-rowsToDeleteAmount]
negative_test_data = pd.concat([custom_negative_test_data, negative_test_data], ignore_index=True)
negative_train_data['sentence'] = negative_train_data['sentence'].apply(
lambda x: x if isinstance(x, str) else ""
)
negative_dev_data['sentence'] = negative_dev_data['sentence'].apply(
lambda x: x if isinstance(x, str) else ""
)
negative_test_data['sentence'] = negative_test_data['sentence'].apply(
lambda x: x if isinstance(x, str) else ""
)
negative_pool_data['sentence'] = negative_pool_data['sentence'].apply(
lambda x: x if isinstance(x, str) else ""
)
# trimmed negative data sizes
print(f"Total clips available in Train without wake words {negative_train_data.shape[0]}")
print(f"Total clips available in Dev without wake words {negative_dev_data.shape[0]}")
print(f"Total clips available in Test without wake words {negative_test_data.shape[0]}")
print('negative_train_data:')
print(negative_train_data.head(20))
print(negative_train_data.tail(20))
print('negative_dev_data:')
print(negative_dev_data.head(10))
print('negative_test_data:')
print(negative_test_data.head(10))
# Filtrar donde 'sentence' no está vacío después de quitar espacios
negative_train_data_with_sentence = negative_train_data[negative_train_data['sentence'].str.strip() != '']
negative_dev_data_with_sentence = negative_dev_data[negative_dev_data['sentence'].str.strip() != '']
negative_test_data_with_sentence = negative_test_data[negative_test_data['sentence'].str.strip() != '']
# Tomar un X% aleatorio de los with_sentence
negative_train_data_with_sentence_piece = negative_train_data_with_sentence.sample(frac=config_datos['negative_with_sentence_percent'])
negative_train_data_with_sentence_rest = negative_train_data_with_sentence.drop(negative_train_data_with_sentence_piece.index)
negative_train_data_with_sentence = negative_train_data_with_sentence_piece
negative_dev_data_with_sentence_piece = negative_dev_data_with_sentence.sample(frac=config_datos['negative_with_sentence_percent'])
negative_dev_data_with_sentence_rest = negative_dev_data_with_sentence.drop(negative_dev_data_with_sentence_piece.index)
negative_dev_data_with_sentence = negative_dev_data_with_sentence_piece
negative_test_data_with_sentence_piece = negative_test_data_with_sentence.sample(frac=config_datos['negative_with_sentence_percent'])
negative_test_data_with_sentence_rest = negative_test_data_with_sentence.drop(negative_test_data_with_sentence_piece.index)
negative_test_data_with_sentence = negative_test_data_with_sentence_piece
# Obtener los without_sentence
negative_train_data_without_sentence = negative_train_data[negative_train_data['sentence'].str.strip() == '']
negative_dev_data_without_sentence = negative_dev_data[negative_dev_data['sentence'].str.strip() == '']
negative_test_data_without_sentence = negative_test_data[negative_test_data['sentence'].str.strip() == '']
# El 50% de los with_sentence que no se tomó los concateno a los without_sentence, porque no los voy a alinear, van como ruido/palabras rotas
negative_train_data_without_sentence = pd.concat([negative_train_data_without_sentence, negative_train_data_with_sentence_rest], ignore_index=True)
negative_dev_data_without_sentence = pd.concat([negative_dev_data_without_sentence, negative_dev_data_with_sentence_rest], ignore_index=True)
negative_test_data_without_sentence = pd.concat([negative_test_data_without_sentence, negative_test_data_with_sentence_rest], ignore_index=True)
print(f"negative_train_data_with_sentence count: {negative_train_data_with_sentence.shape[0]}")
print(f"negative_dev_data_with_sentence count: {negative_dev_data_with_sentence.shape[0]}")
print(f"negative_test_data_with_sentence count: {negative_test_data_with_sentence.shape[0]}")
print(f"negative_train_data_without_sentence count: {negative_train_data_without_sentence.shape[0]}")
print(f"negative_dev_data_without_sentence count: {negative_dev_data_without_sentence.shape[0]}")
print(f"negative_test_data_without_sentence count: {negative_test_data_without_sentence.shape[0]}")
jsonFileData = {
'positive_train_amount': positive_train_amount,
'positive_dev_amount': positive_dev_amount,
'positive_test_amount': positive_test_amount,
'negative_train_amount': negative_train_data.shape[0],
'negative_dev_amount': negative_dev_data.shape[0],
'negative_test_amount': negative_test_data.shape[0],
'negative_pool_data': negative_pool_data.shape[0],
'negative_train_data_with_sentence': negative_train_data_with_sentence.shape[0],
'negative_dev_data_with_sentence': negative_dev_data_with_sentence.shape[0],
'negative_test_data_with_sentence': negative_test_data_with_sentence.shape[0],
'negative_train_data_without_sentence': negative_train_data_without_sentence.shape[0],
'negative_dev_data_without_sentence': negative_dev_data_without_sentence.shape[0],
'negative_test_data_without_sentence': negative_test_data_without_sentence.shape[0]
}
with open("dataset/dataset_conformation.json", "w", encoding="utf-8") as jsonFile:
json.dump(jsonFileData, jsonFile, ensure_ascii=False, indent=2)
# sys.exit()
# save dateframes to json files
jsons_container_folder = 'dataset/json/'
if not os.path.isdir('dataset'):
os.mkdir('dataset')
if os.path.isdir(jsons_container_folder):
shutil.rmtree(jsons_container_folder)
os.mkdir(jsons_container_folder)
positive_train_data.to_json(jsons_container_folder + 'positive_train_data.json')
positive_dev_data.to_json(jsons_container_folder + 'positive_dev_data.json')
positive_test_data.to_json(jsons_container_folder + 'positive_test_data.json')
negative_train_data.to_json(jsons_container_folder + 'negative_train_data.json')
negative_dev_data.to_json(jsons_container_folder + 'negative_dev_data.json')
negative_test_data.to_json(jsons_container_folder + 'negative_test_data.json')
negative_pool_data.to_json(jsons_container_folder + 'negative_pool_data.json')
# copy audios dataset
wake_word_datapath = 'dataset'
positive_audios_container_folder = 'dataset/positive/audio/'
if os.path.isdir('dataset/positive'):
shutil.rmtree('dataset/positive')
os.mkdir('dataset/positive')
os.mkdir(positive_audios_container_folder)
negative_audios_container_folder = 'dataset/negative/audio/'
if os.path.isdir('dataset/negative'):
shutil.rmtree('dataset/negative')
os.mkdir('dataset/negative')
os.mkdir(negative_audios_container_folder)
negative_audios_align_container_folder = 'dataset/negative/audio_align/'
os.mkdir(negative_audios_align_container_folder)
# save the dataframes we got from above in each dataset
positive_train_data[['path', 'sentence']].to_csv(wake_word_datapath + "/positive/train.csv", index=False)
positive_dev_data[['path', 'sentence']].to_csv(wake_word_datapath + "/positive/dev.csv", index=False)
positive_test_data[['path', 'sentence']].to_csv(wake_word_datapath + "/positive/test.csv", index=False)
negative_train_data[['path', 'sentence']].to_csv(wake_word_datapath + "/negative/train.csv", index=False)
negative_dev_data[['path', 'sentence']].to_csv(wake_word_datapath + "/negative/dev.csv", index=False)
negative_test_data[['path', 'sentence']].to_csv(wake_word_datapath + "/negative/test.csv", index=False)
negative_pool_data[['path', 'sentence']].to_csv(wake_word_datapath + "/negative/pool.csv", index=False)
def save_wav_lab(path, filename, sentence, decibels=40):
# load file
sounddata = librosa.core.load(f"{common_voice_datapath}/clips/{filename}", sr=sr, mono=True)[0]
# trim
sounddata = librosa.effects.trim(sounddata, top_db=decibels)[0]
# save as wav file
soundfile.write(f"{wake_word_datapath}{path}/{filename.split('.')[0]}.wav", sounddata, sr)
# write lab file
with open(f"{wake_word_datapath}{path}/{filename.split('.')[0]}.lab", "w", encoding="utf-8") as f:
f.write(sentence)
positive_train_data.apply(lambda x: save_wav_lab('/positive/audio', x['path'], x['sentence']), axis=1)
positive_dev_data.apply(lambda x: save_wav_lab('/positive/audio', x['path'], x['sentence']), axis=1)
positive_test_data.apply(lambda x: save_wav_lab('/positive/audio', x['path'], x['sentence']), axis=1)
negative_train_data_without_sentence.apply(lambda x: save_wav_lab('/negative/audio', x['path'], x['sentence']), axis=1)
negative_dev_data_without_sentence.apply(lambda x: save_wav_lab('/negative/audio', x['path'], x['sentence']), axis=1)
negative_test_data_without_sentence.apply(lambda x: save_wav_lab('/negative/audio', x['path'], x['sentence']), axis=1)
negative_train_data_with_sentence.apply(lambda x: save_wav_lab('/negative/audio_align', x['path'], x['sentence']), axis=1)
negative_dev_data_with_sentence.apply(lambda x: save_wav_lab('/negative/audio_align', x['path'], x['sentence']), axis=1)
negative_test_data_with_sentence.apply(lambda x: save_wav_lab('/negative/audio_align', x['path'], x['sentence']), axis=1)