-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_generation.py
132 lines (102 loc) · 5.85 KB
/
dataset_generation.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
import pandas as pd
import numpy as np
from IPython.display import Audio
import librosa
import soundfile
import json
import math
import os
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')
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)
# 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']]]
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]}")
positive_train_data.head()
negative_train_data = train_data[[not wake_words_search(pattern, sentence) for sentence in train_data['sentence']]]
negative_dev_data = dev_data[[not wake_words_search(pattern, sentence) for sentence in dev_data['sentence']]]
negative_test_data = test_data[[not wake_words_search(pattern, sentence) for sentence in test_data['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 = 1
negative_train_data = negative_train_data.sample(math.floor(negative_train_data.shape[0] * (negative_data_percent/100)))
negative_dev_data = negative_dev_data.sample(math.floor(negative_dev_data.shape[0] * (negative_data_percent/100)))
negative_test_data = negative_test_data.sample(math.floor(negative_test_data.shape[0] * (negative_data_percent/100)))
# 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]}")
# save dateframes to json files
jsons_container_folder = 'dataset/json/'
os.mkdir('dataset')
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')
# copy audios dataset
wake_word_datapath = 'dataset'
positive_audios_container_folder = 'dataset/positive/audio/'
os.mkdir('dataset/positive')
os.mkdir(positive_audios_container_folder)
negative_audios_container_folder = 'dataset/negative/audio/'
os.mkdir('dataset/negative')
os.mkdir(negative_audios_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)
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.apply(lambda x: save_wav_lab('/negative/audio', x['path'], x['sentence']), axis=1)
negative_dev_data.apply(lambda x: save_wav_lab('/negative/audio', x['path'], x['sentence']), axis=1)
negative_test_data.apply(lambda x: save_wav_lab('/negative/audio', x['path'], x['sentence']), axis=1)