Skip to content

Commit c4bb866

Browse files
committed
[src] Add more diagnostic output to lattice determinization programs
1 parent 1582c77 commit c4bb866

File tree

6 files changed

+119
-49
lines changed

6 files changed

+119
-49
lines changed

src/lat/minimize-lattice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace fst {
4040
/// function will not combine as many states as it could, but it won't crash.
4141
/// Returns true on success, and false if it failed due to topological sorting
4242
/// failing.
43+
/// The output will be topologically sorted.
4344
template<class Weight, class IntType>
4445
bool MinimizeCompactLattice(
4546
MutableFst<ArcTpl<CompactLatticeWeightTpl<Weight, IntType> > > *clat,

src/latbin/lattice-depth.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ int main(int argc, char *argv[]) {
3434
using fst::VectorFst;
3535
using fst::StdArc;
3636
typedef StdArc::StateId StateId;
37-
37+
3838
const char *usage =
3939
"Compute the lattice depths in terms of the average number of arcs that\n"
4040
"cross a frame. See also lattice-depth-per-frame\n"
4141
"Usage: lattice-depth <lattice-rspecifier> [<depth-wspecifier>]\n"
4242
"E.g.: lattice-depth ark:- ark,t:-\n";
4343

4444
ParseOptions po(usage);
45-
45+
4646
po.Read(argc, argv);
4747

4848
if (po.NumArgs() < 1 || po.NumArgs() > 2) {
@@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
6363
std::string key = clat_reader.Key();
6464

6565
TopSortCompactLatticeIfNeeded(&clat);
66-
66+
6767
int32 t;
6868
BaseFloat depth = CompactLatticeDepth(clat, &t);
6969

src/latbin/lattice-determinize-non-compact.cc

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ bool DeterminizeLatticeWrapper(const Lattice &lat,
5656
KALDI_WARN << "Detected empty lattice, skipping " << key;
5757
return false;
5858
}
59-
60-
// The work gets done in the next line.
61-
if (DeterminizeLattice(lat, clat, lat_opts, NULL)) {
59+
60+
// The work gets done in the next line.
61+
if (DeterminizeLattice(lat, clat, lat_opts, NULL)) {
6262
if (prune) PruneLattice(cur_beam, clat);
6363
return true;
6464
} else { // failed to determinize..
@@ -91,14 +91,14 @@ bool DeterminizeLatticeWrapper(const Lattice &lat,
9191
}
9292

9393
void ComputeAcousticScoresMap(
94-
const Lattice &lat,
95-
unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
94+
const Lattice &lat,
95+
unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
9696
PairHasher<int32> > *acoustic_scores) {
9797
acoustic_scores->clear();
9898

9999
std::vector<int32> state_times;
100100
LatticeStateTimes(lat, &state_times);
101-
101+
102102
KALDI_ASSERT(lat.Start() == 0);
103103

104104
for (StateId s = 0; s < lat.NumStates(); s++) {
@@ -111,17 +111,17 @@ void ComputeAcousticScoresMap(
111111
int32 tid = arc.ilabel;
112112

113113
if (tid != 0) {
114-
unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
114+
unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
115115
PairHasher<int32> >::iterator it = acoustic_scores->find(std::make_pair(t, tid));
116116
if (it == acoustic_scores->end()) {
117-
acoustic_scores->insert(std::make_pair(std::make_pair(t, tid),
117+
acoustic_scores->insert(std::make_pair(std::make_pair(t, tid),
118118
std::make_pair(weight.Value2(), 1)));
119119
} else {
120-
if (it->second.second == 2
120+
if (it->second.second == 2
121121
&& it->second.first / it->second.second != weight.Value2()) {
122122
KALDI_VLOG(2) << "Transitions on the same frame have different "
123-
<< "acoustic costs for tid " << tid << "; "
124-
<< it->second.first / it->second.second
123+
<< "acoustic costs for tid " << tid << "; "
124+
<< it->second.first / it->second.second
125125
<< " vs " << weight.Value2();
126126
}
127127
it->second.first += weight.Value2();
@@ -135,33 +135,33 @@ void ComputeAcousticScoresMap(
135135

136136
LatticeWeight f = lat.Final(s);
137137
if (f != LatticeWeight::Zero()) {
138-
// Final acoustic cost must be 0 as we are reading from
138+
// Final acoustic cost must be 0 as we are reading from
139139
// non-determinized, non-compact lattice
140140
KALDI_ASSERT(f.Value2() == 0.0);
141141
}
142142
}
143143
}
144144

145145
void ReplaceAcousticScoresFromMap(
146-
const unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
146+
const unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
147147
PairHasher<int32> > &acoustic_scores,
148148
Lattice *lat) {
149149
fst::TopSort(lat);
150-
150+
151151
std::vector<int32> state_times;
152152
LatticeStateTimes(*lat, &state_times);
153-
153+
154154
KALDI_ASSERT(lat->Start() == 0);
155155

156156
for (StateId s = 0; s < lat->NumStates(); s++) {
157157
int32 t = state_times[s];
158-
for (fst::MutableArcIterator<Lattice> aiter(lat, s);
158+
for (fst::MutableArcIterator<Lattice> aiter(lat, s);
159159
!aiter.Done(); aiter.Next()) {
160160
Arc arc(aiter.Value());
161-
161+
162162
int32 tid = arc.ilabel;
163163
if (tid != 0) {
164-
unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
164+
unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
165165
PairHasher<int32> >::const_iterator it = acoustic_scores.find(std::make_pair(t, tid));
166166
if (it == acoustic_scores.end()) {
167167
KALDI_ERR << "Could not find tid " << tid << " at time " << t
@@ -207,7 +207,7 @@ int main(int argc, char *argv[]) {
207207
"\n"
208208
"Usage: lattice-determinize-non-compact [options] lattice-rspecifier lattice-wspecifier\n"
209209
" e.g.: lattice-determinize-non-compact --acoustic-scale=0.1 --beam=15.0 ark:1.lats ark:det.lats\n";
210-
210+
211211
ParseOptions po(usage);
212212
BaseFloat acoustic_scale = 1.0;
213213
BaseFloat beam = 10.0;
@@ -218,7 +218,7 @@ int main(int argc, char *argv[]) {
218218
BaseFloat delta = fst::kDelta;
219219
bool prune = false;
220220
bool minimize = false;
221-
221+
222222
po.Register("acoustic-scale", &acoustic_scale,
223223
"Scaling factor for acoustic likelihoods");
224224
po.Register("beam", &beam,
@@ -238,7 +238,7 @@ int main(int argc, char *argv[]) {
238238
"decrease beam by beam-ratio if determinization fails.");
239239
po.Register("minimize", &minimize,
240240
"If true, push and minimize after determinization");
241-
241+
242242
po.Read(argc, argv);
243243

244244
if (po.NumArgs() != 2) {
@@ -252,34 +252,38 @@ int main(int argc, char *argv[]) {
252252
// Read as regular lattice-- this is the form we need it in for efficient
253253
// pruning.
254254
SequentialLatticeReader lattice_reader(lats_rspecifier);
255-
255+
256256
// Write as regular lattice.
257-
LatticeWriter lattice_writer(lats_wspecifier);
257+
LatticeWriter lattice_writer(lats_wspecifier);
258258

259259
int32 n_done = 0, n_error = 0;
260260

261+
// depth stats (for diagnostics).
262+
double sum_depth_in = 0.0,
263+
sum_depth_out = 0.0, sum_t = 0.0;
264+
261265
if (acoustic_scale == 0.0)
262266
KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
263267
LatticeWeight beam_weight(beam, static_cast<BaseFloat>(0.0));
264268

265269
for (; !lattice_reader.Done(); lattice_reader.Next()) {
266270
std::string key = lattice_reader.Key();
267271
Lattice lat = lattice_reader.Value();
268-
272+
269273
lattice_reader.FreeCurrent();
270-
274+
271275
fst::TopSort(&lat);
272-
276+
273277
fst::ScaleLattice(fst::AcousticLatticeScale(acoustic_scale), &lat);
274278

275279

276-
// Compute a map from each (t, tid) to (sum_of_acoustic_scores, count)
277-
unordered_map<std::pair<int32,int32>, std::pair<BaseFloat, int32>,
280+
// Compute a map from each (t, tid) to (sum_of_acoustic_scores, count)
281+
unordered_map<std::pair<int32,int32>, std::pair<BaseFloat, int32>,
278282
PairHasher<int32> > acoustic_scores;
279283
ComputeAcousticScoresMap(lat, &acoustic_scores);
280-
284+
281285
Invert(&lat); // make it so word labels are on the input.
282-
286+
283287
CompactLattice clat;
284288
if (DeterminizeLatticeWrapper(lat, key, prune,
285289
beam, beam_ratio, max_mem, max_loop,
@@ -290,6 +294,13 @@ int main(int argc, char *argv[]) {
290294
MinimizeCompactLattice(&clat);
291295
}
292296

297+
int32 t;
298+
TopSortCompactLatticeIfNeeded(&clat);
299+
double depth = CompactLatticeDepth(clat, &t);
300+
sum_depth_in += lat.NumStates();
301+
sum_depth_out += depth * t;
302+
sum_t += t;
303+
293304
Lattice out_lat;
294305
fst::ConvertLattice(clat, &out_lat);
295306
fst::TopSort(&out_lat);
@@ -298,7 +309,7 @@ int main(int argc, char *argv[]) {
298309
// the computed map
299310
ReplaceAcousticScoresFromMap(acoustic_scores, &out_lat);
300311

301-
fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale),
312+
fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale),
302313
&out_lat);
303314
lattice_writer.Write(key, out_lat);
304315
n_done++;
@@ -307,11 +318,16 @@ int main(int argc, char *argv[]) {
307318
}
308319
}
309320

321+
if (sum_t != 0.0) {
322+
KALDI_LOG << "Average input-lattice depth (measured at at state level) is "
323+
<< (sum_depth_in / sum_t) << ", output depth is "
324+
<< (sum_depth_out / sum_t) << ", over " << sum_t << "frames "
325+
<< " (average num-frames = " << (sum_t / n_done) << ").";
326+
}
310327
KALDI_LOG << "Done " << n_done << " lattices, errors on " << n_error;
311328
return (n_done != 0 ? 0 : 1);
312329
} catch(const std::exception &e) {
313330
std::cerr << e.what();
314331
return -1;
315332
}
316333
}
317-

src/latbin/lattice-determinize-phone-pruned.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int main(int argc, char *argv[]) {
2828
try {
2929
using namespace kaldi;
3030
typedef kaldi::int32 int32;
31-
31+
3232
const char *usage =
3333
"Determinize lattices, keeping only the best path (sequence of\n"
3434
"acoustic states) for each input-symbol sequence. This version does\n"
@@ -41,13 +41,13 @@ int main(int argc, char *argv[]) {
4141
" <lattice-rspecifier> <lattice-wspecifier>\n"
4242
" e.g.: lattice-determinize-phone-pruned --acoustic-scale=0.1 \\\n"
4343
" final.mdl ark:in.lats ark:det.lats\n";
44-
44+
4545
ParseOptions po(usage);
4646
BaseFloat acoustic_scale = 1.0;
4747
BaseFloat beam = 10.0;
4848
fst::DeterminizeLatticePhonePrunedOptions opts;
4949
opts.max_mem = 50000000;
50-
50+
5151
po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic"
5252
" likelihoods.");
5353
po.Register("beam", &beam, "Pruning beam [applied after acoustic scaling].");
@@ -69,12 +69,16 @@ int main(int argc, char *argv[]) {
6969
// Reads as regular lattice-- this is the form the determinization code
7070
// accepts.
7171
SequentialLatticeReader lat_reader(lats_rspecifier);
72-
72+
7373
// Writes as compact lattice.
74-
CompactLatticeWriter compact_lat_writer(lats_wspecifier);
74+
CompactLatticeWriter compact_lat_writer(lats_wspecifier);
7575

7676
int32 n_done = 0, n_warn = 0;
7777

78+
// depth stats (for diagnostics).
79+
double sum_depth_in = 0.0,
80+
sum_depth_out = 0.0, sum_t = 0.0;
81+
7882
if (acoustic_scale == 0.0)
7983
KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
8084

@@ -95,11 +99,24 @@ int main(int argc, char *argv[]) {
9599
n_warn++;
96100
}
97101

102+
int32 t;
103+
TopSortCompactLatticeIfNeeded(&det_clat);
104+
double depth = CompactLatticeDepth(det_clat, &t);
105+
sum_depth_in += lat.NumStates();
106+
sum_depth_out += depth * t;
107+
sum_t += t;
108+
98109
fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale), &det_clat);
99110
compact_lat_writer.Write(key, det_clat);
100111
n_done++;
101112
}
102113

114+
if (sum_t != 0.0) {
115+
KALDI_LOG << "Average input-lattice depth (measured at at state level) is "
116+
<< (sum_depth_in / sum_t) << ", output depth is "
117+
<< (sum_depth_out / sum_t) << ", over " << sum_t << " frames "
118+
<< " (average num-frames = " << (sum_t / n_done) << ").";
119+
}
103120
KALDI_LOG << "Done " << n_done << " lattices, determinization finished "
104121
<< "earlier than specified by the beam on " << n_warn << " of "
105122
<< "these.";

src/latbin/lattice-determinize-pruned.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ int main(int argc, char *argv[]) {
7474

7575
int32 n_done = 0, n_warn = 0;
7676

77+
// depth stats (for diagnostics).
78+
double sum_depth_in = 0.0,
79+
sum_depth_out = 0.0, sum_t = 0.0;
80+
7781
if (acoustic_scale == 0.0)
7882
KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
7983

@@ -109,11 +113,25 @@ int main(int argc, char *argv[]) {
109113
PushCompactLatticeWeights(&det_clat);
110114
MinimizeCompactLattice(&det_clat);
111115
}
116+
117+
int32 t;
118+
TopSortCompactLatticeIfNeeded(&det_clat);
119+
double depth = CompactLatticeDepth(det_clat, &t);
120+
sum_depth_in += lat.NumStates();
121+
sum_depth_out += depth * t;
122+
sum_t += t;
123+
112124
fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale), &det_clat);
113125
compact_lat_writer.Write(key, det_clat);
114126
n_done++;
115127
}
116128

129+
if (sum_t != 0.0) {
130+
KALDI_LOG << "Average input-lattice depth (measured at at state level) is "
131+
<< (sum_depth_in / sum_t) << ", output depth is "
132+
<< (sum_depth_out / sum_t) << ", over " << sum_t << " frames "
133+
<< " (average num-frames = " << (sum_t / n_done) << ").";
134+
}
117135
KALDI_LOG << "Done " << n_done << " lattices, determinization finished "
118136
<< "earlier than specified by the beam (or output was empty) on "
119137
<< n_warn << " of these.";

0 commit comments

Comments
 (0)