icefall/egs/icmcasr/ASR/prepare.sh
2023-12-19 19:05:48 +08:00

193 lines
5.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# fix segmentation fault reported in https://github.com/k2-fsa/icefall/issues/674
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
set -eou pipefail
nj=15
stage=8
stop_stage=8
# We assume dl_dir (download dir) contains the following
# directories and files. If not, they will be downloaded
# by this script automatically.
#
# - $dl_dir/icmcasr
# You can find data_icmcasr, resource_icmcasr inside it.
# You can download them from https://www.openslr.org/33
#
# - $dl_dir/musan
# This directory contains the following directories downloaded from
# http://www.openslr.org/17/
#
# - music
# - noise
# - speech
# ln -s /your/parent/path/to/ICMC-ASR $PWD/downloa
dl_dir=$PWD/download
. shared/parse_options.sh || exit 1
# vocab size for sentence piece models.
# It will generate data/lang_bbpe_xxx,
# data/lang_bbpe_yyy if the array contains xxx, yyy
vocab_sizes=(
2000
# 1000
# 500
)
# All files generated by this script are saved in "data".
# You can safely remove "data" and rerun this script to regenerate it.
mkdir -p data
log() {
# This function is from espnet
local fname=${BASH_SOURCE[1]##*/}
echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
}
log "dl_dir: $dl_dir"
if [ $stage -le 1 ] && [ $stop_stage -ge 1 ]; then
log "Stage 1: Prepare icmcasr manifest"
# We assume that you have downloaded the icmcasr corpus
# to $dl_dir/icmcasr
if [ ! -f data/manifests/.icmcasr_manifests.done ]; then
mkdir -p data/manifests
for part in ihm sdm mdm; do
lhotse prepare icmcasr --mic ${part} $dl_dir/ICMC-ASR data/manifests
done
touch data/manifests/.icmcasr_manifests.done
fi
fi
if [ $stage -le 2 ] && [ $stop_stage -ge 2 ]; then
log "Stage 2: Prepare musan manifest"
# We assume that you have downloaded the musan corpus
# to data/musan
if [ ! -f data/manifests/.musan_manifests.done ]; then
mkdir -p data/manifests
lhotse prepare musan $dl_dir/musan data/manifests
touch data/manifests/.musan_manifests.done
fi
fi
if [ $stage -le 3 ] && [ $stop_stage -ge 3 ]; then
log "Stage 3: Apply GSS enhancement on MDM data (this stage requires a GPU)"
# We assume that you have installed the GSS package: https://github.com/desh2608/gss
local/prepare_icmc_gss.sh --stage 1 --stop_stage 6 data/manifests exp/icmc_gss
fi
if [ $stage -le 4 ] && [ $stop_stage -ge 4 ]; then
log "Stage 4: Compute fbank for icmcasr"
if [ ! -f data/fbank/.icmcasr.done ]; then
mkdir -p data/fbank
./local/compute_fbank_icmcasr.py --perturb-speed True
echo "Combining manifests"
lhotse combine data/manifests/cuts_train_{ihm,ihm_rvb,sdm,gss}.jsonl.gz - | shuf |\
gzip -c > data/manifests/cuts_train_all.jsonl.gz
touch data/fbank/.icmcasr.done
fi
fi
if [ $stage -le 5 ] && [ $stop_stage -ge 5 ]; then
log "Stage 5: Compute fbank for musan"
if [ ! -f data/fbank/.msuan.done ]; then
mkdir -p data/fbank
./local/compute_fbank_musan.py
touch data/fbank/.msuan.done
fi
fi
lang_char_dir=data/lang_char
if [ $stage -le 6 ] && [ $stop_stage -ge 6 ]; then
log "Stage 6: Prepare char based lang"
mkdir -p $lang_char_dir
if ! which jq; then
echo "This script is intended to be used with jq but you have not installed jq
Note: in Linux, you can install jq with the following command:
1. wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
2. chmod +x ./jq
3. cp jq /usr/bin" && exit 1
fi
if [ ! -f $lang_char_dir/text ] || [ ! -s $lang_char_dir/text ]; then
log "Prepare text."
gunzip -c data/manifests/icmcasr-ihm_supervisions_train.jsonl.gz \
| jq '.text' | sed 's/"//g' \
| ./local/text2token.py -t "char" > $lang_char_dir/text
fi
# The implementation of chinese word segmentation for text,
# and it will take about 15 minutes.
if [ ! -f $lang_char_dir/text_words_segmentation ]; then
python3 ./local/text2segments.py \
--num-process $nj \
--input-file $lang_char_dir/text \
--output-file $lang_char_dir/text_words_segmentation
fi
if [ -f $lang_char_dir/words.txt ]; then
cd $lang_char_dir
ln -s ../../../../wenetspeech/ASR/data/lang_char/words.txt .
cd ..
else
log "Abort! Please run ../../wenetspeech/ASR/prepare.sh"
exit 1
fi
fi
if [ $stage -le 7 ] && [ $stop_stage -ge 7 ]; then
log "Stage 7: Prepare G"
if [ ! -f $lang_char_dir/3-gram.unpruned.arpa ]; then
python3 ./shared/make_kn_lm.py \
-ngram-order 3 \
-text $lang_char_dir/text_words_segmentation \
-lm $lang_char_dir/3-gram.unpruned.arpa
fi
mkdir -p data/lm
if [ ! -f data/lm/G_3_gram.fst.txt ]; then
# It is used in building LG
python3 -m kaldilm \
--read-symbol-table="$lang_char_dir/words.txt" \
--disambig-symbol='#0' \
--max-order=3 \
$lang_char_dir/3-gram.unpruned.arpa > data/lm/G_3_gram.fst.txt
fi
if [ ! -f $lang_char_dir/5-gram.unpruned.arpa ]; then
python3 ./shared/make_kn_lm.py \
-ngram-order 5 \
-text $lang_char_dir/text_words_segmentation \
-lm $lang_char_dir/5-gram.unpruned.arpa
fi
if [ ! -f data/lm/G_5_gram.fst.txt ]; then
# It is used in building LG
python3 -m kaldilm \
--read-symbol-table="$lang_char_dir/words.txt" \
--disambig-symbol='#0' \
--max-order=5 \
$lang_char_dir/5-gram.unpruned.arpa > data/lm/G_5_gram.fst.txt
fi
fi
if [ $stage -le 8 ] && [ $stop_stage -ge 8 ]; then
log "Stage 15: Compile LG"
if [ ! -d data/lang_bpe_2000/ ]; then
log "Abort! Please run ../../multi_zh-hans/ASR/prepare.sh"
exit 1
cd data
ln -s ../../../../multi_zh-hans/ASR/data/lang_bpe_2000 .
cd ..
else
log "data/lang_bpe_2000/ exists"
fi
lang_dir=data/lang_bpe_2000
python3 ./local/compile_lg.py --lang-dir $lang_dir
#python3 ./local/compile_lg.py --lang-dir $lang_dir --lm G_5_gram
fi