2222#include " hmm/transition-model.h"
2323#include " hmm/hmm-utils.h"
2424
25+ namespace kaldi {
26+
27+ void WeightSilencePost (const TransitionModel &trans_model,
28+ const ConstIntegerSet<int32> &silence_set,
29+ BaseFloat silence_scale,
30+ Posterior *post ) {
31+ for (size_t i = 0 ; i < post ->size (); i++) {
32+ std::vector<std::pair<int32, BaseFloat> > this_post;
33+ this_post.reserve ((*post )[i].size ());
34+ for (size_t j = 0 ; j < (*post )[i].size (); j++) {
35+ int32 tid = (*post )[i][j].first ,
36+ phone = trans_model.TransitionIdToPhone (tid);
37+ BaseFloat weight = (*post )[i][j].second ;
38+ if (silence_set.count (phone) != 0 ) { // is a silence.
39+ if (silence_scale != 0.0 )
40+ this_post.push_back (std::make_pair (tid, weight*silence_scale));
41+ } else {
42+ this_post.push_back (std::make_pair (tid, weight));
43+ }
44+ }
45+ (*post )[i].swap (this_post);
46+ }
47+ }
48+
49+
50+ void WeightSilencePostDistributed (const TransitionModel &trans_model,
51+ const ConstIntegerSet<int32> &silence_set,
52+ BaseFloat silence_scale,
53+ Posterior *post ) {
54+ for (size_t i = 0 ; i < post ->size (); i++) {
55+ std::vector<std::pair<int32, BaseFloat> > this_post;
56+ this_post.reserve ((*post )[i].size ());
57+ BaseFloat sil_weight = 0.0 , nonsil_weight = 0.0 ;
58+ for (size_t j = 0 ; j < (*post )[i].size (); j++) {
59+ int32 tid = (*post )[i][j].first ,
60+ phone = trans_model.TransitionIdToPhone (tid);
61+ BaseFloat weight = (*post )[i][j].second ;
62+ if (silence_set.count (phone) != 0 ) sil_weight += weight;
63+ else nonsil_weight += weight;
64+ }
65+ KALDI_ASSERT (sil_weight >= 0.0 && nonsil_weight >= 0.0 ); // This "distributed"
66+ // weighting approach doesn't make sense if we have negative weights.
67+ if (sil_weight + nonsil_weight == 0.0 ) continue ;
68+ BaseFloat frame_scale = (sil_weight * silence_scale + nonsil_weight) /
69+ (sil_weight + nonsil_weight);
70+ if (frame_scale != 0.0 ) {
71+ for (size_t j = 0 ; j < (*post )[i].size (); j++) {
72+ int32 tid = (*post )[i][j].first ;
73+ BaseFloat weight = (*post )[i][j].second ;
74+ this_post.push_back (std::make_pair (tid, weight * frame_scale));
75+ }
76+ }
77+ (*post )[i].swap (this_post);
78+ }
79+ }
2580
81+ } // end namespace
2682
2783int main (int argc, char *argv[]) {
2884 using namespace kaldi ;
@@ -37,6 +93,12 @@ int main(int argc, char *argv[]) {
3793
3894 ParseOptions po (usage);
3995
96+ bool distribute = false ;
97+
98+ po.Register (" distribute" , &distribute, " If true, rather than weighting the "
99+ " individual posteriors, apply the weighting to the whole frame: "
100+ " i.e. on time t, scale all posterior entries by "
101+ " p(sil)*silence-weight + p(non-sil)*1.0" );
40102
41103 po.Read (argc, argv);
42104
@@ -45,7 +107,6 @@ int main(int argc, char *argv[]) {
45107 exit (1 );
46108 }
47109
48-
49110 std::string silence_weight_str = po.GetArg (1 ),
50111 silence_phones_str = po.GetArg (2 ),
51112 model_rxfilename = po.GetArg (3 ),
@@ -73,25 +134,15 @@ int main(int argc, char *argv[]) {
73134 for (; !posterior_reader.Done (); posterior_reader.Next ()) {
74135 num_posteriors++;
75136 // Posterior is vector<vector<pair<int32, BaseFloat> > >
76- const Posterior &posterior = posterior_reader.Value ();
77- // Posterior is vector<vector<pair<int32, BaseFloat> > >
78- Posterior new_post (posterior.size ());
79-
80- for (size_t i = 0 ; i < posterior.size (); i++) {
81- new_post[i].reserve (posterior[i].size ()); // more efficient.
82- for (size_t j = 0 ; j < posterior[i].size (); j++) {
83- int32 tid = posterior[i][j].first ,
84- phone = trans_model.TransitionIdToPhone (tid);
85- BaseFloat weight = posterior[i][j].second ;
86- if (silence_set.count (phone) != 0 ) { // is a silence.
87- if (silence_weight != 0.0 )
88- new_post[i].push_back (std::make_pair (tid, weight*silence_weight));
89- } else {
90- new_post[i].push_back (std::make_pair (tid, weight));
91- }
92- }
137+ Posterior post = posterior_reader.Value ();
138+
139+ if ( distribute ) {
140+ WeightSilencePostDistributed (trans_model, silence_set, silence_weight, &post );
141+ } else {
142+ WeightSilencePost (trans_model, silence_set, silence_weight, &post );
93143 }
94- posterior_writer.Write (posterior_reader.Key (), new_post);
144+
145+ posterior_writer.Write (posterior_reader.Key (), post );
95146 }
96147 KALDI_LOG << " Done " << num_posteriors << " posteriors." ;
97148 return (num_posteriors != 0 ? 0 : 1 );
0 commit comments