run.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env bash
  2. . ./path.sh || exit 1;
  3. # machines configuration
  4. CUDA_VISIBLE_DEVICES="0,1"
  5. gpu_num=2
  6. count=1
  7. gpu_inference=true # Whether to perform gpu decoding, set false for cpu decoding
  8. # for gpu decoding, inference_nj=ngpu*njob; for cpu decoding, inference_nj=njob
  9. njob=1
  10. train_cmd=utils/run.pl
  11. infer_cmd=utils/run.pl
  12. # general configuration
  13. feats_dir="../DATA" #feature output dictionary
  14. exp_dir="."
  15. lang=zh
  16. token_type=char
  17. type=sound
  18. scp=wav.scp
  19. speed_perturb="0.9 1.0 1.1"
  20. stage=0
  21. stop_stage=5
  22. # feature configuration
  23. feats_dim=80
  24. nj=64
  25. # data
  26. raw_data=../raw_data
  27. data_url=www.openslr.org/resources/33
  28. # exp tag
  29. tag="exp1"
  30. model_name=damo/speech_data2vec_pretrain-zh-cn-aishell2-16k-pytorch
  31. init_param="$HOME/.cache/modelscope/hub/$model_name/basemodel.pb"
  32. . utils/parse_options.sh || exit 1;
  33. # Set bash to 'debug' mode, it will exit on :
  34. # -e 'error', -u 'undefined variable', -o ... 'error in pipeline', -x 'print commands',
  35. set -e
  36. set -u
  37. set -o pipefail
  38. train_set=train
  39. valid_set=dev
  40. test_sets="dev test"
  41. asr_config=conf/train_asr_paraformer_transformer_12e_6d_3072_768.yaml
  42. model_dir="baseline_$(basename "${asr_config}" .yaml)_${lang}_${token_type}_${tag}"
  43. inference_config=conf/decode_asr_transformer_noctc_1best.yaml
  44. inference_asr_model=valid.acc.ave_10best.pb
  45. # you can set gpu num for decoding here
  46. gpuid_list=$CUDA_VISIBLE_DEVICES # set gpus for decoding, the same as training stage by default
  47. ngpu=$(echo $gpuid_list | awk -F "," '{print NF}')
  48. if ${gpu_inference}; then
  49. inference_nj=$[${ngpu}*${njob}]
  50. _ngpu=1
  51. else
  52. inference_nj=$njob
  53. _ngpu=0
  54. fi
  55. if [ ${stage} -le -1 ] && [ ${stop_stage} -ge -1 ]; then
  56. echo "stage -1: Data Download"
  57. local/download_and_untar.sh ${raw_data} ${data_url} data_aishell
  58. local/download_and_untar.sh ${raw_data} ${data_url} resource_aishell
  59. fi
  60. if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then
  61. echo "stage 0: Data preparation"
  62. # Data preparation
  63. local/aishell_data_prep.sh ${raw_data}/data_aishell/wav ${raw_data}/data_aishell/transcript ${feats_dir}
  64. for x in train dev test; do
  65. cp ${feats_dir}/data/${x}/text ${feats_dir}/data/${x}/text.org
  66. paste -d " " <(cut -f 1 -d" " ${feats_dir}/data/${x}/text.org) <(cut -f 2- -d" " ${feats_dir}/data/${x}/text.org | tr -d " ") \
  67. > ${feats_dir}/data/${x}/text
  68. utils/text2token.py -n 1 -s 1 ${feats_dir}/data/${x}/text > ${feats_dir}/data/${x}/text.org
  69. mv ${feats_dir}/data/${x}/text.org ${feats_dir}/data/${x}/text
  70. done
  71. fi
  72. if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
  73. echo "stage 1: Feature and CMVN Generation"
  74. utils/compute_cmvn.sh --fbankdir ${feats_dir}/data/${train_set} --cmd "$train_cmd" --nj $nj --feats_dim ${feats_dim} --config_file "$asr_config" --scale 1.0
  75. fi
  76. token_list=${feats_dir}/data/${lang}_token_list/$token_type/tokens.txt
  77. echo "dictionary: ${token_list}"
  78. if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
  79. echo "stage 2: Dictionary Preparation"
  80. mkdir -p ${feats_dir}/data/${lang}_token_list/$token_type/
  81. echo "make a dictionary"
  82. echo "<blank>" > ${token_list}
  83. echo "<s>" >> ${token_list}
  84. echo "</s>" >> ${token_list}
  85. utils/text2token.py -s 1 -n 1 --space "" ${feats_dir}/data/$train_set/text | cut -f 2- -d" " | tr " " "\n" \
  86. | sort | uniq | grep -a -v -e '^\s*$' | awk '{print $0}' >> ${token_list}
  87. echo "<unk>" >> ${token_list}
  88. fi
  89. # LM Training Stage
  90. world_size=$gpu_num # run on one machine
  91. if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
  92. echo "stage 3: LM Training"
  93. fi
  94. # ASR Training Stage
  95. world_size=$gpu_num # run on one machine
  96. if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then
  97. echo "stage 4: ASR Training"
  98. python utils/download_model.py --model_name ${model_name} # download pretrained model on ModelScope
  99. mkdir -p ${exp_dir}/exp/${model_dir}
  100. mkdir -p ${exp_dir}/exp/${model_dir}/log
  101. INIT_FILE=${exp_dir}/exp/${model_dir}/ddp_init
  102. if [ -f $INIT_FILE ];then
  103. rm -f $INIT_FILE
  104. fi
  105. init_method=file://$(readlink -f $INIT_FILE)
  106. echo "$0: init method is $init_method"
  107. for ((i = 0; i < $gpu_num; ++i)); do
  108. {
  109. rank=$i
  110. local_rank=$i
  111. gpu_id=$(echo $CUDA_VISIBLE_DEVICES | cut -d',' -f$[$i+1])
  112. train.py \
  113. --task_name asr \
  114. --gpu_id $gpu_id \
  115. --use_preprocessor true \
  116. --token_type $token_type \
  117. --token_list $token_list \
  118. --data_dir ${feats_dir}/data \
  119. --train_set ${train_set} \
  120. --valid_set ${valid_set} \
  121. --data_file_names "wav.scp,text" \
  122. --init_param ${init_param} \
  123. --cmvn_file ${feats_dir}/data/${train_set}/cmvn/am.mvn \
  124. --resume true \
  125. --output_dir ${exp_dir}/exp/${model_dir} \
  126. --config $asr_config \
  127. --ngpu $gpu_num \
  128. --num_worker_count $count \
  129. --dist_init_method $init_method \
  130. --dist_world_size $world_size \
  131. --dist_rank $rank \
  132. --local_rank $local_rank 1> ${exp_dir}/exp/${model_dir}/log/train.log.$i 2>&1
  133. } &
  134. done
  135. wait
  136. fi
  137. # Testing Stage
  138. if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  139. echo "stage 5: Inference"
  140. for dset in ${test_sets}; do
  141. asr_exp=${exp_dir}/exp/${model_dir}
  142. inference_tag="$(basename "${inference_config}" .yaml)"
  143. _dir="${asr_exp}/${inference_tag}/${inference_asr_model}/${dset}"
  144. _logdir="${_dir}/logdir"
  145. if [ -d ${_dir} ]; then
  146. echo "${_dir} is already exists. if you want to decode again, please delete this dir first."
  147. exit 0
  148. fi
  149. mkdir -p "${_logdir}"
  150. _data="${feats_dir}/data/${dset}"
  151. key_file=${_data}/${scp}
  152. num_scp_file="$(<${key_file} wc -l)"
  153. _nj=$([ $inference_nj -le $num_scp_file ] && echo "$inference_nj" || echo "$num_scp_file")
  154. split_scps=
  155. for n in $(seq "${_nj}"); do
  156. split_scps+=" ${_logdir}/keys.${n}.scp"
  157. done
  158. # shellcheck disable=SC2086
  159. utils/split_scp.pl "${key_file}" ${split_scps}
  160. _opts=
  161. if [ -n "${inference_config}" ]; then
  162. _opts+="--config ${inference_config} "
  163. fi
  164. ${infer_cmd} --gpu "${_ngpu}" --max-jobs-run "${_nj}" JOB=1:"${_nj}" "${_logdir}"/asr_inference.JOB.log \
  165. python -m funasr.bin.asr_inference_launch \
  166. --batch_size 1 \
  167. --ngpu "${_ngpu}" \
  168. --njob ${njob} \
  169. --gpuid_list ${gpuid_list} \
  170. --data_path_and_name_and_type "${_data}/${scp},speech,${type}" \
  171. --cmvn_file ${feats_dir}/data/${train_set}/cmvn/am.mvn \
  172. --speed_perturb ${speed_perturb} \
  173. --key_file "${_logdir}"/keys.JOB.scp \
  174. --asr_train_config "${asr_exp}"/config.yaml \
  175. --asr_model_file "${asr_exp}"/"${inference_asr_model}" \
  176. --output_dir "${_logdir}"/output.JOB \
  177. --mode paraformer \
  178. ${_opts}
  179. for f in token token_int score text; do
  180. if [ -f "${_logdir}/output.1/1best_recog/${f}" ]; then
  181. for i in $(seq "${_nj}"); do
  182. cat "${_logdir}/output.${i}/1best_recog/${f}"
  183. done | sort -k1 >"${_dir}/${f}"
  184. fi
  185. done
  186. python utils/proce_text.py ${_dir}/text ${_dir}/text.proc
  187. python utils/proce_text.py ${_data}/text ${_data}/text.proc
  188. python utils/compute_wer.py ${_data}/text.proc ${_dir}/text.proc ${_dir}/text.cer
  189. tail -n 3 ${_dir}/text.cer > ${_dir}/text.cer.txt
  190. cat ${_dir}/text.cer.txt
  191. done
  192. fi
  193. # Prepare files for ModelScope fine-tuning and inference
  194. if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
  195. echo "stage 6: ModelScope Preparation"
  196. cp ${feats_dir}/data/${train_set}/cmvn/am.mvn ${exp_dir}/exp/${model_dir}/am.mvn
  197. vocab_size=$(cat ${token_list} | wc -l)
  198. python utils/gen_modelscope_configuration.py \
  199. --am_model_name $inference_asr_model \
  200. --mode paraformer \
  201. --model_name data2vec_finetune_paraformer \
  202. --dataset aishell \
  203. --output_dir $exp_dir/exp/$model_dir \
  204. --vocab_size $vocab_size \
  205. --nat _nat \
  206. --tag $tag
  207. fi