posterior.h
Go to the documentation of this file.
1 // hmm/posterior.h
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2013-2014 Johns Hopkins University (author: Daniel Povey)
5 // 2014 Guoguo Chen
6 
7 
8 // See ../../COPYING for clarification regarding multiple authors
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
18 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
19 // MERCHANTABLITY OR NON-INFRINGEMENT.
20 // See the Apache 2 License for the specific language governing permissions and
21 // limitations under the License.
22 
23 #ifndef KALDI_HMM_POSTERIOR_H_
24 #define KALDI_HMM_POSTERIOR_H_
25 
26 #include "base/kaldi-common.h"
27 #include "util/const-integer-set.h"
28 #include "util/kaldi-table.h"
29 #include "hmm/transition-model.h"
30 #include "matrix/kaldi-matrix.h"
31 
32 
33 namespace kaldi {
34 
35 
38 
42 typedef std::vector<std::vector<std::pair<int32, BaseFloat> > > Posterior;
43 
51 typedef std::vector<std::vector<std::pair<int32, Vector<BaseFloat> > > > GaussPost;
52 
53 
54 // PosteriorHolder is a holder for Posterior, which is
55 // std::vector<std::vector<std::pair<int32, BaseFloat> > >
56 // This is used for storing posteriors of transition id's for an
57 // utterance.
59  public:
60  typedef Posterior T;
61 
63 
64  static bool Write(std::ostream &os, bool binary, const T &t);
65 
66  void Clear() { Posterior tmp; std::swap(tmp, t_); }
67 
68  // Reads into the holder.
69  bool Read(std::istream &is);
70 
71  // Kaldi objects always have the stream open in binary mode for
72  // reading.
73  static bool IsReadInBinary() { return true; }
74 
75  T &Value() { return t_; }
76 
77  void Swap(PosteriorHolder *other) {
78  t_.swap(other->t_);
79  }
80 
81  bool ExtractRange(const PosteriorHolder &other, const std::string &range) {
82  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
83  return false;
84  }
85  private:
87  T t_;
88 };
89 
90 
92 void WritePosterior(std::ostream &os, bool binary, const Posterior &post);
93 
95 void ReadPosterior(std::istream &os, bool binary, Posterior *post);
96 
97 
98 // GaussPostHolder is a holder for GaussPost, which is
99 // std::vector<std::vector<std::pair<int32, Vector<BaseFloat> > > >
100 // This is used for storing posteriors of transition id's for an
101 // utterance.
103  public:
104  typedef GaussPost T;
105 
107 
108  static bool Write(std::ostream &os, bool binary, const T &t);
109 
110  void Clear() { GaussPost tmp; std::swap(tmp, t_); }
111 
112  // Reads into the holder.
113  bool Read(std::istream &is);
114 
115  // Kaldi objects always have the stream open in binary mode for
116  // reading.
117  static bool IsReadInBinary() { return true; }
118 
119  const T &Value() const { return t_; }
120 
121  void Swap(GaussPostHolder *other) {
122  t_.swap(other->t_);
123  }
124 
125  bool ExtractRange(const GaussPostHolder &other, const std::string &range) {
126  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
127  return false;
128  }
129  private:
131  T t_;
132 };
133 
134 
135 // Posterior is a typedef: vector<vector<pair<int32, BaseFloat> > >,
136 // representing posteriors over (typically) transition-ids for an
137 // utterance.
141 
142 
143 // typedef std::vector<std::vector<std::pair<int32, Vector<BaseFloat> > > > GaussPost;
147 
148 
150 void ScalePosterior(BaseFloat scale, Posterior *post);
151 
153 BaseFloat TotalPosterior(const Posterior &post);
154 
157  const std::vector<std::pair<int32, BaseFloat> > &post_elem1,
158  const std::vector<std::pair<int32, BaseFloat> > &post_elem2);
159 
160 
168 int32 MergePosteriors(const Posterior &post1,
169  const Posterior &post2,
170  bool merge,
171  bool drop_frames,
172  Posterior *post);
173 
174 // comparator object that can be used to sort from greatest to
175 // least posterior.
177  // view this as an "<" operator used for sorting, except it behaves like
178  // a ">" operator on the .second field of the pair because we want the
179  // sort to be in reverse order (greatest to least) on posterior.
180  bool operator() (const std::pair<int32, BaseFloat> &a,
181  const std::pair<int32, BaseFloat> &b) {
182  return (a.second > b.second);
183  }
184 };
185 
196  const VectorBase<BaseFloat> &log_likes,
197  int32 num_gselect,
198  BaseFloat min_post,
199  std::vector<std::pair<int32, BaseFloat> > *post_entry);
200 
203 void AlignmentToPosterior(const std::vector<int32> &ali,
204  Posterior *post);
205 
208 void SortPosteriorByPdfs(const TransitionModel &tmodel,
209  Posterior *post);
210 
211 
214 void ConvertPosteriorToPdfs(const TransitionModel &tmodel,
215  const Posterior &post_in,
216  Posterior *post_out);
217 
220 void ConvertPosteriorToPhones(const TransitionModel &tmodel,
221  const Posterior &post_in,
222  Posterior *post_out);
223 
228 void WeightSilencePost(const TransitionModel &trans_model,
229  const ConstIntegerSet<int32> &silence_set,
230  BaseFloat silence_scale,
231  Posterior *post);
232 
239 void WeightSilencePostDistributed(const TransitionModel &trans_model,
240  const ConstIntegerSet<int32> &silence_set,
241  BaseFloat silence_scale,
242  Posterior *post);
243 
247 template <typename Real>
248 void PosteriorToMatrix(const Posterior &post,
249  const int32 post_dim, Matrix<Real> *mat);
250 
255 template <typename Real>
256 void PosteriorToPdfMatrix(const Posterior &post,
257  const TransitionModel &model,
258  Matrix<Real> *mat);
259 
261 
262 
263 } // end namespace kaldi
264 
265 
266 #endif
void PosteriorToMatrix(const Posterior &post, const int32 post_dim, Matrix< Real > *mat)
This converts a Posterior to a Matrix.
Definition: posterior.cc:512
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
static bool IsReadInBinary()
Definition: posterior.h:117
void PosteriorToPdfMatrix(const Posterior &post, const TransitionModel &model, Matrix< Real > *mat)
This converts a Posterior to a Matrix.
Definition: posterior.cc:539
SequentialTableReader< PosteriorHolder > SequentialPosteriorReader
Definition: posterior.h:139
static bool Write(std::ostream &os, bool binary, const T &t)
Definition: posterior.cc:127
BaseFloat VectorToPosteriorEntry(const VectorBase< BaseFloat > &log_likes, int32 num_gselect, BaseFloat min_post, std::vector< std::pair< int32, BaseFloat > > *post_entry)
Given a vector of log-likelihoods (typically of Gaussians in a GMM but could be of pdf-ids)...
Definition: posterior.cc:440
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
A class for storing matrices.
Definition: kaldi-matrix.h:823
SequentialTableReader< GaussPostHolder > SequentialGaussPostReader
Definition: posterior.h:145
static bool IsReadInBinary()
Definition: posterior.h:73
RandomAccessTableReader< PosteriorHolder > RandomAccessPosteriorReader
Definition: posterior.h:140
BaseFloat TotalPosterior(const Posterior &post)
Returns the total of all the weights in "post".
Definition: posterior.cc:230
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
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
bool Read(std::istream &is)
Definition: posterior.cc:138
void SortPosteriorByPdfs(const TransitionModel &tmodel, Posterior *post)
Sorts posterior entries so that transition-ids with same pdf-id are next to each other.
Definition: posterior.cc:314
void Swap(GaussPostHolder *other)
Definition: posterior.h:121
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
bool ExtractRange(const GaussPostHolder &other, const std::string &range)
Definition: posterior.h:125
KALDI_DISALLOW_COPY_AND_ASSIGN(PosteriorHolder)
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
void AlignmentToPosterior(const std::vector< int32 > &ali, Posterior *post)
Convert an alignment to a posterior (with a scale of 1.0 on each entry).
Definition: posterior.cc:290
#define KALDI_ERR
Definition: kaldi-error.h:147
bool PosteriorEntriesAreDisjoint(const std::vector< std::pair< int32, BaseFloat > > &post_elem1, const std::vector< std::pair< int32, BaseFloat > > &post_elem2)
Returns true if the two lists of pairs have no common .first element.
Definition: posterior.cc:242
RandomAccessTableReader< GaussPostHolder > RandomAccessGaussPostReader
Definition: posterior.h:146
const T & Value() const
Definition: posterior.h:119
void ScalePosterior(BaseFloat scale, Posterior *post)
Scales the BaseFloat (weight) element in the posterior entries.
Definition: posterior.cc:218
TableWriter< GaussPostHolder > GaussPostWriter
Definition: posterior.h:144
void Swap(PosteriorHolder *other)
Definition: posterior.h:77
void WritePosterior(std::ostream &os, bool binary, const Posterior &post)
stand-alone function for writing a Posterior.
Definition: posterior.cc:32
TableWriter< PosteriorHolder > PosteriorWriter
Definition: posterior.h:138
void ConvertPosteriorToPhones(const TransitionModel &tmodel, const Posterior &post_in, Posterior *post_out)
Converts a posterior over transition-ids to be a posterior over phones.
Definition: posterior.cc:348
std::vector< std::vector< std::pair< int32, Vector< BaseFloat > > > > GaussPost
GaussPost is a typedef for storing Gaussian-level posteriors for an utterance.
Definition: posterior.h:51
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
void ConvertPosteriorToPdfs(const TransitionModel &tmodel, const Posterior &post_in, Posterior *post_out)
Converts a posterior over transition-ids to be a posterior over pdf-ids.
Definition: posterior.cc:322
int32 MergePosteriors(const Posterior &post1, const Posterior &post2, bool merge, bool drop_frames, Posterior *post)
Merge two sets of posteriors, which must have the same length.
Definition: posterior.cc:258
void ReadPosterior(std::istream &is, bool binary, Posterior *post)
stand-alone function for reading a Posterior.
Definition: posterior.cc:64
bool ExtractRange(const PosteriorHolder &other, const std::string &range)
Definition: posterior.h:81