lattice-to-post.cc
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// latbin/lattice-to-post.cc
// Copyright 2009-2011 Saarland University
// Author: Arnab Ghoshal
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "fstext/fstext-lib.h"
#include "lat/kaldi-lattice.h"
#include "lat/lattice-functions.h"
int main(int argc, char *argv[]) {
try {
typedef kaldi::int32 int32;
using fst::SymbolTable;
using fst::VectorFst;
using fst::StdArc;
const char *usage =
"Do forward-backward and collect posteriors over lattices.\n"
"Usage: lattice-to-post [options] lats-rspecifier posts-wspecifier [loglikes-wspecifier]\n"
" e.g.: lattice-to-post --acoustic-scale=0.1 ark:1.lats ark:1.post\n"
"See also: lattice-to-ctm-conf, post-to-pdf-post, lattice-arc-post\n";
kaldi::BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
kaldi::ParseOptions po(usage);
po.Register("acoustic-scale", &acoustic_scale,
"Scaling factor for acoustic likelihoods");
po.Register("lm-scale", &lm_scale,
"Scaling factor for \"graph costs\" (including LM costs)");
po.Read(argc, argv);
if (po.NumArgs() < 2 || po.NumArgs() > 3) {
po.PrintUsage();
exit(1);
}
if (acoustic_scale == 0.0)
KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
std::string lats_rspecifier = po.GetArg(1),
posteriors_wspecifier = po.GetArg(2),
loglikes_wspecifier = po.GetOptArg(3);
// Read as regular lattice
kaldi::SequentialLatticeReader lattice_reader(lats_rspecifier);
kaldi::PosteriorWriter posterior_writer(posteriors_wspecifier);
kaldi::BaseFloatWriter loglikes_writer(loglikes_wspecifier);
int32 n_done = 0;
double total_like = 0.0, lat_like;
double total_ac_like = 0.0, lat_ac_like; // acoustic likelihood weighted by posterior.
double total_time = 0, lat_time;
for (; !lattice_reader.Done(); lattice_reader.Next()) {
std::string key = lattice_reader.Key();
kaldi::Lattice lat = lattice_reader.Value();
// FreeCurrent() is an optimization that prevents the lattice from being
// copied unnecessarily (OpenFst does copy-on-write).
lattice_reader.FreeCurrent();
if (acoustic_scale != 1.0 || lm_scale != 1.0)
fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &lat);
kaldi::uint64 props = lat.Properties(fst::kFstProperties, false);
if (!(props & fst::kTopSorted)) {
if (fst::TopSort(&lat) == false)
KALDI_ERR << "Cycles detected in lattice.";
}
kaldi::Posterior post;
lat_like = kaldi::LatticeForwardBackward(lat, &post, &lat_ac_like);
total_like += lat_like;
lat_time = post.size();
total_time += lat_time;
total_ac_like += lat_ac_like;
KALDI_VLOG(2) << "Processed lattice for utterance: " << key << "; found "
<< lat.NumStates() << " states and " << fst::NumArcs(lat)
<< " arcs. Average log-likelihood = " << (lat_like/lat_time)
<< " over " << lat_time << " frames. Average acoustic log-like"
<< " per frame is " << (lat_ac_like/lat_time);
if (loglikes_writer.IsOpen())
loglikes_writer.Write(key, lat_like);
posterior_writer.Write(key, post);
n_done++;
}
KALDI_LOG << "Overall average log-like/frame is "
<< (total_like/total_time) << " over " << total_time
<< " frames. Average acoustic like/frame is "
<< (total_ac_like/total_time);
KALDI_LOG << "Done " << n_done << " lattices.";
return (n_done != 0 ? 0 : 1);
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}