|
|
@ -1,22 +1,21 @@
|
|
|
|
#include <iostream>
|
|
|
|
#include "ctc_decoders.h"
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
|
|
|
#include <limits>
|
|
|
|
#include "fst/fstlib.h"
|
|
|
|
#include <map>
|
|
|
|
#include "ctc_decoders.h"
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "ThreadPool.h"
|
|
|
|
#include "decoder_utils.h"
|
|
|
|
#include "decoder_utils.h"
|
|
|
|
|
|
|
|
#include "fst/fstlib.h"
|
|
|
|
#include "path_trie.h"
|
|
|
|
#include "path_trie.h"
|
|
|
|
#include "ThreadPool.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string ctc_best_path_decoder(std::vector<std::vector<double> > probs_seq,
|
|
|
|
std::string ctc_best_path_decoder(std::vector<std::vector<double>> probs_seq,
|
|
|
|
std::vector<std::string> vocabulary)
|
|
|
|
std::vector<std::string> vocabulary) {
|
|
|
|
{
|
|
|
|
|
|
|
|
// dimension check
|
|
|
|
// dimension check
|
|
|
|
int num_time_steps = probs_seq.size();
|
|
|
|
int num_time_steps = probs_seq.size();
|
|
|
|
for (int i=0; i<num_time_steps; i++) {
|
|
|
|
for (int i = 0; i < num_time_steps; i++) {
|
|
|
|
if (probs_seq[i].size() != vocabulary.size()+1) {
|
|
|
|
if (probs_seq[i].size() != vocabulary.size() + 1) {
|
|
|
|
std::cout << "The shape of probs_seq does not match"
|
|
|
|
std::cout << "The shape of probs_seq does not match"
|
|
|
|
<< " with the shape of the vocabulary!" << std::endl;
|
|
|
|
<< " with the shape of the vocabulary!" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
exit(1);
|
|
|
@ -42,7 +41,7 @@ std::string ctc_best_path_decoder(std::vector<std::vector<double> > probs_seq,
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<int> idx_vec;
|
|
|
|
std::vector<int> idx_vec;
|
|
|
|
for (int i = 0; i < max_idx_vec.size(); i++) {
|
|
|
|
for (int i = 0; i < max_idx_vec.size(); i++) {
|
|
|
|
if ((i == 0) || ((i > 0) && max_idx_vec[i] != max_idx_vec[i-1])) {
|
|
|
|
if ((i == 0) || ((i > 0) && max_idx_vec[i] != max_idx_vec[i - 1])) {
|
|
|
|
idx_vec.push_back(max_idx_vec[i]);
|
|
|
|
idx_vec.push_back(max_idx_vec[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -56,15 +55,14 @@ std::string ctc_best_path_decoder(std::vector<std::vector<double> > probs_seq,
|
|
|
|
return best_path_result;
|
|
|
|
return best_path_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::pair<double, std::string> >
|
|
|
|
std::vector<std::pair<double, std::string>> ctc_beam_search_decoder(
|
|
|
|
ctc_beam_search_decoder(std::vector<std::vector<double> > probs_seq,
|
|
|
|
std::vector<std::vector<double>> probs_seq,
|
|
|
|
int beam_size,
|
|
|
|
int beam_size,
|
|
|
|
std::vector<std::string> vocabulary,
|
|
|
|
std::vector<std::string> vocabulary,
|
|
|
|
int blank_id,
|
|
|
|
int blank_id,
|
|
|
|
double cutoff_prob,
|
|
|
|
double cutoff_prob,
|
|
|
|
int cutoff_top_n,
|
|
|
|
int cutoff_top_n,
|
|
|
|
Scorer *ext_scorer)
|
|
|
|
Scorer *extscorer) {
|
|
|
|
{
|
|
|
|
|
|
|
|
// dimension check
|
|
|
|
// dimension check
|
|
|
|
int num_time_steps = probs_seq.size();
|
|
|
|
int num_time_steps = probs_seq.size();
|
|
|
|
for (int i = 0; i < num_time_steps; i++) {
|
|
|
|
for (int i = 0; i < num_time_steps; i++) {
|
|
|
@ -82,35 +80,33 @@ std::vector<std::pair<double, std::string> >
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// assign space ID
|
|
|
|
// assign space ID
|
|
|
|
std::vector<std::string>::iterator it = std::find(vocabulary.begin(),
|
|
|
|
std::vector<std::string>::iterator it =
|
|
|
|
vocabulary.end(), " ");
|
|
|
|
std::find(vocabulary.begin(), vocabulary.end(), " ");
|
|
|
|
int space_id = it - vocabulary.begin();
|
|
|
|
int space_id = it - vocabulary.begin();
|
|
|
|
// if no space in vocabulary
|
|
|
|
// if no space in vocabulary
|
|
|
|
if(space_id >= vocabulary.size()) {
|
|
|
|
if (space_id >= vocabulary.size()) {
|
|
|
|
space_id = -2;
|
|
|
|
space_id = -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// init prefixes' root
|
|
|
|
// init prefixes' root
|
|
|
|
PathTrie root;
|
|
|
|
PathTrie root;
|
|
|
|
root._score = root._log_prob_b_prev = 0.0;
|
|
|
|
root.score = root.log_prob_b_prev = 0.0;
|
|
|
|
std::vector<PathTrie*> prefixes;
|
|
|
|
std::vector<PathTrie *> prefixes;
|
|
|
|
prefixes.push_back(&root);
|
|
|
|
prefixes.push_back(&root);
|
|
|
|
|
|
|
|
|
|
|
|
if ( ext_scorer != nullptr) {
|
|
|
|
if (extscorer != nullptr) {
|
|
|
|
if (ext_scorer->is_char_map_empty()) {
|
|
|
|
if (extscorer->is_char_map_empty()) {
|
|
|
|
ext_scorer->set_char_map(vocabulary);
|
|
|
|
extscorer->set_char_map(vocabulary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ext_scorer->is_character_based()) {
|
|
|
|
if (!extscorer->is_character_based()) {
|
|
|
|
if (ext_scorer->dictionary == nullptr) {
|
|
|
|
if (extscorer->dictionary == nullptr) {
|
|
|
|
// fill dictionary for fst
|
|
|
|
// fill dictionary for fst
|
|
|
|
ext_scorer->fill_dictionary(true);
|
|
|
|
extscorer->fill_dictionary(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto fst_dict = static_cast<fst::StdVectorFst*>
|
|
|
|
auto fst_dict = static_cast<fst::StdVectorFst *>(extscorer->dictionary);
|
|
|
|
(ext_scorer->dictionary);
|
|
|
|
fst::StdVectorFst *dict_ptr = fst_dict->Copy(true);
|
|
|
|
fst::StdVectorFst* dict_ptr = fst_dict->Copy(true);
|
|
|
|
|
|
|
|
root.set_dictionary(dict_ptr);
|
|
|
|
root.set_dictionary(dict_ptr);
|
|
|
|
auto matcher = std::make_shared<FSTMATCH>
|
|
|
|
auto matcher = std::make_shared<FSTMATCH>(*dict_ptr, fst::MATCH_INPUT);
|
|
|
|
(*dict_ptr, fst::MATCH_INPUT);
|
|
|
|
|
|
|
|
root.set_matcher(matcher);
|
|
|
|
root.set_matcher(matcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -118,45 +114,44 @@ std::vector<std::pair<double, std::string> >
|
|
|
|
// prefix search over time
|
|
|
|
// prefix search over time
|
|
|
|
for (int time_step = 0; time_step < num_time_steps; time_step++) {
|
|
|
|
for (int time_step = 0; time_step < num_time_steps; time_step++) {
|
|
|
|
std::vector<double> prob = probs_seq[time_step];
|
|
|
|
std::vector<double> prob = probs_seq[time_step];
|
|
|
|
std::vector<std::pair<int, double> > prob_idx;
|
|
|
|
std::vector<std::pair<int, double>> prob_idx;
|
|
|
|
for (int i=0; i<prob.size(); i++) {
|
|
|
|
for (int i = 0; i < prob.size(); i++) {
|
|
|
|
prob_idx.push_back(std::pair<int, double>(i, prob[i]));
|
|
|
|
prob_idx.push_back(std::pair<int, double>(i, prob[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float min_cutoff = -NUM_FLT_INF;
|
|
|
|
float min_cutoff = -NUM_FLT_INF;
|
|
|
|
bool full_beam = false;
|
|
|
|
bool full_beam = false;
|
|
|
|
if (ext_scorer != nullptr) {
|
|
|
|
if (extscorer != nullptr) {
|
|
|
|
int num_prefixes = std::min((int)prefixes.size(), beam_size);
|
|
|
|
int num_prefixes = std::min((int)prefixes.size(), beam_size);
|
|
|
|
std::sort(prefixes.begin(), prefixes.begin() + num_prefixes,
|
|
|
|
std::sort(
|
|
|
|
prefix_compare);
|
|
|
|
prefixes.begin(), prefixes.begin() + num_prefixes, prefix_compare);
|
|
|
|
min_cutoff = prefixes[num_prefixes-1]->_score + log(prob[blank_id])
|
|
|
|
min_cutoff = prefixes[num_prefixes - 1]->score + log(prob[blank_id]) -
|
|
|
|
- std::max(0.0, ext_scorer->beta);
|
|
|
|
std::max(0.0, extscorer->beta);
|
|
|
|
full_beam = (num_prefixes == beam_size);
|
|
|
|
full_beam = (num_prefixes == beam_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// pruning of vacobulary
|
|
|
|
// pruning of vacobulary
|
|
|
|
int cutoff_len = prob.size();
|
|
|
|
int cutoff_len = prob.size();
|
|
|
|
if (cutoff_prob < 1.0 || cutoff_top_n < prob.size()) {
|
|
|
|
if (cutoff_prob < 1.0 || cutoff_top_n < prob.size()) {
|
|
|
|
std::sort(prob_idx.begin(),
|
|
|
|
std::sort(
|
|
|
|
prob_idx.end(),
|
|
|
|
prob_idx.begin(), prob_idx.end(), pair_comp_second_rev<int, double>);
|
|
|
|
pair_comp_second_rev<int, double>);
|
|
|
|
|
|
|
|
if (cutoff_prob < 1.0) {
|
|
|
|
if (cutoff_prob < 1.0) {
|
|
|
|
double cum_prob = 0.0;
|
|
|
|
double cum_prob = 0.0;
|
|
|
|
cutoff_len = 0;
|
|
|
|
cutoff_len = 0;
|
|
|
|
for (int i=0; i<prob_idx.size(); i++) {
|
|
|
|
for (int i = 0; i < prob_idx.size(); i++) {
|
|
|
|
cum_prob += prob_idx[i].second;
|
|
|
|
cum_prob += prob_idx[i].second;
|
|
|
|
cutoff_len += 1;
|
|
|
|
cutoff_len += 1;
|
|
|
|
if (cum_prob >= cutoff_prob) break;
|
|
|
|
if (cum_prob >= cutoff_prob) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cutoff_len = std::min(cutoff_len, cutoff_top_n);
|
|
|
|
cutoff_len = std::min(cutoff_len, cutoff_top_n);
|
|
|
|
prob_idx = std::vector<std::pair<int, double> >( prob_idx.begin(),
|
|
|
|
prob_idx = std::vector<std::pair<int, double>>(
|
|
|
|
prob_idx.begin() + cutoff_len);
|
|
|
|
prob_idx.begin(), prob_idx.begin() + cutoff_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<std::pair<int, float> > log_prob_idx;
|
|
|
|
std::vector<std::pair<int, float>> log_prob_idx;
|
|
|
|
for (int i = 0; i < cutoff_len; i++) {
|
|
|
|
for (int i = 0; i < cutoff_len; i++) {
|
|
|
|
log_prob_idx.push_back(std::pair<int, float>
|
|
|
|
log_prob_idx.push_back(std::pair<int, float>(
|
|
|
|
(prob_idx[i].first, log(prob_idx[i].second + NUM_FLT_MIN)));
|
|
|
|
prob_idx[i].first, log(prob_idx[i].second + NUM_FLT_MIN)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// loop over chars
|
|
|
|
// loop over chars
|
|
|
@ -164,24 +159,22 @@ std::vector<std::pair<double, std::string> >
|
|
|
|
auto c = log_prob_idx[index].first;
|
|
|
|
auto c = log_prob_idx[index].first;
|
|
|
|
float log_prob_c = log_prob_idx[index].second;
|
|
|
|
float log_prob_c = log_prob_idx[index].second;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < prefixes.size() && i<beam_size; i++) {
|
|
|
|
for (int i = 0; i < prefixes.size() && i < beam_size; i++) {
|
|
|
|
auto prefix = prefixes[i];
|
|
|
|
auto prefix = prefixes[i];
|
|
|
|
|
|
|
|
|
|
|
|
if (full_beam && log_prob_c + prefix->_score < min_cutoff) {
|
|
|
|
if (full_beam && log_prob_c + prefix->score < min_cutoff) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// blank
|
|
|
|
// blank
|
|
|
|
if (c == blank_id) {
|
|
|
|
if (c == blank_id) {
|
|
|
|
prefix->_log_prob_b_cur = log_sum_exp(
|
|
|
|
prefix->log_prob_b_cur =
|
|
|
|
prefix->_log_prob_b_cur,
|
|
|
|
log_sum_exp(prefix->log_prob_b_cur, log_prob_c + prefix->score);
|
|
|
|
log_prob_c + prefix->_score);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// repeated character
|
|
|
|
// repeated character
|
|
|
|
if (c == prefix->_character) {
|
|
|
|
if (c == prefix->character) {
|
|
|
|
prefix->_log_prob_nb_cur = log_sum_exp(
|
|
|
|
prefix->log_prob_nb_cur = log_sum_exp(
|
|
|
|
prefix->_log_prob_nb_cur,
|
|
|
|
prefix->log_prob_nb_cur, log_prob_c + prefix->log_prob_nb_prev);
|
|
|
|
log_prob_c + prefix->_log_prob_nb_prev);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// get new prefix
|
|
|
|
// get new prefix
|
|
|
|
auto prefix_new = prefix->get_path_trie(c);
|
|
|
|
auto prefix_new = prefix->get_path_trie(c);
|
|
|
@ -189,36 +182,35 @@ std::vector<std::pair<double, std::string> >
|
|
|
|
if (prefix_new != nullptr) {
|
|
|
|
if (prefix_new != nullptr) {
|
|
|
|
float log_p = -NUM_FLT_INF;
|
|
|
|
float log_p = -NUM_FLT_INF;
|
|
|
|
|
|
|
|
|
|
|
|
if (c == prefix->_character
|
|
|
|
if (c == prefix->character &&
|
|
|
|
&& prefix->_log_prob_b_prev > -NUM_FLT_INF) {
|
|
|
|
prefix->log_prob_b_prev > -NUM_FLT_INF) {
|
|
|
|
log_p = log_prob_c + prefix->_log_prob_b_prev;
|
|
|
|
log_p = log_prob_c + prefix->log_prob_b_prev;
|
|
|
|
} else if (c != prefix->_character) {
|
|
|
|
} else if (c != prefix->character) {
|
|
|
|
log_p = log_prob_c + prefix->_score;
|
|
|
|
log_p = log_prob_c + prefix->score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// language model scoring
|
|
|
|
// language model scoring
|
|
|
|
if (ext_scorer != nullptr &&
|
|
|
|
if (extscorer != nullptr &&
|
|
|
|
(c == space_id || ext_scorer->is_character_based()) ) {
|
|
|
|
(c == space_id || extscorer->is_character_based())) {
|
|
|
|
PathTrie *prefix_to_score = nullptr;
|
|
|
|
PathTrie *prefix_toscore = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
// skip scoring the space
|
|
|
|
// skip scoring the space
|
|
|
|
if (ext_scorer->is_character_based()) {
|
|
|
|
if (extscorer->is_character_based()) {
|
|
|
|
prefix_to_score = prefix_new;
|
|
|
|
prefix_toscore = prefix_new;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
prefix_to_score = prefix;
|
|
|
|
prefix_toscore = prefix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double score = 0.0;
|
|
|
|
double score = 0.0;
|
|
|
|
std::vector<std::string> ngram;
|
|
|
|
std::vector<std::string> ngram;
|
|
|
|
ngram = ext_scorer->make_ngram(prefix_to_score);
|
|
|
|
ngram = extscorer->make_ngram(prefix_toscore);
|
|
|
|
score = ext_scorer->get_log_cond_prob(ngram) *
|
|
|
|
score = extscorer->get_log_cond_prob(ngram) * extscorer->alpha;
|
|
|
|
ext_scorer->alpha;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log_p += score;
|
|
|
|
log_p += score;
|
|
|
|
log_p += ext_scorer->beta;
|
|
|
|
log_p += extscorer->beta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prefix_new->_log_prob_nb_cur = log_sum_exp(
|
|
|
|
prefix_new->log_prob_nb_cur =
|
|
|
|
prefix_new->_log_prob_nb_cur, log_p);
|
|
|
|
log_sum_exp(prefix_new->log_prob_nb_cur, log_p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end of loop over prefix
|
|
|
|
} // end of loop over prefix
|
|
|
|
} // end of loop over chars
|
|
|
|
} // end of loop over chars
|
|
|
@ -242,33 +234,32 @@ std::vector<std::pair<double, std::string> >
|
|
|
|
|
|
|
|
|
|
|
|
// compute aproximate ctc score as the return score
|
|
|
|
// compute aproximate ctc score as the return score
|
|
|
|
for (size_t i = 0; i < beam_size && i < prefixes.size(); i++) {
|
|
|
|
for (size_t i = 0; i < beam_size && i < prefixes.size(); i++) {
|
|
|
|
double approx_ctc = prefixes[i]->_score;
|
|
|
|
double approx_ctc = prefixes[i]->score;
|
|
|
|
|
|
|
|
|
|
|
|
if (ext_scorer != nullptr) {
|
|
|
|
if (extscorer != nullptr) {
|
|
|
|
std::vector<int> output;
|
|
|
|
std::vector<int> output;
|
|
|
|
prefixes[i]->get_path_vec(output);
|
|
|
|
prefixes[i]->get_path_vec(output);
|
|
|
|
size_t prefix_length = output.size();
|
|
|
|
size_t prefix_length = output.size();
|
|
|
|
auto words = ext_scorer->split_labels(output);
|
|
|
|
auto words = extscorer->split_labels(output);
|
|
|
|
// remove word insert
|
|
|
|
// remove word insert
|
|
|
|
approx_ctc = approx_ctc - prefix_length * ext_scorer->beta;
|
|
|
|
approx_ctc = approx_ctc - prefix_length * extscorer->beta;
|
|
|
|
// remove language model weight:
|
|
|
|
// remove language model weight:
|
|
|
|
approx_ctc -= (ext_scorer->get_sent_log_prob(words))
|
|
|
|
approx_ctc -= (extscorer->get_sent_log_prob(words)) * extscorer->alpha;
|
|
|
|
* ext_scorer->alpha;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
prefixes[i]->_approx_ctc = approx_ctc;
|
|
|
|
prefixes[i]->approx_ctc = approx_ctc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// allow for the post processing
|
|
|
|
// allow for the post processing
|
|
|
|
std::vector<PathTrie*> space_prefixes;
|
|
|
|
std::vector<PathTrie *> space_prefixes;
|
|
|
|
if (space_prefixes.empty()) {
|
|
|
|
if (space_prefixes.empty()) {
|
|
|
|
for (size_t i = 0; i < beam_size && i< prefixes.size(); i++) {
|
|
|
|
for (size_t i = 0; i < beam_size && i < prefixes.size(); i++) {
|
|
|
|
space_prefixes.push_back(prefixes[i]);
|
|
|
|
space_prefixes.push_back(prefixes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::sort(space_prefixes.begin(), space_prefixes.end(), prefix_compare);
|
|
|
|
std::sort(space_prefixes.begin(), space_prefixes.end(), prefix_compare);
|
|
|
|
std::vector<std::pair<double, std::string> > output_vecs;
|
|
|
|
std::vector<std::pair<double, std::string>> output_vecs;
|
|
|
|
for (size_t i = 0; i < beam_size && i < space_prefixes.size(); i++) {
|
|
|
|
for (size_t i = 0; i < beam_size && i < space_prefixes.size(); i++) {
|
|
|
|
std::vector<int> output;
|
|
|
|
std::vector<int> output;
|
|
|
|
space_prefixes[i]->get_path_vec(output);
|
|
|
|
space_prefixes[i]->get_path_vec(output);
|
|
|
@ -277,17 +268,16 @@ std::vector<std::pair<double, std::string> >
|
|
|
|
for (int j = 0; j < output.size(); j++) {
|
|
|
|
for (int j = 0; j < output.size(); j++) {
|
|
|
|
output_str += vocabulary[output[j]];
|
|
|
|
output_str += vocabulary[output[j]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::pair<double, std::string>
|
|
|
|
std::pair<double, std::string> output_pair(-space_prefixes[i]->approx_ctc,
|
|
|
|
output_pair(-space_prefixes[i]->_approx_ctc, output_str);
|
|
|
|
output_str);
|
|
|
|
output_vecs.emplace_back(output_pair);
|
|
|
|
output_vecs.emplace_back(output_pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return output_vecs;
|
|
|
|
return output_vecs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::vector<std::pair<double, std::string> > >
|
|
|
|
std::vector<std::vector<std::pair<double, std::string>>>
|
|
|
|
ctc_beam_search_decoder_batch(
|
|
|
|
ctc_beam_search_decoder_batch(
|
|
|
|
std::vector<std::vector<std::vector<double>>> probs_split,
|
|
|
|
std::vector<std::vector<std::vector<double>>> probs_split,
|
|
|
|
int beam_size,
|
|
|
|
int beam_size,
|
|
|
|
std::vector<std::string> vocabulary,
|
|
|
|
std::vector<std::string> vocabulary,
|
|
|
@ -295,8 +285,7 @@ std::vector<std::vector<std::pair<double, std::string> > >
|
|
|
|
int num_processes,
|
|
|
|
int num_processes,
|
|
|
|
double cutoff_prob,
|
|
|
|
double cutoff_prob,
|
|
|
|
int cutoff_top_n,
|
|
|
|
int cutoff_top_n,
|
|
|
|
Scorer *ext_scorer
|
|
|
|
Scorer *extscorer) {
|
|
|
|
) {
|
|
|
|
|
|
|
|
if (num_processes <= 0) {
|
|
|
|
if (num_processes <= 0) {
|
|
|
|
std::cout << "num_processes must be nonnegative!" << std::endl;
|
|
|
|
std::cout << "num_processes must be nonnegative!" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
exit(1);
|
|
|
@ -307,29 +296,32 @@ std::vector<std::vector<std::pair<double, std::string> > >
|
|
|
|
int batch_size = probs_split.size();
|
|
|
|
int batch_size = probs_split.size();
|
|
|
|
|
|
|
|
|
|
|
|
// scorer filling up
|
|
|
|
// scorer filling up
|
|
|
|
if ( ext_scorer != nullptr) {
|
|
|
|
if (extscorer != nullptr) {
|
|
|
|
if (ext_scorer->is_char_map_empty()) {
|
|
|
|
if (extscorer->is_char_map_empty()) {
|
|
|
|
ext_scorer->set_char_map(vocabulary);
|
|
|
|
extscorer->set_char_map(vocabulary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!ext_scorer->is_character_based()
|
|
|
|
if (!extscorer->is_character_based() &&
|
|
|
|
&& ext_scorer->dictionary == nullptr) {
|
|
|
|
extscorer->dictionary == nullptr) {
|
|
|
|
// init dictionary
|
|
|
|
// init dictionary
|
|
|
|
ext_scorer->fill_dictionary(true);
|
|
|
|
extscorer->fill_dictionary(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// enqueue the tasks of decoding
|
|
|
|
// enqueue the tasks of decoding
|
|
|
|
std::vector<std::future<std::vector<std::pair<double, std::string>>>> res;
|
|
|
|
std::vector<std::future<std::vector<std::pair<double, std::string>>>> res;
|
|
|
|
for (int i = 0; i < batch_size; i++) {
|
|
|
|
for (int i = 0; i < batch_size; i++) {
|
|
|
|
res.emplace_back(
|
|
|
|
res.emplace_back(pool.enqueue(ctc_beam_search_decoder,
|
|
|
|
pool.enqueue(ctc_beam_search_decoder, probs_split[i],
|
|
|
|
probs_split[i],
|
|
|
|
beam_size, vocabulary, blank_id, cutoff_prob,
|
|
|
|
beam_size,
|
|
|
|
cutoff_top_n, ext_scorer)
|
|
|
|
vocabulary,
|
|
|
|
);
|
|
|
|
blank_id,
|
|
|
|
|
|
|
|
cutoff_prob,
|
|
|
|
|
|
|
|
cutoff_top_n,
|
|
|
|
|
|
|
|
extscorer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get decoding results
|
|
|
|
// get decoding results
|
|
|
|
std::vector<std::vector<std::pair<double, std::string> > > batch_results;
|
|
|
|
std::vector<std::vector<std::pair<double, std::string>>> batch_results;
|
|
|
|
for (int i = 0; i < batch_size; i++) {
|
|
|
|
for (int i = 0; i < batch_size; i++) {
|
|
|
|
batch_results.emplace_back(res[i].get());
|
|
|
|
batch_results.emplace_back(res[i].get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|