weight-silence-post.cc
Go to the documentation of this file.
1 // bin/weight-silence-post.cc
2 
3 // Copyright 2009-2013 Microsoft Corporation
4 // Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "gmm/am-diag-gmm.h"
25 #include "hmm/transition-model.h"
26 #include "hmm/hmm-utils.h"
27 #include "hmm/posterior.h"
28 
29 int main(int argc, char *argv[]) {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  try {
33  const char *usage =
34  "Apply weight to silences in posts\n"
35  "Usage: weight-silence-post [options] <silence-weight> <silence-phones> "
36  "<model> <posteriors-rspecifier> <posteriors-wspecifier>\n"
37  "e.g.:\n"
38  " weight-silence-post 0.0 1:2:3 1.mdl ark:1.post ark:nosil.post\n";
39 
40  ParseOptions po(usage);
41 
42  bool distribute = false;
43 
44  po.Register("distribute", &distribute, "If true, rather than weighting the "
45  "individual posteriors, apply the weighting to the whole frame: "
46  "i.e. on time t, scale all posterior entries by "
47  "p(sil)*silence-weight + p(non-sil)*1.0");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 5) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56 
57  std::string silence_weight_str = po.GetArg(1),
58  silence_phones_str = po.GetArg(2),
59  model_rxfilename = po.GetArg(3),
60  posteriors_rspecifier = po.GetArg(4),
61  posteriors_wspecifier = po.GetArg(5);
62 
63  BaseFloat silence_weight = 0.0;
64  if (!ConvertStringToReal(silence_weight_str, &silence_weight))
65  KALDI_ERR << "Invalid silence-weight parameter: expected float, got \""
66  << silence_weight_str << '"';
67  std::vector<int32> silence_phones;
68  if (!SplitStringToIntegers(silence_phones_str, ":", false, &silence_phones))
69  KALDI_ERR << "Invalid silence-phones string " << silence_phones_str;
70  if (silence_phones.empty())
71  KALDI_WARN <<"No silence phones, this will have no effect";
72  ConstIntegerSet<int32> silence_set(silence_phones); // faster lookup.
73 
74  TransitionModel trans_model;
75  ReadKaldiObject(model_rxfilename, &trans_model);
76 
77  int32 num_posteriors = 0;
78  SequentialPosteriorReader posterior_reader(posteriors_rspecifier);
79  PosteriorWriter posterior_writer(posteriors_wspecifier);
80 
81  for (; !posterior_reader.Done(); posterior_reader.Next()) {
82  num_posteriors++;
83  // Posterior is vector<vector<pair<int32, BaseFloat> > >
84  Posterior post = posterior_reader.Value();
85  // Posterior is vector<vector<pair<int32, BaseFloat> > >
86  if (distribute)
87  WeightSilencePostDistributed(trans_model, silence_set,
88  silence_weight, &post);
89  else
90  WeightSilencePost(trans_model, silence_set,
91  silence_weight, &post);
92 
93  posterior_writer.Write(posterior_reader.Key(), post);
94  }
95  KALDI_LOG << "Done " << num_posteriors << " posteriors.";
96  return (num_posteriors != 0 ? 0 : 1);
97  } catch(const std::exception &e) {
98  std::cerr << e.what();
99  return -1;
100  }
101 }
102 
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].
int main(int argc, char *argv[])
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
void WeightSilencePost(const TransitionModel &trans_model, const ConstIntegerSet< int32 > &silence_set, BaseFloat silence_scale, Posterior *post)
Weight any silence phones in the posterior (i.e.
Definition: posterior.cc:375
float BaseFloat
Definition: kaldi-types.h:29
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:42
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void WeightSilencePostDistributed(const TransitionModel &trans_model, const ConstIntegerSet< int32 > &silence_set, BaseFloat silence_scale, Posterior *post)
This is similar to WeightSilencePost, except that on each frame it works out the amount by which the ...
Definition: posterior.cc:398
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_ERR
Definition: kaldi-error.h:147
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
Definition: text-utils.cc:238
#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 NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_LOG
Definition: kaldi-error.h:153