]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/kaldi.git/blob - src/sgmm2bin/sgmm2-latgen-faster.cc
[src,scripts] Use ConstFst or decoding (half the memory; slightly faster). (#1661)
[processor-sdk/kaldi.git] / src / sgmm2bin / sgmm2-latgen-faster.cc
1 // sgmm2bin/sgmm2-latgen-faster.cc
3 // Copyright 2009-2012  Saarland University;  Microsoft Corporation;
4 //                      Johns Hopkins University (author: Daniel Povey)
5 //                2014  Guoguo Chen
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 //  http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
22 #include <string>
23 using std::string;
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 #include "sgmm2/am-sgmm2.h"
28 #include "hmm/transition-model.h"
29 #include "fstext/fstext-lib.h"
30 #include "decoder/decoder-wrappers.h"
31 #include "sgmm2/decodable-am-sgmm2.h"
32 #include "base/timer.h"
34 namespace kaldi {
36 // the reference arguments at the beginning are not const as the style guide
37 // requires, but are best viewed as inputs.
38 bool ProcessUtterance(LatticeFasterDecoder &decoder,
39                       const AmSgmm2 &am_sgmm,
40                       const TransitionModel &trans_model,
41                       double log_prune,
42                       double acoustic_scale,
43                       const Matrix<BaseFloat> &features,
44                       RandomAccessInt32VectorVectorReader &gselect_reader,
45                       RandomAccessBaseFloatVectorReaderMapped &spkvecs_reader,
46                       const fst::SymbolTable *word_syms,
47                       const std::string &utt,
48                       bool determinize,
49                       bool allow_partial,
50                       Int32VectorWriter *alignments_writer,
51                       Int32VectorWriter *words_writer,
52                       CompactLatticeWriter *compact_lattice_writer,
53                       LatticeWriter *lattice_writer,
54                       double *like_ptr) { // puts utterance's like in like_ptr on success.
55   using fst::Fst;
57   Sgmm2PerSpkDerivedVars spk_vars;
58   if (spkvecs_reader.IsOpen()) {
59     if (spkvecs_reader.HasKey(utt)) {
60       spk_vars.SetSpeakerVector(spkvecs_reader.Value(utt));
61       am_sgmm.ComputePerSpkDerivedVars(&spk_vars);
62     } else {
63       KALDI_WARN << "Cannot find speaker vector for " << utt << ", not decoding this utterance";
64       return false; // We could use zero, but probably the user would want to know about this
65       // (this would normally be a script error or some kind of failure).
66     }
67   }
68   if (!gselect_reader.HasKey(utt) ||
69       gselect_reader.Value(utt).size() != features.NumRows()) {
70     KALDI_WARN << "No Gaussian-selection info available for utterance "
71                << utt << " (or wrong size)";
72   }
74   const std::vector<std::vector<int32> > &gselect =
75       gselect_reader.Value(utt);
76   
77   DecodableAmSgmm2Scaled sgmm_decodable(am_sgmm, trans_model, features, gselect,
78                                         log_prune, acoustic_scale, &spk_vars);
80   return DecodeUtteranceLatticeFaster(
81       decoder, sgmm_decodable, trans_model, word_syms, utt, acoustic_scale,
82       determinize, allow_partial, alignments_writer, words_writer,
83       compact_lattice_writer, lattice_writer, like_ptr);
84 }
86 } // end namespace kaldi
88 int main(int argc, char *argv[]) {
89   try {
90     using namespace kaldi;
91     typedef kaldi::int32 int32;
92     using fst::SymbolTable;
93     using fst::Fst;
94     using fst::StdArc;
96     const char *usage =
97         "Decode features using SGMM-based model.\n"
98         "Usage:  sgmm2-latgen-faster [options] <model-in> (<fst-in>|<fsts-rspecifier>) "
99         "<features-rspecifier> <lattices-wspecifier> [<words-wspecifier> [<alignments-wspecifier>] ]\n";
100     ParseOptions po(usage);
101     BaseFloat acoustic_scale = 0.1;
102     bool allow_partial = false;
103     BaseFloat log_prune = 5.0;
104     string word_syms_filename, gselect_rspecifier, spkvecs_rspecifier,
105         utt2spk_rspecifier;
107     LatticeFasterDecoderConfig decoder_opts;
108     decoder_opts.Register(&po);    
110     po.Register("acoustic-scale", &acoustic_scale,
111         "Scaling factor for acoustic likelihoods");
112     po.Register("log-prune", &log_prune,
113                 "Pruning beam used to reduce number of exp() evaluations.");
114     po.Register("word-symbol-table", &word_syms_filename,
115         "Symbol table for words [for debug output]");
116     po.Register("allow-partial", &allow_partial,
117                 "Produce output even when final state was not reached");
118     po.Register("gselect", &gselect_rspecifier,
119                 "rspecifier for precomputed per-frame Gaussian indices.");
120     po.Register("spk-vecs", &spkvecs_rspecifier,
121                 "rspecifier for speaker vectors");
122     po.Register("utt2spk", &utt2spk_rspecifier,
123                 "rspecifier for utterance to speaker map");
124     po.Read(argc, argv);
126     if (po.NumArgs() < 4 || po.NumArgs() > 6) {
127       po.PrintUsage();
128       exit(1);
129     }
131     if (gselect_rspecifier == "")
132       KALDI_ERR << "--gselect option is required.";
134     std::string model_in_filename = po.GetArg(1),
135         fst_in_str = po.GetArg(2),
136         feature_rspecifier = po.GetArg(3),
137         lattice_wspecifier = po.GetArg(4),
138         words_wspecifier = po.GetOptArg(5),
139         alignment_wspecifier = po.GetOptArg(6);
141     TransitionModel trans_model;
142     kaldi::AmSgmm2 am_sgmm;
143     {
144       bool binary;
145       Input ki(model_in_filename, &binary);
146       trans_model.Read(ki.Stream(), binary);
147       am_sgmm.Read(ki.Stream(), binary);
148     }
150     CompactLatticeWriter compact_lattice_writer;
151     LatticeWriter lattice_writer;
152     bool determinize = decoder_opts.determinize_lattice;    
153     if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
154            : lattice_writer.Open(lattice_wspecifier)))
155       KALDI_ERR << "Could not open table for writing lattices: "
156                  << lattice_wspecifier;
157     
158     Int32VectorWriter words_writer(words_wspecifier);
160     Int32VectorWriter alignment_writer(alignment_wspecifier);
162     fst::SymbolTable *word_syms = NULL;
163     if (word_syms_filename != "") 
164       if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
165         KALDI_ERR << "Could not read symbol table from file "
166                    << word_syms_filename;
168     RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
169     RandomAccessBaseFloatVectorReaderMapped spkvecs_reader(spkvecs_rspecifier,
170                                                            utt2spk_rspecifier);
172     BaseFloat tot_like = 0.0;
173     kaldi::int64 frame_count = 0;
174     int num_success = 0, num_err = 0;
176     Timer timer;
177         
178     if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) { // a single FST.
179       SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
180       // It's important that we initialize decode_fst after feature_reader, as it
181       // can prevent crashes on systems installed without enough virtual memory.
182       // It has to do with what happens on UNIX systems if you call fork() on a
183       // large process: the page-table entries are duplicated, which requires a
184       // lot of virtual memory.
185       Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
186       timer.Reset(); // exclude graph loading time.
187       
188       {
189         LatticeFasterDecoder decoder(*decode_fst, decoder_opts);
190     
191         const std::vector<std::vector<int32> > empty_gselect;
193         for (; !feature_reader.Done(); feature_reader.Next()) {
194           string utt = feature_reader.Key();
195           const Matrix<BaseFloat> &features(feature_reader.Value());
196           if (features.NumRows() == 0) {
197             KALDI_WARN << "Zero-length utterance: " << utt;
198             num_err++;
199             continue;
200           }
201           double like;
202           if (ProcessUtterance(decoder, am_sgmm, trans_model, log_prune, acoustic_scale,
203                                features, gselect_reader, spkvecs_reader, word_syms,
204                                utt, determinize, allow_partial,
205                                &alignment_writer, &words_writer, &compact_lattice_writer,
206                                &lattice_writer, &like)) {
207             tot_like += like;
208             frame_count += features.NumRows();
209             KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
210                       << (like / features.NumRows()) << " over "
211                       << features.NumRows() << " frames.";
212             num_success++;
213           } else { num_err++; }
214         }
215       }
216       delete decode_fst; // only safe to do this after decoder goes out of scope.
217     } else { // We have different FSTs for different utterances.
218       SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
219       RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);          
220       for (; !fst_reader.Done(); fst_reader.Next()) {
221         std::string utt = fst_reader.Key();
222         if (!feature_reader.HasKey(utt)) {
223           KALDI_WARN << "Not decoding utterance " << utt
224                      << " because no features available.";
225           num_err++;
226           continue;
227         }
228         const Matrix<BaseFloat> &features = feature_reader.Value(utt);
229         if (features.NumRows() == 0) {
230           KALDI_WARN << "Zero-length utterance: " << utt;
231           num_err++;
232           continue;
233         }
234         LatticeFasterDecoder decoder(fst_reader.Value(), decoder_opts);
235         double like;
237         if (ProcessUtterance(decoder, am_sgmm, trans_model, log_prune, acoustic_scale,
238                              features, gselect_reader, spkvecs_reader, word_syms,
239                              utt, determinize, allow_partial,
240                              &alignment_writer, &words_writer, &compact_lattice_writer,
241                              &lattice_writer, &like)) {
242           tot_like += like;
243           frame_count += features.NumRows();
244           KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
245                     << (like / features.NumRows()) << " over "
246                     << features.NumRows() << " frames.";
247           num_success++;
248         } else { num_err++; }
249       }
250     }
251     double elapsed = timer.Elapsed();
252     KALDI_LOG << "Time taken [excluding initialization] "<< elapsed
253               << "s: real-time factor assuming 100 frames/sec is "
254               << (elapsed*100.0/frame_count);
255     KALDI_LOG << "Done " << num_success << " utterances, failed for "
256               << num_err;
257     KALDI_LOG << "Overall log-likelihood per frame = " << (tot_like/frame_count)
258               << " over " << frame_count << " frames.";
260     delete word_syms;
261     return (num_success != 0 ? 0 : 1);
262   } catch(const std::exception &e) {
263     std::cerr << e.what();
264     return -1;
265   }