All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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

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 
43  std::string phone_map_rxfilename;
44  ParseOptions po(usage);
45  po.Register("phone-map", &phone_map_rxfilename,
46  "File name containing old->new phone mapping (each line is: "
47  "old-integer-id new-integer-id)");
48  po.Register("reorder", &reorder,
49  "True if you want the converted alignments to be 'reordered' "
50  "versus the way they appear in the HmmTopology object");
51  po.Register("frame-subsampling-factor", &frame_subsampling_factor,
52  "Can be used in converting alignments to reduced frame rates.");
53 
54  po.Read(argc, argv);
55 
56  if (po.NumArgs() != 5) {
57  po.PrintUsage();
58  exit(1);
59  }
60 
61  std::string old_model_filename = po.GetArg(1);
62  std::string new_model_filename = po.GetArg(2);
63  std::string new_tree_filename = po.GetArg(3);
64  std::string old_alignments_rspecifier = po.GetArg(4);
65  std::string new_alignments_wspecifier = po.GetArg(5);
66 
67  std::vector<int32> phone_map;
68  if (phone_map_rxfilename != "") { // read phone map.
69  ReadPhoneMap(phone_map_rxfilename,
70  &phone_map);
71  }
72 
73  SequentialInt32VectorReader alignment_reader(old_alignments_rspecifier);
74  Int32VectorWriter alignment_writer(new_alignments_wspecifier);
75 
76  TransitionModel old_trans_model;
77  ReadKaldiObject(old_model_filename, &old_trans_model);
78 
79  TransitionModel new_trans_model;
80  ReadKaldiObject(new_model_filename, &new_trans_model);
81 
82  if (!(old_trans_model.GetTopo() == new_trans_model.GetTopo()))
83  KALDI_WARN << "Toplogies of models are not equal: "
84  << "conversion may not be correct or may fail.";
85 
86 
87  ContextDependency new_ctx_dep; // the tree.
88  ReadKaldiObject(new_tree_filename, &new_ctx_dep);
89 
90  int num_success = 0, num_fail = 0;
91 
92  for (; !alignment_reader.Done(); alignment_reader.Next()) {
93  std::string key = alignment_reader.Key();
94  const std::vector<int32> &old_alignment = alignment_reader.Value();
95  std::vector<int32> new_alignment;
96  if (ConvertAlignment(old_trans_model,
97  new_trans_model,
98  new_ctx_dep,
99  old_alignment,
100  frame_subsampling_factor,
101  reorder,
102  (phone_map_rxfilename != "" ? &phone_map : NULL),
103  &new_alignment)) {
104  alignment_writer.Write(key, new_alignment);
105  num_success++;
106  } else {
107  KALDI_WARN << "Could not convert alignment for key " << key
108  <<" (possibly truncated alignment?)";
109  num_fail++;
110  }
111  }
112 
113  KALDI_LOG << "Succeeded converting alignments for " << num_success
114  << " files, failed for " << num_fail;
115 
116  if (num_success != 0) return 0;
117  else return 1;
118  } catch(const std::exception &e) {
119  std::cerr << e.what();
120  return -1;
121  }
122 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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
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:130
const HmmTopology & GetTopo() const
return reference to HMM-topology object.
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 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:853
#define KALDI_LOG
Definition: kaldi-error.h:133