logistic-regression-eval.cc
Go to the documentation of this file.
1 // ivectorbin/logistic-regression-eval.cc
2 
3 // Copyright 2014 David Snyder
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
24 
25 using namespace kaldi;
26 
28  const LogisticRegressionConfig &config,
29  bool apply_log) {
30  std::string model = po.GetArg(1),
31  vector_rspecifier = po.GetArg(2),
32  log_posteriors_wspecifier = po.GetArg(3);
33 
34  LogisticRegression classifier;
35  ReadKaldiObject(model, &classifier);
36 
37  std::vector<Vector<BaseFloat> > vectors;
38  SequentialBaseFloatVectorReader vector_reader(vector_rspecifier);
39  BaseFloatVectorWriter posterior_writer(log_posteriors_wspecifier);
40  std::vector<std::string> utt_list;
41  int32 num_utt_done = 0;
42 
43  for (; !vector_reader.Done(); vector_reader.Next()) {
44  std::string utt = vector_reader.Key();
45  const Vector<BaseFloat> &vector = vector_reader.Value();
46  Vector<BaseFloat> log_posteriors;
47  classifier.GetLogPosteriors(vector, &log_posteriors);
48  if (!apply_log)
49  log_posteriors.ApplyExp();
50  posterior_writer.Write(utt, log_posteriors);
51  num_utt_done++;
52  }
53  KALDI_LOG << "Calculated log posteriors for " << num_utt_done << " vectors.";
54  return (num_utt_done == 0 ? 1 : 0);
55 }
56 
58  bool apply_log) {
59  std::string model_rspecifier = po.GetArg(1),
60  trials_rspecifier = po.GetArg(2),
61  vector_rspecifier = po.GetArg(3),
62  scores_out = po.GetArg(4);
63 
64  SequentialInt32Reader class_reader(trials_rspecifier);
66  ReadKaldiObject(model_rspecifier, &classifier);
67 
68  std::vector<Vector<BaseFloat> > vectors;
69  std::vector<int32> ys;
70  std::vector<std::string> utt_list;
71  int32 num_utt_done = 0, num_utt_err = 0;
72 
73  RandomAccessBaseFloatVectorReader vector_reader(vector_rspecifier);
74  for (; !class_reader.Done(); class_reader.Next()) {
75  std::string utt = class_reader.Key();
76  int32 class_label = class_reader.Value();
77  if (!vector_reader.HasKey(utt)) {
78  KALDI_WARN << "No vector for utterance " << utt;
79  num_utt_err++;
80  } else {
81  utt_list.push_back(utt);
82  ys.push_back(class_label);
83  const Vector<BaseFloat> &vector = vector_reader.Value(utt);
84  vectors.push_back(vector);
85  num_utt_done++;
86  }
87  }
88 
89  if (vectors.empty()) {
90  KALDI_WARN << "Read no input";
91  return 1;
92  }
93 
94  Matrix<BaseFloat> xs(vectors.size(), vectors[0].Dim());
95  for (int i = 0; i < vectors.size(); i++) {
96  xs.Row(i).CopyFromVec(vectors[i]);
97  }
98 
99  Matrix<BaseFloat> log_posteriors;
100  classifier.GetLogPosteriors(xs, &log_posteriors);
101 
102  bool binary = false;
103  Output ko(scores_out.c_str(), binary);
104 
105  if (!apply_log)
106  log_posteriors.ApplyExp();
107 
108  for (int i = 0; i < ys.size(); i++) {
109  ko.Stream() << utt_list[i] << " " << ys[i] << " " << log_posteriors(i, ys[i]) << std::endl;
110  }
111  KALDI_LOG << "Calculated scores for " << num_utt_done
112  << " vectors with "
113  << num_utt_err << " missing. ";
114  return (num_utt_done == 0 ? 1 : 0);
115 }
116 
117 int main(int argc, char *argv[]) {
118  using namespace kaldi;
119  typedef kaldi::int32 int32;
120  try {
121  const char *usage =
122  "Evaluates a model on input vectors and outputs either\n"
123  "log posterior probabilities or scores.\n"
124  "Usage1: logistic-regression-eval <model> <input-vectors-rspecifier>\n"
125  " <output-log-posteriors-wspecifier>\n"
126  "Usage2: logistic-regression-eval <model> <trials-file> <input-vectors-rspecifier>\n"
127  " <output-scores-file>\n";
128 
129  ParseOptions po(usage);
130 
131  bool apply_log = true;
132  po.Register("apply-log", &apply_log,
133  "If false, apply Exp to the log posteriors output. This is "
134  "helpful when combining posteriors from multiple logistic "
135  "regression models.");
137  config.Register(&po);
138  po.Read(argc, argv);
139 
140  if (po.NumArgs() != 3 && po.NumArgs() != 4) {
141  po.PrintUsage();
142  exit(1);
143  }
144 
145  if (po.NumArgs() == 4) {
146  return ComputeScores(po, config, apply_log);
147  } else {
148  return ComputeLogPosteriors(po, config, apply_log);
149  }
150 
151  } catch(const std::exception &e) {
152  std::cerr << e.what();
153  return -1;
154  }
155 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 ComputeScores(ParseOptions &po, const LogisticRegressionConfig &config, bool apply_log)
void ApplyExp()
Apply exponential to each value in vector.
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)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
void GetLogPosteriors(const Matrix< BaseFloat > &xs, Matrix< BaseFloat > *log_posteriors)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int ComputeLogPosteriors(ParseOptions &po, const LogisticRegressionConfig &config, bool apply_log)
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
const T & Value(const std::string &key)
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 main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_LOG
Definition: kaldi-error.h:153