gmm-compute-likes.cc
Go to the documentation of this file.
1 // gmmbin/gmm-compute-likes.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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"
23 #include "gmm/am-diag-gmm.h"
24 #include "hmm/transition-model.h"
25 #include "fstext/fstext-lib.h"
26 #include "base/timer.h"
27 
28 
29 
30 int main(int argc, char *argv[]) {
31  try {
32  using namespace kaldi;
33  typedef kaldi::int32 int32;
34  using fst::SymbolTable;
35  using fst::VectorFst;
36  using fst::StdArc;
37 
38  const char *usage =
39  "Compute log-likelihoods from GMM-based model\n"
40  "(outputs matrices of log-likelihoods indexed by (frame, pdf)\n"
41  "Usage: gmm-compute-likes [options] model-in features-rspecifier likes-wspecifier\n";
42  ParseOptions po(usage);
43 
44  po.Read(argc, argv);
45 
46  if (po.NumArgs() != 3) {
47  po.PrintUsage();
48  exit(1);
49  }
50 
51  std::string model_in_filename = po.GetArg(1),
52  feature_rspecifier = po.GetArg(2),
53  loglikes_wspecifier = po.GetArg(3);
54 
55  AmDiagGmm am_gmm;
56  {
57  bool binary;
58  TransitionModel trans_model; // not needed.
59  Input ki(model_in_filename, &binary);
60  trans_model.Read(ki.Stream(), binary);
61  am_gmm.Read(ki.Stream(), binary);
62  }
63 
64  BaseFloatMatrixWriter loglikes_writer(loglikes_wspecifier);
65  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
66 
67  int32 num_done = 0;
68  for (; !feature_reader.Done(); feature_reader.Next()) {
69  std::string key = feature_reader.Key();
70  const Matrix<BaseFloat> &features (feature_reader.Value());
71  Matrix<BaseFloat> loglikes(features.NumRows(), am_gmm.NumPdfs());
72  for (int32 i = 0; i < features.NumRows(); i++) {
73  for (int32 j = 0; j < am_gmm.NumPdfs(); j++) {
74  SubVector<BaseFloat> feat_row(features, i);
75  loglikes(i, j) = am_gmm.LogLikelihood(j, feat_row);
76  }
77  }
78  loglikes_writer.Write(key, loglikes);
79  num_done++;
80  }
81 
82  KALDI_LOG << "gmm-compute-likes: computed likelihoods for " << num_done
83  << " utterances.";
84  return 0;
85  } catch(const std::exception &e) {
86  std::cerr << e.what();
87  return -1;
88  }
89 }
90 
91 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
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
std::istream & Stream()
Definition: kaldi-io.cc:826
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
BaseFloat LogLikelihood(const int32 pdf_index, const VectorBase< BaseFloat > &data) const
Definition: am-diag-gmm.h:108
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.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501