- Notifications
You must be signed in to change notification settings - Fork 2.9k
Add decoder for deep asr model #753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits Select commit Hold shift + click to select a range
1841cea Add decoder for deep asr model
b333963 Reimpl in class to sepearte init and decoding
185ff85 Remove unused variables and output
77ce7a9 Avoid using namespace kaldi
e3e9bb4 Make the kaldi root directory flexible
4faf79a Support input scores in ndarray format
f029f93 Clean code and add some comments
49c9cc8 Enable decoder in infer_by_ckpt
18e0395 Merge branch 'develop' of upstream into kaldi_decoder
193c7e2 Format license and comments
7473ec0 Resolve conficts in decoder
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
| | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
| | ||
| #include "post_decode_faster.h" | ||
| | ||
| typedef kaldi::int32 int32; | ||
| using fst::SymbolTable; | ||
| using fst::VectorFst; | ||
| using fst::StdArc; | ||
| | ||
| Decoder::Decoder(std::string word_syms_filename, | ||
| std::string fst_in_filename, | ||
| std::string logprior_rxfilename) { | ||
| const char* usage = | ||
| "Decode, reading log-likelihoods (of transition-ids or whatever symbol " | ||
| "is on the graph) as matrices."; | ||
| | ||
| kaldi::ParseOptions po(usage); | ||
| binary = true; | ||
| acoustic_scale = 1.5; | ||
| allow_partial = true; | ||
| kaldi::FasterDecoderOptions decoder_opts; | ||
| decoder_opts.Register(&po, true); // true == include obscure settings. | ||
| po.Register("binary", &binary, "Write output in binary mode"); | ||
| po.Register("allow-partial", | ||
| &allow_partial, | ||
| "Produce output even when final state was not reached"); | ||
| po.Register("acoustic-scale", | ||
| &acoustic_scale, | ||
| "Scaling factor for acoustic likelihoods"); | ||
| | ||
| word_syms = NULL; | ||
| if (word_syms_filename != "") { | ||
| word_syms = fst::SymbolTable::ReadText(word_syms_filename); | ||
| if (!word_syms) | ||
| KALDI_ERR << "Could not read symbol table from file " | ||
| << word_syms_filename; | ||
| } | ||
| | ||
| std::ifstream is_logprior(logprior_rxfilename); | ||
| logprior.Read(is_logprior, false); | ||
| | ||
| // It's important that we initialize decode_fst after loglikes_reader, as it | ||
| // can prevent crashes on systems installed without enough virtual memory. | ||
| // 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 | ||
| // lot of virtual memory. | ||
| decode_fst = fst::ReadFstKaldi(fst_in_filename); | ||
| | ||
| decoder = new kaldi::FasterDecoder(*decode_fst, decoder_opts); | ||
| } | ||
| | ||
| | ||
| Decoder::~Decoder() { | ||
| if (!word_syms) delete word_syms; | ||
| delete decode_fst; | ||
| delete decoder; | ||
| } | ||
| | ||
| std::string Decoder::decode( | ||
| std::string key, | ||
| const std::vector<std::vector<kaldi::BaseFloat>>& log_probs) { | ||
| size_t num_frames = log_probs.size(); | ||
| size_t dim_label = log_probs[0].size(); | ||
| | ||
| kaldi::Matrix<kaldi::BaseFloat> loglikes( | ||
| num_frames, dim_label, kaldi::kSetZero, kaldi::kStrideEqualNumCols); | ||
| for (size_t i = 0; i < num_frames; ++i) { | ||
| memcpy(loglikes.Data() + i * dim_label, | ||
| log_probs[i].data(), | ||
| sizeof(kaldi::BaseFloat) * dim_label); | ||
| } | ||
| | ||
| return decode(key, loglikes); | ||
| } | ||
| | ||
| | ||
| std::vector<std::string> Decoder::decode(std::string posterior_rspecifier) { | ||
| kaldi::SequentialBaseFloatMatrixReader posterior_reader(posterior_rspecifier); | ||
| std::vector<std::string> decoding_results; | ||
| | ||
| for (; !posterior_reader.Done(); posterior_reader.Next()) { | ||
| std::string key = posterior_reader.Key(); | ||
| kaldi::Matrix<kaldi::BaseFloat> loglikes(posterior_reader.Value()); | ||
| | ||
| decoding_results.push_back(decode(key, loglikes)); | ||
| } | ||
| | ||
| return decoding_results; | ||
| } | ||
| | ||
| | ||
| std::string Decoder::decode(std::string key, | ||
| kaldi::Matrix<kaldi::BaseFloat>& loglikes) { | ||
| std::string decoding_result; | ||
| | ||
| if (loglikes.NumRows() == 0) { | ||
| KALDI_WARN << "Zero-length utterance: " << key; | ||
| } | ||
| KALDI_ASSERT(loglikes.NumCols() == logprior.Dim()); | ||
| | ||
| loglikes.ApplyLog(); | ||
| loglikes.AddVecToRows(-1.0, logprior); | ||
| | ||
| kaldi::DecodableMatrixScaled decodable(loglikes, acoustic_scale); | ||
| decoder->Decode(&decodable); | ||
| | ||
| VectorFst<kaldi::LatticeArc> decoded; // linear FST. | ||
| | ||
| if ((allow_partial || decoder->ReachedFinal()) && | ||
| decoder->GetBestPath(&decoded)) { | ||
| if (!decoder->ReachedFinal()) | ||
| KALDI_WARN << "Decoder did not reach end-state, outputting partial " | ||
| "traceback."; | ||
| | ||
| std::vector<int32> alignment; | ||
| std::vector<int32> words; | ||
| kaldi::LatticeWeight weight; | ||
| | ||
| GetLinearSymbolSequence(decoded, &alignment, &words, &weight); | ||
| | ||
| if (word_syms != NULL) { | ||
| for (size_t i = 0; i < words.size(); i++) { | ||
| std::string s = word_syms->Find(words[i]); | ||
| decoding_result += s; | ||
| if (s == "") | ||
| KALDI_ERR << "Word-id " << words[i] << " not in symbol table."; | ||
| } | ||
| } | ||
| } | ||
| | ||
| return decoding_result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
| | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
| | ||
| #include <string> | ||
| #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" | ||
| | ||
| | ||
| class Decoder { | ||
| public: | ||
| Decoder(std::string word_syms_filename, | ||
| std::string fst_in_filename, | ||
| std::string logprior_rxfilename); | ||
| ~Decoder(); | ||
| | ||
| // Interface to accept the scores read from specifier and return | ||
| // the batch decoding results | ||
| std::vector<std::string> decode(std::string posterior_rspecifier); | ||
| | ||
| // Accept the scores of one utterance and return the decoding result | ||
| std::string decode( | ||
| std::string key, | ||
| const std::vector<std::vector<kaldi::BaseFloat>> &log_probs); | ||
| | ||
| private: | ||
| // For decoding one utterance | ||
| std::string decode(std::string key, | ||
| kaldi::Matrix<kaldi::BaseFloat> &loglikes); | ||
| | ||
| fst::SymbolTable *word_syms; | ||
| fst::VectorFst<fst::StdArc> *decode_fst; | ||
| kaldi::FasterDecoder *decoder; | ||
| kaldi::Vector<kaldi::BaseFloat> logprior; | ||
| | ||
| bool binary; | ||
| kaldi::BaseFloat acoustic_scale; | ||
| bool allow_partial; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| | ||
| set -e | ||
| | ||
| if [ ! -d pybind11 ]; then | ||
| git clone https://github.com/pybind/pybind11.git | ||
| | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these settings be configurable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporarily not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better if acoustic_scale is configurable.