34 int32 sz = post.size();
36 for (Posterior::const_iterator iter = post.begin(); iter != post.end(); ++iter) {
37 int32 sz2 = iter->size();
39 for (std::vector<std::pair<int32, BaseFloat> >::const_iterator
40 iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
49 for (Posterior::const_iterator iter = post.begin(); iter != post.end(); ++iter) {
51 for (std::vector<std::pair<int32, BaseFloat> >::const_iterator iter2=iter->begin();
54 os << iter2->first <<
' ' << iter2->second <<
' ';
61 KALDI_ERR <<
"Output stream error writing Posterior.";
69 if (sz < 0 || sz > 10000000)
70 KALDI_ERR <<
"Reading posterior: got negative or improbably large size" 73 for (Posterior::iterator iter = post->begin(); iter != post->end(); ++iter) {
77 KALDI_ERR <<
"Reading posteriors: got negative size";
79 for (std::vector<std::pair<int32, BaseFloat> >::iterator iter2=iter->begin();
91 KALDI_ERR <<
"holder of Posterior: error reading line " << (is.eof() ?
"[eof]" :
"");
92 std::istringstream line_is(line);
96 if (line_is.eof())
break;
102 KALDI_ERR <<
"Reading Posterior object: expecting [, got '" << str
104 "': did you provide alignments instead of posteriors?" :
107 std::vector<std::pair<int32, BaseFloat> > this_vec;
110 if (line_is.peek() ==
']') {
117 KALDI_ERR <<
"Error reading Posterior object (could not get data after \"[\");";
118 this_vec.push_back(std::make_pair(i, p));
120 post->push_back(this_vec);
132 }
catch(
const std::exception &e) {
133 KALDI_WARN <<
"Exception caught writing table of posteriors. " << e.what();
143 KALDI_WARN <<
"Reading Table object, failed reading binary header";
149 }
catch (std::exception &e) {
150 KALDI_WARN <<
"Exception caught reading table of posteriors. " << e.what();
163 for (GaussPost::const_iterator iter = t.begin(); iter != t.end(); ++iter) {
164 int32 sz2 = iter->size();
167 iter2 != iter->end();
170 iter2->second.Write(os, binary);
173 if(!binary) os <<
'\n';
175 }
catch (
const std::exception &e) {
176 KALDI_WARN <<
"Exception caught writing table of posteriors. " << e.what();
186 KALDI_WARN <<
"Reading Table object, failed reading binary header";
193 KALDI_ERR <<
"Reading posteriors: got negative size";
195 for (GaussPost::iterator iter =
t_.begin(); iter !=
t_.end(); ++iter) {
199 KALDI_ERR <<
"Reading posteriors: got negative size";
203 iter2 != iter->end();
206 iter2->second.Read(is, is_binary);
210 }
catch (std::exception &e) {
211 KALDI_WARN <<
"Exception caught reading table of posteriors. " << e.what();
219 if (scale == 1.0)
return;
220 for (
size_t i = 0;
i < post->size();
i++) {
224 for (
size_t j = 0;
j < (*post)[
i].size();
j++)
225 (*post)[
i][
j].second *= scale;
232 size_t T = post.size();
233 for (
size_t t = 0; t <
T; t++) {
234 size_t I = post[t].size();
235 for (
size_t i = 0;
i < I;
i++) {
236 sum += post[t][
i].second;
243 const std::vector<std::pair<int32,BaseFloat> > &post_elem1,
244 const std::vector<std::pair<int32,BaseFloat> > &post_elem2) {
245 unordered_set<int32> set1;
246 for (
size_t i = 0;
i < post_elem1.size();
i++) set1.insert(post_elem1[
i].first);
247 for (
size_t i = 0; i < post_elem2.size(); i++)
248 if (set1.count(post_elem2[i].first) != 0)
return false;
264 post->resize(post1.size());
266 int32 num_disjoint = 0;
267 for (
size_t i = 0;
i < post->size();
i++) {
268 (*post)[
i].reserve(post1[
i].size() + post2[
i].size());
269 (*post)[
i].insert((*post)[
i].end(),
270 post1[
i].begin(), post1[
i].end());
271 (*post)[
i].insert((*post)[
i].end(),
272 post2[
i].begin(), post2[
i].end());
279 std::sort( (*post)[
i].begin(), (*post)[
i].end() );
293 post->resize(ali.size());
294 for (
size_t i = 0;
i < ali.size();
i++) {
295 (*post)[
i].resize(1);
296 (*post)[
i][0].first = ali[
i];
297 (*post)[
i][0].second = 1.0;
304 bool operator() (
const std::pair<int32, BaseFloat> &a,
305 const std::pair<int32, BaseFloat> &b) {
317 for (
size_t i = 0;
i < post->size();
i++) {
318 sort((*post)[
i].begin(), (*post)[
i].end(), compare);
326 post_out->resize(post_in.size());
327 for (
size_t i = 0;
i < post_out->size();
i++) {
328 unordered_map<int32, BaseFloat> pdf_to_post;
329 for (
size_t j = 0;
j < post_in[
i].size();
j++) {
330 int32 tid = post_in[
i][
j].first,
333 if (pdf_to_post.count(pdf_id) == 0)
334 pdf_to_post[pdf_id] = post;
336 pdf_to_post[pdf_id] += post;
338 (*post_out)[
i].reserve(pdf_to_post.size());
339 for (unordered_map<int32, BaseFloat>::const_iterator iter =
340 pdf_to_post.begin(); iter != pdf_to_post.end(); ++iter) {
341 if (iter->second != 0.0)
342 (*post_out)[
i].push_back(
343 std::make_pair(iter->first, iter->second));
352 post_out->resize(post_in.size());
353 for (
size_t i = 0;
i < post_out->size();
i++) {
354 std::map<int32, BaseFloat> phone_to_post;
355 for (
size_t j = 0;
j < post_in[
i].size();
j++) {
356 int32 tid = post_in[
i][
j].first,
359 if (phone_to_post.count(phone_id) == 0)
360 phone_to_post[phone_id] = post;
362 phone_to_post[phone_id] += post;
364 (*post_out)[
i].reserve(phone_to_post.size());
365 for (std::map<int32, BaseFloat>::const_iterator iter =
366 phone_to_post.begin(); iter != phone_to_post.end(); ++iter) {
367 if (iter->second != 0.0)
368 (*post_out)[
i].push_back(
369 std::make_pair(iter->first, iter->second));
379 for (
size_t i = 0;
i < post->size();
i++) {
380 std::vector<std::pair<int32, BaseFloat> > this_post;
381 this_post.reserve((*post)[
i].size());
382 for (
size_t j = 0;
j < (*post)[
i].size();
j++) {
383 int32 tid = (*post)[
i][
j].first,
386 if (silence_set.
count(phone) != 0) {
387 if (silence_scale != 0.0)
388 this_post.push_back(std::make_pair(tid, weight*silence_scale));
390 this_post.push_back(std::make_pair(tid, weight));
393 (*post)[
i].swap(this_post);
402 for (
size_t i = 0;
i < post->size();
i++) {
403 std::vector<std::pair<int32, BaseFloat> > this_post;
404 this_post.reserve((*post)[
i].size());
405 BaseFloat sil_weight = 0.0, nonsil_weight = 0.0;
406 for (
size_t j = 0;
j < (*post)[
i].size();
j++) {
407 int32 tid = (*post)[
i][
j].first,
410 if (silence_set.
count(phone) != 0) sil_weight += weight;
411 else nonsil_weight += weight;
413 KALDI_ASSERT(sil_weight >= 0.0 && nonsil_weight >= 0.0);
415 if (sil_weight + nonsil_weight == 0.0)
continue;
416 BaseFloat frame_scale = (sil_weight * silence_scale + nonsil_weight) /
417 (sil_weight + nonsil_weight);
418 if (frame_scale != 0.0) {
419 for (
size_t j = 0;
j < (*post)[
i].size();
j++) {
420 int32 tid = (*post)[
i][
j].first;
422 this_post.push_back(std::make_pair(tid, weight * frame_scale));
425 (*post)[
i].swap(this_post);
430 const std::vector<std::pair<int32, BaseFloat> > &post_entry) {
432 std::vector<std::pair<int32, BaseFloat> >::const_iterator
433 iter = post_entry.begin(), end = post_entry.end();
434 for (; iter != end; ++iter) {
444 std::vector<std::pair<int32, BaseFloat> > *post_entry) {
445 KALDI_ASSERT(num_gselect > 0 && min_post >= 0 && min_post < 1.0);
451 if (num_gselect > num_gauss)
452 num_gselect = num_gauss;
453 std::vector<std::pair<int32, BaseFloat> > temp_post;
455 if (min_post != 0.0) {
457 for (
int32 g = 0; g < num_gauss; g++) {
459 if (like > like_cutoff) {
461 temp_post.push_back(std::pair<int32, BaseFloat>(g, post));
465 if (temp_post.empty()) {
468 temp_post.resize(num_gauss);
469 for (
int32 g = 0; g < num_gauss; g++)
470 temp_post[g] = std::pair<int32, BaseFloat>(g,
Exp(log_likes(g) - max_like));
474 if (static_cast<int32>(temp_post.size()) > num_gselect * 2) {
478 std::nth_element(temp_post.begin(),
479 temp_post.begin() + num_gselect, temp_post.end(),
481 std::sort(temp_post.begin(), temp_post.begin() + num_gselect,
484 std::sort(temp_post.begin(), temp_post.end(), compare);
487 size_t num_to_insert = std::min<size_t>(temp_post.size(),
491 post_entry->insert(post_entry->end(),
492 temp_post.begin(), temp_post.begin() + num_to_insert);
495 cutoff = min_post * tot_post;
497 while (post_entry->size() > 1 && post_entry->back().second < cutoff) {
498 tot_post -= post_entry->back().second;
499 post_entry->pop_back();
503 auto end = post_entry->end();
504 for (
auto iter = post_entry->begin(); iter != end; ++iter)
505 iter->second *= inv_tot;
507 return max_like + log(tot_post);
511 template <
typename Real>
515 int32 num_rows = post.size();
518 for (
int32 t = 0; t < post.size(); t++) {
519 for (
int32 i = 0;
i < post[t].size();
i++) {
520 int32 col = post[t][
i].first;
521 if (col >= post_dim) {
522 KALDI_ERR <<
"Out-of-bound Posterior element with index " << col
523 <<
", higher than number of columns " << post_dim;
525 (*mat)(t, col) = post[t][
i].second;
531 const int32 post_dim,
534 const int32 post_dim,
538 template <
typename Real>
543 int32 num_rows = post.size(),
547 for (
int32 t = 0; t < post.size(); t++) {
548 for (
int32 i = 0;
i < post[t].size();
i++) {
550 if (col >= num_cols) {
551 KALDI_ERR <<
"Out-of-bound Posterior element with index " << col
552 <<
", higher than number of columns " << num_cols;
554 (*mat)(t, col) += post[t][
i].second;
void PosteriorToMatrix(const Posterior &post, const int32 post_dim, Matrix< Real > *mat)
This converts a Posterior to a Matrix.
const TransitionModel * tmodel_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
bool Read(std::istream &is)
void PosteriorToPdfMatrix(const Posterior &post, const TransitionModel &model, Matrix< Real > *mat)
This converts a Posterior to a Matrix.
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
static bool Write(std::ostream &os, bool binary, const T &t)
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)...
A class for storing matrices.
int32 TransitionIdToPdf(int32 trans_id) const
BaseFloat TotalPosterior(const Posterior &post)
Returns the total of all the weights in "post".
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.
bool Read(std::istream &is)
void SortPosteriorByPdfs(const TransitionModel &tmodel, Posterior *post)
Sorts posterior entries so that transition-ids with same pdf-id are next to each other.
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
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 ...
ComparePosteriorByPdfs(const TransitionModel &tmodel)
void AlignmentToPosterior(const std::vector< int32 > &ali, Posterior *post)
Convert an alignment to a posterior (with a scale of 1.0 on each entry).
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.
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
MatrixIndexT Dim() const
Returns the dimension of the vector.
template void PosteriorToMatrix< float >(const Posterior &post, const int32 post_dim, Matrix< float > *mat)
static bool Write(std::ostream &os, bool binary, const T &t)
static BaseFloat GetTotalPosterior(const std::vector< std::pair< int32, BaseFloat > > &post_entry)
void ScalePosterior(BaseFloat scale, Posterior *post)
Scales the BaseFloat (weight) element in the posterior entries.
A class representing a vector.
template void PosteriorToPdfMatrix< double >(const Posterior &post, const TransitionModel &model, Matrix< double > *mat)
#define KALDI_ASSERT(cond)
void MergePairVectorSumming(std::vector< std::pair< I, F > > *vec)
For a vector of pair<I, F> where I is an integer and F a floating-point or integer type...
template void PosteriorToPdfMatrix< float >(const Posterior &post, const TransitionModel &model, Matrix< float > *mat)
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
void WritePosterior(std::ostream &os, bool binary, const Posterior &post)
stand-alone function for writing a Posterior.
void ConvertPosteriorToPhones(const TransitionModel &tmodel, const Posterior &post_in, Posterior *post_out)
Converts a posterior over transition-ids to be a posterior over phones.
Provides a vector abstraction class.
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.
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.
int32 TransitionIdToPhone(int32 trans_id) const
void ReadPosterior(std::istream &is, bool binary, Posterior *post)
stand-alone function for reading a Posterior.
template void PosteriorToMatrix< double >(const Posterior &post, const int32 post_dim, Matrix< double > *mat)