convert-ali.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "hmm/hmm-utils.h"
#include "hmm/tree-accu.h"
Include dependency graph for convert-ali.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 29 of file convert-ali.cc.

References kaldi::ConvertAlignment(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), TransitionModel::GetTopo(), KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), kaldi::ReadPhoneMap(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

29  {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  try {
33  const char *usage =
34  "Convert alignments from one decision-tree/model to another\n"
35  "Usage: convert-ali [options] <old-model> <new-model> <new-tree> "
36  "<old-alignments-rspecifier> <new-alignments-wspecifier>\n"
37  "e.g.: \n"
38  " convert-ali old/final.mdl new/0.mdl new/tree ark:old/ali.1 ark:new/ali.1\n";
39 
40  int32 frame_subsampling_factor = 1;
41  bool reorder = true;
42  bool repeat_frames = false;
43 
44  std::string phone_map_rxfilename;
45  ParseOptions po(usage);
46  po.Register("phone-map", &phone_map_rxfilename,
47  "File name containing old->new phone mapping (each line is: "
48  "old-integer-id new-integer-id)");
49  po.Register("reorder", &reorder,
50  "True if you want the converted alignments to be 'reordered' "
51  "versus the way they appear in the HmmTopology object");
52  po.Register("repeat-frames", &repeat_frames,
53  "Only relevant when frame-subsampling-factor != 1. If true, "
54  "repeat frames of alignment by 'frame-subsampling-factor' "
55  "after alignment conversion, to keep the alignment the same "
56  "length as the input alignment.");
57  po.Register("frame-subsampling-factor", &frame_subsampling_factor,
58  "Can be used in converting alignments to reduced frame rates.");
59 
60  po.Read(argc, argv);
61 
62  if (po.NumArgs() != 5) {
63  po.PrintUsage();
64  exit(1);
65  }
66 
67  std::string old_model_filename = po.GetArg(1);
68  std::string new_model_filename = po.GetArg(2);
69  std::string new_tree_filename = po.GetArg(3);
70  std::string old_alignments_rspecifier = po.GetArg(4);
71  std::string new_alignments_wspecifier = po.GetArg(5);
72 
73  std::vector<int32> phone_map;
74  if (phone_map_rxfilename != "") { // read phone map.
75  ReadPhoneMap(phone_map_rxfilename,
76  &phone_map);
77  }
78 
79  SequentialInt32VectorReader alignment_reader(old_alignments_rspecifier);
80  Int32VectorWriter alignment_writer(new_alignments_wspecifier);
81 
82  TransitionModel old_trans_model;
83  ReadKaldiObject(old_model_filename, &old_trans_model);
84 
85  TransitionModel new_trans_model;
86  ReadKaldiObject(new_model_filename, &new_trans_model);
87 
88  if (!(old_trans_model.GetTopo() == new_trans_model.GetTopo()))
89  KALDI_WARN << "Toplogies of models are not equal: "
90  << "conversion may not be correct or may fail.";
91 
92 
93  ContextDependency new_ctx_dep; // the tree.
94  ReadKaldiObject(new_tree_filename, &new_ctx_dep);
95 
96  int num_success = 0, num_fail = 0;
97 
98  for (; !alignment_reader.Done(); alignment_reader.Next()) {
99  std::string key = alignment_reader.Key();
100  const std::vector<int32> &old_alignment = alignment_reader.Value();
101  std::vector<int32> new_alignment;
102  if (ConvertAlignment(old_trans_model,
103  new_trans_model,
104  new_ctx_dep,
105  old_alignment,
106  frame_subsampling_factor,
107  repeat_frames,
108  reorder,
109  (phone_map_rxfilename != "" ? &phone_map : NULL),
110  &new_alignment)) {
111  alignment_writer.Write(key, new_alignment);
112  num_success++;
113  } else {
114  KALDI_WARN << "Could not convert alignment for key " << key
115  <<" (possibly truncated alignment?)";
116  num_fail++;
117  }
118  }
119 
120  KALDI_LOG << "Succeeded converting alignments for " << num_success
121  << " files, failed for " << num_fail;
122 
123  if (num_success != 0) return 0;
124  else return 1;
125  } catch(const std::exception &e) {
126  std::cerr << e.what();
127  return -1;
128  }
129 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
void ReadPhoneMap(std::string phone_map_rxfilename, std::vector< int32 > *phone_map)
Definition: tree-accu.cc:106
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const HmmTopology & GetTopo() const
return reference to HMM-topology object.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_LOG
Definition: kaldi-error.h:153
bool ConvertAlignment(const TransitionModel &old_trans_model, const TransitionModel &new_trans_model, const ContextDependencyInterface &new_ctx_dep, const std::vector< int32 > &old_alignment, int32 subsample_factor, bool repeat_frames, bool new_is_reordered, const std::vector< int32 > *phone_map, std::vector< int32 > *new_alignment)
ConvertAlignment converts an alignment that was created using one model, to another model...
Definition: hmm-utils.cc:1013