gmm-boost-silence.cc
Go to the documentation of this file.
1 // gmmbin/gmm-boost-silence.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Daniel Povey)
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 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "hmm/transition-model.h"
23 #include "gmm/am-diag-gmm.h"
24 
25 int main(int argc, char *argv[]) {
26  try {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29 
30  const char *usage =
31  "Modify GMM-based model to boost (by a certain factor) all\n"
32  "probabilities associated with the specified phones (could be\n"
33  "all silence phones, or just the ones used for optional silence).\n"
34  "Note: this is done by modifying the GMM weights. If the silence\n"
35  "model shares a GMM with other models, then it will modify the GMM\n"
36  "weights for all models that may correspond to silence.\n"
37  "\n"
38  "Usage: gmm-boost-silence [options] <silence-phones-list> <model-in> <model-out>\n"
39  "e.g.: gmm-boost-silence --boost=1.5 1:2:3 1.mdl 1_boostsil.mdl\n";
40 
41  bool binary_write = true;
42  BaseFloat boost = 1.5;
43 
44  ParseOptions po(usage);
45  po.Register("binary", &binary_write, "Write output in binary mode");
46  po.Register("boost", &boost, "Factor by which to boost silence probs");
47 
48  po.Read(argc, argv);
49 
50  if (po.NumArgs() != 3) {
51  po.PrintUsage();
52  exit(1);
53  }
54 
55  std::string
56  silence_phones_string = po.GetArg(1),
57  model_rxfilename = po.GetArg(2),
58  model_wxfilename = po.GetArg(3);
59 
60  std::vector<int32> silence_phones;
61  if (silence_phones_string != "") {
62  SplitStringToIntegers(silence_phones_string, ":", false, &silence_phones);
63  std::sort(silence_phones.begin(), silence_phones.end());
64  KALDI_ASSERT(IsSortedAndUniq(silence_phones) && "Silence phones non-unique.");
65  } else {
66  KALDI_WARN << "gmm-boost-silence: no silence phones specified, doing nothing.";
67  }
68 
69  AmDiagGmm am_gmm;
70  TransitionModel trans_model;
71  {
72  bool binary_read;
73  Input ki(model_rxfilename, &binary_read);
74  trans_model.Read(ki.Stream(), binary_read);
75  am_gmm.Read(ki.Stream(), binary_read);
76  }
77 
78  { // Do the modification to the am_gmm object.
79  std::vector<int32> pdfs;
80  bool ans = GetPdfsForPhones(trans_model, silence_phones, &pdfs);
81  if (!ans) {
82  KALDI_WARN << "The pdfs for the silence phones may be shared by other phones "
83  << "(note: this probably does not matter.)";
84  }
85  for (size_t i = 0; i < pdfs.size(); i++) {
86  int32 pdf = pdfs[i];
87  DiagGmm &gmm = am_gmm.GetPdf(pdf);
88  Vector<BaseFloat> weights(gmm.weights());
89  weights.Scale(boost);
90  gmm.SetWeights(weights);
91  gmm.ComputeGconsts();
92  }
93  KALDI_LOG << "Boosted weights for " << pdfs.size()
94  << " pdfs, by factor of " << boost;
95  }
96 
97  {
98  Output ko(model_wxfilename, binary_write);
99  trans_model.Write(ko.Stream(), binary_write);
100  am_gmm.Write(ko.Stream(), binary_write);
101  }
102 
103  KALDI_LOG << "Wrote model to " << model_wxfilename;
104  } catch(const std::exception &e) {
105  std::cerr << e.what() << '\n';
106  return -1;
107  }
108 }
109 
110 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int32 ComputeGconsts()
Sets the gconsts.
Definition: diag-gmm.cc:114
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::istream & Stream()
Definition: kaldi-io.cc:826
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
std::ostream & Stream()
Definition: kaldi-io.cc:701
void Read(std::istream &is, bool binary)
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.
int main(int argc, char *argv[])
const Vector< BaseFloat > & weights() const
Definition: diag-gmm.h:178
void Scale(Real alpha)
Multiplies all elements by this constant.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
void Write(std::ostream &os, bool binary) const
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Write(std::ostream &out_stream, bool binary) const
Definition: am-diag-gmm.cc:163
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
void SetWeights(const VectorBase< Real > &w)
Mutators for both float or double.
Definition: diag-gmm-inl.h:28
bool GetPdfsForPhones(const TransitionModel &trans_model, const std::vector< int32 > &phones, std::vector< int32 > *pdfs)
Works out which pdfs might correspond to the given phones.
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Definition: stl-utils.h:63
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147