29 using namespace kaldi;
32 "Convert state-level posteriors to Gaussian-level posteriors\n" 33 "Usage: gmm-post-to-gpost [options] <model-in> <feature-rspecifier> <posteriors-rspecifier> " 34 "<gpost-wspecifier>\n" 36 " gmm-post-to-gpost 1.mdl scp:train.scp ark:1.post ark:1.gpost\n";
41 po.Register(
"binary", &binary,
"Write output in binary mode");
42 po.Register(
"rand-prune", &rand_prune,
"Randomized pruning of posteriors less than this");
45 if (po.NumArgs() != 4) {
50 std::string model_filename = po.GetArg(1),
51 feature_rspecifier = po.GetArg(2),
52 posteriors_rspecifier = po.GetArg(3),
53 gpost_wspecifier = po.GetArg(4);
55 using namespace kaldi;
62 Input ki(model_filename, &binary);
63 trans_model.
Read(ki.Stream(), binary);
64 am_gmm.
Read(ki.Stream(), binary);
67 double tot_like = 0.0;
75 int32 num_done = 0, num_no_posterior = 0, num_other_error = 0;
76 for (; !feature_reader.Done(); feature_reader.Next()) {
77 std::string key = feature_reader.Key();
78 if (!posteriors_reader.HasKey(key)) {
82 const Posterior &posterior = posteriors_reader.Value(key);
85 if (posterior.size() != mat.
NumRows()) {
86 KALDI_WARN <<
"Posterior vector has wrong size "<< (posterior.size()) <<
" vs. "<< (mat.
NumRows());
92 BaseFloat tot_like_this_file = 0.0, tot_weight = 0.0;
96 for (
size_t i = 0;
i < posterior.size();
i++) {
97 gpost[
i].reserve(pdf_posterior[
i].size());
98 for (
size_t j = 0;
j < pdf_posterior[
i].size();
j++) {
99 int32 pdf_id = pdf_posterior[
i][
j].first;
105 this_post_vec.
Scale(weight);
106 if (rand_prune > 0.0)
107 for (int32 k = 0; k < this_post_vec.
Dim(); k++)
108 this_post_vec(k) =
RandPrune(this_post_vec(k),
110 if (!this_post_vec.
IsZero())
111 gpost[
i].push_back(std::make_pair(pdf_id, this_post_vec));
112 tot_like_this_file += like * weight;
113 tot_weight += weight;
116 KALDI_VLOG(1) <<
"Average like for this file is " 117 << (tot_like_this_file/tot_weight) <<
" over " 118 << tot_weight <<
" frames.";
119 tot_like += tot_like_this_file;
121 gpost_writer.Write(key, gpost);
124 KALDI_LOG <<
"Done " << num_done <<
" files, " << num_no_posterior
125 <<
" with no posteriors, " << num_other_error
126 <<
" with other errors.";
128 KALDI_LOG <<
"Overall avg like per frame (Gaussian only) = " 129 << (tot_like/tot_t) <<
" over " << tot_t <<
" frames.";
131 KALDI_LOG <<
"Done converting post to gpost";
132 if (num_done != 0)
return 0;
134 }
catch(
const std::exception &e) {
135 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool IsZero(Real cutoff=1.0e-06) const
Returns true if matrix is all zeros.
Float RandPrune(Float post, BaseFloat prune_thresh, struct RandomState *state=NULL)
A templated class for writing objects to an archive or script file; see The Table concept...
Allows random access to a collection of objects in an archive or script file; see The Table concept...
BaseFloat ComponentPosteriors(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *posteriors) const
Computes the posterior probabilities of all Gaussian components given a data point.
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 SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
void Read(std::istream &is, bool binary)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
MatrixIndexT Dim() const
Returns the dimension of the vector.
void Scale(Real alpha)
Multiplies all elements by this constant.
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
A class representing a vector.
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition for Gaussian Mixture Model with diagonal covariances.
std::vector< std::vector< std::pair< int32, Vector< BaseFloat > > > > GaussPost
GaussPost is a typedef for storing Gaussian-level posteriors for an utterance.
void ConvertPosteriorToPdfs(const TransitionModel &tmodel, const Posterior &post_in, Posterior *post_out)
Converts a posterior over transition-ids to be a posterior over pdf-ids.
void Read(std::istream &in_stream, bool binary)