All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
align-equal-compiled.cc File Reference
Include dependency graph for align-equal-compiled.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 Write an equally spaced alignment (for getting training started). More...
 

Function Documentation

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

Write an equally spaced alignment (for getting training started).

Definition at line 31 of file align-equal-compiled.cc.

References SequentialTableReader< Holder >::Done(), fst::EqualAlign(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), fst::GetLinearSymbolSequence(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ASSERT, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), words, and TableWriter< Holder >::Write().

31  {
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 an equally spaced alignment (for getting training started)"
40  "Usage: align-equal-compiled <graphs-rspecifier> <features-rspecifier> <alignments-wspecifier>\n"
41  "e.g.: \n"
42  " align-equal-compiled 1.fsts scp:train.scp ark:equal.ali\n";
43 
44  ParseOptions po(usage);
45  bool binary = true;
46  po.Register("binary", &binary, "Write output in binary mode");
47  po.Read(argc, argv);
48 
49  if (po.NumArgs() != 3) {
50  po.PrintUsage();
51  exit(1);
52  }
53 
54  std::string
55  fst_rspecifier = po.GetArg(1),
56  feature_rspecifier = po.GetArg(2),
57  alignment_wspecifier = po.GetArg(3);
58 
59 
60  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_rspecifier);
61  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
62  Int32VectorWriter alignment_writer(alignment_wspecifier);
63 
64  int32 done = 0, no_feat = 0, error = 0;
65 
66  for (; !fst_reader.Done(); fst_reader.Next()) {
67  std::string key = fst_reader.Key();
68  if (!feature_reader.HasKey(key)) {
69  KALDI_WARN << "No features for utterance " << key;
70  no_feat++;
71  } else {
72  const Matrix<BaseFloat> &features = feature_reader.Value(key);
73  VectorFst<StdArc> decode_fst(fst_reader.Value());
74  fst_reader.FreeCurrent(); // this stops copy-on-write of the fst
75  // by deleting the fst inside the reader, since we're about to mutate
76  // the fst by adding transition probs.
77 
78  if (features.NumRows() == 0) {
79  KALDI_WARN << "Zero-length utterance: " << key;
80  error++;
81  continue;
82  }
83  if (decode_fst.Start() == fst::kNoStateId) {
84  KALDI_WARN << "Empty decoding graph for " << key;
85  error++;
86  continue;
87  }
88 
89  VectorFst<StdArc> path;
90  int32 rand_seed = StringHasher()(key); // StringHasher() produces new anonymous
91  // object of type StringHasher; we then call operator () on it, with "key".
92  if (EqualAlign(decode_fst, features.NumRows(), rand_seed, &path) ) {
93  std::vector<int32> aligned_seq, words;
95  GetLinearSymbolSequence(path, &aligned_seq, &words, &w);
96  KALDI_ASSERT(aligned_seq.size() == features.NumRows());
97  alignment_writer.Write(key, aligned_seq);
98  done++;
99  } else {
100  KALDI_WARN << "AlignEqual: did not align utterence " << key;
101  error++;
102  }
103  }
104  }
105 
106  if (done != 0 && no_feat == 0 && error == 0) {
107  KALDI_LOG << "Success: done " << done << " utterances.";
108  } else {
109  KALDI_WARN << "Computed " << done << " alignments; " << no_feat
110  << " lacked features, " << error
111  << " had other errors.";
112  }
113  if (done != 0) return 0;
114  else return 1;
115  } catch(const std::exception &e) {
116  std::cerr << e.what();
117  return -1;
118  }
119 }
int32 words[kMaxOrder]
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
A hashing function object for strings.
Definition: stl-utils.h:250
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
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
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.
fst::StdArc::Weight Weight
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_LOG
Definition: kaldi-error.h:133