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  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 }
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)
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:967
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.
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.