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

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 31 of file gmm-rescore-lattice.cc.

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

31  {
32  try {
33  using namespace kaldi;
34  typedef kaldi::int32 int32;
35  typedef kaldi::int64 int64;
36  using fst::SymbolTable;
37  using fst::VectorFst;
38  using fst::StdArc;
39 
40  const char *usage =
41  "Replace the acoustic scores on a lattice using a new model.\n"
42  "Usage: gmm-rescore-lattice [options] <model-in> <lattice-rspecifier> "
43  "<feature-rspecifier> <lattice-wspecifier>\n"
44  " e.g.: gmm-rescore-lattice 1.mdl ark:1.lats scp:trn.scp ark:2.lats\n";
45 
46  kaldi::BaseFloat old_acoustic_scale = 0.0;
47  kaldi::ParseOptions po(usage);
48  po.Register("old-acoustic-scale", &old_acoustic_scale,
49  "Add in the scores in the input lattices with this scale, rather "
50  "than discarding them.");
51  po.Read(argc, argv);
52 
53  if (po.NumArgs() != 4) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58  std::string model_filename = po.GetArg(1),
59  lats_rspecifier = po.GetArg(2),
60  feature_rspecifier = po.GetArg(3),
61  lats_wspecifier = po.GetArg(4);
62 
63  AmDiagGmm am_gmm;
64  TransitionModel trans_model;
65  {
66  bool binary;
67  Input ki(model_filename, &binary);
68  trans_model.Read(ki.Stream(), binary);
69  am_gmm.Read(ki.Stream(), binary);
70  }
71 
72  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
73  // Read as regular lattice
74  SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
75  // Write as compact lattice.
76  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
77 
78  int32 num_done = 0, num_err = 0;
79  int64 num_frames = 0;
80  for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
81  std::string key = compact_lattice_reader.Key();
82  if (!feature_reader.HasKey(key)) {
83  KALDI_WARN << "No feature found for utterance " << key << ". Skipping";
84  num_err++;
85  continue;
86  }
87 
88  CompactLattice clat = compact_lattice_reader.Value();
89  compact_lattice_reader.FreeCurrent();
90  if (old_acoustic_scale != 1.0)
91  fst::ScaleLattice(fst::AcousticLatticeScale(old_acoustic_scale), &clat);
92 
93  const Matrix<BaseFloat> &feats = feature_reader.Value(key);
94 
95  DecodableAmDiagGmm gmm_decodable(am_gmm, trans_model, feats);
96  if (kaldi::RescoreCompactLattice(&gmm_decodable, &clat)) {
97  compact_lattice_writer.Write(key, clat);
98  num_done++;
99  num_frames += feats.NumRows();
100  } else num_err++;
101  }
102 
103  KALDI_LOG << "Done " << num_done << " lattices with errors on "
104  << num_err << ", #frames is " << num_frames;
105  return (num_done != 0 ? 0 : 1);
106  } catch(const std::exception &e) {
107  std::cerr << e.what();
108  return -1;
109  }
110 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
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
kaldi::int32 int32
bool RescoreCompactLattice(DecodableInterface *decodable, CompactLattice *clat)
This function *adds* the negated scores obtained from the Decodable object, to the acoustic scores on...
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)
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: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
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147