Alize完整的ivector例程,包括:数据准备、特征提取、训练以及测试等。最终生成的得分文件res/scores_PLDA_lengthnorm.txt,其含义参考GMM-UBM。
M S0002 1 BAC009S0002W0122 0.644295
M S0003 1 BAC009S0002W0122 0.520998
M S0004 1 BAC009S0002W0122 0.48462
M S0002 1 BAC009S0002W0123 0.65574
M S0003 1 BAC009S0002W0123 0.722522
M S0004 1 BAC009S0002W0123 0.434874
利用自己的数据训练,只需要按照data/目录下对应的文件夹进行添加/删除即可。修改gmm/ivector/plda等参数配置,只需要修改cfg/目录下对应的文件即可。
data/目录如下:
提取特征,并产生相应的lst文件。
import os
def gen_lst (src_fpath, lst_fpath):
''' 用于产生.lst文件
src_fpath 为UBM的wav文件路径,src_fpath = './data/ubm'
lst_fpath 为UBM.lst存放路径,lst_fpath = './lst/UBM.lst'
'''flist=open(lst_fpath,'w')
spks = os.listdir(src_fpath)
spks.sort()
for spk in spks:
spk_path = os.path.join(src_fpath,spk)
spk_files = os.listdir(spk_path)
spk_files.sort()
for file in spk_files:
if file[-4:] != '.wav':
print('%s not .wav file'%(file))
continue
flist.write(file[0:-4]+'\n');
flist.close()def extraction_feature(src_path):
'''提取特征
src_path = './data/ubm'
'''
spks = os.listdir(src_path)
spks.sort()
for spk in spks:
spk_path = os.path.join(src_path,spk)
if not os.path.isdir(spk_path):
continue
spk_files = os.listdir(spk_path)
spk_files.sort()
for file in spk_files:
if file[-4:] != '.wav':
print('%s not .wav file'%(file))
continue
COMMAND_LINE = '%s%s%s%s%s'%('bin\\sfbcep.exe -m -k 0.97 -p19 -n 24 -r 22 -e -D -A -F wave ',
os.path.join(spk_path,file),' ./data/prm/',file[:-4],'.tmp.prm')
os.system(COMMAND_LINE)
print('%s'%(COMMAND_LINE))if __name__== "__main__" :
src_ubm = './data/ubm'
lst_ubm = './lst/UBM.lst'
gen_lst (src_ubm, lst_ubm)src_test = './data/test'
lst_test = './lst/test.lst'
gen_lst (src_test, lst_test)src_train = './data/train'
lst_train = './lst/train.lst'
gen_lst (src_train, lst_train)
# 提取特征
extraction_feature(src_ubm)
extraction_feature(src_test)
extraction_feature(src_train)
能量检测(VAD)、特征归一化。
CMD_NORM_E="bin\\NormFeat.exe --config cfg/NormFeat_energy_SPro.cfg --inputFeatureFilename data/data.lst --featureFilesPath data/prm/"
os.system(CMD_NORM_E)
CMD_ENERGY="bin\\EnergyDetector.exe --config cfg/EnergyDetector_SPro.cfg --inputFeatureFilename data/data.lst --featureFilesPath data/prm/ --labelFilesPath data/lbl/"
os.system(CMD_ENERGY)
CMD_NORM="bin\\NormFeat.exe --config cfg/NormFeat_SPro.cfg --inputFeatureFilename data/data.lst --featureFilesPath data/prm/ --labelFilesPath data/lbl/"
os.system(CMD_NORM)
import os
def gen_ndx (src_fpath, lst_fpath):
''' 用于产生.ndx文件
lst_fpath = ndx/ivExtractor.ndx
'''flist=open(lst_fpath,'w')
spks = os.listdir(src_fpath)
spks.sort()
for spk in spks:
flist.write(spk+' ')
spk_path = os.path.join(src_fpath,spk)
spk_files = os.listdir(spk_path)
spk_files.sort()
for file in spk_files:
if file[-4:] != '.wav':
print('%s not .wav file'%(file))
continue
flist.write(file[0:-4]+' ')
flist.write('\n')
flist.close()def gen_ivndx (src_fpath, lst_fpath):
'''
'''flist=open(lst_fpath,'w')
spks = os.listdir(src_fpath)
spks.sort()
for spk in spks:
spk_path = os.path.join(src_fpath,spk)
spk_files = os.listdir(spk_path)
spk_files.sort()
for file in spk_files:
if file[-4:] != '.wav':
print('%s not .wav file'%(file))
continue
flist.write(file[0:-4]+' '+file[0:-4]+'\n');
flist.close()def gen_pladndx (src_fpath, lst_fpath):
''' 用于产生.ndx文件
lst_fpath = ndx/ivExtractor.ndx
'''
flist=open(lst_fpath,'w')
spks = os.listdir(src_fpath)
spks.sort()
for spk in spks:
#flist.write(spk+' ')
spk_path = os.path.join(src_fpath,spk)
spk_files = os.listdir(spk_path)
spk_files.sort()
for file in spk_files:
if file[-4:] != '.wav':
print('%s not .wav file'%(file))
continue
flist.write(file[0:-4]+' ')
flist.write('\n')
flist.close()if __name__== "__main__" :
'''???
'''
src_train = './data/train'
lst_train = 'ndx/ivExtractor_train.ndx'
gen_ivndx (src_train, lst_train)src_test = './data/test'
lst_test = 'ndx/ivExtractor_test.ndx'
gen_ivndx (src_test, lst_test)gen_ivndx ('data/ubm', 'ndx/ivExtractor_ubm.ndx')
filelist = ['ndx/ivExtractor_ubm.ndx','ndx/ivExtractor_train.ndx','ndx/ivExtractor_test.ndx']
newfile=open('ndx/ivExtractor.ndx','w')
for item in filelist:
for txt in open(item,'r'):
newfile.write(txt)newfile.close()
'''
'''
src_plda = './data/ubm'
lst_plda = 'ndx/Plda.ndx'
gen_pladndx (src_plda, lst_plda)src_plda = './data/ubm'
lst_plda = 'ndx/ivNorm.ndx'
gen_pladndx (src_plda, lst_plda)
#trainModel.ndx
src_train = './data/train'
lst_train = 'ndx/trainModel.ndx'
gen_ndx (src_train, lst_train)#ivTest_plda_target-seg.ndx
src_test = './data/test'
# src_train
ndx_fpath = 'ndx/ivTest_plda_target-seg.ndx'
flist=open(ndx_fpath,'w')
spks_train = os.listdir(src_train)
spks = os.listdir(src_test)
spks.sort()
for spk in spks:
spk_path = os.path.join(src_test,spk)
spk_files = os.listdir(spk_path)
spk_files.sort()
for file in spk_files:
if file[-4:] != '.wav':
print('%s not .wav file'%(file))
continue
flist.write(file[0:-4]+' ')
for spk_train in spks_train:
flist.write(spk_train+' ')
flist.write('\n')
flist.close()
# 1. UBM training
print( "Train Universal Background Model by EM algorithm")
CMD_LINE = "bin\\TrainWorld.exe --config cfg/TrainWorld.cfg "
os.system(CMD_LINE)
# 2. Total Variability matrix Estimation
print( "Train TotalVariability matrix")
CMD_LINE = "bin\\TotalVariability.exe --config cfg/TotalVariability.cfg"
os.system(CMD_LINE)
# 3. I-vector extraction
print( "Extract i-vectors")
CMD_LINE = "bin\\IvExtractor.exe --config cfg/ivExtractor.cfg "
os.system(CMD_LINE)
# 1. I-vector Normalization
print( "Normalize i-vectors")
CMD_LINE = "bin\\IvNorm.exe --config cfg/ivNorm.cfg "
os.system(CMD_LINE)
# 2. PLDA Training
print( "Train Probabilistic Linear Discriminant Analysis model")
CMD_LINE = "bin\\PLDA.exe --config cfg/Plda.cfg "
os.system(CMD_LINE)
# 3. PLDA Testing
print( "Compare models to test segments using PLDA native scoring")
CMD_LINE = "bin\\IvTest.exe --config cfg/ivTest_Plda.cfg "
os.system(CMD_LINE)
完整代码下载地址:https://download.csdn.net/download/u012594175/11100607
声纹识别交流QQ群:875705987