ivector-compute-plda.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "ivector/plda.h"
Include dependency graph for ivector-compute-plda.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 26 of file ivector-compute-plda.cc.

References PldaStats::AddSamples(), PldaStats::Dim(), SequentialTableReader< Holder >::Done(), PldaEstimator::Estimate(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), PldaEstimationConfig::Register(), MatrixBase< Real >::Row(), PldaStats::Sort(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and kaldi::WriteKaldiObject().

26  {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29  try {
30  const char *usage =
31  "Computes a Plda object (for Probabilistic Linear Discriminant Analysis)\n"
32  "from a set of iVectors. Uses speaker information from a spk2utt file\n"
33  "to compute within and between class variances.\n"
34  "\n"
35  "Usage: ivector-compute-plda [options] <spk2utt-rspecifier> <ivector-rspecifier> "
36  "<plda-out>\n"
37  "e.g.: \n"
38  " ivector-compute-plda ark:spk2utt ark,s,cs:ivectors.ark plda\n";
39 
40  ParseOptions po(usage);
41 
42  bool binary = true;
43  PldaEstimationConfig plda_config;
44 
45  plda_config.Register(&po);
46 
47  po.Register("binary", &binary, "Write output in binary mode");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 3) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56  std::string spk2utt_rspecifier = po.GetArg(1),
57  ivector_rspecifier = po.GetArg(2),
58  plda_wxfilename = po.GetArg(3);
59 
60  int64 num_spk_done = 0, num_spk_err = 0,
61  num_utt_done = 0, num_utt_err = 0;
62 
63  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
64  RandomAccessBaseFloatVectorReader ivector_reader(ivector_rspecifier);
65 
66  PldaStats plda_stats;
67 
68  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
69  std::string spk = spk2utt_reader.Key();
70  const std::vector<std::string> &uttlist = spk2utt_reader.Value();
71  if (uttlist.empty()) {
72  KALDI_ERR << "Speaker with no utterances.";
73  }
74  std::vector<Vector<BaseFloat> > ivectors;
75  ivectors.reserve(uttlist.size());
76 
77  for (size_t i = 0; i < uttlist.size(); i++) {
78  std::string utt = uttlist[i];
79  if (!ivector_reader.HasKey(utt)) {
80  KALDI_WARN << "No iVector present in input for utterance " << utt;
81  num_utt_err++;
82  } else {
83  ivectors.resize(ivectors.size() + 1);
84  ivectors.back() = ivector_reader.Value(utt);
85  num_utt_done++;
86  }
87  }
88 
89  if (ivectors.size() == 0) {
90  KALDI_WARN << "Not producing output for speaker " << spk
91  << " since no utterances had iVectors";
92  num_spk_err++;
93  } else {
94  Matrix<double> ivector_mat(ivectors.size(), ivectors[0].Dim());
95  for (size_t i = 0; i < ivectors.size(); i++)
96  ivector_mat.Row(i).CopyFromVec(ivectors[i]);
97  double weight = 1.0; // The code supports weighting but
98  // we don't support this at the command-line
99  // level yet.
100  plda_stats.AddSamples(weight, ivector_mat);
101  num_spk_done++;
102  }
103  }
104 
105  if (num_utt_done <= plda_stats.Dim())
106  KALDI_ERR << "Number of training iVectors is not greater than their "
107  << "dimension, unable to estimate PLDA.";
108 
109  KALDI_LOG << "Accumulated stats from " << num_spk_done << " speakers ("
110  << num_spk_err << " with no utterances), consisting of "
111  << num_utt_done << " utterances (" << num_utt_err
112  << " absent from input).";
113 
114  if (num_spk_done == 0)
115  KALDI_ERR << "No stats accumulated, unable to estimate PLDA.";
116  if (num_spk_done == num_utt_done)
117  KALDI_ERR << "No speakers with multiple utterances, "
118  << "unable to estimate PLDA.";
119 
120  plda_stats.Sort();
121  PldaEstimator plda_estimator(plda_stats);
122  Plda plda;
123  plda_estimator.Estimate(plda_config, &plda);
124 
125  WriteKaldiObject(plda, plda_wxfilename, binary);
126 
127  return (num_spk_done != 0 ? 0 : 1);
128  } catch(const std::exception &e) {
129  std::cerr << e.what();
130  return -1;
131  }
132 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
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
int32 Dim() const
Definition: plda.h:184
void AddSamples(double weight, const Matrix< double > &group)
The dimension is set up the first time you add samples.
Definition: plda.cc:286
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
void Register(OptionsItf *opts)
Definition: plda.h:229
#define KALDI_LOG
Definition: kaldi-error.h:153
void Sort()
Definition: plda.h:188