lattice-combine.cc
9.31 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// latbin/lattice-combine.cc
// Copyright 2012 Arnab Ghoshal
// 2016 Johns Hopkins University (author: Daniel Povey)
// 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.
// This program is for system combination using MBR decoding as described in:
// "Minimum Bayes Risk decoding and system combination based on a recursion for
// edit distance", Haihua Xu, Daniel Povey, Lidia Mangu and Jie Zhu, Computer
// Speech and Language, 2011. However, instead of averaging the posteriors, as
// described in the paper, this removes the total backward probability from the
// individual lattices being combined and outputs the union of them. The output
// should be used with lattice-mbr-decode (without any acoustic or LM scaling)
// or with lattice-to-ctm-conf with --decode-mbr=true (also without any scaling)
// IMPORTANT CAVEAT: the total backward probability (which is a float) is
// removed from value1_ of arc weight. So graph scores are no longer correct
// but instead only the combined acoustic and graph scores are valid. So no
// acoustic or LM scaling should be done with the output of this program.
#include <string>
using std::string;
#include <vector>
using std::vector;
#include "util/common-utils.h"
#include "lat/lattice-functions.h"
#include "lat/kaldi-lattice.h"
#include "lat/sausages.h"
namespace kaldi {
// This removes the total weight from a CompactLattice. Since the total backward
// score is in log likelihood domain, and the lattice weights are in negative
// log likelihood domain, the total weight is *added* to the weight of the final
// states. This is equivalent to dividing the probability of each path by the
// total probability over all paths. There is an additional weight to control
// the relative contribution of individual lattices-- the log of the weight will
// become the total weight of the lattice.
bool CompactLatticeNormalize(CompactLattice *clat, BaseFloat weight) {
if (weight <= 0.0) {
KALDI_WARN << "Weights must be positive; found: " << weight;
return false;
}
if (clat->Properties(fst::kTopSorted, false) == 0) {
if (fst::TopSort(clat) == false) {
KALDI_WARN << "Cycles detected in lattice: cannot normalize.";
return false;
}
}
vector<double> beta;
if (!ComputeCompactLatticeBetas(*clat, &beta)) {
KALDI_WARN << "Failed to compute backward probabilities on lattice.";
return false;
}
typedef CompactLattice::Arc::StateId StateId;
StateId start = clat->Start(); // Should be 0
BaseFloat total_backward_cost = beta[start];
total_backward_cost -= Log(weight);
for (fst::StateIterator<CompactLattice> sit(*clat); !sit.Done(); sit.Next()) {
CompactLatticeWeight f = clat->Final(sit.Value());
LatticeWeight w = f.Weight();
w.SetValue1(w.Value1() + total_backward_cost);
f.SetWeight(w);
clat->SetFinal(sit.Value(), f);
}
return true;
}
// This is a wrapper for SplitStringToFloats, with added checks to make sure
// the weights are valid probabilities.
void SplitStringToWeights(const string &full, const char *delim,
vector<BaseFloat> *out) {
vector<BaseFloat> tmp;
SplitStringToFloats(full, delim, true /*omit empty strings*/, &tmp);
if (tmp.size() != out->size()) {
KALDI_WARN << "Expecting " << out->size() << " weights, found " << tmp.size()
<< ": using uniform weights.";
return;
}
BaseFloat sum = 0;
for (vector<BaseFloat>::const_iterator itr = tmp.begin();
itr != tmp.end(); ++itr) {
if (*itr < 0.0) {
KALDI_WARN << "Cannot use negative weight: " << *itr << "; input string: "
<< full << "\n\tUsing uniform weights.";
return;
}
sum += (*itr);
}
if (sum != 1.0) {
KALDI_WARN << "Weights sum to " << sum << " instead of 1: renormalizing";
for (vector<BaseFloat>::iterator itr = tmp.begin();
itr != tmp.end(); ++itr)
(*itr) /= sum;
}
out->swap(tmp);
}
} // end namespace kaldi
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
typedef kaldi::int32 int32;
const char *usage =
"Combine lattices generated by different systems by removing the total\n"
"cost of all paths (backward cost) from individual lattices and doing\n"
"a union of the reweighted lattices. Note: the acoustic and LM scales\n"
"that this program applies are not removed before outputting the lattices.\n"
"Intended for use in system combination prior to MBR decoding, see comments\n"
"in code.\n"
"Usage: lattice-combine [options] <lattice-rspecifier1> <lattice-rspecifier2>"
" [<lattice-rspecifier3> ... ] <lattice-wspecifier>\n"
"E.g.: lattice-combine 'ark:gunzip -c foo/lat.1.gz|' 'ark:gunzip -c bar/lat.1.gz|' ark:- | ...\n";
ParseOptions po(usage);
BaseFloat acoustic_scale = 1.0, inv_acoustic_scale = 1.0, lm_scale = 1.0;
string weight_str;
po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for "
"acoustic likelihoods");
po.Register("inv-acoustic-scale", &inv_acoustic_scale, "An alternative way "
"of setting the acoustic scale: you can set its inverse.");
po.Register("lm-scale", &lm_scale, "Scaling factor for language model "
"probabilities");
po.Register("lat-weights", &weight_str, "Colon-separated list of weights "
"for each rspecifier (which should sum to 1), e.g. '0.2:0.8'");
po.Read(argc, argv);
KALDI_ASSERT(acoustic_scale == 1.0 || inv_acoustic_scale == 1.0);
if (inv_acoustic_scale != 1.0)
acoustic_scale = 1.0 / inv_acoustic_scale;
int32 num_args = po.NumArgs();
if (num_args < 3) {
po.PrintUsage();
exit(1);
}
string lats_rspecifier1 = po.GetArg(1),
lats_wspecifier = po.GetArg(num_args);
// Output lattice
CompactLatticeWriter clat_writer(lats_wspecifier);
// Input lattices
SequentialCompactLatticeReader clat_reader1(lats_rspecifier1);
vector<RandomAccessCompactLatticeReader*> clat_reader_vec(
num_args-2, static_cast<RandomAccessCompactLatticeReader*>(NULL));
vector<string> clat_rspec_vec(num_args-2);
for (int32 i = 2; i < num_args; ++i) {
clat_reader_vec[i-2] = new RandomAccessCompactLatticeReader(po.GetArg(i));
clat_rspec_vec[i-2] = po.GetArg(i);
}
vector<BaseFloat> lat_weights(num_args-1, 1.0/(num_args-1));
if (!weight_str.empty())
SplitStringToWeights(weight_str, ":", &lat_weights);
int32 n_utts = 0, n_total_lats = 0, n_success = 0, n_missing = 0,
n_other_errors = 0;
vector< vector<double> > lat_scale = fst::LatticeScale(lm_scale,
acoustic_scale);
for (; !clat_reader1.Done(); clat_reader1.Next()) {
std::string key = clat_reader1.Key();
CompactLattice clat1 = clat_reader1.Value();
clat_reader1.FreeCurrent();
n_utts++;
n_total_lats++;
fst::ScaleLattice(lat_scale, &clat1);
bool success = CompactLatticeNormalize(&clat1, lat_weights[0]);
if (!success) {
KALDI_WARN << "Could not normalize lattice for system 1, utterance: "
<< key;
n_other_errors++;
continue;
}
for (int32 i = 0; i < num_args-2; ++i) {
if (clat_reader_vec[i]->HasKey(key)) {
CompactLattice clat2 = clat_reader_vec[i]->Value(key);
n_total_lats++;
fst::ScaleLattice(lat_scale, &clat2);
success = CompactLatticeNormalize(&clat2, lat_weights[i+1]);
if (!success) {
KALDI_WARN << "Could not normalize lattice for system "<< (i + 2)
<< ", utterance: " << key;
n_other_errors++;
continue;
}
fst::Union(&clat1, clat2);
} else {
KALDI_WARN << "No lattice found for utterance " << key << " for "
<< "system " << (i + 2) << ", rspecifier: "
<< clat_rspec_vec[i];
n_missing++;
}
}
clat_writer.Write(key, clat1);
n_success++;
}
KALDI_LOG << "Processed " << n_utts << " utterances: with a total of "
<< n_total_lats << " lattices across " << (num_args-1)
<< " different systems";
KALDI_LOG << "Produced output for " << n_success << " utterances; "
<< n_missing << " total missing lattices and " << n_other_errors
<< " total lattices had errors in processing.";
DeletePointers(&clat_reader_vec);
// The success code we choose is that at least one lattice was output,
// and more lattices were "all there" than had at least one system missing.
return (n_success != 0 && n_missing < (n_success - n_missing) ? 0 : 1);
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}