-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[speechx]add wfst decoder #2886
Merged
Merged
Changes from all commits
Commits
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 contains 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 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 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 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 |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
#include "decoder/ctc_tlg_decoder.h" | ||
namespace ppspeech { | ||
|
||
TLGDecoder::TLGDecoder(TLGDecoderOptions opts) { | ||
TLGDecoder::TLGDecoder(TLGDecoderOptions opts) : opts_(opts) { | ||
fst_.reset(fst::Fst<fst::StdArc>::Read(opts.fst_path)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opts -> opts_ |
||
CHECK(fst_ != nullptr); | ||
|
||
|
@@ -68,14 +68,52 @@ std::string TLGDecoder::GetPartialResult() { | |
return words; | ||
} | ||
|
||
void TLGDecoder::FinalizeSearch() { | ||
decoder_->FinalizeDecoding(); | ||
kaldi::CompactLattice clat; | ||
decoder_->GetLattice(&clat, true); | ||
kaldi::Lattice lat, nbest_lat; | ||
fst::ConvertLattice(clat, &lat); | ||
fst::ShortestPath(lat, &nbest_lat, opts_.nbest); | ||
std::vector<kaldi::Lattice> nbest_lats; | ||
fst::ConvertNbestToVector(nbest_lat, &nbest_lats); | ||
|
||
hypotheses_.clear(); | ||
hypotheses_.reserve(nbest_lats.size()); | ||
likelihood_.clear(); | ||
likelihood_.reserve(nbest_lats.size()); | ||
times_.clear(); | ||
times_.reserve(nbest_lats.size()); | ||
for (auto lat : nbest_lats) { | ||
kaldi::LatticeWeight weight; | ||
std::vector<int> hypothese; | ||
std::vector<int> time; | ||
std::vector<int> alignment; | ||
std::vector<int> words_id; | ||
fst::GetLinearSymbolSequence(lat, &alignment, &words_id, &weight); | ||
int idx = 0; | ||
for (; idx < alignment.size() - 1; ++idx) { | ||
if (alignment[idx] == 0) continue; | ||
if (alignment[idx] != alignment[idx + 1]) { | ||
hypothese.push_back(alignment[idx] - 1); | ||
time.push_back(idx); // fake time, todo later | ||
} | ||
} | ||
hypothese.push_back(alignment[idx] - 1); | ||
time.push_back(idx); // fake time, todo later | ||
hypotheses_.push_back(hypothese); | ||
times_.push_back(time); | ||
olabels.push_back(words_id); | ||
likelihood_.push_back(-(weight.Value2() + weight.Value1())); | ||
} | ||
} | ||
|
||
std::string TLGDecoder::GetFinalBestPath() { | ||
if (num_frame_decoded_ == 0) { | ||
// Assertion failed: (this->NumFramesDecoded() > 0 && "You cannot call | ||
// BestPathEnd if no frames were decoded.") | ||
return std::string(""); | ||
} | ||
|
||
decoder_->FinalizeDecoding(); | ||
kaldi::Lattice lat; | ||
kaldi::LatticeWeight weight; | ||
std::vector<int> alignment; | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -14,16 +14,16 @@ | |
|
||
// todo refactor, repalce with gtest | ||
|
||
#include "base/common.h" | ||
#include "decoder/ctc_tlg_decoder.h" | ||
#include "base/common.h" | ||
#include "decoder/param.h" | ||
#include "frontend/audio/data_cache.h" | ||
#include "frontend/data_cache.h" | ||
#include "kaldi/util/table-types.h" | ||
#include "nnet/decodable.h" | ||
#include "nnet/ds2_nnet.h" | ||
#include "nnet/nnet_producer.h" | ||
|
||
|
||
DEFINE_string(feature_rspecifier, "", "test feature rspecifier"); | ||
DEFINE_string(nnet_prob_rspecifier, "", "test feature rspecifier"); | ||
DEFINE_string(result_wspecifier, "", "test result wspecifier"); | ||
|
||
|
||
|
@@ -39,8 +39,8 @@ int main(int argc, char* argv[]) { | |
google::InstallFailureSignalHandler(); | ||
FLAGS_logtostderr = 1; | ||
|
||
kaldi::SequentialBaseFloatMatrixReader feature_reader( | ||
FLAGS_feature_rspecifier); | ||
kaldi::SequentialBaseFloatMatrixReader nnet_prob_reader( | ||
FLAGS_nnet_prob_rspecifier); | ||
kaldi::TokenWriter result_writer(FLAGS_result_wspecifier); | ||
|
||
int32 num_done = 0, num_err = 0; | ||
|
@@ -53,66 +53,19 @@ int main(int argc, char* argv[]) { | |
|
||
ppspeech::ModelOptions model_opts = ppspeech::ModelOptions::InitFromFlags(); | ||
|
||
std::shared_ptr<ppspeech::PaddleNnet> nnet( | ||
new ppspeech::PaddleNnet(model_opts)); | ||
std::shared_ptr<ppspeech::DataCache> raw_data(new ppspeech::DataCache()); | ||
std::shared_ptr<ppspeech::NnetProducer> nnet_producer = | ||
std::make_shared<ppspeech::NnetProducer>(nullptr); | ||
std::shared_ptr<ppspeech::Decodable> decodable( | ||
new ppspeech::Decodable(nnet, raw_data, FLAGS_acoustic_scale)); | ||
|
||
int32 chunk_size = FLAGS_receptive_field_length + | ||
(FLAGS_nnet_decoder_chunk - 1) * FLAGS_subsampling_rate; | ||
int32 chunk_stride = FLAGS_subsampling_rate * FLAGS_nnet_decoder_chunk; | ||
int32 receptive_field_length = FLAGS_receptive_field_length; | ||
LOG(INFO) << "chunk size (frame): " << chunk_size; | ||
LOG(INFO) << "chunk stride (frame): " << chunk_stride; | ||
LOG(INFO) << "receptive field (frame): " << receptive_field_length; | ||
new ppspeech::Decodable(nnet_producer, FLAGS_acoustic_scale)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make_shared |
||
|
||
decoder.InitDecoder(); | ||
kaldi::Timer timer; | ||
for (; !feature_reader.Done(); feature_reader.Next()) { | ||
string utt = feature_reader.Key(); | ||
kaldi::Matrix<BaseFloat> feature = feature_reader.Value(); | ||
raw_data->SetDim(feature.NumCols()); | ||
LOG(INFO) << "process utt: " << utt; | ||
LOG(INFO) << "rows: " << feature.NumRows(); | ||
LOG(INFO) << "cols: " << feature.NumCols(); | ||
|
||
int32 row_idx = 0; | ||
int32 padding_len = 0; | ||
int32 ori_feature_len = feature.NumRows(); | ||
if ((feature.NumRows() - chunk_size) % chunk_stride != 0) { | ||
padding_len = | ||
chunk_stride - (feature.NumRows() - chunk_size) % chunk_stride; | ||
feature.Resize(feature.NumRows() + padding_len, | ||
feature.NumCols(), | ||
kaldi::kCopyData); | ||
} | ||
int32 num_chunks = (feature.NumRows() - chunk_size) / chunk_stride + 1; | ||
for (int chunk_idx = 0; chunk_idx < num_chunks; ++chunk_idx) { | ||
kaldi::Vector<kaldi::BaseFloat> feature_chunk(chunk_size * | ||
feature.NumCols()); | ||
int32 feature_chunk_size = 0; | ||
if (ori_feature_len > chunk_idx * chunk_stride) { | ||
feature_chunk_size = std::min( | ||
ori_feature_len - chunk_idx * chunk_stride, chunk_size); | ||
} | ||
if (feature_chunk_size < receptive_field_length) break; | ||
|
||
int32 start = chunk_idx * chunk_stride; | ||
for (int row_id = 0; row_id < chunk_size; ++row_id) { | ||
kaldi::SubVector<kaldi::BaseFloat> tmp(feature, start); | ||
kaldi::SubVector<kaldi::BaseFloat> f_chunk_tmp( | ||
feature_chunk.Data() + row_id * feature.NumCols(), | ||
feature.NumCols()); | ||
f_chunk_tmp.CopyFromVec(tmp); | ||
++start; | ||
} | ||
raw_data->Accept(feature_chunk); | ||
if (chunk_idx == num_chunks - 1) { | ||
raw_data->SetFinished(); | ||
} | ||
decoder.AdvanceDecode(decodable); | ||
} | ||
|
||
for (; !nnet_prob_reader.Done(); nnet_prob_reader.Next()) { | ||
string utt = nnet_prob_reader.Key(); | ||
kaldi::Matrix<BaseFloat> prob = nnet_prob_reader.Value(); | ||
decodable->Acceptlikelihood(prob); | ||
decoder.AdvanceDecode(decodable); | ||
std::string result; | ||
result = decoder.GetFinalBestPath(); | ||
decodable->Reset(); | ||
|
This file contains 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 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 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 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
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.
unit table 应该是模型的输出,不一定是 word