compute-cmvn-stats.cc
Go to the documentation of this file.
1 // featbin/compute-cmvn-stats.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "matrix/kaldi-matrix.h"
24 #include "transform/cmvn.h"
25 
26 namespace kaldi {
27 
28 bool AccCmvnStatsWrapper(const std::string &utt,
29  const MatrixBase<BaseFloat> &feats,
30  RandomAccessBaseFloatVectorReader *weights_reader,
31  Matrix<double> *cmvn_stats) {
32  if (!weights_reader->IsOpen()) {
33  AccCmvnStats(feats, NULL, cmvn_stats);
34  return true;
35  } else {
36  if (!weights_reader->HasKey(utt)) {
37  KALDI_WARN << "No weights available for utterance " << utt;
38  return false;
39  }
40  const Vector<BaseFloat> &weights = weights_reader->Value(utt);
41  if (weights.Dim() != feats.NumRows()) {
42  KALDI_WARN << "Weights for utterance " << utt << " have wrong dimension "
43  << weights.Dim() << " vs. " << feats.NumRows();
44  return false;
45  }
46  AccCmvnStats(feats, &weights, cmvn_stats);
47  return true;
48  }
49 }
50 
51 
52 }
53 
54 int main(int argc, char *argv[]) {
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 }
179 
180 
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
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
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...
Definition: kaldi-table.h:368
kaldi::int32 int32
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...
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
const T & Value(const std::string &key)
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
#define KALDI_WARN
Definition: kaldi-error.h:150
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
bool HasKey(const std::string &key)
void AccCmvnStats(const VectorBase< BaseFloat > &feats, BaseFloat weight, MatrixBase< double > *stats)
Accumulation from a single frame (weighted).
Definition: cmvn.cc:30
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
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
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153