All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
convert-ali.cc
Go to the documentation of this file.
1 // bin/convert-ali.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2013 Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "gmm/am-diag-gmm.h"
25 #include "hmm/transition-model.h"
26 #include "hmm/hmm-utils.h"
27 #include "hmm/tree-accu.h" // for ReadPhoneMap
28 
29 int main(int argc, char *argv[]) {
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
void Write(const std::string &key, const T &value) const
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#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
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_LOG
Definition: kaldi-error.h:133
int main(int argc, char *argv[])
Definition: convert-ali.cc:29
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.