-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Speechx] add tlg decoder #1599
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 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad8ec17
add tlg decoder
SmileGoat 15f434a
add make TLG binary & script
SmileGoat cc43456
rm tools/fst
SmileGoat 5170ccf
rm example/aishell
SmileGoat c2ee6bc
merge develop
SmileGoat 642e084
dev ing
SmileGoat 2455d88
make wfst work & align frame
SmileGoat 18b3225
Merge branch 'develop' of github.com:SmileGoat/PaddleSpeech into add_tlg
SmileGoat 90d6b6f
add aishell wfst eg script
SmileGoat 1f23c4b
remove unnecessary log
SmileGoat 3456ae4
add log & rename LogFrameLikelihood
SmileGoat 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 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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) 2022 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. | ||
|
||
// todo refactor, repalce with gtest | ||
|
||
#include "base/flags.h" | ||
#include "base/log.h" | ||
#include "decoder/ctc_tlg_decoder.h" | ||
#include "frontend/raw_audio.h" | ||
#include "kaldi/util/table-types.h" | ||
#include "nnet/decodable.h" | ||
#include "nnet/paddle_nnet.h" | ||
|
||
DEFINE_string(feature_respecifier, "", "test feature rspecifier"); | ||
DEFINE_string(model_path, "avg_1.jit.pdmodel", "paddle nnet model"); | ||
DEFINE_string(param_path, "avg_1.jit.pdiparams", "paddle nnet model param"); | ||
DEFINE_string(word_symbol_table, "vocab.txt", "word symbol table"); | ||
DEFINE_string(graph_path, "TLG", "decoder graph"); | ||
|
||
|
||
using kaldi::BaseFloat; | ||
using kaldi::Matrix; | ||
using std::vector; | ||
|
||
int main(int argc, char* argv[]) { | ||
gflags::ParseCommandLineFlags(&argc, &argv, false); | ||
google::InitGoogleLogging(argv[0]); | ||
|
||
kaldi::SequentialBaseFloatMatrixReader feature_reader( | ||
FLAGS_feature_respecifier); | ||
std::string model_graph = FLAGS_model_path; | ||
std::string model_params = FLAGS_param_path; | ||
std::string word_symbol_table = FLAGS_word_symbol_table; | ||
std::string graph_path = FLAGS_graph_path; | ||
|
||
int32 num_done = 0, num_err = 0; | ||
|
||
ppspeech::TLGDecoderOptions opts; | ||
opts.word_symbol_table = word_symbol_table; | ||
opts.fst_path = graph_path; | ||
ppspeech::TLGDecoder decoder(opts); | ||
|
||
ppspeech::ModelOptions model_opts; | ||
model_opts.model_path = model_graph; | ||
model_opts.params_path = model_params; | ||
std::shared_ptr<ppspeech::PaddleNnet> nnet( | ||
new ppspeech::PaddleNnet(model_opts)); | ||
std::shared_ptr<ppspeech::RawDataCache> raw_data( | ||
new ppspeech::RawDataCache()); | ||
std::shared_ptr<ppspeech::Decodable> decodable( | ||
new ppspeech::Decodable(nnet, raw_data)); | ||
|
||
int32 chunk_size = 35; | ||
decoder.InitDecoder(); | ||
|
||
for (; !feature_reader.Done(); feature_reader.Next()) { | ||
string utt = feature_reader.Key(); | ||
const kaldi::Matrix<BaseFloat> feature = feature_reader.Value(); | ||
raw_data->SetDim(feature.NumCols()); | ||
int32 row_idx = 0; | ||
int32 num_chunks = feature.NumRows() / chunk_size; | ||
zh794390558 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (int chunk_idx = 0; chunk_idx < num_chunks; ++chunk_idx) { | ||
kaldi::Vector<kaldi::BaseFloat> feature_chunk(chunk_size * | ||
feature.NumCols()); | ||
for (int row_id = 0; row_id < chunk_size; ++row_id) { | ||
kaldi::SubVector<kaldi::BaseFloat> tmp(feature, row_idx); | ||
kaldi::SubVector<kaldi::BaseFloat> f_chunk_tmp( | ||
feature_chunk.Data() + row_id * feature.NumCols(), | ||
feature.NumCols()); | ||
f_chunk_tmp.CopyFromVec(tmp); | ||
row_idx++; | ||
} | ||
raw_data->Accept(feature_chunk); | ||
if (chunk_idx == num_chunks - 1) { | ||
raw_data->SetFinished(); | ||
} | ||
decoder.AdvanceDecode(decodable); | ||
} | ||
std::string result; | ||
result = decoder.GetFinalBestPath(); | ||
KALDI_LOG << " the result of " << utt << " is " << result; | ||
decodable->Reset(); | ||
decoder.Reset(); | ||
++num_done; | ||
} | ||
|
||
KALDI_LOG << "Done " << num_done << " utterances, " << num_err | ||
<< " with errors."; | ||
return (num_done != 0 ? 0 : 1); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "decoder/ctc_tlg_decoder.h" | ||
namespace ppspeech { | ||
|
||
TLGDecoder::TLGDecoder(TLGDecoderOptions opts) { | ||
fst_.reset(fst::Fst<fst::StdArc>::Read(opts.fst_path)); | ||
CHECK(fst_ != nullptr); | ||
word_symbol_table_.reset(fst::SymbolTable::ReadText(opts.word_symbol_table)); | ||
decoder_.reset(new kaldi::LatticeFasterOnlineDecoder(*fst_, opts.opts)); | ||
decoder_->InitDecoding(); | ||
} | ||
|
||
void TLGDecoder::InitDecoder() { | ||
decoder_->InitDecoding(); | ||
} | ||
|
||
void TLGDecoder::AdvanceDecode(const std::shared_ptr<kaldi::DecodableInterface>& decodable) { | ||
while (1) { | ||
AdvanceDecoding(decodable.get()); | ||
if (decodable->IsLastFrame(num_frame_decoded_)) break; | ||
} | ||
} | ||
|
||
void TLGDecoder::AdvanceDecoding(kaldi::DecodableInterface* decodable) { | ||
// skip blank frame? | ||
decoder_->AdvanceDecoding(decodable, 1); | ||
num_frame_decoded_++; | ||
} | ||
|
||
void TLGDecoder::Reset() { | ||
decoder_->InitDecoding(); | ||
return; | ||
} | ||
|
||
std::string TLGDecoder::GetFinalBestPath() { | ||
decoder_->FinalizeDecoding(); | ||
kaldi::Lattice lat; | ||
kaldi::LatticeWeight weight; | ||
std::vector<int> alignment; | ||
std::vector<int> words_id; | ||
decoder_->GetBestPath(&lat, true); | ||
fst::GetLinearSymbolSequence(lat, &alignment, &words_id, &weight); | ||
std::string words; | ||
for (int32 idx = 0; idx < words_id.size(); ++idx) { | ||
std::string word = word_symbol_table_->Find(words_id[idx]); | ||
words += word; | ||
} | ||
return words; | ||
} | ||
|
||
} |
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,47 @@ | ||
#pragma once | ||
|
||
#include "kaldi/decoder/lattice-faster-online-decoder.h" | ||
#include "kaldi/decoder/decodable-itf.h" | ||
#include "util/parse-options.h" | ||
#include "base/basic_types.h" | ||
|
||
namespace ppspeech { | ||
|
||
struct TLGDecoderOptions { | ||
kaldi::LatticeFasterDecoderConfig opts; | ||
// todo remove later, add into decode resource | ||
std::string word_symbol_table; | ||
std::string fst_path; | ||
|
||
TLGDecoderOptions() | ||
: word_symbol_table(""), | ||
fst_path("") {} | ||
}; | ||
|
||
class TLGDecoder { | ||
public: | ||
explicit TLGDecoder(TLGDecoderOptions opts); | ||
void InitDecoder(); | ||
void Decode(); | ||
std::string GetBestPath(); | ||
std::vector<std::pair<double, std::string>> GetNBestPath(); | ||
std::string GetFinalBestPath(); | ||
int NumFrameDecoded(); | ||
int DecodeLikelihoods(const std::vector<std::vector<BaseFloat>>& probs, | ||
std::vector<std::string>& nbest_words); | ||
void AdvanceDecode( | ||
const std::shared_ptr<kaldi::DecodableInterface>& decodable); | ||
void Reset(); | ||
|
||
private: | ||
void AdvanceDecoding(kaldi::DecodableInterface* decodable); | ||
|
||
std::shared_ptr<kaldi::LatticeFasterOnlineDecoder> decoder_; | ||
std::shared_ptr<fst::Fst<fst::StdArc>> fst_; | ||
std::shared_ptr<fst::SymbolTable> word_symbol_table_; | ||
int32 num_frame_decoded_; | ||
}; | ||
|
||
|
||
|
||
} // namespace ppspeech |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
add_library(kaldi-decoder | ||
lattice-faster-decoder.cc | ||
lattice-faster-online-decoder.cc | ||
) | ||
target_link_libraries(kaldi-decoder PUBLIC kaldi-lat) |
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
add_library(kaldi-fstext | ||
kaldi-fst-io.cc | ||
) | ||
target_link_libraries(kaldi-fstext PUBLIC kaldi-util) |
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.
Uh oh!
There was an error while loading. Please reload this page.