Skip to content

Commit 2538a32

Browse files
committed
add test2
1 parent 4b30697 commit 2538a32

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// bin/latgen-faster-mapped.cc
2+
3+
// Copyright 2009-2012 Microsoft Corporation, Karel Vesely
4+
// 2013 Johns Hopkins University (author: Daniel Povey)
5+
// 2014 Guoguo Chen
6+
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.
21+
22+
23+
#include "base/kaldi-common.h"
24+
#include "util/common-utils.h"
25+
#include "tree/context-dep.h"
26+
#include "hmm/transition-model.h"
27+
#include "fstext/fstext-lib.h"
28+
#include "decoder/decoder-wrappers.h"
29+
#include "decoder/decodable-matrix.h"
30+
#include "base/timer.h"
31+
32+
33+
int main(int argc, char *argv[]) {
34+
try {
35+
using namespace kaldi;
36+
typedef kaldi::int32 int32;
37+
using fst::SymbolTable;
38+
using fst::Fst;
39+
using fst::StdArc;
40+
41+
const char *usage =
42+
"Generate lattices, reading log-likelihoods as matrices\n"
43+
" (model is needed only for the integer mappings in its transition-model)\n"
44+
"Usage: latgen-faster-mapped [options] trans-model-in (fst-in|fsts-rspecifier) loglikes-rspecifier"
45+
" lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
46+
ParseOptions po(usage);
47+
Timer timer;
48+
bool allow_partial = false;
49+
BaseFloat acoustic_scale = 0.1;
50+
LatticeFasterDecoderCombineConfig config;
51+
52+
std::string word_syms_filename;
53+
config.Register(&po);
54+
po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
55+
56+
po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
57+
po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
58+
59+
po.Read(argc, argv);
60+
61+
if (po.NumArgs() < 4 || po.NumArgs() > 6) {
62+
po.PrintUsage();
63+
exit(1);
64+
}
65+
66+
std::string model_in_filename = po.GetArg(1),
67+
fst_in_str = po.GetArg(2),
68+
feature_rspecifier = po.GetArg(3),
69+
lattice_wspecifier = po.GetArg(4),
70+
words_wspecifier = po.GetOptArg(5),
71+
alignment_wspecifier = po.GetOptArg(6);
72+
73+
TransitionModel trans_model;
74+
ReadKaldiObject(model_in_filename, &trans_model);
75+
76+
bool determinize = config.determinize_lattice;
77+
CompactLatticeWriter compact_lattice_writer;
78+
LatticeWriter lattice_writer;
79+
if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
80+
: lattice_writer.Open(lattice_wspecifier)))
81+
KALDI_ERR << "Could not open table for writing lattices: "
82+
<< lattice_wspecifier;
83+
84+
Int32VectorWriter words_writer(words_wspecifier);
85+
86+
Int32VectorWriter alignment_writer(alignment_wspecifier);
87+
88+
fst::SymbolTable *word_syms = NULL;
89+
if (word_syms_filename != "")
90+
if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
91+
KALDI_ERR << "Could not read symbol table from file "
92+
<< word_syms_filename;
93+
94+
double tot_like = 0.0;
95+
kaldi::int64 frame_count = 0;
96+
int num_success = 0, num_fail = 0;
97+
98+
if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
99+
SequentialBaseFloatMatrixReader loglike_reader(feature_rspecifier);
100+
// Input FST is just one FST, not a table of FSTs.
101+
Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
102+
timer.Reset();
103+
104+
{
105+
LatticeFasterDecoderCombine decoder(*decode_fst, config);
106+
107+
for (; !loglike_reader.Done(); loglike_reader.Next()) {
108+
std::string utt = loglike_reader.Key();
109+
Matrix<BaseFloat> loglikes (loglike_reader.Value());
110+
loglike_reader.FreeCurrent();
111+
if (loglikes.NumRows() == 0) {
112+
KALDI_WARN << "Zero-length utterance: " << utt;
113+
num_fail++;
114+
continue;
115+
}
116+
117+
DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
118+
119+
double like;
120+
if (DecodeUtteranceLatticeFasterCombine(
121+
decoder, decodable, trans_model, word_syms, utt,
122+
acoustic_scale, determinize, allow_partial, &alignment_writer,
123+
&words_writer, &compact_lattice_writer, &lattice_writer,
124+
&like)) {
125+
tot_like += like;
126+
frame_count += loglikes.NumRows();
127+
num_success++;
128+
} else num_fail++;
129+
}
130+
}
131+
delete decode_fst; // delete this only after decoder goes out of scope.
132+
} else { // We have different FSTs for different utterances.
133+
SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
134+
RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
135+
for (; !fst_reader.Done(); fst_reader.Next()) {
136+
std::string utt = fst_reader.Key();
137+
if (!loglike_reader.HasKey(utt)) {
138+
KALDI_WARN << "Not decoding utterance " << utt
139+
<< " because no loglikes available.";
140+
num_fail++;
141+
continue;
142+
}
143+
const Matrix<BaseFloat> &loglikes = loglike_reader.Value(utt);
144+
if (loglikes.NumRows() == 0) {
145+
KALDI_WARN << "Zero-length utterance: " << utt;
146+
num_fail++;
147+
continue;
148+
}
149+
LatticeFasterDecoderCombine decoder(fst_reader.Value(), config);
150+
DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
151+
double like;
152+
if (DecodeUtteranceLatticeFasterCombine(
153+
decoder, decodable, trans_model, word_syms, utt, acoustic_scale,
154+
determinize, allow_partial, &alignment_writer, &words_writer,
155+
&compact_lattice_writer, &lattice_writer, &like)) {
156+
tot_like += like;
157+
frame_count += loglikes.NumRows();
158+
num_success++;
159+
} else num_fail++;
160+
}
161+
}
162+
163+
double elapsed = timer.Elapsed();
164+
KALDI_LOG << "Time taken "<< elapsed
165+
<< "s: real-time factor assuming 100 frames/sec is "
166+
<< (elapsed*100.0/frame_count);
167+
KALDI_LOG << "Done " << num_success << " utterances, failed for "
168+
<< num_fail;
169+
KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
170+
<< frame_count<<" frames.";
171+
172+
delete word_syms;
173+
if (num_success != 0) return 0;
174+
else return 1;
175+
} catch(const std::exception &e) {
176+
std::cerr << e.what();
177+
return -1;
178+
}
179+
}

0 commit comments

Comments
 (0)