gmm-rescore-lattice.cc
Go to the documentation of this file.
1 // gmmbin/gmm-rescore-lattice.cc
2 
3 // Copyright 2009-2011 Saarland University (Author: Arnab Ghoshal)
4 // Cisco Systems (Author: Neha Agrawal)
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 "util/stl-utils.h"
24 #include "gmm/am-diag-gmm.h"
25 #include "hmm/transition-model.h"
26 #include "fstext/fstext-lib.h"
27 #include "lat/kaldi-lattice.h"
28 #include "lat/lattice-functions.h"
30 
31 int main(int argc, char *argv[]) {
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
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: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...
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
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)
std::istream & Stream()
Definition: kaldi-io.cc:826
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)
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
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:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int main(int argc, char *argv[])
bool HasKey(const std::string &key)
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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