33 const std::vector<int32> &state_times,
35 kaldi::uint64 props = lat->Properties(fst::kFstProperties,
false);
36 if (!(props & fst::kTopSorted))
37 KALDI_ERR <<
"Input lattice must be topologically sorted.";
40 std::vector<std::vector<int32> > time_to_state(log_likes.
NumRows());
41 for (
size_t i = 0;
i < state_times.size();
i++) {
43 if (state_times[
i] < log_likes.
NumRows())
44 time_to_state[state_times[
i]].push_back(
i);
47 &&
"There appears to be lattice/feature mismatch.");
51 for (
size_t i = 0;
i < time_to_state[t].size();
i++) {
52 int32 state = time_to_state[t][
i];
53 for (fst::MutableArcIterator<Lattice> aiter(lat, state); !aiter.Done();
56 int32 trans_id = arc.ilabel;
59 if (pdf_id > log_likes.
NumCols())
60 KALDI_ERR <<
"Pdf-id " << pdf_id <<
" is out of the range of " 61 <<
"input log-likelihoods " << log_likes.
NumCols()
62 <<
" (probably some kind of mismatch).";
64 arc.weight.SetValue2(-ll + arc.weight.Value2());
74 int main(
int argc,
char *argv[]) {
76 using namespace kaldi;
78 typedef kaldi::int64 int64;
79 using fst::SymbolTable;
84 "Replace the acoustic scores on a lattice using log-likelihoods read in\n" 85 "as a matrix for each utterance, indexed (frame, pdf-id). This does the same\n" 86 "as (e.g.) gmm-rescore-lattice, but from a matrix. The \"mapped\" means that\n" 87 "the transition-model is used to map transition-ids to pdf-ids. (c.f.\n" 88 "latgen-faster-mapped). Note: <transition-model-in> can be any type of\n" 89 "model file, e.g. GMM-based or neural-net based; only the transition model is read.\n" 91 "Usage: lattice-rescore-mapped [options] <transition-model-in> <lattice-rspecifier> " 92 "<loglikes-rspecifier> <lattice-wspecifier>\n" 93 " e.g.: nnet-logprob [args] .. | lattice-rescore-mapped final.mdl ark:1.lats ark:- ark:2.lats\n";
97 po.
Register(
"old-acoustic-scale", &old_acoustic_scale,
98 "Add in the scores in the input lattices with this scale, rather " 99 "than discarding them.");
107 std::string model_filename = po.
GetArg(1),
108 lats_rspecifier = po.
GetArg(2),
109 loglike_rspecifier = po.
GetArg(3),
110 lats_wspecifier = po.
GetArg(4);
115 Input ki(model_filename, &binary);
126 int32 num_done = 0, num_err = 0;
127 int64 num_frames = 0;
128 for (; !lattice_reader.
Done(); lattice_reader.
Next()) {
129 std::string key = lattice_reader.
Key();
130 if (!loglike_reader.
HasKey(key)) {
131 KALDI_WARN <<
"No log-likes found for utterance " << key <<
". Skipping";
138 if (old_acoustic_scale != 1.0)
141 kaldi::uint64 props = lat.Properties(fst::kFstProperties,
false);
142 if (!(props & fst::kTopSorted)) {
143 if (fst::TopSort(&lat) ==
false)
144 KALDI_ERR <<
"Cycles detected in lattice.";
147 std::vector<int32> state_times;
150 if (log_likes.
NumRows() != max_time) {
151 KALDI_WARN <<
"Skipping utterance " << key <<
" since number of time " 152 <<
"frames in lattice ("<< max_time <<
") differ from " 153 <<
"number of frames in log-likelihoods (" << log_likes.
NumRows() <<
").";
162 compact_lattice_writer.
Write(key, clat_out);
164 num_frames += log_likes.
NumRows();
167 KALDI_LOG <<
"Done " << num_done <<
" lattices, " << num_err
168 <<
" with errors, #frames is " << num_frames;
169 return (num_done != 0 ? 0 : 1);
170 }
catch(
const std::exception &e) {
171 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
fst::ArcTpl< LatticeWeight > LatticeArc
int32 LatticeStateTimes(const Lattice &lat, vector< int32 > *times)
This function iterates over the states of a topologically sorted lattice and counts the time instance...
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
void LatticeAcousticRescore(const TransitionModel &trans_model, const Matrix< BaseFloat > &log_likes, const std::vector< int32 > &state_times, Lattice *lat)
int32 TransitionIdToPdf(int32 trans_id) const
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...
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
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)
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
fst::VectorFst< LatticeArc > Lattice
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
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)
int main(int argc, char *argv[])
fst::VectorFst< CompactLatticeArc > CompactLattice
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).