Skip to content

Commit

Permalink
fix a bug in scorer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yibing Liu committed Jul 3, 2017
1 parent 59b4b87 commit d6c3028
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
3 changes: 2 additions & 1 deletion deep_speech_2/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ def infer():
for i, probs in enumerate(probs_split)
]

# external scorer
ext_scorer = Scorer(args.alpha, args.beta, args.language_model_path)
## decode and print

## decode and print
wer_sum, wer_counter = 0, 0
for i, probs in enumerate(probs_split):
beam_result = ctc_beam_search_decoder(
Expand Down
26 changes: 13 additions & 13 deletions deep_speech_2/deploy/ctc_beam_search_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ bool pair_comp_second_rev(const std::pair<T1, T2> a, const std::pair<T1, T2> b)
return a.second > b.second;
}

/* CTC beam search decoder in C++, the interface is consistent with the original
/* CTC beam search decoder in C++, the interface is consistent with the original
decoder in Python version.
*/
std::vector<std::pair<double, std::string> >
std::vector<std::pair<double, std::string> >
ctc_beam_search_decoder(std::vector<std::vector<double> > probs_seq,
int beam_size,
std::vector<std::string> vocabulary,
Expand All @@ -29,15 +29,15 @@ std::vector<std::pair<double, std::string> >
)
{
int num_time_steps = probs_seq.size();
// assign space ID

// assign space ID
std::vector<std::string>::iterator it = std::find(vocabulary.begin(), vocabulary.end(), " ");
int space_id = it-vocabulary.begin();
if(space_id >= vocabulary.size()) {
std::cout<<"The character space is not in the vocabulary!";
exit(1);
exit(1);
}

// initialize
// two sets containing selected and candidate prefixes respectively
std::map<std::string, double> prefix_set_prev, prefix_set_next;
Expand All @@ -47,7 +47,7 @@ std::vector<std::pair<double, std::string> >
prefix_set_prev["\t"] = 1.0;
probs_b_prev["\t"] = 1.0;
probs_nb_prev["\t"] = 0.0;

for (int time_step=0; time_step<num_time_steps; time_step++) {
prefix_set_next.clear();
probs_b_cur.clear();
Expand All @@ -70,8 +70,8 @@ std::vector<std::pair<double, std::string> >
}
prob_idx = std::vector<std::pair<int, double> >(prob_idx.begin(), prob_idx.begin()+cutoff_len);
}
// extend prefix
for (std::map<std::string, double>::iterator it = prefix_set_prev.begin();
// extend prefix
for (std::map<std::string, double>::iterator it = prefix_set_prev.begin();
it != prefix_set_prev.end(); it++) {
std::string l = it->first;
if( prefix_set_next.find(l) == prefix_set_next.end()) {
Expand Down Expand Up @@ -109,12 +109,12 @@ std::vector<std::pair<double, std::string> >
}
}

prefix_set_next[l] = probs_b_cur[l]+probs_nb_cur[l];
prefix_set_next[l] = probs_b_cur[l]+probs_nb_cur[l];
}

probs_b_prev = probs_b_cur;
probs_nb_prev = probs_nb_cur;
std::vector<std::pair<std::string, double> >
std::vector<std::pair<std::string, double> >
prefix_vec_next(prefix_set_next.begin(), prefix_set_next.end());
std::sort(prefix_vec_next.begin(), prefix_vec_next.end(), pair_comp_second_rev<std::string, double>);
int k = beam_size<prefix_vec_next.size() ? beam_size : prefix_vec_next.size();
Expand All @@ -124,7 +124,7 @@ std::vector<std::pair<double, std::string> >

// post processing
std::vector<std::pair<double, std::string> > beam_result;
for (std::map<std::string, double>::iterator it = prefix_set_prev.begin();
for (std::map<std::string, double>::iterator it = prefix_set_prev.begin();
it != prefix_set_prev.end(); it++) {
if (it->second > 0.0 && it->first.size() > 1) {
double prob = it->second;
Expand All @@ -134,7 +134,7 @@ std::vector<std::pair<double, std::string> >
prob = prob * ext_scorer->get_score(sentence);
}
double log_prob = log(it->second);
beam_result.push_back(std::pair<double, std::string>(log_prob, it->first));
beam_result.push_back(std::pair<double, std::string>(log_prob, sentence));
}
}
// sort the result and return
Expand Down
9 changes: 4 additions & 5 deletions deep_speech_2/deploy/scorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ inline void strip(std::string &str, char ch=' ') {
break;
}
}

if (start == 0 && end == str.size()-1) return;
if (start > end) {
std::string emp_str;
Expand All @@ -47,13 +47,12 @@ inline void strip(std::string &str, char ch=' ') {

int Scorer::word_count(std::string sentence) {
strip(sentence);
int cnt = 0;
int cnt = 1;
for (int i=0; i<sentence.size(); i++) {
if (sentence[i] == ' ' && sentence[i-1] != ' ') {
cnt ++;
}
}
if (cnt > 0) cnt ++;
return cnt;
}

Expand All @@ -68,8 +67,8 @@ double Scorer::language_model_score(std::string sentence) {
ret = model->FullScore(state, vocab, out_state);
state = out_state;
}
double score = ret.prob;
double score = ret.prob;

return pow(10, score);
}

Expand Down

0 comments on commit d6c3028

Please sign in to comment.