Skip to content

Commit 3bf4f03

Browse files
author
jamfly
committed
Clean up
1 parent 2082e3e commit 3bf4f03

6 files changed

Lines changed: 108 additions & 45 deletions

File tree

recipes/Fisher-Callhome-Spanish/ST/hparams/transformer.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,4 @@ checkpointer: !new:speechbrain.utils.checkpoints.Checkpointer
232232

233233
bleu_computer: !name:speechbrain.utils.bleu.BLEUStats
234234
merge_words: False
235-
acc_computer: !name:speechbrain.utils.Accuracy.AccuracyStats
235+
acc_computer: !name:speechbrain.utils.Accuracy.AccuracyStats

recipes/Fisher-Callhome-Spanish/ST/hparams/transformer_mtl_asr.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ debug: False
1414
seed: 8886
1515
num_workers: 8
1616
__set_seed: !apply:torch.manual_seed [!ref <seed>]
17-
output_folder: !ref results/transformer_mtl_asr_0.3_one_stage/<seed>
17+
output_folder: !ref results/transformer_mtl_asr_0.3/<seed>
1818
ckpt_interval_minutes: 15 # save checkpoint every N min
1919
bleu_file: !ref <output_folder>/bleu.txt
2020
save_folder: !ref <output_folder>/save
@@ -70,7 +70,7 @@ loss_reduction: batchmean
7070
sorting: random
7171

7272
# stages related parameters
73-
stage_one_epochs: 100
73+
stage_one_epochs: 20
7474
lr_adam: 1.0
7575
lr_sgd: 0.000025
7676

@@ -233,4 +233,4 @@ checkpointer: !new:speechbrain.utils.checkpoints.Checkpointer
233233

234234
bleu_computer: !name:speechbrain.utils.bleu.BLEUStats
235235
merge_words: False
236-
acc_computer: !name:speechbrain.utils.Accuracy.AccuracyStats
236+
acc_computer: !name:speechbrain.utils.Accuracy.AccuracyStats

recipes/Fisher-Callhome-Spanish/ST/hparams/transformer_mtl_asr_mt.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,4 @@ checkpointer: !new:speechbrain.utils.checkpoints.Checkpointer
234234

235235
bleu_computer: !name:speechbrain.utils.bleu.BLEUStats
236236
merge_words: False
237-
acc_computer: !name:speechbrain.utils.Accuracy.AccuracyStats
237+
acc_computer: !name:speechbrain.utils.Accuracy.AccuracyStats

recipes/Fisher-Callhome-Spanish/fisher_callhome_prepare.py

Lines changed: 91 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import torch
1818
import torchaudio
1919

20-
from splits.split_train import TrainOption
2120
from pre_processing import (
2221
clean_transcription,
2322
clean_translation,
@@ -55,6 +54,7 @@ class TDF:
5554
transcript: str
5655
transcript of utteranc
5756
"""
57+
5858
channel: int
5959
start: int
6060
end: int
@@ -67,6 +67,7 @@ class Data:
6767
each data contains a transcription and a translation for train set
6868
four translations for dev, dev2, test set
6969
"""
70+
7071
uid: str = ""
7172
wav: str = ""
7273
transcription: str = ""
@@ -98,7 +99,9 @@ def prepare_fisher_callhome_spanish(
9899

99100
# If the dataset doesn't exist yet, terminate the whole program
100101
speech_folder = os.path.join(f"{data_folder}/LDC2010S01/data/speech")
101-
transcription_folder = os.path.join(f"{data_folder}/LDC2010T04/fisher_spa_tr/data/transcripts")
102+
transcription_folder = os.path.join(
103+
f"{data_folder}/LDC2010T04/fisher_spa_tr/data/transcripts"
104+
)
102105

103106
if check_folders(speech_folder, transcription_folder) is not True:
104107
logger.error(
@@ -117,17 +120,23 @@ def prepare_fisher_callhome_spanish(
117120
os.makedirs(f"{save_folder}/{dataset}/wav")
118121

119122
if skip(save_folder, dataset):
120-
logger.info(f"Skipping preparation of {dataset}, completed in previous run.")
123+
logger.info(
124+
f"Skipping preparation of {dataset}, completed in previous run."
125+
)
121126
continue
122127

123128
# get file lists
124-
transcription_files = get_transcription_files_by_dataset(dataset, transcription_folder=transcription_folder)
129+
transcription_files = get_transcription_files_by_dataset(
130+
dataset, transcription_folder=transcription_folder
131+
)
125132

126133
# extract all transcriptions from files
127134
extracted_transcriptions = {}
128135
for transcription_file in transcription_files:
129136
filename = transcription_file.split("/")[-1].split(".")[0]
130-
extracted_transcriptions[filename] = extract_transcription(transcription_file)
137+
extracted_transcriptions[filename] = extract_transcription(
138+
transcription_file
139+
)
131140

132141
# concate short utterance via mapping file
133142
concated_data = concate_transcriptions_by_mapping_file(
@@ -140,26 +149,48 @@ def prepare_fisher_callhome_spanish(
140149
if dataset != "train":
141150
# dev, dev2, test got four translations
142151
for number in range(4):
143-
translation_path = f"{corpus_path}/corpus/ldc/fisher_{dataset}.en.{number}"
152+
translation_path = (
153+
f"{corpus_path}/corpus/ldc/fisher_{dataset}.en.{number}"
154+
)
144155
translations = get_translations_from_path(translation_path)
145156

146-
concated_data = insert_translation_into_existing_dataset(data=concated_data, translations=translations)
157+
concated_data = insert_translation_into_existing_dataset(
158+
data=concated_data, translations=translations
159+
)
147160
else:
148161
translation_path = f"{corpus_path}/corpus/ldc/fisher_{dataset}.en"
149162
translations = get_translations_from_path(translation_path)
150-
concated_data = insert_translation_into_existing_dataset(data=concated_data, translations=translations)
163+
concated_data = insert_translation_into_existing_dataset(
164+
data=concated_data, translations=translations
165+
)
151166

152167
# filter out empty or long transcription/translation
153-
concated_data = list(filter(lambda data: 0 < len(data.transcription) < 400, concated_data))
168+
concated_data = list(
169+
filter(
170+
lambda data: 0 < len(data.transcription) < 400, concated_data
171+
)
172+
)
154173

155174
if dataset != "train":
156175
for number in range(4):
157-
concated_data = list(filter(lambda data: 0 < len(data.translations[number]) < 400, concated_data))
176+
concated_data = list(
177+
filter(
178+
lambda data: 0 < len(data.translations[number]) < 400,
179+
concated_data,
180+
)
181+
)
158182
else:
159-
concated_data = list(filter(lambda data: 0 < len(data.translations[0]) < 400, concated_data))
183+
concated_data = list(
184+
filter(
185+
lambda data: 0 < len(data.translations[0]) < 400,
186+
concated_data,
187+
)
188+
)
160189

161190
# ignore empty or long utterances
162-
concated_data = list(filter(lambda data: 0 < data.duration < 30, concated_data))
191+
concated_data = list(
192+
filter(lambda data: 0 < data.duration < 30, concated_data)
193+
)
163194

164195
# sort by utterance id
165196
concated_data = sorted(concated_data, key=lambda data: data.uid)
@@ -189,7 +220,9 @@ def prepare_fisher_callhome_spanish(
189220
}
190221

191222
for number in range(4):
192-
translation_dict = {f"translation_{number}": data.translations[number]}
223+
translation_dict = {
224+
f"translation_{number}": data.translations[number]
225+
}
193226
data_dict[data.uid].update(translation_dict)
194227
else:
195228
data_dict[data.uid] = {
@@ -287,22 +320,39 @@ def concate_transcriptions_by_mapping_file(
287320
# concate multiple transcripts
288321
if len(need_to_be_concate_lines) > 1:
289322
# index shift one is because id is count from 1 in file however, list start from 0
290-
concated_transcripts = selected_transcription[need_to_be_concate_lines[0] - 1: need_to_be_concate_lines[-1]]
291-
concated_transcripts = list(map(lambda tdf: tdf.transcript, concated_transcripts))
323+
concated_transcripts = selected_transcription[
324+
need_to_be_concate_lines[0]
325+
- 1 : need_to_be_concate_lines[-1]
326+
]
327+
concated_transcripts = list(
328+
map(lambda tdf: tdf.transcript, concated_transcripts)
329+
)
292330
concated_transcripts = " ".join(concated_transcripts)
293331

294-
start = selected_transcription[need_to_be_concate_lines[0] - 1].start
295-
end = selected_transcription[need_to_be_concate_lines[-1] - 1].end
332+
start = selected_transcription[
333+
need_to_be_concate_lines[0] - 1
334+
].start
335+
end = selected_transcription[
336+
need_to_be_concate_lines[-1] - 1
337+
].end
296338
else:
297-
concated_transcripts = selected_transcription[need_to_be_concate_lines[-1] - 1].transcript
298-
start = selected_transcription[need_to_be_concate_lines[-1] - 1].start
299-
end = selected_transcription[need_to_be_concate_lines[-1] - 1].end
339+
concated_transcripts = selected_transcription[
340+
need_to_be_concate_lines[-1] - 1
341+
].transcript
342+
start = selected_transcription[
343+
need_to_be_concate_lines[-1] - 1
344+
].start
345+
end = selected_transcription[
346+
need_to_be_concate_lines[-1] - 1
347+
].end
300348

301349
# clean up
302350
concated_transcripts = normalize_punctuation(concated_transcripts)
303351
concated_transcripts = es_normalizer.normalize(concated_transcripts)
304352

305-
channel = selected_transcription[need_to_be_concate_lines[0] - 1].channel
353+
channel = selected_transcription[
354+
need_to_be_concate_lines[0] - 1
355+
].channel
306356
channel_symbol = "B" if channel == 1 else "A"
307357
uttrance_id = f"{uid}-{channel_symbol}-{start:06d}-{end:06d}"
308358

@@ -325,15 +375,17 @@ def segment_audio(
325375
end: int,
326376
save_path: str,
327377
sample_rate: int = 16000,
328-
device: str = "cpu"
378+
device: str = "cpu",
329379
):
330380
"""segment and resample audio"""
331381

332382
start = int(start / 100 * 8000)
333383
end = int(end / 100 * 8000)
334384
num_frames = end - start
335385

336-
data, _ = torchaudio.load(audio_path, frame_offset=start, num_frames=num_frames)
386+
data, _ = torchaudio.load(
387+
audio_path, frame_offset=start, num_frames=num_frames
388+
)
337389

338390
resampler = Resample(orig_freq=8000, new_freq=sample_rate).to(device=device)
339391

@@ -343,13 +395,21 @@ def segment_audio(
343395
torchaudio.save(save_path, src=data, sample_rate=sample_rate)
344396

345397

346-
def get_transcription_files_by_dataset(dataset: str, transcription_folder: str) -> List[str]:
398+
def get_transcription_files_by_dataset(
399+
dataset: str, transcription_folder: str
400+
) -> List[str]:
347401
"""return paths of transcriptions from the given data set and the path of all of transcriptions"""
348402
train_set = get_data_list(f"splits/{dataset}")
349-
transcription_train_set = list(map(lambda path: path.split(".")[0], train_set))
350-
transcription_train_set = list(map(lambda path: f"{path}.tdf", transcription_train_set))
403+
transcription_train_set = list(
404+
map(lambda path: path.split(".")[0], train_set)
405+
)
406+
transcription_train_set = list(
407+
map(lambda path: f"{path}.tdf", transcription_train_set)
408+
)
351409

352-
transcription_files = get_all_files(transcription_folder, match_or=transcription_train_set)
410+
transcription_files = get_all_files(
411+
transcription_folder, match_or=transcription_train_set
412+
)
353413

354414
return transcription_files
355415

@@ -376,7 +436,9 @@ def get_translations_from_path(translation_path: str) -> List[str]:
376436
return extracted_translations
377437

378438

379-
def insert_translation_into_existing_dataset(data: List[Data], translations: List[str]) -> List[Data]:
439+
def insert_translation_into_existing_dataset(
440+
data: List[Data], translations: List[str]
441+
) -> List[Data]:
380442
"""insert corresponding translation to given data"""
381443

382444
for index in range(len(data)):
@@ -390,9 +452,7 @@ def download_translations(path: str):
390452
repo = "https://github.com/joshua-decoder/fisher-callhome-corpus.git"
391453

392454
if not os.path.isdir(path):
393-
logger.info(
394-
f"Translation file not found. Downloading from {repo}."
395-
)
455+
logger.info(f"Translation file not found. Downloading from {repo}.")
396456
subprocess.run(["git", "clone", repo])
397457
subprocess.run(["mv", "fisher-callhome-corpus", f"{path}"])
398458

speechbrain/tokenizers/SentencePiece.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ def _csv2text(self):
220220
index_label = headers.index(annotation)
221221
row_idx = 0
222222
for row in reader:
223-
if self.num_sequences is not None and row_idx > self.num_sequences:
223+
if (
224+
self.num_sequences is not None
225+
and row_idx > self.num_sequences
226+
):
224227
print(
225228
"Using %d sequences to train the tokenizer."
226229
% self.num_sequences
@@ -232,7 +235,7 @@ def _csv2text(self):
232235
(sent,) = merge_char([sent.split()])
233236
sent = " ".join(sent)
234237
text_file.write(sent + "\n")
235-
238+
236239
text_file.close()
237240
annotation_file.close()
238241
logger.info("Text file created at: " + self.text_file)
@@ -247,10 +250,7 @@ def _json2text(self):
247250
)
248251
annotation = ",".join(self.annotation_read)
249252
logger.info(
250-
"Extract "
251-
+ annotation
252-
+ " sequences from:"
253-
+ self.annotation_train
253+
"Extract " + annotation + " sequences from:" + self.annotation_train
254254
)
255255

256256
# Read JSON
@@ -263,7 +263,10 @@ def _json2text(self):
263263

264264
for annotation in self.annotation_read:
265265
for snt_id in out_json.keys():
266-
if self.num_sequences is not None and row_idx > self.num_sequences:
266+
if (
267+
self.num_sequences is not None
268+
and row_idx > self.num_sequences
269+
):
267270
print(
268271
"Using %d sequences to train the tokenizer."
269272
% self.num_sequences
@@ -276,7 +279,7 @@ def _json2text(self):
276279
sent = " ".join(sent)
277280

278281
text_file.write(sent + "\n")
279-
282+
280283
text_file.close()
281284

282285
logger.info("Text file created at: " + self.text_file)

tests/unittests/test_tokenizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_tokenizer():
1919
annotation_train=os.path.abspath(
2020
"tests/unittests/tokenizer_data/dev-clean.csv"
2121
),
22-
annotation_read="wrd",
22+
annotation_read=["wrd"],
2323
model_type="bpe",
2424
)
2525
encoded_seq_ids, encoded_seq_pieces = spm(

0 commit comments

Comments
 (0)