kaldi 源码分析(九) - topo 文件分析

简介: 在 egs/wsj/s5/steps/nnet3/chain/gen_topo*.py 与 src/hmm/hmm-topology.cc 文件进行对应在 gen_topo*.p{l, y} 文件中进行自动创建 topo 文件, 然后在 hmm-topology.cc 文件中的 HmmTopology::Read() 函数中解析 topo 文件生成 HmmTopology 对象。

在 egs/wsj/s5/steps/nnet3/chain/gen_topo*.py 与 src/hmm/hmm-topology.cc 文件进行对应

在 gen_topo*.p{l, y} 文件中进行自动创建 topo 文件, 然后在 hmm-topology.cc 文件中的 HmmTopology::Read() 函数中解析 topo 文件生成 HmmTopology 对象。

在 steps/nnet3/chain/gen_topo*.p{l, y} 文件中可以看到 topo 文件大致结构:


args = parser.parse_args()

silence_phones = [ int(x) for x in args.silence_phones.split(":") ]
nonsilence_phones = [ int(x) for x in args.nonsilence_phones.split(":") ]
all_phones = silence_phones +  nonsilence_phones

print("<Topology>")
print("<TopologyEntry>")
# 当前 TopologyEntry 配置相关的所有 Phone 元素
print("<ForPhones>")
print(" ".join([str(x) for x in all_phones]))
print("</ForPhones>")
# 当前 State 配置相关
# state 0 is nonemitting
print("<State> 0 <Transition> 1 0.5 <Transition> 2 0.5 </State>")
# state 1 is for when we traverse it in 1 state
print("<State> 1 <PdfClass> 0 <Transition> 4 1.0 </State>")
# state 2 is for when we traverse it in >1 state, for the first state.
print("<State> 2 <PdfClass> 2 <Transition> 3 1.0 </State>")
# state 3 is for the self-loop.  Use pdf-class 1 here so that the default
# phone-class clustering (which uses only pdf-class 1 by default) gets only
# stats from longer phones.
print("<State> 3 <PdfClass> 1 <Transition> 3 0.5 <Transition> 4 0.5 </State>")
print("<State> 4 </State>")
print("</TopologyEntry>")
print("</Topology>")

在 hmm-topology.h 文件中

  /// TopologyEntry is a typedef that represents the topology of
  /// a single (prototype) state.
  typedef std::vector<HmmState> TopologyEntry;

对于 HmmTopology::Read() 函数中可以了解 topo 文件解析的过程

        ExpectToken(is, binary, "<ForPhones>");
        std::vector<int32> phones;
        std::string s;
        // 获取所有 phones 列表
        while (1) {
          is >> s;
          if (is.fail()) KALDI_ERR << "Reading HmmTopology object, unexpected end of file while expecting phones.";
          if (s == "</ForPhones>") break;
          else {
            int32 phone;
            if (!ConvertStringToInteger(s, &phone))
              KALDI_ERR << "Reading HmmTopology object, expected "
                        << "integer, got instead " << s;
            phones.push_back(phone);
          }
        }
        // 构建状态转换概率
        std::vector<HmmState> this_entry;
        std::string token;
        ReadToken(is, binary, &token);
        while (token != "</TopologyEntry>") {
          if (token != "<State>")
            KALDI_ERR << "Expected </TopologyEntry> or <State>, got instead "<<token;
          int32 state;
          ReadBasicType(is, binary, &state);
          if (state != static_cast<int32>(this_entry.size()))
            KALDI_ERR << "States are expected to be in order from zero, expected "
                      << this_entry.size() <<  ", got " << state;
          ReadToken(is, binary, &token);
          int32 forward_pdf_class = kNoPdf;  // -1 by default, means no pdf.
          if (token == "<PdfClass>") {
            // 根据 pdfClass 来创建一个 HmmState
            ReadBasicType(is, binary, &forward_pdf_class);
            this_entry.push_back(HmmState(forward_pdf_class));
            ReadToken(is, binary, &token);
            if (token == "<SelfLoopPdfClass>")
              KALDI_ERR << "pdf classes should be defined using <PdfClass> "
                        << "or <ForwardPdfClass>/<SelfLoopPdfClass> pair";
          } else if (token == "<ForwardPdfClass>") {
            // 根据 <ForwardPdfClass> <SelfLoopPdfClass> 组合, 来创建一个 HmmState
            int32 self_loop_pdf_class = kNoPdf;
            ReadBasicType(is, binary, &forward_pdf_class);
            ReadToken(is, binary, &token);
            KALDI_ASSERT(token == "<SelfLoopPdfClass>");
            ReadBasicType(is, binary, &self_loop_pdf_class);
            this_entry.push_back(HmmState(forward_pdf_class, self_loop_pdf_class));
            ReadToken(is, binary, &token);
          } else {
            // 若 <State> 后没有 <PdfClass>, <ForwardPdfClass> , 则添加 kNoPdf 的 HmmState
            this_entry.push_back(HmmState(forward_pdf_class));
          }
          while (token == "<Transition>") {
            int32 dst_state;
            BaseFloat trans_prob;
            ReadBasicType(is, binary, &dst_state);
            ReadBasicType(is, binary, &trans_prob);
            // 获取最后一个 HmmState 状态 并将 transitions 中配置 状态切换的概率
            this_entry.back().transitions.push_back(std::make_pair(dst_state, trans_prob));
            ReadToken(is, binary, &token);
          }
          if(token == "<Final>") // TODO: remove this clause after a while.
            KALDI_ERR << "You are trying to read old-format topology with new Kaldi.";
          if (token != "</State>")
            KALDI_ERR << "Reading HmmTopology,  unexpected token "<<token;
          ReadToken(is, binary, &token);
        }
        int32 my_index = entries_.size();
        // entries_ 中为 TopologyEntry 列表
        entries_.push_back(this_entry);

        // 注意: 这里将所有涉及的 phones 通过 phone2idx 数组将 phone 与 my_index 进行对应起来,这样获取 phone 的状态相关的 HmmState
        for (size_t i = 0; i < phones.size(); i++) {
          int32 phone = phones[i];
          if (static_cast<int32>(phone2idx_.size()) <= phone)
            phone2idx_.resize(phone+1, -1);  // -1 is invalid index.
          KALDI_ASSERT(phone > 0);
          if (phone2idx_[phone] != -1)
            KALDI_ERR << "Phone with index "<<(i)<<" appears in multiple topology entries.";
          // 这里将每个 phone 相关的 HmmState 切换过程TopologyEntry的 index 设置到 phone2idx 中
          phone2idx_[phone] = my_index;
          phones_.push_back(phone);
        }
目录
相关文章
|
30天前
|
存储 机器学习/深度学习 人工智能
【LangChain系列】第一篇:文档加载简介及实践
【5月更文挑战第14天】 LangChain提供80多种文档加载器,简化了从PDF、网站、YouTube视频和Notion等多来源加载与标准化数据的过程。这些加载器将不同格式的数据转化为标准文档对象,便于机器学习工作流程中的数据处理。文中介绍了非结构化、专有和结构化数据的加载示例,包括PDF、YouTube视频、网站和Notion数据库的加载方法。通过LangChain,用户能轻松集成和交互各类数据源,加速智能应用的开发。
106 1
|
1月前
|
JavaScript Java 测试技术
基于Java的智能训练管理平台的设计与实现(源码+lw+部署文档+讲解等)
基于Java的智能训练管理平台的设计与实现(源码+lw+部署文档+讲解等)
27 0
|
1月前
|
JavaScript Java 测试技术
基于Java的在线学习系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的在线学习系统的设计与实现(源码+lw+部署文档+讲解等)
36 0
|
11月前
|
网络架构 内存技术
OpenPose原理解析
Openpose论文原理总结
241 0
|
分布式计算 算法 搜索推荐
Java编写的Spark ALS协同过滤推荐算法的源代码能共享一下
Java编写的Spark ALS协同过滤推荐算法的源代码能共享一下
|
数据采集 PyTorch 数据处理
【菜菜的CV进阶之路-Pytorch基础-数据处理】自定义数据集加载及预处理
【菜菜的CV进阶之路-Pytorch基础-数据处理】自定义数据集加载及预处理
169 0
|
机器学习/深度学习 数据采集 算法
机器学习中令你事半功倍的pipeline处理机制​
机器学习中令你事半功倍的pipeline处理机制​
机器学习中令你事半功倍的pipeline处理机制​
kaldi 源码分析(十) - gmm-init-mono.c分析
一直没有搞明白 hmm-gmm 之间是通过什么联系起来的,花了些时间查代码,看到最直观联系的就是 gmm-init-mono 工具。 gmm-init-mono 基础类 通过上述看到,主要的配置都是 在 topo 文件中, 这里需要将一些常...
1881 0
|
算法 语音技术
kaldi 源码分析(七) - HCLG 分析
Kaldi 语音识别主流程: 语音识别过程 解码网络使用 HCLG.fst 的方式, 它由 4 个 fst 经过一系列算法组合而成。分别是 H.fst、C.fst、L.fst 和 G.fst 4 个 fst 文件: 1. G:语言模型,输入输出类型相同,实际是一个WFSA(acceptor接受机),为了方便与其它三个WFST进行操作,将其视为一个输入输出相同的WFST。
5251 0

热门文章

最新文章