【sphinx】中文聲學模型訓練


一 .使用CMUSphinx訓練聲學模型

   CMUSphinx工具包中自帶好幾個高質量的聲學模型。美語模型,法語,中文模型。這些模型是經過優化的,為了得到最佳的性能,大多數指令交互系統能直接用這些模型,甚至一些大詞匯量的應用也能直接用他們。

   除此之外,CMUSphinx提供了功能,能適應現存的模型,為了滿足有些需要更高精度的需求。當你需要使用不同的錄音環境,(比如近距離,遠離麥克分或者通過通話過程中),這些情況下做適應結果都是不錯的,或者當需要轉換一種口音,比如美語和英語的轉換,印度英語的使用等。自適應能滿足這樣的要求:那就是你需要在很短的時間內,支持一門新的語言,那么你只需要基於詞典做出一個聲學模型音素集到目標音素集的轉換就可。

   然而,在某些時候,當下的模型並沒法用。比如手寫識別中,或者其他語言的監測中。這些情況下,你需要重新訓練你自己的聲學模型。如下教程會指導你如何開始訓練。

二 開始訓練

  訓練之前,假設你有充足的數據:

  • 用於單個人的指令應用,至少需要一小時錄音,
  • 用於很多人指令應用,需要200個錄音人,每人5小時
  • 用於單個人的聽寫,需要10小時他的錄音
  • 用於多個人的聽寫,需要200個說話人,每人50小時的錄音
  • 同時你要有這門語言的語音學知識,以及你有足夠的比如一個月的時間,來訓練模型

  而如果你沒有足夠的數據,足夠的時間,足夠的經驗,那么建議你還是做已有模型的自適應來滿足你的要求。

數據准備

訓練者需要知道,使用哪一個聲音單元來學習參數,至少每個序列都要在你的訓練集中出現。這個信息儲存在transcript file中。

然后通過詞典dictionary,其中對於每個單詞都有相應的聲音序列做了映射。

所以,除了語音數據,你還需要一個transcripts,和兩個詞典。一個中是每個單詞到發音的對應表,以及一個中是不發音的單元的表,記為filler dictionay.

訓練開始

訓練之前需要准備如下兩個目錄

  • etc
    • your_db.dic - Phonetic dictionary
    • your_db.phone - Phoneset file
    • your_db.lm.DMP - Language model -語言模型
    • your_db.filler - List of fillers
    • your_db_train.fileids - List of files for training
    • your_db_train.transcription - Transcription for training
    • your_db_test.fileids - List of files for testing
    • your_db_test.transcription - Transcription for testing
  • wav
    • speaker_1
      • file_1.wav - Recording of speech utterance
    • speaker_2
      • file_2.wav

Fileids (your_db_train.fileids and your_db_test.fileids中列出了語音數據的文件名。如果是多個人的錄音,可以加上錄音人的信息,注意文件名不要加上后綴。

   speaker_1/file_1
   speaker_2/file_2

Transcription file (your_db_train.transcription and your_db_test.transcription) 中列出了你的錄音文本。句子前后加上<s>標簽,末尾加上句子序號。

   <s> hello world </s> (file_1)
   <s> foo bar </s> (file_2)

注意transcript中的行的序號和fileids中的序號要一致,如下,第二個句子放在第一位,就是個錯誤例子,會報錯。

   speaker_2/file_2
   speaker_1/file_1
   //Error! Do not create fileids file like this!
錄音文件,比如是MS WAV格式,采樣率為16khz,16bit,mono單聲道的錄音用於桌面應用。8khz,16bit,mono錄音用於電話應用。 注意這點,錯誤的語音格式,常常是訓練錯誤的原因。錄音句子不能太長,也不能太短。一般是5s-30s之間。句子頭尾的靜音不能超過0.2s。

你必須確認你的語音數據是16khz,16bit,單聲道。如果你用於電話應用,那可以是8khz,但是必須保證訓練期間的參數設置也是8khz的設置。注意不能上采樣語音數據,也就是說,不能用8khz的數據訓練16khz的模型。

詞典文件 (your_db.dict) 一行一個單詞,后面空格后跟着的是發音

HELLO HH AH L OW
WORLD W AO R L D

如果需要自己創建音素詞典,學習下它的語音致使。sphinxtrain不支持類似“*”,“/"這樣的音素符號。可以支持”+“”-“+“”:“這屆符號。不過最好還是用字母表。可以將”a~"替換為“aa”

如果實在沒有發音詞典,可以復寫它的單詞如下。

ONE O N E
TWO T W O

CMUSphinx與其他工具有一點不同,CMUSPhinx不支持單詞模型,如果你要訓練,你需要一個基於單詞的詞典

(For small vocabulary CMUSphinx is different from other toolkits. It's often recommended to train word-based models for small vocabulary databases like digits. But it only makes sense if your HMMs could have variable length. CMUSphinx does not support word models. Instead, you need to use a word-dependent phone dictionary:)

ONE W_ONE AH_ONE N_ONE
TWO T_TWO UH_TWO
NINE N_NINE AY_NINE N_END_NINE

This is actually equivalent to word-based models and some times even gives better accuracy. Do not use word-based models with CMUSphinx.

音素文件 (your_db.phone) 中一行一個音素,詞典中出現的所有音素都要包含,額外再加一個sil

AH
AX
DH
IX

語言模型文件 file (your_db.lm.DMP)需要時arpa格式(后綴為lm)或者是DMP格式。 Language Model training chapter.

Filler 詞典 (your_db.filler) 含有停頓,呼吸,感嘆詞,笑聲等這些無法用語言模型覆蓋的單元。可以只包含靜音 

<s> SIL
</s> SIL
<sil> SIL
或者如下內容,這些內容需要在transcription中出現過。
+um+ ++um++
+noise+ ++noise++

可以使用an4語音庫。an4 database NIST's Sphere audio (.sph) format ,

工具准備

訓練模型需要用到的工具包

  • sphinxbase-5prealpha  (pocketsphinx用的時候需要將這個包放在一起)
  • sphinxtrain-5prealpha   (訓練聲學模型的)
  • pocketsphinx-5prealpha  (識別用)

以及另外兩個工具:perl和python

  • perl, for example ActivePerl on Windows
  • python, for example ActivePython on Windows

建議在linux上做訓練,這樣可以利用到sphinxtrain的所有特征。如果是windows上訓練,那么可以使用activeperl。

********************************************************************************************************

*****此處,an4語音庫無法下載,如果能下載,可以看看里面的 格式,內容,數量就好了******

*********************************************************************

安裝了后用以下命令加載路徑。

export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

如果你不想裝在系統目錄中, 你可以裝在home目錄下,然后用如下命令,添加到autogen.sh中。
--prefix=/home/user/local
使用中如果遇到failed to open libsphinx.so.0 no such file or directory 這類問題代表安裝路徑,環境沒配置好。

訓練文件建立

a.如下命令,建立目錄

On Linux

sphinxtrain -t an4 setup  (其中,an4換成你的數據庫的名字,可以是data3 data4)

On Windows

python ../sphinxtrain/scripts/sphinxtrain -t an4 setup

會在data3目錄下得到如下兩個目錄。

  etc
  wav

等訓練完后,data3中內容是如下樣子:

  etc
  feat
  logdir
  model_parameters
  model_architecture
  result
  wav

b.訓練參數的設置

在文件etc/sphinx_train.cfg中做如下改動

  (1)數據格式的配置

$CFG_WAVFILES_DIR = "$CFG_BASE_DIR/wav";
$CFG_WAVFILE_EXTENSION = 'sph';
$CFG_WAVFILE_TYPE = 'nist'; # one of nist, mswav, raw

如果語音是wav格式,上面中sph,nist改為下面

$CFG_WAVFILES_DIR = "$CFG_BASE_DIR/wav";
$CFG_WAVFILE_EXTENSION = 'wav';
$CFG_WAVFILE_TYPE = 'mswav'; # one of nist, mswav, raw
(2)訓練路徑的配置
如下,要確定CFG_DB_NAME就是數據在目錄中的名字格式
# Variables used in main training of models
$CFG_DICTIONARY     = "$CFG_LIST_DIR/$CFG_DB_NAME.dic";
$CFG_RAWPHONEFILE   = "$CFG_LIST_DIR/$CFG_DB_NAME.phone";
$CFG_FILLERDICT     = "$CFG_LIST_DIR/$CFG_DB_NAME.filler";
$CFG_LISTOFFILES    = "$CFG_LIST_DIR/${CFG_DB_NAME}_train.fileids";
$CFG_TRANSCRIPTFILE = "$CFG_LIST_DIR/${CFG_DB_NAME}_train.transcription"

(3)模型格式和模型參數的配置

$CFG_HMM_TYPE = '.cont.'; # Sphinx4, Pocketsphinx
#$CFG_HMM_TYPE  = '.semi.'; # PocketSphinx only
#$CFG_HMM_TYPE  = '.ptm.'; # Sphinx4, Pocketsphinx, faster model
 $CFG_FINAL_NUM_DENSITIES = 8;

此處,如果有充足的數據(>100小時),則使用32,否則是8.階數分別是 2: 4, 8, 16, 32, 64.

如果訓練的是半連續的或者PTM模型,使用256 高斯。

# Number of tied states (senones) to create in decision-tree clustering
$CFG_N_TIED_STATES = 1000;

這個是模型中訓練的聲元的個數。聲元越多越精確。但是也不能過訓練。否則會得到如下錯誤:

ERROR: "gauden.c", line 1700: Variance (mgau= 948, feat= 0, density=3, 
component=38) is less then 0. Most probably the number of senones is too
high for such a small training database. Use smaller $CFG_N_TIED_STATES.

 

如下是各個應用,詞匯量,需要的數據時間,需要的發音人,階數對比表。

Vocabulary Hours in db Senones Densities Example
20 5 200 8 Tidigits Digits Recognition
100 20 2000 8 RM1 Command and Control
5000 30 4000 16 WSJ1 5k Small Dictation
20000 80 4000 32 WSJ1 20k Big Dictation
60000 200 6000 16 HUB4 Broadcast News
60000 2000 12000 64 Fisher Rich Telephone Transcription

重點:最好的結果,依賴於你的數據庫。

(4)聲音參數的配置

如果用的是8khz的語料,需要做如下修改:

# Feature extraction parameters
$CFG_WAVFILE_SRATE = 8000.0;
$CFG_NUM_FILT = 31; # For wideband speech it's 40, for telephone 8khz reasonable value is 31
$CFG_LO_FILT = 200; # For telephone 8kHz speech value is 200
$CFG_HI_FILT = 3500; # For telephone 8kHz speech value is 3500

 

(5)訓練速度的設置

如果是在多核機器上,修改如下設置,加快速度。
$DEC_CFG_DICTIONARY     = "$DEC_CFG_BASE_DIR/etc/$DEC_CFG_DB_NAME.dic";
$DEC_CFG_FILLERDICT     = "$DEC_CFG_BASE_DIR/etc/$DEC_CFG_DB_NAME.filler";
$DEC_CFG_LISTOFFILES    = "$DEC_CFG_BASE_DIR/etc/${DEC_CFG_DB_NAME}_test.fileids";
$DEC_CFG_TRANSCRIPTFILE = "$DEC_CFG_BASE_DIR/etc/${DEC_CFG_DB_NAME}_test.transcription";
$DEC_CFG_RESULT_DIR     = "$DEC_CFG_BASE_DIR/result";

# These variables, used by the decoder, have to be user defined, and
# may affect the decoder output

$DEC_CFG_LANGUAGEMODEL_DIR = "$DEC_CFG_BASE_DIR/etc";
$DEC_CFG_LANGUAGEMODEL  = "$DEC_CFG_LANGUAGEMODEL_DIR/an4.lm.DMP"

 

(6)解碼參數的設置

etc/sphinx_train.cfg, (此處是否有誤,應該是decode.cfg吧)

$DEC_CFG_DICTIONARY     = "$DEC_CFG_BASE_DIR/etc/$DEC_CFG_DB_NAME.dic";
$DEC_CFG_FILLERDICT     = "$DEC_CFG_BASE_DIR/etc/$DEC_CFG_DB_NAME.filler";
$DEC_CFG_LISTOFFILES    = "$DEC_CFG_BASE_DIR/etc/${DEC_CFG_DB_NAME}_test.fileids";
$DEC_CFG_TRANSCRIPTFILE = "$DEC_CFG_BASE_DIR/etc/${DEC_CFG_DB_NAME}_test.transcription";
$DEC_CFG_RESULT_DIR     = "$DEC_CFG_BASE_DIR/result";

# These variables, used by the decoder, have to be user defined, and
# may affect the decoder output

$DEC_CFG_LANGUAGEMODEL_DIR = "$DEC_CFG_BASE_DIR/etc";
$DEC_CFG_LANGUAGEMODEL  = "$DEC_CFG_LANGUAGEMODEL_DIR/an4.lm.DMP"

訓練

cd an4  進入數據目錄,其中已有各種小目錄以及各種配置文件。

On Linux

sphinxtrain run   開始了

On Windows

python ../sphinxtrain/scripts/sphinxtrain run

訓練開始,首先會檢查各個文件格式是否正確。不要忽視每一個錯誤。

Do not ignore the errors reported on the first 00.verify_all step.

典型的解碼過程中輸出如下所示:

        Baum welch starting for 2 Gaussian(s), iteration: 3 (1 of 1)
        0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 
        Normalization for iteration: 3
        Current Overall Likelihood Per Frame = 30.6558644286942
        Convergence Ratio = 0.633864444461992
        Baum welch starting for 2 Gaussian(s), iteration: 4 (1 of 1)
        0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 
        Normalization for iteration: 4

訓練過程詳解

  在scripts-pl目錄下,有00-99的流程,他們是如下順序執行的:

perl scripts_pl/000.comp_feat/slave_feat.pl
perl scripts_pl/00.verify/verify_all.pl
perl scripts_pl/10.vector_quantize/slave.VQ.pl
perl scripts_pl/20.ci_hmm/slave_convg.pl
perl scripts_pl/30.cd_hmm_untied/slave_convg.pl
perl scripts_pl/40.buildtrees/slave.treebuilder.pl
perl scripts_pl/45.prunetree/slave-state-tying.pl
perl scripts_pl/50.cd_hmm_tied/slave_convg.pl
perl scripts_pl/90.deleted_interpolation/deleted_interpolation.pl

其中有個文件是  an4.html,其中記有已執行的進程信息。 

其中執行00-90步驟時候,會生成很多組聲學模型,他們每個都能用於識別。只有在選擇semi-continuous的時候,會需要一些步驟。選擇continuous時候,其中一些步驟不會被用到。

000.comp_feat目錄下,slave-feat.pl產生MFCC特征。提取完后會自動移動到feat目錄下。CMUSphinx可以使用多種類型的特征。

20.ci_hmm 中,開始訓練獨立於上下文的模型。(單個phone)

30.cd_hmm_untied 中開始訓練依賴上下文的(triphone)的模型,稱為 CD-untied models,它的作用對於建立決策樹,連接各個狀態是必要的。 

40.buildtrees 會對每個單詞單元,每個狀態建立決策樹。

 45.prunetree 對決策樹做剪枝,同時將各個狀態連接起來。

50.cd-hmm_tied 是訓練最終的triphone的模型。叫做 CD-tied models. CD-tied models是多個階段訓練得到的。每個HMM狀態,我們先從1高斯開始,接着是2高斯,直到8高斯。

轉換矩陣訓練(高級版)

通過在train.config中做一些設置,會加做一些額外的訓練,可以提高識別精度。

MMIE Training (advanced)

同上。

測試開始

sphinxtrain -s decode run

得到的結果,統計詞錯誤率和句子錯誤率。10小時的應用,WER應該是10%附近。更大應用,WER會在30%附近

在result目錄下,有結果的詳細內容,如在 an4.align中有:

p   I   T      t   s   b   u   r   g   H      (MMXG-CEN5-MMXG-B)
p   R   EIGHTY t   s   b   u   r   g   EIGHT  (MMXG-CEN5-MMXG-B)
Words: 10 Correct: 7 Errors: 3 Percent correct = 70.00% Error = 30.00% Accuracy = 70.00%
Insertions: 0 Deletions: 0 Substitutions: 3
october twenty four nineteen seventy  (MMXG-CEN8-MMXG-B)
october twenty four nineteen seventy  (MMXG-CEN8-MMXG-B)
Words: 5 Correct: 5 Errors: 0 Percent correct = 100.00% Error = 0.00% Accuracy = 100.00%
Insertions: 0 Deletions: 0 Substitutions: 0
TOTAL Words: 773 Correct: 587 Errors: 234
TOTAL Percent correct = 75.94% Error = 30.27% Accuracy = 69.73%
TOTAL Insertions: 48 Deletions: 15 Substitutions: 171

使用模型

訓練后的模型在如下目錄:

model_parameters/<your_db_name>.cd_cont_<number_of senones>

or in

model_parameters/<your_db_name>.cd_semi_<number_of senones>

其中文件如下

mdef
feat.params
mixture_weights
means
noisedict
transition_matrices
variances

如下命令開始使用

pocketsphinx_continuous -hmm <your_new_model_folder> -lm <your_lm> -dict <your_dict>.

用sphinx4的話,需要代碼中設置模型路徑

     configuration.setAcousticModelPath("file:model_parameters/db.cd_cont_200");

一些問題

發現有問題后,1是檢查 logdir 中的細節log,2是看your_project_name.html

如下還有一些常見問題及原因:

WARNING: this phone (something) appears in the dictionary (dictionary file name), but not in the phone list (phone file name).
WARNING: This word (word) has duplicate entries in (dictionary file name). Check for duplicates.(詞典中不能有重復單詞,大小寫兩種也不行)
WARNING: This word: word was in the transcript file, but is not in the dictionary (transcript line) Do cases match?(文本中所有單詞都要在詞典中)
WARNING: CTL file, audio file name.mfc, does not exist, or is empty. (是不是有新加了語音,但是沒有提取特征這一步)
Very low recognition accuracy.
ERROR: "backward.c", line 430: Failed to align audio to transcript: final state of the search is not reached.

   這個原因是因為錄音和文本對不上,錄音可能是hello hello word,而文本只有hello word。解決辦法:1.盡量嚴格對齊文本,改變文本中內容,2.設置強行對齊參數,在train.cfg中。

$CFG_FORCEDALIGN = 'yes';,然后重新訓練。會執行10,11目錄來過濾數據庫。
Can't open */*-1-1.match word_align.pl failed with error code 65280

This error occurs because the decoder did not run properly after training. First check if the correct executable (psdecode_batch if the decoding script being used is psdecode.pl as set by $DEC_CFG_SCRIPT variable in sphinx_train.cfg) is present in PATH. On Linux run

which pocketsphinx_batch

and see if it is located. If it is not, you need to set the PATH variable properly. Similarly on Windows, run

where pocketsphinx_batch*

If the path to decoding executable is set properly, read the log files at logdir/decode/ to find out other reasons behind the error.

 
pocketsphinx_continuous -hmm <your_new_model_folder> -lm <your_lm> -dict <your_dict>.









+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
分隔線


對於目前使用的版本,大約是sphinx3版本,在linux上,使用過程中各個步驟如下:
1.建立各個目錄;
SphinxTrain/scripts_pl/setup_SphinxTrain.pl -task data3
2.修改配置:相同
3.開始訓練:
 cd /sphinx/MyTrain
./scripts_pl/make_feats.pl -ctl etc/my_db_train.fileids
./scripts_pl/make_feats.pl -ctl etc/my_db_test.fileids
./scripts_pl/RunAll.pl




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM