You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PaddleSpeech/runtime/engine/asr/recognizer/recognizer_resource.h

108 lines
4.2 KiB

2 years ago
#pragma once
#include "decoder/ctc_beam_search_opt.h"
#include "decoder/ctc_tlg_decoder.h"
#include "frontend/feature_pipeline.h"
2 years ago
2 years ago
DECLARE_int32(nnet_decoder_chunk);
DECLARE_int32(num_left_chunks);
DECLARE_double(ctc_weight);
DECLARE_double(rescoring_weight);
DECLARE_double(reverse_weight);
DECLARE_int32(nbest);
DECLARE_int32(blank);
DECLARE_double(acoustic_scale);
DECLARE_double(blank_threshold);
DECLARE_string(word_symbol_table);
2 years ago
2 years ago
namespace ppspeech {
2 years ago
struct DecodeOptions {
// chunk_size is the frame number of one chunk after subsampling.
// e.g. if subsample rate is 4 and chunk_size = 16, the frames in
// one chunk are 67=16*4 + 3, stride is 64=16*4
2 years ago
int chunk_size{16};
int num_left_chunks{-1};
2 years ago
// final_score = rescoring_weight * rescoring_score + ctc_weight *
// ctc_score;
// rescoring_score = left_to_right_score * (1 - reverse_weight) +
// right_to_left_score * reverse_weight
// Please note the concept of ctc_scores
// in the following two search methods are different. For
// CtcPrefixBeamSerch,
// it's a sum(prefix) score + context score For CtcWfstBeamSerch, it's a
// max(viterbi) path score + context score So we should carefully set
// ctc_weight accroding to the search methods.
2 years ago
float ctc_weight{0.0};
float rescoring_weight{1.0};
float reverse_weight{0.0};
2 years ago
// CtcEndpointConfig ctc_endpoint_opts;
2 years ago
CTCBeamSearchOptions ctc_prefix_search_opts{};
TLGDecoderOptions tlg_decoder_opts{};
2 years ago
2 years ago
static DecodeOptions InitFromFlags() {
2 years ago
DecodeOptions decoder_opts;
2 years ago
decoder_opts.chunk_size = FLAGS_nnet_decoder_chunk;
2 years ago
decoder_opts.num_left_chunks = FLAGS_num_left_chunks;
decoder_opts.ctc_weight = FLAGS_ctc_weight;
decoder_opts.rescoring_weight = FLAGS_rescoring_weight;
decoder_opts.reverse_weight = FLAGS_reverse_weight;
decoder_opts.ctc_prefix_search_opts.blank = FLAGS_blank;
decoder_opts.ctc_prefix_search_opts.first_beam_size = FLAGS_nbest;
decoder_opts.ctc_prefix_search_opts.second_beam_size = FLAGS_nbest;
decoder_opts.ctc_prefix_search_opts.word_symbol_table =
FLAGS_word_symbol_table;
decoder_opts.tlg_decoder_opts =
ppspeech::TLGDecoderOptions::InitFromFlags();
2 years ago
LOG(INFO) << "chunk_size: " << decoder_opts.chunk_size;
LOG(INFO) << "num_left_chunks: " << decoder_opts.num_left_chunks;
LOG(INFO) << "ctc_weight: " << decoder_opts.ctc_weight;
LOG(INFO) << "rescoring_weight: " << decoder_opts.rescoring_weight;
LOG(INFO) << "reverse_weight: " << decoder_opts.reverse_weight;
LOG(INFO) << "blank: " << FLAGS_blank;
LOG(INFO) << "first_beam_size: " << FLAGS_nbest;
LOG(INFO) << "second_beam_size: " << FLAGS_nbest;
2 years ago
return decoder_opts;
2 years ago
}
};
struct RecognizerResource {
// decodable opt
kaldi::BaseFloat acoustic_scale{1.0};
kaldi::BaseFloat blank_threshold{0.98};
2 years ago
FeaturePipelineOptions feature_pipeline_opts{};
ModelOptions model_opts{};
DecodeOptions decoder_opts{};
std::shared_ptr<NnetBase> nnet;
2 years ago
static RecognizerResource InitFromFlags() {
RecognizerResource resource;
2 years ago
resource.acoustic_scale = FLAGS_acoustic_scale;
resource.blank_threshold = FLAGS_blank_threshold;
2 years ago
LOG(INFO) << "acoustic_scale: " << resource.acoustic_scale;
2 years ago
resource.feature_pipeline_opts =
ppspeech::FeaturePipelineOptions::InitFromFlags();
resource.feature_pipeline_opts.assembler_opts.fill_zero = false;
2 years ago
LOG(INFO) << "u2 need fill zero be false: "
<< resource.feature_pipeline_opts.assembler_opts.fill_zero;
2 years ago
resource.model_opts = ppspeech::ModelOptions::InitFromFlags();
resource.decoder_opts = ppspeech::DecodeOptions::InitFromFlags();
#ifndef USE_ONNX
resource.nnet.reset(new U2Nnet(resource.model_opts));
#else
if (resource.model_opts.with_onnx_model){
resource.nnet.reset(new U2OnnxNnet(resource.model_opts));
} else {
resource.nnet.reset(new U2Nnet(resource.model_opts));
}
#endif
2 years ago
return resource;
}
2 years ago
};
} //namespace ppspeech