sgmm2-rescore-lattice.cc File Reference
Include dependency graph for sgmm2-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 32 of file sgmm2-rescore-lattice.cc.

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

32  {
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
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
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...
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)
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
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_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
void SetSpeakerVector(const Vector< BaseFloat > &v_s_in)
Definition: am-sgmm2.h:180
#define KALDI_LOG
Definition: kaldi-error.h:153