-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasr.sh
More file actions
executable file
·1844 lines (1611 loc) · 78 KB
/
Copy pathasr.sh
File metadata and controls
executable file
·1844 lines (1611 loc) · 78 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Set bash to 'debug' mode, it will exit on :
# -e 'error', -u 'undefined variable', -o ... 'error in pipeline', -x 'print commands',
set -e
set -u
set -o pipefail
log() {
local fname=${BASH_SOURCE[1]##*/}
echo -e "$(date '+%Y-%m-%dT%H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
}
min() {
local a b
a=$1
for b in "$@"; do
if [ "${b}" -le "${a}" ]; then
a="${b}"
fi
done
echo "${a}"
}
SECONDS=0
# General configuration
stage=1 # Processes starts from the specified stage.
stop_stage=10000 # Processes is stopped at the specified stage.
skip_stages= # Spicify the stage to be skipped
skip_data_prep=false # Skip data preparation stages.
skip_train=false # Skip training stages.
skip_eval=false # Skip decoding and evaluation stages.
skip_upload=true # Skip packing and uploading to zenodo
skip_upload_hf=true # Skip uploading to hugging face stages.
eval_valid_set=false # Run decoding for the validation set
ngpu=1 # The number of gpus ("0" uses cpu, otherwise use gpu).
num_nodes=1 # The number of nodes.
nj=32 # The number of parallel jobs.
inference_nj=32 # The number of parallel jobs in decoding.
gpu_inference=false # Whether to perform gpu decoding.
dumpdir=dump # Directory to dump features.
expdir=exp # Directory to save experiments.
python=python3 # Specify python to execute espnet commands.
# Data preparation related
local_data_opts= # The options given to local/data.sh.
post_process_local_data_opts= # The options given to local/data.sh for additional processing in stage 4.
auxiliary_data_tags= # the names of training data for auxiliary tasks
# Speed perturbation related
speed_perturb_factors= # perturbation factors, e.g. "0.9 1.0 1.1" (separated by space).
# Feature extraction related
feats_type=raw # Feature type (raw, raw_copy, fbank_pitch, or extracted).
audio_format=wav # Audio format: wav, flac, wav.ark, flac.ark (only in feats_type=raw).
multi_columns_input_wav_scp=false # Enable multi columns mode for input wav.scp for format_wav_scp.py
multi_columns_output_wav_scp=false # Enable multi columns mode for output wav.scp for format_wav_scp.py
fs=16k # Sampling rate.
min_wav_duration=2 # Minimum duration in second.
max_wav_duration=30 # Maximum duration in second.
# Tokenization related
token_type=bpe # Tokenization type (char or bpe).
nbpe=100 # The number of BPE vocabulary.
bpemode=unigram # Mode of BPE (unigram or bpe).
oov="<unk>" # Out of vocabulary symbol.
blank="<blank>" # CTC blank symbol
sos_eos="<sos/eos>" # sos and eos symbole
bpe_input_sentence_size=100000000 # Size of input sentence for BPE.
bpe_nlsyms= # non-linguistic symbols list, separated by a comma or a file containing 1 symbol per line, for BPE
bpe_char_cover=1.0 # character coverage when modeling BPE
hugging_face_model_name_or_path="" # Hugging Face model or path for hugging_face tokenizer
# Ngram model related
use_ngram=false
ngram_exp=
ngram_num=3
# Language model related
use_lm=true # Use language model for ASR decoding.
lm_tag= # Suffix to the result dir for language model training.
lm_exp= # Specify the directory path for LM experiment.
# If this option is specified, lm_tag is ignored.
lm_stats_dir= # Specify the directory path for LM statistics.
lm_config= # Config for language model training.
lm_args= # Arguments for language model training, e.g., "--max_epoch 10".
# Note that it will overwrite args in lm config.
use_word_lm=false # Whether to use word language model.
num_splits_lm=1 # Number of splitting for lm corpus.
# shellcheck disable=SC2034
word_vocab_size=10000 # Size of word vocabulary.
# ASR model related
asr_task=asr # ASR task mode. Either 'asr' or 'asr_transducer'.
asr_tag=transformer # Suffix to the result dir for asr model training.
asr_exp= # Specify the directory path for ASR experiment.
# If this option is specified, asr_tag is ignored.
asr_stats_dir= # Specify the directory path for ASR statistics.
asr_config= # Config for asr model training.
asr_args= # Arguments for asr model training, e.g., "--max_epoch 10".
# Note that it will overwrite args in asr config.
pretrained_model= # Pretrained model to load
ignore_init_mismatch=false # Ignore initial mismatch
feats_normalize=global_mvn # Normalizaton layer type.
num_splits_asr=1 # Number of splitting for lm corpus.
num_ref=1 # Number of references for training.
# In supervised learning based speech enhancement / separation, it is equivalent to number of speakers.
num_inf= # Number of inferences output by the model
# Note that if it is not specified, it will be the same as num_ref. Otherwise, it will be overwritten.
# In MixIT, number of outputs is larger than that of references.
sot_asr=false # Whether to use Serialized Output Training (SOT)
# Upload model related
hf_repo=
# Decoding related
use_k2=false # Whether to use k2 based decoder
k2_ctc_decoding=true
use_nbest_rescoring=true # use transformer-decoder
# and transformer language model for nbest rescoring
num_paths=1000 # The 3rd argument of k2.random_paths.
nll_batch_size=100 # Affect GPU memory usage when computing nll
# during nbest rescoring
k2_config=./conf/decode_asr_transformer_with_k2.yaml
use_streaming=false # Whether to use streaming decoding
use_maskctc=false # Whether to use maskctc decoding
batch_size=1
inference_tag= # Suffix to the result dir for decoding.
inference_config= # Config for decoding.
inference_args= # Arguments for decoding, e.g., "--lm_weight 0.1".
# Note that it will overwrite args in inference config.
inference_lm=valid.loss.ave.pth # Language model path for decoding.
inference_ngram=${ngram_num}gram.bin
inference_asr_model=valid.acc.ave.pth # ASR model path for decoding.
# e.g.
# inference_asr_model=train.loss.best.pth
# inference_asr_model=3epoch.pth
# inference_asr_model=valid.acc.best.pth
# inference_asr_model=valid.loss.ave.pth
download_model= # Download a model from Model Zoo and use it for decoding.
# [Task dependent] Set the datadir name created by local/data.sh
train_set= # Name of training set.
valid_set= # Name of validation set used for monitoring/tuning network training.
test_sets= # Names of test sets. Multiple items (e.g., both dev and eval sets) can be specified.
bpe_train_text= # Text file path of bpe training set.
lm_train_text= # Text file path of language model training set.
lm_dev_text= # Text file path of language model development set.
lm_test_text= # Text file path of language model evaluation set.
nlsyms_txt=none # Non-linguistic symbol list if existing.
cleaner=none # Text cleaner.
hyp_cleaner=none # Text cleaner for hypotheses (may be used with external tokenizers)
g2p=none # g2p method (needed if token_type=phn).
lang=noinfo # The language type of corpus.
score_opts= # The options given to sclite scoring
local_score_opts= # The options given to local/score.sh.
asr_speech_fold_length=800 # fold_length for speech data during ASR training.
asr_text_fold_length=150 # fold_length for text data during ASR training.
lm_fold_length=150 # fold_length for LM training.
help_message=$(cat << EOF
Usage: $0 --train-set "<train_set_name>" --valid-set "<valid_set_name>" --test_sets "<test_set_names>"
Options:
# General configuration
--stage # Processes starts from the specified stage (default="${stage}").
--stop_stage # Processes is stopped at the specified stage (default="${stop_stage}").
--skip_stages # Spicify the stage to be skipped (default="${skip_stages}").
--skip_data_prep # Skip data preparation stages (default="${skip_data_prep}").
--skip_train # Skip training stages (default="${skip_train}").
--skip_eval # Skip decoding and evaluation stages (default="${skip_eval}").
--skip_upload # Skip packing and uploading stages (default="${skip_upload}").
--skip_upload_hf # Skip packing and uploading stages (default="${skip_upload_hf}").
--eval_valid_set # Run decoding for the validation set (default="${eval_valid_set}").
--ngpu # The number of gpus ("0" uses cpu, otherwise use gpu, default="${ngpu}").
--num_nodes # The number of nodes (default="${num_nodes}").
--nj # The number of parallel jobs (default="${nj}").
--inference_nj # The number of parallel jobs in decoding (default="${inference_nj}").
--gpu_inference # Whether to perform gpu decoding (default="${gpu_inference}").
--dumpdir # Directory to dump features (default="${dumpdir}").
--expdir # Directory to save experiments (default="${expdir}").
--python # Specify python to execute espnet commands (default="${python}").
# Data preparation related
--local_data_opts # The options given to local/data.sh (default="${local_data_opts}").
# Speed perturbation related
--speed_perturb_factors # speed perturbation factors, e.g. "0.9 1.0 1.1" (separated by space, default="${speed_perturb_factors}").
# Feature extraction related
--feats_type # Feature type (raw, raw_copy, fbank_pitch or extracted, default="${feats_type}").
--audio_format # Audio format: wav, flac, wav.ark, flac.ark (only in feats_type=raw or raw_copy, default="${audio_format}").
--fs # Sampling rate (default="${fs}").
--min_wav_duration # Minimum duration in second (default="${min_wav_duration}").
--max_wav_duration # Maximum duration in second (default="${max_wav_duration}").
# Tokenization related
--token_type # Tokenization type (char or bpe, default="${token_type}").
--nbpe # The number of BPE vocabulary (default="${nbpe}").
--bpemode # Mode of BPE (unigram or bpe, default="${bpemode}").
--oov # Out of vocabulary symbol (default="${oov}").
--blank # CTC blank symbol (default="${blank}").
--sos_eos # sos and eos symbole (default="${sos_eos}").
--bpe_input_sentence_size # Size of input sentence for BPE (default="${bpe_input_sentence_size}").
--bpe_nlsyms # Non-linguistic symbol list for sentencepiece, separated by a comma or a file containing 1 symbol per line . (default="${bpe_nlsyms}").
--bpe_char_cover # Character coverage when modeling BPE (default="${bpe_char_cover}").
# Language model related
--lm_tag # Suffix to the result dir for language model training (default="${lm_tag}").
--lm_exp # Specify the directory path for LM experiment.
# If this option is specified, lm_tag is ignored (default="${lm_exp}").
--lm_stats_dir # Specify the directory path for LM statistics (default="${lm_stats_dir}").
--lm_config # Config for language model training (default="${lm_config}").
--lm_args # Arguments for language model training (default="${lm_args}").
# e.g., --lm_args "--max_epoch 10"
# Note that it will overwrite args in lm config.
--use_word_lm # Whether to use word language model (default="${use_word_lm}").
--word_vocab_size # Size of word vocabulary (default="${word_vocab_size}").
--num_splits_lm # Number of splitting for lm corpus (default="${num_splits_lm}").
# ASR model related
--asr_task # ASR task mode. Either 'asr' or 'asr_transducer'. (default="${asr_task}").
--asr_tag # Suffix to the result dir for asr model training (default="${asr_tag}").
--asr_exp # Specify the directory path for ASR experiment.
# If this option is specified, asr_tag is ignored (default="${asr_exp}").
--asr_stats_dir # Specify the directory path for ASR statistics (default="${asr_stats_dir}").
--asr_config # Config for asr model training (default="${asr_config}").
--asr_args # Arguments for asr model training (default="${asr_args}").
# e.g., --asr_args "--max_epoch 10"
# Note that it will overwrite args in asr config.
--pretrained_model= # Pretrained model to load (default="${pretrained_model}").
--ignore_init_mismatch= # Ignore mismatch parameter init with pretrained model (default="${ignore_init_mismatch}").
--feats_normalize # Normalizaton layer type (default="${feats_normalize}").
--num_splits_asr # Number of splitting for lm corpus (default="${num_splits_asr}").
--num_ref # Number of references for training (default="${num_ref}").
# In supervised learning based speech recognition, it is equivalent to number of speakers.
--num_inf # Number of inference audio generated by the model (default="${num_inf}")
# Note that if it is not specified, it will be the same as num_ref. Otherwise, it will be overwritten.
--sot_asr # Whether to use Serialized Output Training (SOT) (default="${sot_asr}")
# Decoding related
--inference_tag # Suffix to the result dir for decoding (default="${inference_tag}").
--inference_config # Config for decoding (default="${inference_config}").
--inference_args # Arguments for decoding (default="${inference_args}").
# e.g., --inference_args "--lm_weight 0.1"
# Note that it will overwrite args in inference config.
--inference_lm # Language model path for decoding (default="${inference_lm}").
--inference_asr_model # ASR model path for decoding (default="${inference_asr_model}").
--download_model # Download a model from Model Zoo and use it for decoding (default="${download_model}").
--use_streaming # Whether to use streaming decoding (default="${use_streaming}").
--use_maskctc # Whether to use maskctc decoding (default="${use_streaming}").
# [Task dependent] Set the datadir name created by local/data.sh
--train_set # Name of training set (required).
--valid_set # Name of validation set used for monitoring/tuning network training (required).
--test_sets # Names of test sets.
# Multiple items (e.g., both dev and eval sets) can be specified (required).
--bpe_train_text # Text file path of bpe training set.
--lm_train_text # Text file path of language model training set.
--lm_dev_text # Text file path of language model development set (default="${lm_dev_text}").
--lm_test_text # Text file path of language model evaluation set (default="${lm_test_text}").
--nlsyms_txt # Non-linguistic symbol list if existing (default="${nlsyms_txt}").
--cleaner # Text cleaner (default="${cleaner}").
--g2p # g2p method (default="${g2p}").
--lang # The language type of corpus (default=${lang}).
--score_opts # The options given to sclite scoring (default="{score_opts}").
--local_score_opts # The options given to local/score.sh (default="{local_score_opts}").
--asr_speech_fold_length # fold_length for speech data during ASR training (default="${asr_speech_fold_length}").
--asr_text_fold_length # fold_length for text data during ASR training (default="${asr_text_fold_length}").
--lm_fold_length # fold_length for LM training (default="${lm_fold_length}").
EOF
)
log "$0 $*"
# Save command line args for logging (they will be lost after utils/parse_options.sh)
run_args=$(scripts/utils/print_args.sh $0 "$@")
. utils/parse_options.sh
if [ $# -ne 0 ]; then
log "${help_message}"
log "Error: No positional arguments are required."
exit 2
fi
. ./path.sh
. ./cmd.sh
# Check required arguments
if ! "${skip_train}"; then
[ -z "${train_set}" ] && { log "${help_message}"; log "Error: --train_set is required"; exit 2; };
[ -z "${valid_set}" ] && { log "${help_message}"; log "Error: --valid_set is required"; exit 2; };
fi
if ! "${eval_valid_set}"; then
[ -z "${test_sets}" ] && { log "${help_message}"; log "Error: --test_sets is required"; exit 2; };
else
[ -z "${valid_set}" ] && { log "${help_message}"; log "Error: --valid_set is required"; exit 2; };
fi
if [ -n "${train_set}" ] && [ "${train_set}" = "${valid_set}" ]; then
log "Error: train_set and valid_set must be different. --train_set ${train_set} --valid_set ${valid_set}"
exit 1
fi
_test_sets=
for dset in ${test_sets}; do
if [ "${dset}" = "${train_set}" ]; then
log "Error: train_set and test_sets must be different. --train_set ${train_set} --test_sets ${test_sets}"
exit 1
fi
if [ "${dset}" = "${valid_set}" ]; then
log "Info: The valid_set '${valid_set}' is included in the test_sets. '--eval_valid_set true' is set and '${valid_set}' is removed from the test_sets"
eval_valid_set=true
elif [[ " ${_test_sets} " =~ [[:space:]]${dset}[[:space:]] ]]; then
log "Info: ${dset} is duplicated in the test_sets. One is removed"
else
_test_sets+="${dset} "
fi
done
test_sets=${_test_sets}
# Check feature type
if [ "${feats_type}" = raw ]; then
data_feats=${dumpdir}/raw
elif [ "${feats_type}" = raw_copy ]; then
# raw_copy is as same as raw except for skipping the format_wav stage
data_feats=${dumpdir}/raw_copy
elif [ "${feats_type}" = fbank_pitch ]; then
data_feats=${dumpdir}/fbank_pitch
elif [ "${feats_type}" = fbank ]; then
data_feats=${dumpdir}/fbank
elif [ "${feats_type}" == extracted ]; then
data_feats=${dumpdir}/extracted
else
log "${help_message}"
log "Error: not supported: --feats_type ${feats_type}"
exit 2
fi
num_inf=${num_inf:=${num_ref}}
# Preprocessor related
if [ ${num_ref} -eq 1 ]; then
# For single speaker, text file path and name are text
ref_text_files_str="text "
ref_text_names_str="text "
else
# For multiple speakers, text file path and name are text_spk[1-N] and [text, text_spk2, ...]
#TODO(simpleoier): later to support flexibly defined text prefix
ref_text_files_str="text_spk1 "
ref_text_names_str="text "
for n in $(seq 2 ${num_ref}); do
ref_text_files_str+="text_spk${n} "
ref_text_names_str+="text_spk${n} "
done
fi
# shellcheck disable=SC2206
ref_text_files=(${ref_text_files_str// / })
# shellcheck disable=SC2206
ref_text_names=(${ref_text_names_str// / })
[ -z "${bpe_train_text}" ] && bpe_train_text="${data_feats}/org/${train_set}/${ref_text_files[0]}"
# Use the same text as ASR for lm training if not specified.
[ -z "${lm_train_text}" ] && lm_train_text="${data_feats}/org/${train_set}/${ref_text_files[0]}"
# Use the same text as ASR for lm training if not specified.
[ -z "${lm_dev_text}" ] && lm_dev_text="${data_feats}/org/${valid_set}/${ref_text_files[0]}"
if [ -z "${lm_test_text}" ]; then
if [ -z "${test_sets}" ]; then
lm_test_text="${data_feats}/org/${valid_set}/${ref_text_files[0]}"
else
# Use the text of the 1st evaldir if lm_test is not specified
lm_test_text="${data_feats}/${test_sets%% *}/${ref_text_files[0]}"
fi
fi
# Check tokenization type
if [ "${lang}" != noinfo ]; then
token_listdir=data/${lang}_token_list
else
token_listdir=data/token_list
fi
bpedir="${token_listdir}/bpe_${bpemode}${nbpe}"
bpeprefix="${bpedir}"/bpe
bpemodel="${bpeprefix}".model
bpetoken_list="${bpedir}"/tokens.txt
chartoken_list="${token_listdir}"/char/tokens.txt
hugging_face_token_list="${token_listdir}/hugging_face_"${hugging_face_model_name_or_path/\//-}/tokens.txt
# NOTE: keep for future development.
# shellcheck disable=SC2034
wordtoken_list="${token_listdir}"/word/tokens.txt
if [ "${token_type}" = bpe ]; then
token_list="${bpetoken_list}"
elif [ "${token_type}" = char ]; then
token_list="${chartoken_list}"
bpemodel=none
elif [ "${token_type}" = word ]; then
token_list="${wordtoken_list}"
bpemodel=none
elif [ "${token_type}" = whisper_en ]; then # should make token_list an output filepath here
token_list="${token_listdir}"/whisper_en/tokens.txt
bpemodel=whisper_en
hyp_cleaner=${cleaner}
elif [ "${token_type}" = whisper_multilingual ]; then
token_list="${token_listdir}"/whisper_multilingual/tokens.txt
bpemodel=whisper_multilingual
hyp_cleaner=${cleaner}
elif [ "${token_type}" = hugging_face ]; then
token_list="${hugging_face_token_list}"
bpemodel=${hugging_face_model_name_or_path}
else
log "Error: not supported --token_type '${token_type}'"
exit 2
fi
if ${use_word_lm}; then
log "Error: Word LM is not supported yet"
exit 2
else
lm_token_list="${token_list}"
lm_token_type="${token_type}"
fi
# Set tag for naming of model directory
if [ -z "${asr_tag}" ]; then
if [ -n "${asr_config}" ]; then
asr_tag="$(basename "${asr_config}" .yaml)_${feats_type}"
else
asr_tag="train_${feats_type}"
fi
if [ "${lang}" != noinfo ]; then
asr_tag+="_${lang}_${token_type}"
else
asr_tag+="_${token_type}"
fi
if [ "${token_type}" = bpe ]; then
asr_tag+="${nbpe}"
fi
if [ "${token_type}" = hugging_face ]; then
asr_tag+="_"${hugging_face_model_name_or_path/\//-}
fi
# Add overwritten arg's info
if [ -n "${asr_args}" ]; then
asr_tag+="$(echo "${asr_args}" | sed -e "s/--/\_/g" -e "s/[ |=/]//g")"
fi
if [ -n "${speed_perturb_factors}" ]; then
asr_tag+="_sp"
fi
fi
if [ -z "${lm_tag}" ]; then
if [ -n "${lm_config}" ]; then
lm_tag="$(basename "${lm_config}" .yaml)"
else
lm_tag="train"
fi
if [ "${lang}" != noinfo ]; then
lm_tag+="_${lang}_${lm_token_type}"
else
lm_tag+="_${lm_token_type}"
fi
if [ "${lm_token_type}" = bpe ]; then
lm_tag+="${nbpe}"
fi
# Add overwritten arg's info
if [ -n "${lm_args}" ]; then
lm_tag+="$(echo "${lm_args}" | sed -e "s/--/\_/g" -e "s/[ |=/]//g")"
fi
fi
# The directory used for collect-stats mode
if [ -z "${asr_stats_dir}" ]; then
if [ "${lang}" != noinfo ]; then
asr_stats_dir="${expdir}/asr_stats_${feats_type}_${lang}_${token_type}"
else
asr_stats_dir="${expdir}/asr_stats_${feats_type}_${token_type}"
fi
if [ "${token_type}" = bpe ]; then
asr_stats_dir+="${nbpe}"
fi
if [ "${token_type}" = hugging_face ]; then
asr_stats_dir+="_"${hugging_face_model_name_or_path/\//-}
fi
if [ -n "${speed_perturb_factors}" ]; then
asr_stats_dir+="_sp"
fi
fi
if [ -z "${lm_stats_dir}" ]; then
if [ "${lang}" != noinfo ]; then
lm_stats_dir="${expdir}/lm_stats_${lang}_${lm_token_type}"
else
lm_stats_dir="${expdir}/lm_stats_${lm_token_type}"
fi
if [ "${lm_token_type}" = bpe ]; then
lm_stats_dir+="${nbpe}"
fi
fi
# The directory used for training commands
if [ -z "${asr_exp}" ]; then
asr_exp="${expdir}/asr_${asr_tag}"
fi
if [ -z "${lm_exp}" ]; then
lm_exp="${expdir}/lm_${lm_tag}"
fi
if [ -z "${ngram_exp}" ]; then
ngram_exp="${expdir}/ngram"
fi
if [ -z "${inference_tag}" ]; then
if [ -n "${inference_config}" ]; then
inference_tag="$(basename "${inference_config}" .yaml)"
else
inference_tag=inference
fi
# Add overwritten arg's info
if [ -n "${inference_args}" ]; then
inference_tag+="$(echo "${inference_args}" | sed -e "s/--/\_/g" -e "s/[ |=]//g")"
fi
if "${use_lm}"; then
inference_tag+="_lm_$(basename "${lm_exp}")_$(echo "${inference_lm}" | sed -e "s/\//_/g" -e "s/\.[^.]*$//g")"
fi
if "${use_ngram}"; then
inference_tag+="_ngram_$(basename "${ngram_exp}")_$(echo "${inference_ngram}" | sed -e "s/\//_/g" -e "s/\.[^.]*$//g")"
fi
inference_tag+="_asr_model_$(echo "${inference_asr_model}" | sed -e "s/\//_/g" -e "s/\.[^.]*$//g")"
if "${use_k2}"; then
inference_tag+="_use_k2"
inference_tag+="_k2_ctc_decoding_${k2_ctc_decoding}"
inference_tag+="_use_nbest_rescoring_${use_nbest_rescoring}"
fi
fi
if "${skip_data_prep}"; then
skip_stages+="1 2 3 4 5 "
fi
if "${skip_train}"; then
skip_stages+="2 4 5 6 7 8 9 10 11 "
elif ! "${use_lm}"; then
skip_stages+="6 7 8 "
fi
if ! "${use_ngram}"; then
skip_stages+="9 "
fi
if "${skip_eval}"; then
skip_stages+="12 13 "
fi
if "${skip_upload}" && "${skip_upload_hf}"; then
skip_stages+="14 15 16 "
elif "${skip_upload}"; then
skip_stages+="15 "
elif "${skip_upload_hf}"; then
skip_stages+="16 "
fi
skip_stages=$(echo "${skip_stages}" | tr ' ' '\n' | sort -nu | tr '\n' ' ')
log "Skipped stages: ${skip_stages}"
# ========================== Main stages start from here. ==========================
if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ] && ! [[ " ${skip_stages} " =~ [[:space:]]1[[:space:]] ]]; then
log "Stage 1: Data preparation for data/${train_set}, data/${valid_set}, etc."
# [Task dependent] Need to create data.sh for new corpus
local/data.sh ${local_data_opts}
fi
if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ] && ! [[ " ${skip_stages} " =~ [[:space:]]2[[:space:]] ]]; then
if [ -n "${speed_perturb_factors}" ]; then
log "Stage 2: Speed perturbation: data/${train_set} -> data/${train_set}_sp"
for factor in ${speed_perturb_factors}; do
if python3 -c "assert ${factor} != 1.0" 2>/dev/null; then
scripts/utils/perturb_data_dir_speed.sh \
${ref_text_files_str:+--utt_extra_files "${ref_text_files_str}"} \
"${factor}" "data/${train_set}" "data/${train_set}_sp${factor}"
_dirs+="data/${train_set}_sp${factor} "
else
# If speed factor is 1, same as the original
_dirs+="data/${train_set} "
fi
done
utils/combine_data.sh \
${ref_text_files_str:+--extra_files "${ref_text_files_str}"} \
"data/${train_set}_sp" ${_dirs}
else
log "Skip stage 2: Speed perturbation"
fi
fi
if [ -n "${speed_perturb_factors}" ]; then
train_set="${train_set}_sp"
fi
if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ] && ! [[ " ${skip_stages} " =~ [[:space:]]3[[:space:]] ]]; then
if "${skip_train}"; then
if "${eval_valid_set}"; then
_dsets="${valid_set} ${test_sets}"
else
_dsets="${test_sets}"
fi
else
_dsets="${train_set} ${valid_set} ${test_sets}"
fi
if [ "${feats_type}" = raw ]; then
log "Stage 3: Format wav.scp: data/ -> ${data_feats}"
# ====== Recreating "wav.scp" ======
# Kaldi-wav.scp, which can describe the file path with unix-pipe, like "cat /some/path |",
# shouldn't be used in training process.
# "format_wav_scp.sh" dumps such pipe-style-wav to real audio file
# and it can also change the audio-format and sampling rate.
# If nothing is need, then format_wav_scp.sh does nothing:
# i.e. the input file format and rate is same as the output.
for dset in ${_dsets}; do
if [ "${dset}" = "${train_set}" ] || [ "${dset}" = "${valid_set}" ]; then
_suf="/org"
else
_suf=""
fi
utils/copy_data_dir.sh --validate_opts --non-print data/"${dset}" "${data_feats}${_suf}/${dset}"
rm -f ${data_feats}${_suf}/${dset}/{segments,wav.scp,reco2file_and_channel,reco2dur}
# Copy reference text files if there is more than 1 reference
if [ ${#ref_text_files[@]} -gt 1 ]; then
# shellcheck disable=SC2068
for ref_txt in ${ref_text_files[@]}; do
[ -f data/${dset}/${ref_txt} ] && cp data/${dset}/${ref_txt} ${data_feats}${_suf}/${dset}
done
fi
_opts=
if [ -e data/"${dset}"/segments ]; then
# "segments" is used for splitting wav files which are written in "wav".scp
# into utterances. The file format of segments:
# <segment_id> <record_id> <start_time> <end_time>
# "e.g. call-861225-A-0050-0065 call-861225-A 5.0 6.5"
# Where the time is written in seconds.
_opts+="--segments data/${dset}/segments "
fi
# shellcheck disable=SC2086
scripts/audio/format_wav_scp.sh --nj "${nj}" --cmd "${train_cmd}" \
--audio-format "${audio_format}" --fs "${fs}" ${_opts} \
--multi-columns-input "${multi_columns_input_wav_scp}" \
--multi-columns-output "${multi_columns_output_wav_scp}" \
"data/${dset}/wav.scp" "${data_feats}${_suf}/${dset}"
echo "${feats_type}" > "${data_feats}${_suf}/${dset}/feats_type"
if "${multi_columns_output_wav_scp}"; then
echo "multi_${audio_format}" > "${data_feats}${_suf}/${dset}/audio_format"
else
echo "${audio_format}" > "${data_feats}${_suf}/${dset}/audio_format"
fi
done
elif [ "${feats_type}" = raw_copy ]; then
# If you guaranteed that the data already satisfy the raw format, you can skip format_wav_scp.py for reduce the overhead
for dset in ${_dsets}; do
if [ -e "data/${dset}/segments" ]; then
log "Error: data/${dset}/segments is existing. Please use --feats_type raw"
exit 1
fi
if [ "${dset}" = "${train_set}" ] || [ "${dset}" = "${valid_set}" ]; then
_suf="/org"
else
_suf=""
fi
utils/copy_data_dir.sh --validate_opts --non-print data/"${dset}" "${data_feats}${_suf}/${dset}"
if [ "${dset}" = "${train_set}" ] || [ "${dset}" = "${valid_set}" ]; then
_suf="/org"
if [ -e "data/${dset}/utt2dur" ]; then
_fs=$(python3 -c "import humanfriendly as h;print(h.parse_size('${fs}'))")
<data/${dset}/utt2dur awk '{ print $1, int($2*'${_fs}'); }' > "${data_feats}${_suf}/${dset}"/utt2num_samples
elif [ -e "data/${dset}/utt2num_samples" ]; then
cp "data/${dset}/utt2num_samples" "${data_feats}${_suf}/${dset}"/utt2num_samples
else
log "Error: data/${dset}/utt2dur or data/${dset}/utt2num_samples must be existing for train_set and valid_set. Please use --feats_type raw. If you'd like to perform this script for evaluation, please give --skip_train true"
exit 1
fi
fi
# Copy reference text files if there is more than 1 reference
if [ ${#ref_text_files[@]} -gt 1 ]; then
# shellcheck disable=SC2068
for ref_txt in ${ref_text_files[@]}; do
[ -f data/${dset}/${ref_txt} ] && cp data/${dset}/${ref_txt} ${data_feats}${_suf}/${dset}
done
fi
echo "raw" > "${data_feats}${_suf}/${dset}/feats_type"
if "${multi_columns_input_wav_scp}"; then
echo "multi_${audio_format}" > "${data_feats}${_suf}/${dset}/audio_format"
else
echo "${audio_format}" > "${data_feats}${_suf}/${dset}/audio_format"
fi
done
elif [ "${feats_type}" = fbank_pitch ]; then
log "[Require Kaldi] Stage 3: ${feats_type} extract: data/ -> ${data_feats}"
for dset in ${_dsets}; do
if [ "${dset}" = "${train_set}" ] || [ "${dset}" = "${valid_set}" ]; then
_suf="/org"
else
_suf=""
fi
# 1. Copy datadir
utils/copy_data_dir.sh --validate_opts --non-print data/"${dset}" "${data_feats}${_suf}/${dset}"
# Copy reference text files if there is more than 1 reference
if [ ${#ref_text_files[@]} -gt 1 ]; then
# shellcheck disable=SC2068
for ref_txt in ${ref_text_files[@]}; do
[ -f data/${dset}/${ref_txt} ] && cp data/${dset}/${ref_txt} ${data_feats}${_suf}/${dset}
done
fi
# 2. Feature extract
_nj=$(min "${nj}" "$(<"${data_feats}${_suf}/${dset}/utt2spk" wc -l)")
steps/make_fbank_pitch.sh --nj "${_nj}" --cmd "${train_cmd}" "${data_feats}${_suf}/${dset}"
utils/fix_data_dir.sh "${data_feats}${_suf}/${dset}"
# 3. Derive the the frame length and feature dimension
scripts/feats/feat_to_shape.sh --nj "${_nj}" --cmd "${train_cmd}" \
"${data_feats}${_suf}/${dset}/feats.scp" "${data_feats}${_suf}/${dset}/feats_shape"
# 4. Write feats_dim
head -n 1 "${data_feats}${_suf}/${dset}/feats_shape" | awk '{ print $2 }' \
| cut -d, -f2 > ${data_feats}${_suf}/${dset}/feats_dim
# 5. Write feats_type
echo "${feats_type}" > "${data_feats}${_suf}/${dset}/feats_type"
done
elif [ "${feats_type}" = fbank ]; then
log "Stage 3: ${feats_type} extract: data/ -> ${data_feats}"
log "${feats_type} is not supported yet."
exit 1
elif [ "${feats_type}" = extracted ]; then
log "Stage 3: ${feats_type} extract: data/ -> ${data_feats}"
# Assumming you don't have wav.scp, but feats.scp is created by local/data.sh instead.
for dset in ${_dsets}; do
if [ "${dset}" = "${train_set}" ] || [ "${dset}" = "${valid_set}" ]; then
_suf="/org"
else
_suf=""
fi
# Generate dummy wav.scp to avoid error by copy_data_dir.sh
if [ ! -f data/"${dset}"/wav.scp ]; then
if [ ! -f data/"${dset}"/segments ]; then
<data/"${dset}"/feats.scp awk ' { print($1,"<DUMMY>") }' > data/"${dset}"/wav.scp
else
<data/"${dset}"/segments awk ' { print($2,"<DUMMY>") }' > data/"${dset}"/wav.scp
fi
fi
utils/copy_data_dir.sh --validate_opts --non-print data/"${dset}" "${data_feats}${_suf}/${dset}"
# Copy reference text files if there is more than 1 reference
# shellcheck disable=SC2068
if [ ${#ref_text_files[@]} -gt 1 ]; then
for ref_txt in ${ref_text_files[@]}; do
[ -f data/${dset}/${ref_txt} ] && cp data/${dset}/${ref_txt} ${data_feats}${_suf}/${dset}
done
fi
# Derive the the frame length and feature dimension
_nj=$(min "${nj}" "$(<"${data_feats}${_suf}/${dset}/utt2spk" wc -l)")
scripts/feats/feat_to_shape.sh --nj "${_nj}" --cmd "${train_cmd}" \
"${data_feats}${_suf}/${dset}/feats.scp" "${data_feats}${_suf}/${dset}/feats_shape"
pyscripts/feats/feat-to-shape.py "scp:head -n 1 ${data_feats}${_suf}/${dset}/feats.scp |" - | \
awk '{ print $2 }' | cut -d, -f2 > "${data_feats}${_suf}/${dset}/feats_dim"
echo "${feats_type}" > "${data_feats}${_suf}/${dset}/feats_type"
done
else
log "Error: not supported: --feats_type ${feats_type}"
exit 2
fi
fi
if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ] && ! [[ " ${skip_stages} " =~ [[:space:]]4[[:space:]] ]]; then
log "Stage 4: Remove long/short data: ${data_feats}/org -> ${data_feats}"
# NOTE(kamo): Not applying to test_sets to keep original data
for dset in "${train_set}" "${valid_set}"; do
# Copy data dir
utils/copy_data_dir.sh --validate_opts --non-print "${data_feats}/org/${dset}" "${data_feats}/${dset}"
cp "${data_feats}/org/${dset}/feats_type" "${data_feats}/${dset}/feats_type"
# Remove short utterances
_feats_type="$(<${data_feats}/${dset}/feats_type)"
if [ "${_feats_type}" = raw ]; then
_fs=$(python3 -c "import humanfriendly as h;print(h.parse_size('${fs}'))")
_min_length=$(python3 -c "print(int(${min_wav_duration} * ${_fs}))")
_max_length=$(python3 -c "print(int(${max_wav_duration} * ${_fs}))")
# utt2num_samples is created by format_wav_scp.sh
<"${data_feats}/org/${dset}/utt2num_samples" \
awk -v min_length="${_min_length}" -v max_length="${_max_length}" \
'{ if ($2 > min_length && $2 < max_length ) print $0; }' \
>"${data_feats}/${dset}/utt2num_samples"
<"${data_feats}/org/${dset}/wav.scp" \
utils/filter_scp.pl "${data_feats}/${dset}/utt2num_samples" \
>"${data_feats}/${dset}/wav.scp"
else
# Get frame shift in ms from conf/fbank.conf
_frame_shift=
if [ -f conf/fbank.conf ] && [ "$(<conf/fbank.conf grep -c frame-shift)" -gt 0 ]; then
# Assume using conf/fbank.conf for feature extraction
_frame_shift="$(<conf/fbank.conf grep frame-shift | sed -e 's/[-a-z =]*\([0-9]*\)/\1/g')"
fi
if [ -z "${_frame_shift}" ]; then
# If not existing, use the default number in Kaldi (=10ms).
# If you are using different number, you have to change the following value manually.
_frame_shift=10
fi
_min_length=$(python3 -c "print(int(${min_wav_duration} / ${_frame_shift} * 1000))")
_max_length=$(python3 -c "print(int(${max_wav_duration} / ${_frame_shift} * 1000))")
cp "${data_feats}/org/${dset}/feats_dim" "${data_feats}/${dset}/feats_dim"
<"${data_feats}/org/${dset}/feats_shape" awk -F, ' { print $1 } ' \
| awk -v min_length="${_min_length}" -v max_length="${_max_length}" \
'{ if ($2 > min_length && $2 < max_length) print $0; }' \
>"${data_feats}/${dset}/feats_shape"
<"${data_feats}/org/${dset}/feats.scp" \
utils/filter_scp.pl "${data_feats}/${dset}/feats_shape" \
>"${data_feats}/${dset}/feats.scp"
fi
# Remove empty text
# shellcheck disable=SC2068
for ref_txt in ${ref_text_files[@]}; do
<"${data_feats}/org/${dset}/${ref_txt}" \
awk ' { if( NF != 1 ) print $0; } ' >"${data_feats}/${dset}/${ref_txt}"
done
# fix_data_dir.sh leaves only utts which exist in all files
utils/fix_data_dir.sh \
${ref_text_files_str:+--utt_extra_files "${ref_text_files_str}"} \
"${data_feats}/${dset}"
done
if [ -n "${post_process_local_data_opts}" ]; then
# Do any additional local data post-processing here
local/data.sh ${post_process_local_data_opts} --asr_data_dir "${data_feats}/${train_set}"
fi
# shellcheck disable=SC2002,SC2068,SC2005
for lm_txt in ${lm_train_text[@]}; do
suffix=$(echo "$(basename ${lm_txt})" | sed 's/text//')
<${lm_txt} awk -v suffix=${suffix} ' { if( NF != 1 ) {$1=$1 suffix; print $0; }} '
done > "${data_feats}/lm_train.txt"
fi
if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ] && ! [[ " ${skip_stages} " =~ [[:space:]]5[[:space:]] ]]; then
if [ "${token_type}" = bpe ]; then
log "Stage 5: Generate token_list from ${bpe_train_text} using BPE"
mkdir -p "${bpedir}"
# shellcheck disable=SC2002
cat ${bpe_train_text} | cut -f 2- -d" " > "${bpedir}"/train.txt
if [ -n "${bpe_nlsyms}" ]; then
if test -f "${bpe_nlsyms}"; then
bpe_nlsyms_list=$(awk '{print $1}' ${bpe_nlsyms} | paste -s -d, -)
_opts_spm="--user_defined_symbols=${bpe_nlsyms_list}"
else
_opts_spm="--user_defined_symbols=${bpe_nlsyms}"
fi
else
_opts_spm=""
fi
if ${sot_asr}; then
# For SOT training, we add <sc> as an user-defined modeling unit.
# The input text may be `text^1 <sc> text^2 <sc> text^3`, where `text^n`
# refers to the transcription of `speaker n`.
# The order of different texts is determined by their start times.
_opts_spm+=" --user_defined_symbols=<sc>"
fi
spm_train \
--input="${bpedir}"/train.txt \
--vocab_size="${nbpe}" \
--model_type="${bpemode}" \
--model_prefix="${bpeprefix}" \
--character_coverage=${bpe_char_cover} \
--input_sentence_size="${bpe_input_sentence_size}" \
${_opts_spm}
{
echo "${blank}"
echo "${oov}"
# Remove <unk>, <s>, </s> from the vocabulary
<"${bpeprefix}".vocab awk '{ if( NR != 1 && NR != 2 && NR != 3 ){ print $1; } }'
echo "${sos_eos}"
} > "${token_list}"
elif [ "${token_type}" = char ] || [ "${token_type}" = word ]; then
log "Stage 5: Generate character level token_list from ${lm_train_text}"
_opts="--non_linguistic_symbols ${nlsyms_txt}"
if ${sot_asr} && [ "${token_type}" = char ]; then
# For SOT training, we add <sc> as an user-defined modeling unit.
# The input text may be `text^1 <sc> text^2 <sc> text^3`, where `text^n`
# refers to the transcription of `speaker n`.
# The order of different texts is determined by their start times.
_opts+=" --add_nonsplit_symbol <sc>:2 "
fi
# The first symbol in token_list must be "<blank>" and the last must be also sos/eos:
# 0 is reserved for CTC-blank for ASR and also used as ignore-index in the other task
${python} -m espnet2.bin.tokenize_text \
--token_type "${token_type}" \
--input "${data_feats}/lm_train.txt" --output "${token_list}" ${_opts} \
--field 2- \
--cleaner "${cleaner}" \
--g2p "${g2p}" \
--write_vocabulary true \
--add_symbol "${blank}:0" \
--add_symbol "${oov}:1" \
--add_symbol "${sos_eos}:-1"
# Duplicated <sc> token may be counted for char token type,
# so we shoud remove it
if ${sot_asr} && [ "${token_type}" = char ]; then
cp ${token_list} ${token_list}".duplicated"
awk '!seen[$0]++' ${token_list}".duplicated" > ${token_list}
rm ${token_list}".duplicated"
fi
elif grep -q "whisper" <<< ${token_type}; then
log "Stage 5: Generate whisper token_list from ${token_type} tokenizer"
if ${sot_asr}; then
log "Error: not supported SOT training for whisper token_list"
exit 2
fi
_opts=""
if [ "${token_type}" = "whisper_multilingual" ]; then
_opts+=" --language ${lang}"
fi
# The first symbol in token_list must be "<blank>" and the last must be also sos/eos:
# 0 is reserved for CTC-blank for ASR and also used as ignore-index in the other task
echo ${token_list}
${python} -m espnet2.bin.whisper_export_vocabulary \
--whisper_model "${token_type}" \
--output "${token_list}" ${_opts}
elif [ "${token_type}" = hugging_face ]; then
log "Stage 5: Generate hugging_face token_list from ${hugging_face_model_name_or_path}"
if ${sot_asr}; then
log "Error: not supported SOT training for hugging_face token_list"
exit 2
fi
# The first symbol in token_list must be "<blank>" and the last must be also sos/eos:
# 0 is reserved for CTC-blank for ASR and also used as ignore-index in the other task
${python} -m espnet2.bin.hugging_face_export_vocabulary \
--model_name_or_path "${hugging_face_model_name_or_path}" \
--output "${token_list}"
else
log "Error: not supported --token_type '${token_type}'"
exit 2
fi
# Create word-list for word-LM training
if ${use_word_lm} && [ "${token_type}" != word ]; then
log "Generate word level token_list from ${data_feats}/lm_train.txt"
${python} -m espnet2.bin.tokenize_text \
--token_type word \
--input "${data_feats}/lm_train.txt" --output "${lm_token_list}" \
--field 2- \
--cleaner "${cleaner}" \
--g2p "${g2p}" \
--write_vocabulary true \
--vocabulary_size "${word_vocab_size}" \
--add_symbol "${blank}:0" \
--add_symbol "${oov}:1" \
--add_symbol "${sos_eos}:-1"
fi
fi