29 int main(
int argc,
char *argv[]) {
32 using fst::SymbolTable;
35 using namespace kaldi;
38 "Do forward-backward and collect frame level MPE posteriors over\n" 39 "lattices, which can be fed into gmm-acc-stats2 to do MPE traning.\n" 40 "Caution: this is not really MPE, this is MPFE (minimum phone frame\n" 41 "error). The posteriors may be positive or negative.\n" 42 "Usage: lattice-to-mpe-post [options] <model> <num-posteriors-rspecifier>\n" 43 " <lats-rspecifier> <posteriors-wspecifier> \n" 44 "e.g.: lattice-to-mpe-post --acoustic-scale=0.1 1.mdl ark:num.post\n" 45 " ark:1.lats ark:1.post\n";
48 bool one_silence_class =
false;
49 std::string silence_phones_str;
51 po.
Register(
"acoustic-scale", &acoustic_scale,
52 "Scaling factor for acoustic likelihoods");
54 "Scaling factor for \"graph costs\" (including LM costs)");
55 po.
Register(
"one-silence-class", &one_silence_class,
"If true, newer " 56 "behavior which will tend to reduce insertions.");
57 po.
Register(
"silence-phones", &silence_phones_str,
58 "Colon-separated list of integer id's of silence phones, e.g. 46:47");
66 std::vector<int32> silence_phones;
68 KALDI_ERR <<
"Invalid silence-phones string " << silence_phones_str;
70 if (silence_phones.empty())
71 KALDI_WARN <<
"No silence phones specified, make sure this is what you intended.";
73 if (acoustic_scale == 0.0)
74 KALDI_ERR <<
"Do not use a zero acoustic scale (cannot be inverted)";
76 std::string model_filename = po.
GetArg(1),
77 alignments_rspecifier = po.
GetArg(2),
78 lats_rspecifier = po.
GetArg(3),
79 posteriors_wspecifier = po.
GetArg(4);
83 if (alignments_rspecifier.find(
"ali-to-post") != std::string::npos) {
84 KALDI_WARN <<
"Warning, this program has been changed to read alignments " 85 <<
"not posteriors. Remove ali-to-post from your scripts.";
92 Input ki(model_filename, &binary);
96 int32 num_done = 0, num_err = 0;
97 double total_lat_frame_acc = 0.0, lat_frame_acc;
98 double total_time = 0, lat_time;
101 for (; !lattice_reader.
Done(); lattice_reader.
Next()) {
102 std::string key = lattice_reader.
Key();
105 if (acoustic_scale != 1.0 || lm_scale != 1.0)
108 kaldi::uint64 props = lat.Properties(fst::kFstProperties,
false);
109 if (!(props & fst::kTopSorted)) {
110 if (fst::TopSort(&lat) ==
false)
111 KALDI_ERR <<
"Cycles detected in lattice.";
114 if (!alignments_reader.
HasKey(key)) {
115 KALDI_WARN <<
"No alignment for utterance " << key;
118 const std::vector<int32> &alignment = alignments_reader.
Value(key);
121 trans_model, silence_phones, lat, alignment,
122 "mpfe", one_silence_class, &post);
123 total_lat_frame_acc += lat_frame_acc;
124 lat_time = post.size();
125 total_time += lat_time;
126 KALDI_VLOG(2) <<
"Processed lattice for utterance: " << key <<
"; found " 127 << lat.NumStates() <<
" states and " <<
fst::NumArcs(lat)
128 <<
" arcs. Average frame accuracies = " << (lat_frame_acc/lat_time)
129 <<
" over " << lat_time <<
" frames.";
130 posterior_writer.
Write(key, post);
135 KALDI_LOG <<
"Overall average frame-accuracy is " 136 << (total_lat_frame_acc/total_time) <<
" over " << total_time
138 KALDI_LOG <<
"Done " << num_done <<
" lattices.";
139 return (num_done != 0 ? 0 : 1);
140 }
catch(
const std::exception &e) {
141 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...
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).
int main(int argc, char *argv[])
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.