acc-lda.cc
Go to the documentation of this file.
1 // bin/acc-lda.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation, Go-Vivace Inc.
4 // 2014 Guoguo Chen
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 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "hmm/transition-model.h"
25 #include "hmm/posterior.h"
26 #include "transform/lda-estimate.h"
27 
32 int main(int argc, char *argv[]) {
33  using namespace kaldi;
34  typedef kaldi::int32 int32;
35  try {
36  const char *usage =
37  "Accumulate LDA statistics based on pdf-ids.\n"
38  "Usage: acc-lda [options] <transition-gmm/model> <features-rspecifier> <posteriors-rspecifier> <lda-acc-out>\n"
39  "Typical usage:\n"
40  " ali-to-post ark:1.ali ark:- | acc-lda 1.mdl \"ark:splice-feats scp:train.scp|\" ark:- ldaacc.1\n";
41 
42  bool binary = true;
43  BaseFloat rand_prune = 0.0;
44  ParseOptions po(usage);
45  po.Register("binary", &binary, "Write accumulators in binary mode.");
46  po.Register("rand-prune", &rand_prune,
47  "Randomized pruning threshold for posteriors");
48  po.Read(argc, argv);
49 
50  if (po.NumArgs() != 4) {
51  po.PrintUsage();
52  exit(1);
53  }
54 
55  std::string model_rxfilename = po.GetArg(1);
56  std::string features_rspecifier = po.GetArg(2);
57  std::string posteriors_rspecifier = po.GetArg(3);
58  std::string acc_wxfilename = po.GetArg(4);
59 
60  TransitionModel trans_model;
61  {
62  bool binary_read;
63  Input ki(model_rxfilename, &binary_read);
64  trans_model.Read(ki.Stream(), binary_read);
65  // discard rest of file.
66  }
67 
68  LdaEstimate lda;
69 
70  SequentialBaseFloatMatrixReader feature_reader(features_rspecifier);
71  RandomAccessPosteriorReader posterior_reader(posteriors_rspecifier);
72 
73  int32 num_done = 0, num_fail = 0;
74  for (;!feature_reader.Done(); feature_reader.Next()) {
75  std::string utt = feature_reader.Key();
76  if (!posterior_reader.HasKey(utt)) {
77  KALDI_WARN << "No posteriors for utterance " << utt;
78  num_fail++;
79  continue;
80  }
81  const Posterior &post (posterior_reader.Value(utt));
82  const Matrix<BaseFloat> &feats(feature_reader.Value());
83 
84  if (lda.Dim() == 0)
85  lda.Init(trans_model.NumPdfs(), feats.NumCols());
86 
87  if (feats.NumRows() != static_cast<int32>(post.size())) {
88  KALDI_WARN << "Posterior vs. feats size mismatch "
89  << post.size() << " vs. " << feats.NumRows();
90  num_fail++;
91  continue;
92  }
93  if (lda.Dim() != 0 && lda.Dim() != feats.NumCols()) {
94  KALDI_WARN << "Feature dimension mismatch " << lda.Dim()
95  << " vs. " << feats.NumCols();
96  num_fail++;
97  continue;
98  }
99 
100  Posterior pdf_post;
101  ConvertPosteriorToPdfs(trans_model, post, &pdf_post);
102  for (int32 i = 0; i < feats.NumRows(); i++) {
103  SubVector<BaseFloat> feat(feats, i);
104  for (size_t j = 0; j < pdf_post[i].size(); j++) {
105  int32 pdf_id = pdf_post[i][j].first;
106  BaseFloat weight = RandPrune(pdf_post[i][j].second, rand_prune);
107  if (weight != 0.0) {
108  lda.Accumulate(feat, pdf_id, weight);
109  }
110  }
111  }
112  num_done++;
113  if (num_done % 100 == 0)
114  KALDI_LOG << "Done " << num_done << " utterances.";
115  }
116 
117  KALDI_LOG << "Done " << num_done << " files, failed for "
118  << num_fail;
119 
120  Output ko(acc_wxfilename, binary);
121  lda.Write(ko.Stream(), binary);
122  KALDI_LOG << "Written statistics.";
123  return (num_done != 0 ? 0 : 1);
124  } catch(const std::exception &e) {
125  std::cerr << e.what();
126  return -1;
127  }
128 }
void Accumulate(const VectorBase< BaseFloat > &data, int32 class_id, BaseFloat weight=1.0)
Accumulates data.
Definition: lda-estimate.cc:45
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Class for computing linear discriminant analysis (LDA) transform.
Definition: lda-estimate.h:57
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: lda-estimate.h:66
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
Float RandPrune(Float post, BaseFloat prune_thresh, struct RandomState *state=NULL)
Definition: kaldi-math.h:174
int main(int argc, char *argv[])
Accumulate LDA statistics based on pdf-ids.
Definition: acc-lda.cc:32
void Write(std::ostream &out_stream, bool binary) const
kaldi::int32 int32
void Init(int32 num_classes, int32 dimension)
Allocates memory for accumulators.
Definition: lda-estimate.cc:26
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
std::istream & Stream()
Definition: kaldi-io.cc:826
float BaseFloat
Definition: kaldi-types.h:29
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:42
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
std::ostream & Stream()
Definition: kaldi-io.cc:701
const T & Value(const std::string &key)
void Read(std::istream &is, bool binary)
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_WARN
Definition: kaldi-error.h:150
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).
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.
Definition: posterior.cc:322
#define KALDI_LOG
Definition: kaldi-error.h:153
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501