sgmm2-rescore-lattice.cc
Go to the documentation of this file.
1 // sgmm2bin/sgmm2-rescore-lattice.cc
2 
3 // Copyright 2009-2012 Saarland University (Author: Arnab Ghoshal)
4 // Johns Hopkins University (Author: Daniel Povey)
5 // Cisco Systems (Author: Neha Agrawal)
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "util/stl-utils.h"
25 #include "sgmm2/am-sgmm2.h"
26 #include "hmm/transition-model.h"
27 #include "fstext/fstext-lib.h"
28 #include "lat/kaldi-lattice.h"
29 #include "lat/lattice-functions.h"
31 
32 int main(int argc, char *argv[]) {
33  try {
34  using namespace kaldi;
35  typedef kaldi::int32 int32;
36  typedef kaldi::int64 int64;
37  using fst::SymbolTable;
38  using fst::VectorFst;
39  using fst::StdArc;
40 
41  const char *usage =
42  "Replace the acoustic scores on a lattice using a new model.\n"
43  "Usage: sgmm2-rescore-lattice [options] <model-in> <lattice-rspecifier> "
44  "<feature-rspecifier> <lattice-wspecifier>\n"
45  " e.g.: sgmm2-rescore-lattice 1.mdl ark:1.lats scp:trn.scp ark:2.lats\n";
46 
47  kaldi::BaseFloat old_acoustic_scale = 0.0;
48  bool speedup = false;
49  BaseFloat log_prune = 5.0;
50  std::string gselect_rspecifier, spkvecs_rspecifier, utt2spk_rspecifier;
51 
52  kaldi::ParseOptions po(usage);
53  po.Register("old-acoustic-scale", &old_acoustic_scale,
54  "Add the current acoustic scores with some scale.");
55  po.Register("log-prune", &log_prune,
56  "Pruning beam used to reduce number of exp() evaluations.");
57  po.Register("spk-vecs", &spkvecs_rspecifier, "Speaker vectors (rspecifier)");
58  po.Register("utt2spk", &utt2spk_rspecifier,
59  "rspecifier for utterance to speaker map");
60  po.Register("gselect", &gselect_rspecifier,
61  "Precomputed Gaussian indices (rspecifier)");
62  po.Register("speedup", &speedup,
63  "If true, enable a faster version of the computation that "
64  "saves times when there is only one pdf-id on a single frame "
65  "by only sometimes (randomly) computing the probabilities, and "
66  "then scaling them up to preserve corpus-level diagnostics.");
67 
68 
69  po.Read(argc, argv);
70 
71  if (po.NumArgs() != 4) {
72  po.PrintUsage();
73  exit(1);
74  }
75  if (gselect_rspecifier == "")
76  KALDI_ERR << "--gselect-rspecifier option is required.";
77 
78  std::string model_filename = po.GetArg(1),
79  lats_rspecifier = po.GetArg(2),
80  feature_rspecifier = po.GetArg(3),
81  lats_wspecifier = po.GetArg(4);
82 
83  AmSgmm2 am_sgmm;
84  TransitionModel trans_model;
85  {
86  bool binary;
87  Input ki(model_filename, &binary);
88  trans_model.Read(ki.Stream(), binary);
89  am_sgmm.Read(ki.Stream(), binary);
90  }
91 
92  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
93  RandomAccessBaseFloatVectorReaderMapped spkvecs_reader(spkvecs_rspecifier,
94  utt2spk_rspecifier);
95  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
96  // Read as compact lattice
97  SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
98  // Write as compact lattice.
99  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
100 
101  int32 num_done = 0, num_err = 0;
102  for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
103  std::string utt = compact_lattice_reader.Key();
104  if (!feature_reader.HasKey(utt)) {
105  KALDI_WARN << "No feature found for utterance " << utt;
106  num_err++;
107  continue;
108  }
109 
110  CompactLattice clat = compact_lattice_reader.Value();
111  compact_lattice_reader.FreeCurrent();
112  if (old_acoustic_scale != 1.0)
113  fst::ScaleLattice(fst::AcousticLatticeScale(old_acoustic_scale), &clat);
114 
115  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
116 
117  // Get speaker vectors
118  Sgmm2PerSpkDerivedVars spk_vars;
119  if (spkvecs_reader.IsOpen()) {
120  if (spkvecs_reader.HasKey(utt)) {
121  spk_vars.SetSpeakerVector(spkvecs_reader.Value(utt));
122  am_sgmm.ComputePerSpkDerivedVars(&spk_vars);
123  } else {
124  KALDI_WARN << "Cannot find speaker vector for " << utt;
125  num_err++;
126  continue;
127  }
128  } // else spk_vars is "empty"
129 
130  if (!gselect_reader.HasKey(utt) ||
131  gselect_reader.Value(utt).size() != feats.NumRows()) {
132  KALDI_WARN << "No Gaussian-selection info available for utterance "
133  << utt << " (or wrong size)";
134  num_err++;
135  continue;
136  }
137  const std::vector<std::vector<int32> > &gselect =
138  gselect_reader.Value(utt);
139 
140  DecodableAmSgmm2 sgmm2_decodable(am_sgmm, trans_model, feats,
141  gselect, log_prune, &spk_vars);
142 
143  if (!speedup) {
144  if (kaldi::RescoreCompactLattice(&sgmm2_decodable, &clat)) {
145  compact_lattice_writer.Write(utt, clat);
146  num_done++;
147  } else num_err++;
148  } else {
149  BaseFloat speedup_factor = 100.0;
150  if (kaldi::RescoreCompactLatticeSpeedup(trans_model, speedup_factor,
151  &sgmm2_decodable,
152  &clat)) {
153  compact_lattice_writer.Write(utt, clat);
154  num_done++;
155  } else num_err++;
156  }
157  }
158 
159  KALDI_LOG << "Done " << num_done << " lattices, errors on "
160  << num_err;
161  return (num_done != 0 ? 0 : 1);
162  } catch(const std::exception &e) {
163  std::cerr << e.what();
164  return -1;
165  }
166 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Class for definition of the subspace Gmm acoustic model.
Definition: am-sgmm2.h:231
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
This class is for when you are reading something in random access, but it may actually be stored per-...
Definition: kaldi-table.h:432
int main(int argc, char *argv[])
void Read(std::istream &is, bool binary)
Definition: am-sgmm2.cc:89
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 RescoreCompactLatticeSpeedup(const TransitionModel &tmodel, BaseFloat speedup_factor, DecodableInterface *decodable, CompactLattice *clat)
This function is like RescoreCompactLattice, but it is modified to avoid computing probabilities on m...
void ComputePerSpkDerivedVars(Sgmm2PerSpkDerivedVars *vars) const
Computes the per-speaker derived vars; assumes vars->v_s is already set up.
Definition: am-sgmm2.cc:1369
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
float BaseFloat
Definition: kaldi-types.h:29
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_ERR
Definition: kaldi-error.h:147
#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.
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
const T & Value(const std::string &key)
void SetSpeakerVector(const Vector< BaseFloat > &v_s_in)
Definition: am-sgmm2.h:180
#define KALDI_LOG
Definition: kaldi-error.h:153