DecodeUtteranceLatticeFasterClass Class Reference

This class basically does the same job as the function DecodeUtteranceLatticeFaster, but in a way that allows us to build a multi-threaded command line program more easily. More...

#include <decoder-wrappers.h>

Collaboration diagram for DecodeUtteranceLatticeFasterClass:

Public Member Functions

 DecodeUtteranceLatticeFasterClass (LatticeFasterDecoder *decoder, DecodableInterface *decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, const std::string &utt, BaseFloat acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignments_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_sum, int64 *frame_sum, int32 *num_done, int32 *num_err, int32 *num_partial)
 
void operator() ()
 
 ~DecodeUtteranceLatticeFasterClass ()
 

Private Attributes

LatticeFasterDecoderdecoder_
 
DecodableInterfacedecodable_
 
const TransitionModeltrans_model_
 
const fst::SymbolTable * word_syms_
 
std::string utt_
 
BaseFloat acoustic_scale_
 
bool determinize_
 
bool allow_partial_
 
Int32VectorWriteralignments_writer_
 
Int32VectorWriterwords_writer_
 
CompactLatticeWritercompact_lattice_writer_
 
LatticeWriterlattice_writer_
 
double * like_sum_
 
int64 * frame_sum_
 
int32num_done_
 
int32num_err_
 
int32num_partial_
 
bool computed_
 
bool success_
 
bool partial_
 
CompactLatticeclat_
 
Latticelat_
 

Detailed Description

This class basically does the same job as the function DecodeUtteranceLatticeFaster, but in a way that allows us to build a multi-threaded command line program more easily.

The main computation takes place in operator (), and the output happens in the destructor.

Definition at line 142 of file decoder-wrappers.h.

Constructor & Destructor Documentation

◆ DecodeUtteranceLatticeFasterClass()

DecodeUtteranceLatticeFasterClass ( LatticeFasterDecoder decoder,
DecodableInterface decodable,
const TransitionModel trans_model,
const fst::SymbolTable *  word_syms,
const std::string &  utt,
BaseFloat  acoustic_scale,
bool  determinize,
bool  allow_partial,
Int32VectorWriter alignments_writer,
Int32VectorWriter words_writer,
CompactLatticeWriter compact_lattice_writer,
LatticeWriter lattice_writer,
double *  like_sum,
int64 *  frame_sum,
int32 num_done,
int32 num_err,
int32 num_partial 
)

Definition at line 32 of file decoder-wrappers.cc.

49  : // If partial decode (final-state not reached), increments this.
50  decoder_(decoder), decodable_(decodable), trans_model_(&trans_model),
51  word_syms_(word_syms), utt_(utt), acoustic_scale_(acoustic_scale),
52  determinize_(determinize), allow_partial_(allow_partial),
53  alignments_writer_(alignments_writer),
54  words_writer_(words_writer),
55  compact_lattice_writer_(compact_lattice_writer),
56  lattice_writer_(lattice_writer),
57  like_sum_(like_sum), frame_sum_(frame_sum),
58  num_done_(num_done), num_err_(num_err),
59  num_partial_(num_partial),
60  computed_(false), success_(false), partial_(false),
61  clat_(NULL), lat_(NULL) { }
CompactLatticeWriter * compact_lattice_writer_

◆ ~DecodeUtteranceLatticeFasterClass()

Definition at line 116 of file decoder-wrappers.cc.

References DecodeUtteranceLatticeFasterClass::alignments_writer_, DecodeUtteranceLatticeFasterClass::clat_, DecodeUtteranceLatticeFasterClass::compact_lattice_writer_, DecodeUtteranceLatticeFasterClass::computed_, DecodeUtteranceLatticeFasterClass::decodable_, DecodeUtteranceLatticeFasterClass::decoder_, DecodeUtteranceLatticeFasterClass::determinize_, DecodeUtteranceLatticeFasterClass::frame_sum_, LatticeFasterDecoderTpl< FST, Token >::GetBestPath(), fst::GetLinearSymbolSequence(), rnnlm::i, TableWriter< Holder >::IsOpen(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, DecodeUtteranceLatticeFasterClass::lat_, DecodeUtteranceLatticeFasterClass::lattice_writer_, DecodeUtteranceLatticeFasterClass::like_sum_, DecodeUtteranceLatticeFasterClass::num_done_, DecodeUtteranceLatticeFasterClass::num_err_, DecodeUtteranceLatticeFasterClass::num_partial_, DecodeUtteranceLatticeFasterClass::partial_, DecodeUtteranceLatticeFasterClass::success_, DecodeUtteranceLatticeFasterClass::utt_, LatticeWeightTpl< FloatType >::Value1(), LatticeWeightTpl< FloatType >::Value2(), DecodeUtteranceLatticeFasterClass::word_syms_, words, DecodeUtteranceLatticeFasterClass::words_writer_, and TableWriter< Holder >::Write().

116  {
117  if (!computed_)
118  KALDI_ERR << "Destructor called without operator (), error in calling code.";
119 
120  if (!success_) {
121  if (num_err_ != NULL) (*num_err_)++;
122  } else { // successful decode.
123  // Getting the one-best output is lightweight enough that we can do it in
124  // the destructor (easier than adding more variables to the class, and
125  // will rarely slow down the main thread.)
126  double likelihood;
127  LatticeWeight weight;
128  int32 num_frames;
129  { // First do some stuff with word-level traceback...
130  // This is basically for diagnostics.
131  fst::VectorFst<LatticeArc> decoded;
132  decoder_->GetBestPath(&decoded);
133  if (decoded.NumStates() == 0) {
134  // Shouldn't really reach this point as already checked success.
135  KALDI_ERR << "Failed to get traceback for utterance " << utt_;
136  }
137  std::vector<int32> alignment;
138  std::vector<int32> words;
139  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
140  num_frames = alignment.size();
141  if (words_writer_->IsOpen())
142  words_writer_->Write(utt_, words);
143  if (alignments_writer_->IsOpen())
144  alignments_writer_->Write(utt_, alignment);
145  if (word_syms_ != NULL) {
146  std::cerr << utt_ << ' ';
147  for (size_t i = 0; i < words.size(); i++) {
148  std::string s = word_syms_->Find(words[i]);
149  if (s == "")
150  KALDI_ERR << "Word-id " << words[i] << " not in symbol table.";
151  std::cerr << s << ' ';
152  }
153  std::cerr << '\n';
154  }
155  likelihood = -(weight.Value1() + weight.Value2());
156  }
157 
158  // Ouptut the lattices.
159  if (determinize_) { // CompactLattice output.
160  KALDI_ASSERT(compact_lattice_writer_ != NULL && clat_ != NULL);
161  if (clat_->NumStates() == 0) {
162  KALDI_WARN << "Empty lattice for utterance " << utt_;
163  } else {
165  }
166  delete clat_;
167  clat_ = NULL;
168  } else {
169  KALDI_ASSERT(lattice_writer_ != NULL && lat_ != NULL);
170  if (lat_->NumStates() == 0) {
171  KALDI_WARN << "Empty lattice for utterance " << utt_;
172  } else {
173  lattice_writer_->Write(utt_, *lat_);
174  }
175  delete lat_;
176  lat_ = NULL;
177  }
178 
179  // Print out logging information.
180  KALDI_LOG << "Log-like per frame for utterance " << utt_ << " is "
181  << (likelihood / num_frames) << " over "
182  << num_frames << " frames.";
183  KALDI_VLOG(2) << "Cost for utterance " << utt_ << " is "
184  << weight.Value1() << " + " << weight.Value2();
185 
186  // Now output the various diagnostic variables.
187  if (like_sum_ != NULL) *like_sum_ += likelihood;
188  if (frame_sum_ != NULL) *frame_sum_ += num_frames;
189  if (num_done_ != NULL) (*num_done_)++;
190  if (partial_ && num_partial_ != NULL) (*num_partial_)++;
191  }
192  // We were given ownership of these two objects that were passed in in
193  // the initializer.
194  delete decoder_;
195  delete decodable_;
196 }
int32 words[kMaxOrder]
CompactLatticeWriter * compact_lattice_writer_
kaldi::int32 int32
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void Write(const std::string &key, const T &value) const
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
Definition: kaldi-lattice.h:32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
bool GetBestPath(Lattice *ofst, bool use_final_probs=true) const
Outputs an FST corresponding to the single best path through the lattice.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
#define KALDI_LOG
Definition: kaldi-error.h:153

Member Function Documentation

◆ operator()()

void operator() ( )

Definition at line 64 of file decoder-wrappers.cc.

References DecodeUtteranceLatticeFasterClass::acoustic_scale_, fst::AcousticLatticeScale(), DecodeUtteranceLatticeFasterClass::allow_partial_, DecodeUtteranceLatticeFasterClass::clat_, DecodeUtteranceLatticeFasterClass::computed_, DecodeUtteranceLatticeFasterClass::decodable_, LatticeFasterDecoderTpl< FST, Token >::Decode(), DecodeUtteranceLatticeFasterClass::decoder_, LatticeFasterDecoderConfig::det_opts, DecodeUtteranceLatticeFasterClass::determinize_, fst::DeterminizeLatticePhonePrunedWrapper(), LatticeFasterDecoderTpl< FST, Token >::GetOptions(), LatticeFasterDecoderTpl< FST, Token >::GetRawLattice(), KALDI_ERR, KALDI_WARN, DecodeUtteranceLatticeFasterClass::lat_, LatticeFasterDecoderConfig::lattice_beam, DecodeUtteranceLatticeFasterClass::partial_, LatticeFasterDecoderTpl< FST, Token >::ReachedFinal(), fst::ScaleLattice(), DecodeUtteranceLatticeFasterClass::success_, DecodeUtteranceLatticeFasterClass::trans_model_, and DecodeUtteranceLatticeFasterClass::utt_.

64  {
65  // Decoding and lattice determinization happens here.
66  computed_ = true; // Just means this function was called-- a check on the
67  // calling code.
68  success_ = true;
69  using fst::VectorFst;
70  if (!decoder_->Decode(decodable_)) {
71  KALDI_WARN << "Failed to decode utterance with id " << utt_;
72  success_ = false;
73  }
74  if (!decoder_->ReachedFinal()) {
75  if (allow_partial_) {
76  KALDI_WARN << "Outputting partial output for utterance " << utt_
77  << " since no final-state reached\n";
78  partial_ = true;
79  } else {
80  KALDI_WARN << "Not producing output for utterance " << utt_
81  << " since no final-state reached and "
82  << "--allow-partial=false.\n";
83  success_ = false;
84  }
85  }
86  if (!success_) return;
87 
88  // Get lattice, and do determinization if requested.
89  lat_ = new Lattice;
91  if (lat_->NumStates() == 0)
92  KALDI_ERR << "Unexpected problem getting lattice for utterance " << utt_;
93  fst::Connect(lat_);
94  if (determinize_) {
95  clat_ = new CompactLattice;
97  *trans_model_,
98  lat_,
100  clat_,
102  KALDI_WARN << "Determinization finished earlier than the beam for "
103  << "utterance " << utt_;
104  delete lat_;
105  lat_ = NULL;
106  // We'll write the lattice without acoustic scaling.
107  if (acoustic_scale_ != 0.0)
109  } else {
110  // We'll write the lattice without acoustic scaling.
111  if (acoustic_scale_ != 0.0)
113  }
114 }
const LatticeFasterDecoderConfig & GetOptions() const
bool GetRawLattice(Lattice *ofst, bool use_final_probs=true) const
Outputs an FST corresponding to the raw, state-level tracebacks.
bool ReachedFinal() const
says whether a final-state was active on the last frame.
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
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...
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
fst::DeterminizeLatticePhonePrunedOptions det_opts
bool Decode(DecodableInterface *decodable)
Decodes until there are no more frames left in the "decodable" object.
bool DeterminizeLatticePhonePrunedWrapper(const kaldi::TransitionModel &trans_model, MutableFst< kaldi::LatticeArc > *ifst, double beam, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePhonePrunedOptions opts)
This function is a wrapper of DeterminizeLatticePhonePruned() that works for Lattice type FSTs...

Member Data Documentation

◆ acoustic_scale_

BaseFloat acoustic_scale_
private

Definition at line 174 of file decoder-wrappers.h.

Referenced by DecodeUtteranceLatticeFasterClass::operator()().

◆ alignments_writer_

Int32VectorWriter* alignments_writer_
private

◆ allow_partial_

bool allow_partial_
private

Definition at line 176 of file decoder-wrappers.h.

Referenced by DecodeUtteranceLatticeFasterClass::operator()().

◆ clat_

◆ compact_lattice_writer_

CompactLatticeWriter* compact_lattice_writer_
private

◆ computed_

◆ decodable_

◆ decoder_

◆ determinize_

◆ frame_sum_

int64* frame_sum_
private

◆ lat_

◆ lattice_writer_

LatticeWriter* lattice_writer_
private

◆ like_sum_

double* like_sum_
private

◆ num_done_

int32* num_done_
private

◆ num_err_

int32* num_err_
private

◆ num_partial_

int32* num_partial_
private

◆ partial_

◆ success_

◆ trans_model_

const TransitionModel* trans_model_
private

Definition at line 171 of file decoder-wrappers.h.

Referenced by DecodeUtteranceLatticeFasterClass::operator()().

◆ utt_

◆ word_syms_

const fst::SymbolTable* word_syms_
private

◆ words_writer_


The documentation for this class was generated from the following files: