ivector-compute-dot-products.cc File Reference
Include dependency graph for ivector-compute-dot-products.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 28 of file ivector-compute-dot-products.cc.

References ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::SplitStringToVector(), Output::Stream(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), and kaldi::VecVec().

28  {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31  typedef kaldi::int64 int64;
32  try {
33  const char *usage =
34  "Computes dot-products between iVectors; useful in application of an\n"
35  "iVector-based system. The 'trials-file' has lines of the form\n"
36  "<key1> <key2>\n"
37  "and the output will have the form\n"
38  "<key1> <key2> [<dot-product>]\n"
39  "(if either key could not be found, the dot-product field in the output\n"
40  "will be absent, and this program will print a warning)\n"
41  "\n"
42  "Usage: ivector-compute-dot-products [options] <trials-in> "
43  "<ivector1-rspecifier> <ivector2-rspecifier> <scores-out>\n"
44  "e.g.: \n"
45  " ivector-compute-dot-products trials ark:train_ivectors.scp ark:test_ivectors.scp trials.scored\n"
46  "See also: ivector-plda-scoring\n";
47 
48  ParseOptions po(usage);
49 
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() != 4) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string trials_rxfilename = po.GetArg(1),
58  ivector1_rspecifier = po.GetArg(2),
59  ivector2_rspecifier = po.GetArg(3),
60  scores_wxfilename = po.GetArg(4);
61 
62 
63  int64 num_done = 0, num_err = 0;
64 
65  RandomAccessBaseFloatVectorReader ivector1_reader(ivector1_rspecifier);
66  RandomAccessBaseFloatVectorReader ivector2_reader(ivector2_rspecifier);
67 
68  Input ki(trials_rxfilename);
69 
70  bool binary = false;
71  Output ko(scores_wxfilename, binary);
72  double sum = 0.0, sumsq = 0.0;
73 
74  std::string line;
75  while (std::getline(ki.Stream(), line)) {
76  std::vector<std::string> fields;
77  SplitStringToVector(line, " \t\n\r", true, &fields);
78  if (fields.size() != 2) {
79  KALDI_ERR << "Bad line " << (num_done + num_err) << " in input "
80  << "(expected two fields: key1 key2): " << line;
81  }
82  std::string key1 = fields[0], key2 = fields[1];
83  if (!ivector1_reader.HasKey(key1)) {
84  KALDI_WARN << "Key " << key1 << " not present in 1st table of ivectors.";
85  num_err++;
86  continue;
87  }
88  if (!ivector2_reader.HasKey(key2)) {
89  KALDI_WARN << "Key " << key2 << " not present in 2nd table of ivectors.";
90  num_err++;
91  continue;
92  }
93  const Vector<BaseFloat> &ivector1 = ivector1_reader.Value(key1),
94  &ivector2 = ivector2_reader.Value(key2);
95  // The following will crash if the dimensions differ, but
96  // they would likely also differ for all the ivectors so it's probably
97  // best to just crash.
98  BaseFloat dot_prod = VecVec(ivector1, ivector2);
99  sum += dot_prod;
100  sumsq += dot_prod * dot_prod;
101  num_done++;
102  ko.Stream() << key1 << ' ' << key2 << ' ' << dot_prod << std::endl;
103  }
104 
105  if (num_done != 0) {
106  BaseFloat mean = sum / num_done, scatter = sumsq / num_done,
107  variance = scatter - mean * mean, stddev = sqrt(variance);
108  KALDI_LOG << "Mean dot-product was " << mean << ", standard deviation was "
109  << stddev;
110  }
111  KALDI_LOG << "Processed " << num_done << " trials " << num_err
112  << " had errors.";
113  return (num_done != 0 ? 0 : 1);
114  } catch(const std::exception &e) {
115  std::cerr << e.what();
116  return -1;
117  }
118 }
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
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void SplitStringToVector(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< std::string > *out)
Split a string using any of the single character delimiters.
Definition: text-utils.cc:63
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_LOG
Definition: kaldi-error.h:153
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37