提交 b3339631 编写于 作者: Y Yibing Liu

Reimpl in class to sepearte init and decoding

上级 1841cea1
...@@ -13,38 +13,26 @@ See the License for the specific language governing permissions and ...@@ -13,38 +13,26 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "post_decode_faster.h" #include "post_decode_faster.h"
#include "base/kaldi-common.h"
#include "base/timer.h"
#include "decoder/decodable-matrix.h"
#include "decoder/faster-decoder.h"
#include "fstext/fstext-lib.h"
#include "hmm/transition-model.h"
#include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
#include "tree/context-dep.h"
#include "util/common-utils.h"
std::vector<std::string> decode(std::string word_syms_filename,
std::string fst_in_filename,
std::string logprior_rxfilename,
std::string posterior_rspecifier,
std::string words_wspecifier,
std::string alignment_wspecifier) {
std::vector<std::string> decoding_results;
try { using namespace kaldi;
using namespace kaldi; typedef kaldi::int32 int32;
typedef kaldi::int32 int32; using fst::SymbolTable;
using fst::SymbolTable; using fst::VectorFst;
using fst::VectorFst; using fst::StdArc;
using fst::StdArc;
Decoder::Decoder(std::string word_syms_filename,
std::string fst_in_filename,
std::string logprior_rxfilename) {
const char *usage = const char *usage =
"Decode, reading log-likelihoods (of transition-ids or whatever symbol " "Decode, reading log-likelihoods (of transition-ids or whatever symbol "
"is on the graph) as matrices."; "is on the graph) as matrices.";
std::string words_wspecifier = "ark,t:out.ark";
std::string alignment_wspecifier = "";
ParseOptions po(usage); ParseOptions po(usage);
bool binary = true; binary = true;
BaseFloat acoustic_scale = 1.5; acoustic_scale = 1.5;
bool allow_partial = true; allow_partial = true;
FasterDecoderOptions decoder_opts; FasterDecoderOptions decoder_opts;
decoder_opts.Register(&po, true); // true == include obscure settings. decoder_opts.Register(&po, true); // true == include obscure settings.
po.Register("binary", &binary, "Write output in binary mode"); po.Register("binary", &binary, "Write output in binary mode");
...@@ -55,10 +43,10 @@ std::vector<std::string> decode(std::string word_syms_filename, ...@@ -55,10 +43,10 @@ std::vector<std::string> decode(std::string word_syms_filename,
&acoustic_scale, &acoustic_scale,
"Scaling factor for acoustic likelihoods"); "Scaling factor for acoustic likelihoods");
Int32VectorWriter words_writer(words_wspecifier); words_writer = new Int32VectorWriter(words_wspecifier);
Int32VectorWriter alignment_writer(alignment_wspecifier); alignment_writer = new Int32VectorWriter(alignment_wspecifier);
fst::SymbolTable *word_syms = NULL; word_syms = NULL;
if (word_syms_filename != "") { if (word_syms_filename != "") {
word_syms = fst::SymbolTable::ReadText(word_syms_filename); word_syms = fst::SymbolTable::ReadText(word_syms_filename);
if (!word_syms) if (!word_syms)
...@@ -66,9 +54,7 @@ std::vector<std::string> decode(std::string word_syms_filename, ...@@ -66,9 +54,7 @@ std::vector<std::string> decode(std::string word_syms_filename,
<< word_syms_filename; << word_syms_filename;
} }
SequentialBaseFloatMatrixReader posterior_reader(posterior_rspecifier);
std::ifstream is_logprior(logprior_rxfilename); std::ifstream is_logprior(logprior_rxfilename);
Vector<BaseFloat> logprior;
logprior.Read(is_logprior, false); logprior.Read(is_logprior, false);
// It's important that we initialize decode_fst after loglikes_reader, as it // It's important that we initialize decode_fst after loglikes_reader, as it
...@@ -76,15 +62,29 @@ std::vector<std::string> decode(std::string word_syms_filename, ...@@ -76,15 +62,29 @@ std::vector<std::string> decode(std::string word_syms_filename,
// It has to do with what happens on UNIX systems if you call fork() on a // It has to do with what happens on UNIX systems if you call fork() on a
// large process: the page-table entries are duplicated, which requires a // large process: the page-table entries are duplicated, which requires a
// lot of virtual memory. // lot of virtual memory.
VectorFst<StdArc> *decode_fst = fst::ReadFstKaldi(fst_in_filename); decode_fst = fst::ReadFstKaldi(fst_in_filename);
decoder = new FasterDecoder(*decode_fst, decoder_opts);
}
Decoder::~Decoder() {
if (!word_syms) delete word_syms;
delete decode_fst;
delete decoder;
delete words_writer;
delete alignment_writer;
}
std::vector<std::string> Decoder::decode(std::string posterior_rspecifier) {
SequentialBaseFloatMatrixReader posterior_reader(posterior_rspecifier);
std::vector<std::string> decoding_results;
BaseFloat tot_like = 0.0; BaseFloat tot_like = 0.0;
kaldi::int64 frame_count = 0; kaldi::int64 frame_count = 0;
int num_success = 0, num_fail = 0; int num_success = 0, num_fail = 0;
FasterDecoder decoder(*decode_fst, decoder_opts);
Timer timer; Timer timer;
for (; !posterior_reader.Done(); posterior_reader.Next()) { for (; !posterior_reader.Done(); posterior_reader.Next()) {
std::string key = posterior_reader.Key(); std::string key = posterior_reader.Key();
Matrix<BaseFloat> loglikes(posterior_reader.Value()); Matrix<BaseFloat> loglikes(posterior_reader.Value());
...@@ -102,14 +102,14 @@ std::vector<std::string> decode(std::string word_syms_filename, ...@@ -102,14 +102,14 @@ std::vector<std::string> decode(std::string word_syms_filename,
loglikes.AddVecToRows(-1.0, logprior); loglikes.AddVecToRows(-1.0, logprior);
DecodableMatrixScaled decodable(loglikes, acoustic_scale); DecodableMatrixScaled decodable(loglikes, acoustic_scale);
decoder.Decode(&decodable); decoder->Decode(&decodable);
VectorFst<LatticeArc> decoded; // linear FST. VectorFst<LatticeArc> decoded; // linear FST.
if ((allow_partial || decoder.ReachedFinal()) && if ((allow_partial || decoder->ReachedFinal()) &&
decoder.GetBestPath(&decoded)) { decoder->GetBestPath(&decoded)) {
num_success++; num_success++;
if (!decoder.ReachedFinal()) if (!decoder->ReachedFinal())
KALDI_WARN << "Decoder did not reach end-state, outputting partial " KALDI_WARN << "Decoder did not reach end-state, outputting partial "
"traceback."; "traceback.";
...@@ -120,8 +120,8 @@ std::vector<std::string> decode(std::string word_syms_filename, ...@@ -120,8 +120,8 @@ std::vector<std::string> decode(std::string word_syms_filename,
GetLinearSymbolSequence(decoded, &alignment, &words, &weight); GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
words_writer.Write(key, words); words_writer->Write(key, words);
if (alignment_writer.IsOpen()) alignment_writer.Write(key, alignment); if (alignment_writer->IsOpen()) alignment_writer->Write(key, alignment);
if (word_syms != NULL) { if (word_syms != NULL) {
std::string res; std::string res;
for (size_t i = 0; i < words.size(); i++) { for (size_t i = 0; i < words.size(); i++) {
...@@ -136,8 +136,8 @@ std::vector<std::string> decode(std::string word_syms_filename, ...@@ -136,8 +136,8 @@ std::vector<std::string> decode(std::string word_syms_filename,
BaseFloat like = -weight.Value1() - weight.Value2(); BaseFloat like = -weight.Value1() - weight.Value2();
tot_like += like; tot_like += like;
KALDI_LOG << "Log-like per frame for utterance " << key << " is " KALDI_LOG << "Log-like per frame for utterance " << key << " is "
<< (like / loglikes.NumRows()) << " over " << (like / loglikes.NumRows()) << " over " << loglikes.NumRows()
<< loglikes.NumRows() << " frames."; << " frames.";
} else { } else {
num_fail++; num_fail++;
...@@ -150,16 +150,9 @@ std::vector<std::string> decode(std::string word_syms_filename, ...@@ -150,16 +150,9 @@ std::vector<std::string> decode(std::string word_syms_filename,
KALDI_LOG << "Time taken [excluding initialization] " << elapsed KALDI_LOG << "Time taken [excluding initialization] " << elapsed
<< "s: real-time factor assuming 100 frames/sec is " << "s: real-time factor assuming 100 frames/sec is "
<< (elapsed * 100.0 / frame_count); << (elapsed * 100.0 / frame_count);
KALDI_LOG << "Done " << num_success << " utterances, failed for " KALDI_LOG << "Done " << num_success << " utterances, failed for " << num_fail;
<< num_fail;
KALDI_LOG << "Overall log-likelihood per frame is " KALDI_LOG << "Overall log-likelihood per frame is "
<< (tot_like / frame_count) << " over " << frame_count << (tot_like / frame_count) << " over " << frame_count
<< " frames."; << " frames.";
delete word_syms;
delete decode_fst;
} catch (const std::exception &e) {
std::cerr << e.what();
}
return decoding_results; return decoding_results;
} }
...@@ -14,10 +14,36 @@ limitations under the License. */ ...@@ -14,10 +14,36 @@ limitations under the License. */
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/kaldi-common.h"
#include "base/timer.h"
#include "decoder/decodable-matrix.h"
#include "decoder/faster-decoder.h"
#include "fstext/fstext-lib.h"
#include "hmm/transition-model.h"
#include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
#include "tree/context-dep.h"
#include "util/common-utils.h"
std::vector<std::string> decode(std::string word_syms_filename,
class Decoder {
public:
Decoder(std::string word_syms_filename,
std::string fst_in_filename, std::string fst_in_filename,
std::string logprior_rxfilename, std::string logprior_rxfilename);
std::string posterior_respecifier, ~Decoder();
std::string words_wspecifier,
std::string alignment_wspecifier = ""); std::vector<std::string> decode(std::string posterior_rspecifier);
private:
fst::SymbolTable *word_syms;
fst::VectorFst<fst::StdArc> *decode_fst;
kaldi::FasterDecoder *decoder;
kaldi::Vector<kaldi::BaseFloat> logprior;
kaldi::Int32VectorWriter *words_writer;
kaldi::Int32VectorWriter *alignment_writer;
bool binary;
kaldi::BaseFloat acoustic_scale;
bool allow_partial;
};
...@@ -20,10 +20,12 @@ limitations under the License. */ ...@@ -20,10 +20,12 @@ limitations under the License. */
namespace py = pybind11; namespace py = pybind11;
PYBIND11_MODULE(post_decode_faster, m) { PYBIND11_MODULE(post_decode_faster, m) {
m.doc() = "Decode function for Deep ASR model"; m.doc() = "Decoder for Deep ASR model";
m.def("decode", py::class_<Decoder>(m, "Decoder")
&decode, .def(py::init<std::string, std::string, std::string>())
.def("decode",
&Decoder::decode,
"Decode one input probability matrix " "Decode one input probability matrix "
"and return the transcription"); "and return the transcription");
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册