acc-lda.cc File Reference
Include dependency graph for acc-lda.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 Accumulate LDA statistics based on pdf-ids. More...
 

Function Documentation

◆ main()

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

Accumulate LDA statistics based on pdf-ids.

Inputs are the source models, that serve as the input (and may potentially contain the current transformation), the un-transformed features and state posterior probabilities

Definition at line 32 of file acc-lda.cc.

References LdaEstimate::Accumulate(), kaldi::ConvertPosteriorToPdfs(), LdaEstimate::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, LdaEstimate::Init(), rnnlm::j, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), TransitionModel::NumPdfs(), ParseOptions::PrintUsage(), kaldi::RandPrune(), ParseOptions::Read(), TransitionModel::Read(), ParseOptions::Register(), Output::Stream(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and LdaEstimate::Write().

32  {
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
Float RandPrune(Float post, BaseFloat prune_thresh, struct RandomState *state=NULL)
Definition: kaldi-math.h:174
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
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
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
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
#define KALDI_WARN
Definition: kaldi-error.h:150
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