compute-cmvn-stats.cc File Reference
Include dependency graph for compute-cmvn-stats.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

bool AccCmvnStatsWrapper (const std::string &utt, const MatrixBase< BaseFloat > &feats, RandomAccessBaseFloatVectorReader *weights_reader, Matrix< double > *cmvn_stats)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 54 of file compute-cmvn-stats.cc.

References kaldi::AccCmvnStatsWrapper(), kaldi::ClassifyWspecifier(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, kaldi::InitCmvnStats(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::kNoWspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), kaldi::PrintableWxfilename(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), TableWriter< Holder >::Write(), and kaldi::WriteKaldiObject().

54  {
55  try {
56  using namespace kaldi;
57  using kaldi::int32;
58 
59  const char *usage =
60  "Compute cepstral mean and variance normalization statistics\n"
61  "If wspecifier provided: per-utterance by default, or per-speaker if\n"
62  "spk2utt option provided; if wxfilename: global\n"
63  "Usage: compute-cmvn-stats [options] <feats-rspecifier> (<stats-wspecifier>|<stats-wxfilename>)\n"
64  "e.g.: compute-cmvn-stats --spk2utt=ark:data/train/spk2utt"
65  " scp:data/train/feats.scp ark,scp:/foo/bar/cmvn.ark,data/train/cmvn.scp\n"
66  "See also: apply-cmvn, modify-cmvn-stats\n";
67 
68  ParseOptions po(usage);
69  std::string spk2utt_rspecifier, weights_rspecifier;
70  bool binary = true;
71  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to utterance-list map");
72  po.Register("binary", &binary, "write in binary mode (applies only to global CMN/CVN)");
73  po.Register("weights", &weights_rspecifier, "rspecifier for a vector of floats "
74  "for each utterance, that's a per-frame weight.");
75 
76  po.Read(argc, argv);
77 
78  if (po.NumArgs() != 2) {
79  po.PrintUsage();
80  exit(1);
81  }
82 
83  int32 num_done = 0, num_err = 0;
84  std::string rspecifier = po.GetArg(1);
85  std::string wspecifier_or_wxfilename = po.GetArg(2);
86 
87  RandomAccessBaseFloatVectorReader weights_reader(weights_rspecifier);
88 
89  if (ClassifyWspecifier(wspecifier_or_wxfilename, NULL, NULL, NULL)
90  != kNoWspecifier) { // writing to a Table: per-speaker or per-utt CMN/CVN.
91  std::string wspecifier = wspecifier_or_wxfilename;
92 
93  DoubleMatrixWriter writer(wspecifier);
94 
95  if (spk2utt_rspecifier != "") {
96  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
97  RandomAccessBaseFloatMatrixReader feat_reader(rspecifier);
98 
99  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
100  std::string spk = spk2utt_reader.Key();
101  const std::vector<std::string> &uttlist = spk2utt_reader.Value();
102  bool is_init = false;
103  Matrix<double> stats;
104  for (size_t i = 0; i < uttlist.size(); i++) {
105  std::string utt = uttlist[i];
106  if (!feat_reader.HasKey(utt)) {
107  KALDI_WARN << "Did not find features for utterance " << utt;
108  num_err++;
109  continue;
110  }
111  const Matrix<BaseFloat> &feats = feat_reader.Value(utt);
112  if (!is_init) {
113  InitCmvnStats(feats.NumCols(), &stats);
114  is_init = true;
115  }
116  if (!AccCmvnStatsWrapper(utt, feats, &weights_reader, &stats)) {
117  num_err++;
118  } else {
119  num_done++;
120  }
121  }
122  if (stats.NumRows() == 0) {
123  KALDI_WARN << "No stats accumulated for speaker " << spk;
124  } else {
125  writer.Write(spk, stats);
126  }
127  }
128  } else { // per-utterance normalization
129  SequentialBaseFloatMatrixReader feat_reader(rspecifier);
130 
131  for (; !feat_reader.Done(); feat_reader.Next()) {
132  std::string utt = feat_reader.Key();
133  Matrix<double> stats;
134  const Matrix<BaseFloat> &feats = feat_reader.Value();
135  InitCmvnStats(feats.NumCols(), &stats);
136 
137  if (!AccCmvnStatsWrapper(utt, feats, &weights_reader, &stats)) {
138  num_err++;
139  continue;
140  }
141  writer.Write(feat_reader.Key(), stats);
142  num_done++;
143  }
144  }
145  } else { // accumulate global stats
146  if (spk2utt_rspecifier != "")
147  KALDI_ERR << "--spk2utt option not compatible with wxfilename as output "
148  << "(did you forget ark:?)";
149  std::string wxfilename = wspecifier_or_wxfilename;
150  bool is_init = false;
151  Matrix<double> stats;
152  SequentialBaseFloatMatrixReader feat_reader(rspecifier);
153  for (; !feat_reader.Done(); feat_reader.Next()) {
154  std::string utt = feat_reader.Key();
155  const Matrix<BaseFloat> &feats = feat_reader.Value();
156  if (!is_init) {
157  InitCmvnStats(feats.NumCols(), &stats);
158  is_init = true;
159  }
160  if (!AccCmvnStatsWrapper(utt, feats, &weights_reader, &stats)) {
161  num_err++;
162  } else {
163  num_done++;
164  }
165  }
166  Matrix<float> stats_float(stats);
167  WriteKaldiObject(stats_float, wxfilename, binary);
168  KALDI_LOG << "Wrote global CMVN stats to "
169  << PrintableWxfilename(wxfilename);
170  }
171  KALDI_LOG << "Done accumulating CMVN stats for " << num_done
172  << " utterances; " << num_err << " had errors.";
173  return (num_done != 0 ? 0 : 1);
174  } catch(const std::exception &e) {
175  std::cerr << e.what();
176  return -1;
177  }
178 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool AccCmvnStatsWrapper(const std::string &utt, const MatrixBase< BaseFloat > &feats, RandomAccessBaseFloatVectorReader *weights_reader, Matrix< double > *cmvn_stats)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void InitCmvnStats(int32 dim, Matrix< double > *stats)
This function initializes the matrix to dimension 2 by (dim+1); 1st "dim" elements of 1st row are mea...
Definition: cmvn.cc:25
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
#define KALDI_LOG
Definition: kaldi-error.h:153