reorganize cpp files

pull/2/head
Yibing Liu 7 years ago
parent 37b236869f
commit 1b707054a9

@ -11,7 +11,7 @@ import paddle.v2 as paddle
from data_utils.data import DataGenerator
from model import deep_speech2
from deploy.swig_decoders import *
from swig_scorer import LmScorer
from swig_scorer import Scorer
from error_rate import wer
import utils
import time
@ -19,7 +19,7 @@ import time
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--num_samples",
default=100,
default=10,
type=int,
help="Number of samples for inference. (default: %(default)s)")
parser.add_argument(
@ -164,7 +164,7 @@ def infer():
]
# external scorer
ext_scorer = LmScorer(args.alpha, args.beta, args.language_model_path)
ext_scorer = Scorer(args.alpha, args.beta, args.language_model_path)
## decode and print
time_begin = time.time()

@ -5,9 +5,11 @@
#include <cmath>
#include <limits>
#include "ctc_decoders.h"
#include "decoder_utils.h"
typedef double log_prob_type;
template <typename T1, typename T2>
bool pair_comp_first_rev(const std::pair<T1, T2> a, const std::pair<T1, T2> b)
{
@ -81,7 +83,7 @@ std::vector<std::pair<double, std::string> >
std::vector<std::string> vocabulary,
int blank_id,
double cutoff_prob,
LmScorer *ext_scorer,
Scorer *ext_scorer,
bool nproc) {
// dimension check
int num_time_steps = probs_seq.size();

@ -28,7 +28,7 @@ std::vector<std::pair<double, std::string> >
std::vector<std::string> vocabulary,
int blank_id,
double cutoff_prob=1.0,
LmScorer *ext_scorer=NULL,
Scorer *ext_scorer=NULL,
bool nproc=false
);

@ -19,4 +19,5 @@ namespace std{
}
%import scorer.h
%import decoder_utils.h
%include "ctc_decoders.h"

@ -39,8 +39,10 @@ os.system('swig -python -c++ ./ctc_decoders.i')
ctc_beam_search_decoder_module = [
Extension(
name='_swig_ctc_decoders',
sources=FILES +
['scorer.cpp', 'ctc_decoders_wrap.cxx', 'ctc_decoders.cpp'],
sources=FILES + [
'scorer.cpp', 'ctc_decoders_wrap.cxx', 'ctc_decoders.cpp',
'decoder_utils.cpp'
],
language='C++',
include_dirs=['.', './kenlm'],
libraries=LIBS,

@ -0,0 +1,5 @@
#include <limits>
#include <algorithm>
#include <cmath>
#include "decoder_utils.h"

@ -0,0 +1,15 @@
#ifndef DECODER_UTILS_H
#define DECODER_UTILS_H
#pragma once
#include <utility>
/*
template <typename T1, typename T2>
bool pair_comp_first_rev(const std::pair<T1, T2> a, const std::pair<T1, T2> b);
template <typename T1, typename T2>
bool pair_comp_second_rev(const std::pair<T1, T2> a, const std::pair<T1, T2> b);
template <typename T> T log_sum_exp(T x, T y);
*/
#endif // DECODER_UTILS_H

@ -7,7 +7,7 @@
using namespace lm::ngram;
LmScorer::LmScorer(float alpha, float beta, std::string lm_model_path) {
Scorer::Scorer(float alpha, float beta, std::string lm_model_path) {
this->_alpha = alpha;
this->_beta = beta;
@ -18,7 +18,7 @@ LmScorer::LmScorer(float alpha, float beta, std::string lm_model_path) {
this->_language_model = LoadVirtual(lm_model_path.c_str());
}
LmScorer::~LmScorer(){
Scorer::~Scorer(){
delete (lm::base::Model *)this->_language_model;
}
@ -57,7 +57,7 @@ inline void strip(std::string &str, char ch=' ') {
}
}
int LmScorer::word_count(std::string sentence) {
int Scorer::word_count(std::string sentence) {
strip(sentence);
int cnt = 1;
for (int i=0; i<sentence.size(); i++) {
@ -68,7 +68,7 @@ int LmScorer::word_count(std::string sentence) {
return cnt;
}
double LmScorer::language_model_score(std::string sentence) {
double Scorer::language_model_score(std::string sentence) {
lm::base::Model *model = (lm::base::Model *)this->_language_model;
State state, out_state;
lm::FullScoreReturn ret;
@ -84,12 +84,12 @@ double LmScorer::language_model_score(std::string sentence) {
return log_prob;
}
void LmScorer::reset_params(float alpha, float beta) {
void Scorer::reset_params(float alpha, float beta) {
this->_alpha = alpha;
this->_beta = beta;
}
double LmScorer::get_score(std::string sentence, bool log) {
double Scorer::get_score(std::string sentence, bool log) {
double lm_score = language_model_score(sentence);
int word_cnt = word_count(sentence);

@ -8,10 +8,10 @@
* count and language model scoring.
* Example:
* LmScorer ext_scorer(alpha, beta, "path_to_language_model.klm");
* Scorer ext_scorer(alpha, beta, "path_to_language_model.klm");
* double score = ext_scorer.get_score("sentence_to_score");
*/
class LmScorer{
class Scorer{
private:
float _alpha;
float _beta;
@ -23,9 +23,9 @@ private:
double language_model_score(std::string);
public:
LmScorer(){}
LmScorer(float alpha, float beta, std::string lm_model_path);
~LmScorer();
Scorer(){}
Scorer(float alpha, float beta, std::string lm_model_path);
~Scorer();
// reset params alpha & beta
void reset_params(float alpha, float beta);

@ -4,7 +4,8 @@ from __future__ import division
from __future__ import print_function
import swig_ctc_decoders
import multiprocessing
#import multiprocessing
from pathos.multiprocessing import Pool
def ctc_best_path_decoder(probs_seq, vocabulary):
@ -73,14 +74,37 @@ def ctc_beam_search_decoder_batch(probs_split,
if not num_processes > 0:
raise ValueError("Number of processes must be positive!")
pool = multiprocessing.Pool(processes=num_processes)
pool = Pool(processes=num_processes)
results = []
args_list = []
for i, probs_list in enumerate(probs_split):
args = (probs_list, beam_size, vocabulary, blank_id, cutoff_prob,
ext_scoring_func)
args_list.append(args)
results.append(pool.apply_async(ctc_beam_search_decoder, args))
pool.close()
pool.join()
beam_search_results = [result.get() for result in results]
"""
len_args = len(probs_split)
beam_search_results = pool.map(ctc_beam_search_decoder,
probs_split,
[beam_size for i in xrange(len_args)],
[vocabulary for i in xrange(len_args)],
[blank_id for i in xrange(len_args)],
[cutoff_prob for i in xrange(len_args)],
[ext_scoring_func for i in xrange(len_args)]
)
"""
'''
processes = [mp.Process(target=ctc_beam_search_decoder,
args=(probs_list, beam_size, vocabulary, blank_id, cutoff_prob,
ext_scoring_func) for probs_list in probs_split]
for p in processes:
p.start()
for p in processes:
p.join()
beam_search_results = []
'''
return beam_search_results

Loading…
Cancel
Save