All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-est-fmllr-gpost.cc File Reference
#include <string>
#include <vector>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "transform/fmllr-diag-gmm.h"
#include "hmm/posterior.h"
Include dependency graph for gmm-est-fmllr-gpost.cc:

Go to the source code of this file.

Namespaces

 kaldi
 Relabels neural network egs with the read pdf-id alignments.
 

Functions

void AccumulateForUtterance (const Matrix< BaseFloat > &feats, const GaussPost &gpost, const TransitionModel &trans_model, const AmDiagGmm &am_gmm, FmllrDiagGmmAccs *spk_stats)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 53 of file gmm-est-fmllr-gpost.cc.

References kaldi::AccumulateForUtterance(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), FmllrOptions::Register(), ParseOptions::Register(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

53  {
54  try {
55  typedef kaldi::int32 int32;
56  using namespace kaldi;
57  const char *usage =
58  "Estimate global fMLLR transforms, either per utterance or for the supplied\n"
59  "set of speakers (spk2utt option). Reads Gaussian-level posteriors. Writes\n"
60  "to a table of matrices.\n"
61  "Usage: gmm-est-fmllr-gpost [options] <model-in> "
62  "<feature-rspecifier> <gpost-rspecifier> <transform-wspecifier>\n";
63 
64  ParseOptions po(usage);
65  FmllrOptions fmllr_opts;
66  string spk2utt_rspecifier;
67  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
68  "utterance-list map");
69  fmllr_opts.Register(&po);
70 
71  po.Read(argc, argv);
72 
73  if (po.NumArgs() != 4) {
74  po.PrintUsage();
75  exit(1);
76  }
77 
78  string
79  model_rxfilename = po.GetArg(1),
80  feature_rspecifier = po.GetArg(2),
81  gpost_rspecifier = po.GetArg(3),
82  trans_wspecifier = po.GetArg(4);
83 
84  TransitionModel trans_model;
85  AmDiagGmm am_gmm;
86  {
87  bool binary;
88  Input ki(model_rxfilename, &binary);
89  trans_model.Read(ki.Stream(), binary);
90  am_gmm.Read(ki.Stream(), binary);
91  }
92 
93  RandomAccessGaussPostReader gpost_reader(gpost_rspecifier);
94 
95  double tot_impr = 0.0, tot_t = 0.0;
96 
97  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
98 
99  int32 num_done = 0, num_no_gpost = 0, num_other_error = 0;
100  if (spk2utt_rspecifier != "") { // per-speaker adaptation
101  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
102  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
103 
104  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
105  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
106  string spk = spk2utt_reader.Key();
107  const vector<string> &uttlist = spk2utt_reader.Value();
108  for (size_t i = 0; i < uttlist.size(); i++) {
109  std::string utt = uttlist[i];
110  if (!feature_reader.HasKey(utt)) {
111  KALDI_WARN << "Did not find features for utterance " << utt;
112  num_other_error++;
113  continue;
114  }
115  if (!gpost_reader.HasKey(utt)) {
116  KALDI_WARN << "Did not find posteriors for utterance " << utt;
117  num_no_gpost++;
118  continue;
119  }
120  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
121  const GaussPost &gpost = gpost_reader.Value(utt);
122  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
123  KALDI_WARN << "GaussPost vector has wrong size " << (gpost.size())
124  << " vs. " << (feats.NumRows());
125  num_other_error++;
126  continue;
127  }
128 
129  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &spk_stats);
130 
131  num_done++;
132  } // end looping over all utterances of the current speaker
133 
134  BaseFloat impr, spk_tot_t;
135  { // Compute the transform and write it out.
136  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
137  transform.SetUnit();
138  spk_stats.Update(fmllr_opts, &transform, &impr, &spk_tot_t);
139  transform_writer.Write(spk, transform);
140  }
141  KALDI_LOG << "For speaker " << spk << ", auxf-impr from fMLLR is "
142  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.\n";
143  tot_impr += impr;
144  tot_t += spk_tot_t;
145  } // end looping over speakers
146  } else { // per-utterance adaptation
147  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
148  for (; !feature_reader.Done(); feature_reader.Next()) {
149  string utt = feature_reader.Key();
150  if (!gpost_reader.HasKey(utt)) {
151  KALDI_WARN << "Did not find gposts for utterance "
152  << utt;
153  num_no_gpost++;
154  continue;
155  }
156  const Matrix<BaseFloat> &feats = feature_reader.Value();
157  const GaussPost &gpost = gpost_reader.Value(utt);
158 
159  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
160  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
161  << " vs. " << (feats.NumRows());
162  num_other_error++;
163  continue;
164  }
165  num_done++;
166 
167  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
168 
169  AccumulateForUtterance(feats, gpost, trans_model, am_gmm,
170  &spk_stats);
171 
172  BaseFloat impr, utt_tot_t;
173  { // Compute the transform and write it out.
174  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
175  transform.SetUnit();
176  spk_stats.Update(fmllr_opts, &transform, &impr, &utt_tot_t);
177  transform_writer.Write(utt, transform);
178  }
179  KALDI_LOG << "For utterancer " << utt << ", auxf-impr from fMLLR is "
180  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
181  tot_impr += impr;
182  tot_t += utt_tot_t;
183  }
184  }
185 
186  KALDI_LOG << "Done " << num_done << " files, " << num_no_gpost
187  << " with no gposts, " << num_other_error << " with other errors.";
188  KALDI_LOG << "Overall fMLLR auxf impr per frame is "
189  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
190  return (num_done != 0 ? 0 : 1);
191  } catch(const std::exception &e) {
192  std::cerr << e.what();
193  return -1;
194  }
195 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void AccumulateForUtterance(const Matrix< BaseFloat > &feats, const GaussPost &gpost, const TransitionModel &trans_model, const AmDiagGmm &am_gmm, FmllrDiagGmmAccs *spk_stats)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:130
void Register(OptionsItf *opts)
std::vector< std::vector< std::pair< int32, Vector< BaseFloat > > > > GaussPost
GaussPost is a typedef for storing Gaussian-level posteriors for an utterance.
Definition: posterior.h:52
#define KALDI_LOG
Definition: kaldi-error.h:133