@@ -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
9393void 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
145145void 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-
0 commit comments