lattice-rescore-mapped.cc File Reference
Include dependency graph for lattice-rescore-mapped.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

void LatticeAcousticRescore (const TransitionModel &trans_model, const Matrix< BaseFloat > &log_likes, const std::vector< int32 > &state_times, Lattice *lat)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 74 of file lattice-rescore-mapped.cc.

References fst::AcousticLatticeScale(), fst::ConvertLattice(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::LatticeAcousticRescore(), kaldi::LatticeStateTimes(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), TransitionModel::Read(), ParseOptions::Register(), fst::ScaleLattice(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

74  {
75  try {
76  using namespace kaldi;
77  typedef kaldi::int32 int32;
78  typedef kaldi::int64 int64;
79  using fst::SymbolTable;
80  using fst::VectorFst;
81  using fst::StdArc;
82 
83  const char *usage =
84  "Replace the acoustic scores on a lattice using log-likelihoods read in\n"
85  "as a matrix for each utterance, indexed (frame, pdf-id). This does the same\n"
86  "as (e.g.) gmm-rescore-lattice, but from a matrix. The \"mapped\" means that\n"
87  "the transition-model is used to map transition-ids to pdf-ids. (c.f.\n"
88  "latgen-faster-mapped). Note: <transition-model-in> can be any type of\n"
89  "model file, e.g. GMM-based or neural-net based; only the transition model is read.\n"
90  "\n"
91  "Usage: lattice-rescore-mapped [options] <transition-model-in> <lattice-rspecifier> "
92  "<loglikes-rspecifier> <lattice-wspecifier>\n"
93  " e.g.: nnet-logprob [args] .. | lattice-rescore-mapped final.mdl ark:1.lats ark:- ark:2.lats\n";
94 
95  kaldi::BaseFloat old_acoustic_scale = 0.0;
96  kaldi::ParseOptions po(usage);
97  po.Register("old-acoustic-scale", &old_acoustic_scale,
98  "Add in the scores in the input lattices with this scale, rather "
99  "than discarding them.");
100  po.Read(argc, argv);
101 
102  if (po.NumArgs() != 4) {
103  po.PrintUsage();
104  exit(1);
105  }
106 
107  std::string model_filename = po.GetArg(1),
108  lats_rspecifier = po.GetArg(2),
109  loglike_rspecifier = po.GetArg(3),
110  lats_wspecifier = po.GetArg(4);
111 
112  TransitionModel trans_model;
113  {
114  bool binary;
115  Input ki(model_filename, &binary);
116  trans_model.Read(ki.Stream(), binary);
117  // Ignore what follows it in the model.
118  }
119 
120  RandomAccessBaseFloatMatrixReader loglike_reader(loglike_rspecifier);
121  // Read as regular lattice
122  SequentialLatticeReader lattice_reader(lats_rspecifier);
123  // Write as compact lattice.
124  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
125 
126  int32 num_done = 0, num_err = 0;
127  int64 num_frames = 0;
128  for (; !lattice_reader.Done(); lattice_reader.Next()) {
129  std::string key = lattice_reader.Key();
130  if (!loglike_reader.HasKey(key)) {
131  KALDI_WARN << "No log-likes found for utterance " << key << ". Skipping";
132  num_err++;
133  continue;
134  }
135 
136  Lattice lat = lattice_reader.Value();
137  lattice_reader.FreeCurrent();
138  if (old_acoustic_scale != 1.0)
139  fst::ScaleLattice(fst::AcousticLatticeScale(old_acoustic_scale), &lat);
140 
141  kaldi::uint64 props = lat.Properties(fst::kFstProperties, false);
142  if (!(props & fst::kTopSorted)) {
143  if (fst::TopSort(&lat) == false)
144  KALDI_ERR << "Cycles detected in lattice.";
145  }
146 
147  std::vector<int32> state_times;
148  int32 max_time = kaldi::LatticeStateTimes(lat, &state_times);
149  const Matrix<BaseFloat> &log_likes = loglike_reader.Value(key);
150  if (log_likes.NumRows() != max_time) {
151  KALDI_WARN << "Skipping utterance " << key << " since number of time "
152  << "frames in lattice ("<< max_time << ") differ from "
153  << "number of frames in log-likelihoods (" << log_likes.NumRows() << ").";
154  num_err++;
155  continue;
156  }
157 
158  kaldi::LatticeAcousticRescore(trans_model, log_likes, state_times,
159  &lat);
160  CompactLattice clat_out;
161  ConvertLattice(lat, &clat_out);
162  compact_lattice_writer.Write(key, clat_out);
163  num_done++;
164  num_frames += log_likes.NumRows();
165  }
166 
167  KALDI_LOG << "Done " << num_done << " lattices, " << num_err
168  << " with errors, #frames is " << num_frames;
169  return (num_done != 0 ? 0 : 1);
170  } catch(const std::exception &e) {
171  std::cerr << e.what();
172  return -1;
173  }
174 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 LatticeStateTimes(const Lattice &lat, vector< int32 > *times)
This function iterates over the states of a topologically sorted lattice and counts the time instance...
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void LatticeAcousticRescore(const TransitionModel &trans_model, const Matrix< BaseFloat > &log_likes, const std::vector< int32 > &state_times, Lattice *lat)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
void Read(std::istream &is, bool binary)
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_LOG
Definition: kaldi-error.h:153