Sphinx武林秘籍(中)
――训练自已的中文语言模型与声学模型
一、训练语言模型
(1) 安装语言模型训练工具CMUCLMTK
./configure
sudo make
sudo make install
默认安装在/usr/local/bin下面,可以看到下面这些生成文件
idngram2lm idngram2stats mergeidngram text2idngram
text2wfreq text2wngram wfreq2vocab
(2) 准备训练的文档
sudo -i
vi my_db.txt
在 .txt中输入如下内容,记住结尾不可留“\n”(实验证明了这一点)。
前进
后退
左转
右转
停止
wq 保存退出
(3)训练文档成模板
cd/usr/local/bin
text2wfreq < my_db.txt | wfreq2vocab > my_db.tmp.vocab
text2idngram -vocab my_db.vocab -idngram my_db.idngram < my_db.txt
idngram2lm –vocab my_db.vocab -idngram my_db.idngram -arpa my_db.arpa
sphinx_lm_convert -i my_db.arpa -o my_db.lm.DMP
最终生成了语言模型my_db.lm.DMP
二、训练声学模型
(1)准备一些文件
分别创建两个目录etc和wav,然后在两个目录下面制作一些数据文件,方便后面训练时使用。如下所示:
- etc
- my_db.dic - Phonetic dictionary
- my_db.phone - Phoneset file
- my _db.lm.DMP - Language model
- my_db.fillers - List of fillers
- my_db_train.fileids - List of files for training
- my_db_train.transcription - Transcription for training
- my_db_test.fileids - List of files for testing
- my_db_test.transcription - Transcription for testing
- wav
- speaker_1
- 1_11.wav - Recording of speech utterance
- speaker_2
- 2_11.wav - Recording of speech utterance
- speaker_3
- 3_11.wav - Recording of speech utterance
- speaker_4
- 4_11.wav - Recording of speech utterance
- speaker_5
- 5_11.wav - Recording of speech utterance
- speaker_1
(2)准备etc下的数据文件
cd /sphinx/Mytrain
mkdir etc
cd etc
1. vi my_db_train.fileids
2. vi my_db_test.fileids
输入内容:
speaker_1/1_11
speaker_2/2_11
speaker_3/3_11
speaker_4/4_11
speaker_5/5_11
3. vi my_db_train.transcription
4. vi my_db_test.transcription
输入内容如下:
前进 (1_11)
后退 (2_11)
左转 (3_11)
右转 (4_11)
停止 (5_11)
5. vi my_db.dic
输入内容:
前进 Q IAN J IN
后退 H OU T UI
左转 Z UO ZH UAN
右转 Y OU ZH UAN
停止 T ING ZH IB
6.vi my_db.filler
输入内容:
SIL
SIL
SIL
7.生成my_db.phone
cd ..
cd scripts
cp make_phoneset.pl /sphinx/Mytrain/etc
./make_phoneset.pl my_db.dic my_db.filler > my_db.phone
cd ..
cd etc
vi my_db.phone //查看下生成的.phone中的内容
H
IAN
IB
IN
ING
J
OU
Q
SIL
T
UAN
UI
UO
Y
Z
ZH
以上若有多余字母,可手工删除。
至此加上之前生成的my_db.lm.DMP,etc下的数据文件已准备完毕。
注意:以上所有的文件在输入结尾不可留下“/n”。
(3)准备wav下的音频文件
我们先在windows下面,录下前进、后退、左转、右转、停止五个命令词的wav音频文件,然后放在共享文件中。
mkdir speaker_1 speaker_2 speaker_3 speaker_4 speaker_5
把相应的音频1_11.wav 2_11.wav 3_11.wav 4_11.wav 5_11.wav 分别拷贝到相应的speaker_1 speaker_2 speaker_3 speaker_4 speaker_5 目录下面。
注意:此处音频文件采样率16KHz,单声道录音。
(4) an4与sphinxtrain搭建语音训练系统
cd /sphinix/MyTrain/
SphinxTrain/scripts_pl/setup_SphinxTrain.pl -task an4
pocketsphinx/scripts/setup_sphinx.pl -task an4
在当前目录下面会产生如下目录:
bin
bwaccumdir
etc
feat
logdir
model_parameters
model_architecture
scripts_pl
wav
把(2)、(3)中etc、wav下面的文件相应的拷贝到刚生成的wav与etc中。
(5) 修改etc/sphinx_train.cfg配置
1.路径
# These are filled in at configuration time
$CFG_DB_NAME = "an4";
$CFG_BASE_DIR = "/sphinx/MyTrain/an4";
$CFG_SPHINXTRAIN_DIR = "SphinxTrain";
改为
# These are filled in at configuration time
$CFG_DB_NAME = "my_db";
$CFG_BASE_DIR = "/sphinx/MyTrain";
$CFG_SPHINXTRAIN_DIR = "SphinxTrain";
2.音频
# Audio waveform and feature file information
$CFG_WAVFILES_DIR = "$CFG_BASE_DIR/wav";
$CFG_WAVFILE_EXTENSION = 'sph';
$CFG_WAVFILE_TYPE = 'nist'; # one of nist, mswav, raw
改为
# Audio waveform and feature file information
$CFG_WAVFILES_DIR = "$CFG_BASE_DIR/wav";
$CFG_WAVFILE_EXTENSION = 'wav';
$CFG_WAVFILE_TYPE = 'mswav'; # one of nist, mswav, raw
3.文件命名
#*******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";
$CFG_FEATPARAMS = "$CFG_LIST_DIR/feat.params";
确定etc下面的文件是以这种方式命名的。
4.模板类型与模板参数
#*******variables used in characterizing models*******
$CFG_HMM_TYPE = '.cont.'; # Sphinx III
#$CFG_HMM_TYPE = '.semi.'; # Sphinx II
pocketsphinx和Sphinx III一样,都是.cont。
(6) 配置声音特征参数
这里的操作为采样率为8KHz的情况,若采样率为16KHz,则可以直接略过此步操作。
vi feat.params
添加修改如下:
-samprate 8000.0
-nfilt 31
-lowerf 200.00
-upperf 3500.00
-dither yes
(7) 配置解码参数
1.路径
# These are filled in at configuration time
$DEC_CFG_DB_NAME = 'an4';
$DEC_CFG_BASE_DIR = '/sphinx/MyTrain/an4';
$DEC_CFG_SPHINXDECODER_DIR = 'pocketsphinx';
改为
# These are filled in at configuration time
$DEC_CFG_DB_NAME = 'my_db';
$DEC_CFG_BASE_DIR = '/sphinx/MyTrain';
$DEC_CFG_SPHINXDECODER_DIR = 'pocketsphinx';
2、语言模型名称
# This 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/my_db.lm.DMP";
$DEC_CFG_LANGUAGEWEIGHT = "10";
$DEC_CFG_BEAMWIDTH = "1e-80";
$DEC_CFG_WORDBEAM = "1e-40";
改为
# This 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";
$DEC_CFG_LANGUAGEWEIGHT = "10";
$DEC_CFG_BEAMWIDTH = "1e-80";
$DEC_CFG_WORDBEAM = "1e-40";
(8) 开始训练
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
在目录model_parameters下面生成了新的声学模型my_db.cd_cont_1000