def split_data(self, audiopath, labelpath): """ 音频数据、标注地址分割 :param audiopath: 音频路径 :param labelpath: 标注路径 :return: """ generator = AcousticModel.init_audio(audiopath, labelpath) # 音频和标注的迭代器 file_count = generator.__next__() # 文件总数 chunk = file_count // self.num for job_id in range(self.num - 1): path_file = PARAMETERS_FILE_PATH + '/%s/pathInfo_%d.csv' % ( self.kwargs['unit_type'], job_id) '''文件已存在''' if os.path.exists(path_file): continue with open(path_file, 'w') as f: count = 0 while count < chunk: count += 1 path = generator.__next__() f.write(path[0]) # 写入音频路径 f.write(path[1]) # 写入标注路径 '''写入最后一个路径列表''' path_file = PARAMETERS_FILE_PATH + '/%s/pathInfo_%d.csv' % ( self.kwargs['unit_type'], (self.num - 1)) with open(path_file, 'w') as f: for path in generator: f.write(path[0]) # 写入音频路径 f.write(path[1]) # 写入标注路径
def main(record_seconds, n=2, savepath=path_record): """执行整个过程""" global state_num, mfcc, unit_time, lexicon, unit, ngram '''初始化声学模型''' am = AcousticModel(mix_level=4) am.initialize_unit(unit_type='XIF_tone') am.init_parameter() unit = am.unit unit_time = dict([[key, -1] for key in list(am.unit.keys())]) state_num = am.statenum # 设置HMM状态数 '''初始化语言模型''' for i in range(n): ngram_ = Ngram(n=i + 1) ngram_.init_gram() ngram.append(ngram_) '''初始化发音词典''' p_lexicon = PronunciationLexicon() p_lexicon.init_lexicon() lexicon = p_lexicon.lexicon '''获取MFCC帧''' mfcc = record_audio(record_seconds, savepath) '''生成首词模型''' generate_first_word(am) a = time.time() '''执行令牌传递算法''' token_passing() b = time.time() transfer() print('耗时:', b - a)
def parallel_data(self, env, mode=1, init=True): """ 并行处理数据 :param env: 用于标识机器的环境变量名 :param mode: 训练方案 :param init: 是否初始化 :return: """ '''获取作业ID''' try: job_id = os.environ[env] except JobIDExistError: raise JobIDExistError(self.log) amodel = AcousticModel(self.log, self.kwargs['unit_type'], processes=self.kwargs['processes'], job_id=job_id, console=self.console, state_num=self.kwargs['state_num'], mix_level=self.kwargs['mix_level'], delta_1=self.kwargs['delta_1'], delta_2=self.kwargs['delta_2']) amodel.load_unit() amodel.process_data(mode=mode, load_line=self.kwargs['load_line'], init=init, proportion=self.kwargs['proportion'], step=self.kwargs['step'], differentiation=self.kwargs['differentiation'], coefficient=self.kwargs['coefficient'])
def split_unit(self): """ 基元分割 :return: 各机器的训练清单 """ amodel = AcousticModel(self.log, self.kwargs['unit_type'], processes=self.kwargs['processes'], console=self.console, state_num=self.kwargs['state_num'], mix_level=self.kwargs['mix_level'], delta_1=self.kwargs['delta_1'], delta_2=self.kwargs['delta_2']) amodel.load_unit() units = amodel.loaded_units chunk = len(units) // self.num traininfo_path = PARAMETERS_FILE_PATH + '/%s' % self.kwargs['unit_type'] for job_id in range(self.num - 1): trainInfo_file = traininfo_path + '/trainInfo_%d.csv' % job_id '''删除现存trainInfo文件''' if os.path.exists(trainInfo_file): os.remove(trainInfo_file) units_slice = set(units).difference( set(units[job_id * chunk:(job_id + 1) * chunk])) for unit in units_slice: trainInfo = open(trainInfo_file, 'a+') trainInfo.writelines('%s\n' % unit) '''最后一个机器的训练清单''' units_slice = set(units).difference(set(units[(self.num - 1) * chunk:])) trainInfo_file = traininfo_path + '/trainInfo_%d.csv' % (self.num - 1) '''删除现存trainInfo文件''' if os.path.exists(trainInfo_file): os.remove(trainInfo_file) for unit in units_slice: trainInfo = open( traininfo_path + '/trainInfo_%d.csv' % (self.num - 1), 'a+') trainInfo.writelines('%s\n' % unit)
def parallel_train(self, env, init=True, show_q=False, show_a=False, c_covariance=1e-3): """ 并行训练 :param env: 用于标识机器的环境变量名 :param init: 是否初始化 :param show_q: 显示HMM/GMM当前似然度 :param show_a: 显示HMM重估后状态转移矩阵 :param c_covariance: 修正数值,纠正GMM中的Singular Matrix :return: """ '''获取作业ID''' try: job_id = os.environ[env] except JobIDExistError: raise JobIDExistError(self.log) '''从配置项中获取协方差修正数值,获取失败则在参数中获取,默认为1e-3''' try: c_covariance_ = os.environ['c_covariance'] except KeyError: c_covariance_ = c_covariance amodel = AcousticModel(self.log, self.kwargs['unit_type'], processes=self.kwargs['processes'], job_id=job_id, console=self.console, state_num=self.kwargs['state_num'], mix_level=self.kwargs['mix_level'], delta_1=self.kwargs['delta_1'], delta_2=self.kwargs['delta_2']) amodel.load_unit() amodel.training(init=init, show_q=show_q, show_a=show_a, c_covariance=c_covariance_)
def parallel_data(self, env, init=True): """ 并行处理数据 :param env: 用于标识机器的环境变量名 :param init: 是否初始化 :return: """ '''获取作业ID''' try: job_id = os.environ[env] except JobIDExistError: raise JobIDExistError(self.log) amodel = AcousticModel(self.log, self.kwargs['unit_type'], processes=self.kwargs['processes'], job_id=job_id, console=self.console, state_num=self.kwargs['state_num'], mix_level=self.kwargs['mix_level'], delta_1=self.kwargs['delta_1'], delta_2=self.kwargs['delta_2']) amodel.load_unit() amodel.split_data(load_line=self.kwargs['load_line'], init=init)
def parallel_train(self, env, mode=1, init=True, show_q=False, show_a=False): """ 并行训练 :param env: 用于标识机器的环境变量名 :param mode: 训练方案 :param init: 是否初始化 :param show_q: 显示HMM/GMM当前似然度 :param show_a: 显示HMM重估后状态转移矩阵 :return: """ '''获取作业ID''' try: job_id = os.environ[env] except JobIDExistError: raise JobIDExistError(self.log) amodel = AcousticModel(self.log, self.kwargs['unit_type'], processes=self.kwargs['processes'], job_id=job_id, console=self.console, state_num=self.kwargs['state_num'], mix_level=self.kwargs['mix_level'], delta_1=self.kwargs['delta_1'], delta_2=self.kwargs['delta_2']) amodel.load_unit() amodel.training(mode=mode, init=init, show_q=show_q, show_a=show_a, load_line=self.kwargs['load_line'], c_covariance=self.c_covariance)