Upload playground_yeet_the_unclean.py
Browse files- playground_yeet_the_unclean.py +212 -0
playground_yeet_the_unclean.py
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import multiprocessing
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
|
5 |
+
import librosa as lb
|
6 |
+
import numpy as np
|
7 |
+
import soundfile as sf
|
8 |
+
from deepmultilingualpunctuation import PunctuationModel
|
9 |
+
from pyannote.audio import Pipeline
|
10 |
+
from rpunct import RestorePuncts
|
11 |
+
from tqdm import tqdm
|
12 |
+
|
13 |
+
|
14 |
+
class UncleanYeeter:
|
15 |
+
def __init__(self):
|
16 |
+
"""
|
17 |
+
all the models and persistent stuff
|
18 |
+
"""
|
19 |
+
self.diarizer = Pipeline.from_pretrained("pyannote/speaker-diarization@2.1")
|
20 |
+
|
21 |
+
def create_list_of_samples_marked_for_deletion(self, list_of_audios):
|
22 |
+
marked_for_yeeting = list()
|
23 |
+
for audio_file in tqdm(list_of_audios):
|
24 |
+
try:
|
25 |
+
wav, sr = sf.read(audio_file)
|
26 |
+
except RuntimeError:
|
27 |
+
print(f"PROBLEMATIC FILE: {audio_file}")
|
28 |
+
continue
|
29 |
+
wav = to_mono(wav)
|
30 |
+
|
31 |
+
# check duration
|
32 |
+
if 5 < len(wav) / sr < 15:
|
33 |
+
continue
|
34 |
+
|
35 |
+
# check SNR
|
36 |
+
if wada_snr(wav) < 20.0:
|
37 |
+
continue
|
38 |
+
|
39 |
+
# check amount of speakers
|
40 |
+
try:
|
41 |
+
output = self.diarizer(audio_file)
|
42 |
+
except ValueError:
|
43 |
+
print("Diarizer is unhappy")
|
44 |
+
continue
|
45 |
+
speakers = set()
|
46 |
+
for _, _, speaker in output.itertracks(yield_label=True):
|
47 |
+
speakers.add(speaker)
|
48 |
+
if len(speakers) > 1:
|
49 |
+
continue
|
50 |
+
|
51 |
+
marked_for_yeeting.append(audio_file.split("/")[-1])
|
52 |
+
|
53 |
+
# save list of files to be yoten to a file for later yeeting
|
54 |
+
with open("files_to_keep.txt", "a", encoding="utf8") as file:
|
55 |
+
file.write("\n".join(marked_for_yeeting) + "\n")
|
56 |
+
print(marked_for_yeeting)
|
57 |
+
|
58 |
+
|
59 |
+
class Punctuator:
|
60 |
+
def __init__(self, lang="eng"):
|
61 |
+
if lang == "en":
|
62 |
+
model = RestorePuncts()
|
63 |
+
self.punctuate_transcripts = model.punctuate # pass a string into it and you get a punctuated string returned
|
64 |
+
else:
|
65 |
+
model = PunctuationModel()
|
66 |
+
self.punctuate_transcripts = model.restore_punctuation # pass a string into it and you get a punctuated string returned
|
67 |
+
|
68 |
+
|
69 |
+
def wada_snr(wav):
|
70 |
+
# Direct blind estimation of the SNR of a speech signal.
|
71 |
+
#
|
72 |
+
# Paper on WADA SNR:
|
73 |
+
# http://www.cs.cmu.edu/~robust/Papers/KimSternIS08.pdf
|
74 |
+
#
|
75 |
+
# This function was adapted from this matlab code:
|
76 |
+
# https://labrosa.ee.columbia.edu/projects/snreval/#9
|
77 |
+
|
78 |
+
# init
|
79 |
+
eps = 1e-10
|
80 |
+
# next 2 lines define a fancy curve derived from a gamma distribution -- see paper
|
81 |
+
db_vals = np.arange(-20, 101)
|
82 |
+
g_vals = np.array(
|
83 |
+
[0.40974774, 0.40986926, 0.40998566, 0.40969089, 0.40986186, 0.40999006, 0.41027138, 0.41052627, 0.41101024, 0.41143264, 0.41231718, 0.41337272, 0.41526426, 0.4178192, 0.42077252, 0.42452799, 0.42918886, 0.43510373, 0.44234195, 0.45161485, 0.46221153, 0.47491647, 0.48883809, 0.50509236, 0.52353709, 0.54372088, 0.56532427,
|
84 |
+
0.58847532, 0.61346212, 0.63954496, 0.66750818, 0.69583724, 0.72454762, 0.75414799, 0.78323148, 0.81240985, 0.84219775, 0.87166406, 0.90030504, 0.92880418, 0.95655449, 0.9835349, 1.01047155, 1.0362095, 1.06136425, 1.08579312, 1.1094819, 1.13277995, 1.15472826, 1.17627308, 1.19703503, 1.21671694, 1.23535898, 1.25364313,
|
85 |
+
1.27103891, 1.28718029, 1.30302865, 1.31839527, 1.33294817, 1.34700935, 1.3605727, 1.37345513, 1.38577122, 1.39733504, 1.40856397, 1.41959619, 1.42983624, 1.43958467, 1.44902176, 1.45804831, 1.46669568, 1.47486938, 1.48269965, 1.49034339, 1.49748214, 1.50435106, 1.51076426, 1.51698915, 1.5229097, 1.528578, 1.53389835, 1.5391211,
|
86 |
+
1.5439065, 1.54858517, 1.55310776, 1.55744391, 1.56164927, 1.56566348, 1.56938671, 1.57307767, 1.57654764, 1.57980083, 1.58304129, 1.58602496, 1.58880681, 1.59162477, 1.5941969, 1.59693155, 1.599446, 1.60185011, 1.60408668, 1.60627134, 1.60826199, 1.61004547, 1.61192472, 1.61369656, 1.61534074, 1.61688905, 1.61838916, 1.61985374,
|
87 |
+
1.62135878, 1.62268119, 1.62390423, 1.62513143, 1.62632463, 1.6274027, 1.62842767, 1.62945532, 1.6303307, 1.63128026, 1.63204102])
|
88 |
+
|
89 |
+
# peak normalize, get magnitude, clip lower bound
|
90 |
+
wav = np.array(wav)
|
91 |
+
wav = wav / abs(wav).max()
|
92 |
+
abs_wav = abs(wav)
|
93 |
+
abs_wav[abs_wav < eps] = eps
|
94 |
+
|
95 |
+
# calcuate statistics
|
96 |
+
# E[|z|]
|
97 |
+
v1 = max(eps, abs_wav.mean())
|
98 |
+
# E[log|z|]
|
99 |
+
v2 = np.log(abs_wav).mean()
|
100 |
+
# log(E[|z|]) - E[log(|z|)]
|
101 |
+
v3 = np.log(v1) - v2
|
102 |
+
|
103 |
+
# table interpolation
|
104 |
+
wav_snr_idx = None
|
105 |
+
if any(g_vals < v3):
|
106 |
+
wav_snr_idx = np.where(g_vals < v3)[0].max()
|
107 |
+
# handle edge cases or interpolate
|
108 |
+
if wav_snr_idx is None:
|
109 |
+
wav_snr = db_vals[0]
|
110 |
+
elif wav_snr_idx == len(db_vals) - 1:
|
111 |
+
wav_snr = db_vals[-1]
|
112 |
+
else:
|
113 |
+
wav_snr = db_vals[wav_snr_idx] + \
|
114 |
+
(v3 - g_vals[wav_snr_idx]) / (g_vals[wav_snr_idx + 1] - g_vals[wav_snr_idx]) * (db_vals[wav_snr_idx + 1] - db_vals[wav_snr_idx])
|
115 |
+
|
116 |
+
# Calculate SNR
|
117 |
+
dEng = sum(wav ** 2)
|
118 |
+
dFactor = 10 ** (wav_snr / 10)
|
119 |
+
dNoiseEng = dEng / (1 + dFactor) # Noise energy
|
120 |
+
dSigEng = dEng * dFactor / (1 + dFactor) # Signal energy
|
121 |
+
snr = 10 * np.log10(dSigEng / dNoiseEng)
|
122 |
+
|
123 |
+
return snr
|
124 |
+
|
125 |
+
|
126 |
+
def to_mono(x):
|
127 |
+
"""
|
128 |
+
make sure we deal with a 1D array
|
129 |
+
"""
|
130 |
+
if len(x.shape) == 2:
|
131 |
+
return lb.to_mono(np.transpose(x))
|
132 |
+
else:
|
133 |
+
return x
|
134 |
+
|
135 |
+
|
136 |
+
def clean_mls_ger():
|
137 |
+
clean_mls("mls_german", "de")
|
138 |
+
|
139 |
+
|
140 |
+
def clean_mls_fr():
|
141 |
+
clean_mls("mls_french", "fr")
|
142 |
+
|
143 |
+
|
144 |
+
def clean_mls_it():
|
145 |
+
clean_mls("mls_italian", "it")
|
146 |
+
|
147 |
+
|
148 |
+
def clean_mls_eng():
|
149 |
+
clean_mls("mls_english", "en")
|
150 |
+
|
151 |
+
|
152 |
+
def clean_mls(lang_dir, lang):
|
153 |
+
punco = Punctuator(lang=lang)
|
154 |
+
new_file = ""
|
155 |
+
shutil.copy(f"/mount/resources/speech/corpora/MultiLingLibriSpeech/{lang_dir}/train/transcripts.txt", f"/mount/resources/speech/corpora/MultiLingLibriSpeech/{lang_dir}/train/orig_transcripts.txt")
|
156 |
+
with open(f"/mount/resources/speech/corpora/MultiLingLibriSpeech/{lang_dir}/train/transcripts.txt", "r", encoding="utf8") as file:
|
157 |
+
sentence_list = file.read().split("\n")
|
158 |
+
for sentence in tqdm(sentence_list):
|
159 |
+
if sentence.strip() == "":
|
160 |
+
continue
|
161 |
+
sent_id = sentence.split()[0]
|
162 |
+
punc_sent = punco.punctuate_transcripts(" ".join(sentence.split()[1:]))
|
163 |
+
new_file = new_file + f"{sent_id}\t{punc_sent}\n"
|
164 |
+
with open(f"/mount/resources/speech/corpora/MultiLingLibriSpeech/{lang_dir}/train/transcripts.txt", "w", encoding="utf8") as file:
|
165 |
+
file.write(new_file)
|
166 |
+
|
167 |
+
|
168 |
+
def build_path_to_transcript_dict_gigaspeech():
|
169 |
+
path_to_transcript = dict()
|
170 |
+
root = "/mount/resources/speech/corpora/GigaSpeech/"
|
171 |
+
with open(os.path.join(root, "transcripts.txt"), "r", encoding="utf8") as file:
|
172 |
+
lookup = file.read()
|
173 |
+
for line in lookup.split("\n"):
|
174 |
+
if line.strip() != "":
|
175 |
+
norm_transcript = line.split("\t")[1]
|
176 |
+
wav_path = os.path.join(root, "wavs", line.split("\t")[0])
|
177 |
+
if os.path.exists(wav_path):
|
178 |
+
path_to_transcript[wav_path] = norm_transcript
|
179 |
+
return path_to_transcript
|
180 |
+
|
181 |
+
|
182 |
+
def split_list(lst, n):
|
183 |
+
if n <= 0:
|
184 |
+
return []
|
185 |
+
|
186 |
+
quotient, remainder = divmod(len(lst), n)
|
187 |
+
shards = [lst[i * quotient + min(i, remainder):(i + 1) * quotient + min(i + 1, remainder)] for i in range(n)]
|
188 |
+
return shards
|
189 |
+
|
190 |
+
def yonkus(shard):
|
191 |
+
yeet = UncleanYeeter()
|
192 |
+
yeet.create_list_of_samples_marked_for_deletion(shard)
|
193 |
+
|
194 |
+
if __name__ == '__main__':
|
195 |
+
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
196 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "6"
|
197 |
+
print(f"Making GPU {os.environ['CUDA_VISIBLE_DEVICES']} the only visible device.")
|
198 |
+
|
199 |
+
list_of_files = os.listdir("/mount/resources/speech/corpora/GigaSpeech/wavs")
|
200 |
+
absolute_list_of_files = list()
|
201 |
+
for filo in list_of_files:
|
202 |
+
absolute_list_of_files.append(f"/mount/resources/speech/corpora/GigaSpeech/wavs/{filo}")
|
203 |
+
processes = list()
|
204 |
+
for sublist in split_list(absolute_list_of_files, 20):
|
205 |
+
processes.append(multiprocessing.Process(args=(sublist,), target=yonkus, daemon=True))
|
206 |
+
processes[-1].start()
|
207 |
+
for processo in processes:
|
208 |
+
processo.join()
|
209 |
+
# clean_mls_it()
|
210 |
+
# clean_mls_fr()
|
211 |
+
# clean_mls_ger()
|
212 |
+
# clean_mls_eng()
|