29 int main(
int argc,
char *argv[]) {
32 using fst::SymbolTable;
35 using namespace kaldi;
38 "Do forward-backward and collect frame level posteriors for\n" 39 "the state-level minimum Bayes Risk criterion (SMBR), which\n" 40 "is like MPE with the criterion at a context-dependent state level.\n" 41 "The output may be fed into gmm-acc-stats2 or similar to train the\n" 42 "models discriminatively. The posteriors may be positive or negative.\n" 43 "Usage: lattice-to-smbr-post [options] <model> <num-posteriors-rspecifier>\n" 44 " <lats-rspecifier> <posteriors-wspecifier> \n" 45 "e.g.: lattice-to-smbr-post --acoustic-scale=0.1 1.mdl ark:num.post\n" 46 " ark:1.lats ark:1.post\n";
49 bool one_silence_class =
false;
50 std::string silence_phones_str;
52 po.
Register(
"acoustic-scale", &acoustic_scale,
53 "Scaling factor for acoustic likelihoods");
55 "Scaling factor for \"graph costs\" (including LM costs)");
56 po.
Register(
"one-silence-class", &one_silence_class,
"If true, newer " 57 "behavior which will tend to reduce insertions.");
58 po.
Register(
"silence-phones", &silence_phones_str,
"Colon-separated " 59 "list of integer id's of silence phones, e.g. 46:47");
67 std::vector<int32> silence_phones;
69 KALDI_ERR <<
"Invalid silence-phones string " << silence_phones_str;
71 if (silence_phones.empty())
72 KALDI_WARN <<
"No silence phones specified, make sure this is what you intended.";
74 if (acoustic_scale == 0.0)
75 KALDI_ERR <<
"Do not use a zero acoustic scale (cannot be inverted)";
77 std::string model_filename = po.
GetArg(1),
78 alignments_rspecifier = po.
GetArg(2),
79 lats_rspecifier = po.
GetArg(3),
80 posteriors_wspecifier = po.
GetArg(4);
84 if (alignments_rspecifier.find(
"ali-to-post") != std::string::npos) {
85 KALDI_WARN <<
"Warning, this program has been changed to read alignments " 86 <<
"not posteriors. Remove ali-to-post from your scripts.";
93 Input ki(model_filename, &binary);
97 int32 num_done = 0, num_err = 0;
98 double total_lat_frame_acc = 0.0, lat_frame_acc;
99 double total_time = 0, lat_time;
102 for (; !lattice_reader.
Done(); lattice_reader.
Next()) {
103 std::string key = lattice_reader.
Key();
106 if (acoustic_scale != 1.0 || lm_scale != 1.0)
109 kaldi::uint64 props = lat.Properties(fst::kFstProperties,
false);
110 if (!(props & fst::kTopSorted)) {
111 if (fst::TopSort(&lat) ==
false)
112 KALDI_ERR <<
"Cycles detected in lattice.";
115 if (!alignments_reader.
HasKey(key)) {
116 KALDI_WARN <<
"No alignment for utterance " << key;
119 const std::vector<int32> &alignment = alignments_reader.
Value(key);
122 trans_model, silence_phones, lat, alignment,
123 "smbr", one_silence_class, &post);
124 total_lat_frame_acc += lat_frame_acc;
125 lat_time = post.size();
126 total_time += lat_time;
127 KALDI_VLOG(2) <<
"Processed lattice for utterance: " << key <<
"; found " 128 << lat.NumStates() <<
" states and " <<
fst::NumArcs(lat)
129 <<
" arcs. Average frame accuracies = " << (lat_frame_acc/lat_time)
130 <<
" over " << lat_time <<
" frames.";
131 posterior_writer.
Write(key, post);
136 KALDI_LOG <<
"Overall average frame-accuracy is " 137 << (total_lat_frame_acc/total_time) <<
" over " << total_time
139 KALDI_LOG <<
"Done " << num_done <<
" lattices.";
140 return (num_done != 0 ? 0 : 1);
141 }
catch(
const std::exception &e) {
142 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
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 SortAndUniq(std::vector< T > *vec)
Sorts and uniq's (removes duplicates) from a vector.
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< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
int main(int argc, char *argv[])
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...
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
fst::VectorFst< LatticeArc > Lattice
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
BaseFloat LatticeForwardBackwardMpeVariants(const TransitionModel &trans, const std::vector< int32 > &silence_phones, const Lattice &lat, const std::vector< int32 > &num_ali, std::string criterion, bool one_silence_class, Posterior *post)
This function implements either the MPFE (minimum phone frame error) or SMBR (state-level minimum bay...
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 NumArgs() const
Number of positional parameters (c.f. argc-1).
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.