All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
align-equal.cc
Go to the documentation of this file.
1 // bin/align-equal.cc
2 
3 // Copyright 2009-2013 Microsoft Corporation
4 // 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 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "tree/context-dep.h"
24 #include "hmm/transition-model.h"
25 #include "fstext/fstext-lib.h"
27 
28 
31 int main(int argc, char *argv[]) {
32  try {
33  using namespace kaldi;
34  typedef kaldi::int32 int32;
35  using fst::SymbolTable;
36  using fst::VectorFst;
37  using fst::StdArc;
38 
39  const char *usage = "Write equally spaced alignments of utterances (to get training started)\n"
40  "Usage: align-equal <tree-in> <model-in> <lexicon-fst-in> <features-rspecifier> <transcriptions-rspecifier> <alignments-wspecifier>\n"
41  "e.g.: \n"
42  " align-equal 1.tree 1.mdl lex.fst scp:train.scp ark:train.tra ark:equal.ali\n";
43 
44  ParseOptions po(usage);
45  std::string disambig_rxfilename;
46  po.Register("read-disambig-syms", &disambig_rxfilename, "File containing "
47  "list of disambiguation symbols in phone symbol table");
48  po.Read(argc, argv);
49 
50  if (po.NumArgs() != 6) {
51  po.PrintUsage();
52  exit(1);
53  }
54 
55  std::string tree_in_filename = po.GetArg(1);
56  std::string model_in_filename = po.GetArg(2);
57  std::string lex_in_filename = po.GetArg(3);
58  std::string feature_rspecifier = po.GetArg(4);
59  std::string transcript_rspecifier = po.GetArg(5);
60  std::string alignment_wspecifier = po.GetArg(6);
61 
62  ContextDependency ctx_dep;
63  ReadKaldiObject(tree_in_filename, &ctx_dep);
64 
65  TransitionModel trans_model;
66  ReadKaldiObject(model_in_filename, &trans_model);
67 
68  // need VectorFst because we will change it by adding subseq symbol.
69  VectorFst<StdArc> *lex_fst = fst::ReadFstKaldi(lex_in_filename);
70 
71  TrainingGraphCompilerOptions gc_opts(1.0, true); // true -> Dan style graph.
72 
73  std::vector<int32> disambig_syms;
74  if (disambig_rxfilename != "")
75  if (!ReadIntegerVectorSimple(disambig_rxfilename, &disambig_syms))
76  KALDI_ERR << "fstcomposecontext: Could not read disambiguation symbols from "
77  << disambig_rxfilename;
78 
79  TrainingGraphCompiler gc(trans_model,
80  ctx_dep,
81  lex_fst,
82  disambig_syms,
83  gc_opts);
84 
85  lex_fst = NULL; // we gave ownership to gc.
86 
87 
88  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
89 
90  RandomAccessInt32VectorReader transcript_reader(transcript_rspecifier);
91 
92  Int32VectorWriter alignment_writer(alignment_wspecifier);
93 
94  int32 done = 0, no_transcript = 0, other_error = 0;
95  for (; !feature_reader.Done(); feature_reader.Next()) {
96  std::string key = feature_reader.Key();
97  if (transcript_reader.HasKey(key)) {
98  const std::vector<int32> &transcript = transcript_reader.Value(key);
99  int32 num_frames = feature_reader.Value().NumRows();
100  if (num_frames == 0) {
101  KALDI_WARN << "Zero-length utterance for key " << key;
102  other_error++;
103  continue;
104  }
105  VectorFst<StdArc> decode_fst;
106  if (!gc.CompileGraphFromText(transcript, &decode_fst)) {
107  KALDI_WARN << "Problem creating decoding graph for utterance "
108  << key <<" [serious error]";
109  other_error++;
110  continue;
111  }
112  VectorFst<StdArc> path;
113  int32 rand_seed = StringHasher()(key); // StringHasher() produces new anonymous
114  // object of type StringHasher; we then call operator () on it, with "key".
115  if (EqualAlign(decode_fst, num_frames, rand_seed, &path) ) {
116  std::vector<int32> aligned_seq, words;
117  StdArc::Weight w;
118  GetLinearSymbolSequence(path, &aligned_seq, &words, &w);
119  KALDI_ASSERT(aligned_seq.size() == num_frames);
120  KALDI_ASSERT(words == transcript);
121  alignment_writer.Write(key, aligned_seq);
122  done++;
123  } else {
124  KALDI_WARN << "AlignEqual: did not align utterence " << key;
125  other_error++;
126  }
127  } else {
128  KALDI_WARN << "AlignEqual: no transcript for utterance " << key;
129  no_transcript++;
130  }
131  }
132  if (done != 0 && no_transcript == 0 && other_error == 0) {
133  KALDI_LOG << "Success: done " << done << " utterances.";
134  } else {
135  KALDI_WARN << "Computed " << done << " alignments; " << no_transcript
136  << " lacked transcripts, " << other_error
137  << " had other errors.";
138  }
139  if (done != 0) return 0;
140  else return 1;
141  } catch(const std::exception &e) {
142  std::cerr << e.what();
143  return -1;
144  }
145 }
146 
147 
int32 words[kMaxOrder]
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].
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
A hashing function object for strings.
Definition: stl-utils.h:262
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
int main(int argc, char *argv[])
Write equally spaced alignments of utterances (to get training started).
Definition: align-equal.cc:31
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
bool EqualAlign(const Fst< Arc > &ifst, typename Arc::StateId length, int rand_seed, MutableFst< Arc > *ofst, int num_retries)
EqualAlign is similar to RandGen, but it generates a sequence with exactly "length" input symbols...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
bool CompileGraphFromText(const std::vector< int32 > &transcript, fst::VectorFst< fst::StdArc > *out_fst)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
bool HasKey(const std::string &key)
bool GetLinearSymbolSequence(const Fst< Arc > &fst, vector< I > *isymbols_out, vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_LOG
Definition: kaldi-error.h:133
bool ReadIntegerVectorSimple(std::string rxfilename, std::vector< int32 > *list)
ReadFromList attempts to read this list of integers, one per line, from the given file...
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.