Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
b3339631
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b3339631
编写于
3月 21, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reimpl in class to sepearte init and decoding
上级
1841cea1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
174 addition
and
153 deletion
+174
-153
fluid/DeepASR/decoder/post_decode_faster.cc
fluid/DeepASR/decoder/post_decode_faster.cc
+134
-141
fluid/DeepASR/decoder/post_decode_faster.h
fluid/DeepASR/decoder/post_decode_faster.h
+32
-6
fluid/DeepASR/decoder/pybind.cc
fluid/DeepASR/decoder/pybind.cc
+8
-6
未找到文件。
fluid/DeepASR/decoder/post_decode_faster.cc
浏览文件 @
b3339631
...
...
@@ -13,153 +13,146 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "post_decode_faster.h"
#include "base/kaldi-common.h"
#include "base/timer.h"
#include "decoder/decodable-matrix.h"
#include "decoder/faster-decoder.h"
#include "fstext/fstext-lib.h"
#include "hmm/transition-model.h"
#include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
#include "tree/context-dep.h"
#include "util/common-utils.h"
std
::
vector
<
std
::
string
>
decode
(
std
::
string
word_syms_filename
,
std
::
string
fst_in_filename
,
std
::
string
logprior_rxfilename
,
std
::
string
posterior_rspecifier
,
std
::
string
words_wspecifier
,
std
::
string
alignment_wspecifier
)
{
using
namespace
kaldi
;
typedef
kaldi
::
int32
int32
;
using
fst
::
SymbolTable
;
using
fst
::
VectorFst
;
using
fst
::
StdArc
;
Decoder
::
Decoder
(
std
::
string
word_syms_filename
,
std
::
string
fst_in_filename
,
std
::
string
logprior_rxfilename
)
{
const
char
*
usage
=
"Decode, reading log-likelihoods (of transition-ids or whatever symbol "
"is on the graph) as matrices."
;
std
::
string
words_wspecifier
=
"ark,t:out.ark"
;
std
::
string
alignment_wspecifier
=
""
;
ParseOptions
po
(
usage
);
binary
=
true
;
acoustic_scale
=
1.5
;
allow_partial
=
true
;
FasterDecoderOptions
decoder_opts
;
decoder_opts
.
Register
(
&
po
,
true
);
// true == include obscure settings.
po
.
Register
(
"binary"
,
&
binary
,
"Write output in binary mode"
);
po
.
Register
(
"allow-partial"
,
&
allow_partial
,
"Produce output even when final state was not reached"
);
po
.
Register
(
"acoustic-scale"
,
&
acoustic_scale
,
"Scaling factor for acoustic likelihoods"
);
words_writer
=
new
Int32VectorWriter
(
words_wspecifier
);
alignment_writer
=
new
Int32VectorWriter
(
alignment_wspecifier
);
word_syms
=
NULL
;
if
(
word_syms_filename
!=
""
)
{
word_syms
=
fst
::
SymbolTable
::
ReadText
(
word_syms_filename
);
if
(
!
word_syms
)
KALDI_ERR
<<
"Could not read symbol table from file "
<<
word_syms_filename
;
}
std
::
ifstream
is_logprior
(
logprior_rxfilename
);
logprior
.
Read
(
is_logprior
,
false
);
// It's important that we initialize decode_fst after loglikes_reader, as it
// can prevent crashes on systems installed without enough virtual memory.
// It has to do with what happens on UNIX systems if you call fork() on a
// large process: the page-table entries are duplicated, which requires a
// lot of virtual memory.
decode_fst
=
fst
::
ReadFstKaldi
(
fst_in_filename
);
decoder
=
new
FasterDecoder
(
*
decode_fst
,
decoder_opts
);
}
Decoder
::~
Decoder
()
{
if
(
!
word_syms
)
delete
word_syms
;
delete
decode_fst
;
delete
decoder
;
delete
words_writer
;
delete
alignment_writer
;
}
std
::
vector
<
std
::
string
>
Decoder
::
decode
(
std
::
string
posterior_rspecifier
)
{
SequentialBaseFloatMatrixReader
posterior_reader
(
posterior_rspecifier
);
std
::
vector
<
std
::
string
>
decoding_results
;
try
{
using
namespace
kaldi
;
typedef
kaldi
::
int32
int32
;
using
fst
::
SymbolTable
;
using
fst
::
VectorFst
;
using
fst
::
StdArc
;
const
char
*
usage
=
"Decode, reading log-likelihoods (of transition-ids or whatever symbol "
"is on the graph) as matrices."
;
ParseOptions
po
(
usage
);
bool
binary
=
true
;
BaseFloat
acoustic_scale
=
1.5
;
bool
allow_partial
=
true
;
FasterDecoderOptions
decoder_opts
;
decoder_opts
.
Register
(
&
po
,
true
);
// true == include obscure settings.
po
.
Register
(
"binary"
,
&
binary
,
"Write output in binary mode"
);
po
.
Register
(
"allow-partial"
,
&
allow_partial
,
"Produce output even when final state was not reached"
);
po
.
Register
(
"acoustic-scale"
,
&
acoustic_scale
,
"Scaling factor for acoustic likelihoods"
);
Int32VectorWriter
words_writer
(
words_wspecifier
);
Int32VectorWriter
alignment_writer
(
alignment_wspecifier
);
fst
::
SymbolTable
*
word_syms
=
NULL
;
if
(
word_syms_filename
!=
""
)
{
word_syms
=
fst
::
SymbolTable
::
ReadText
(
word_syms_filename
);
if
(
!
word_syms
)
KALDI_ERR
<<
"Could not read symbol table from file "
<<
word_syms_filename
;
BaseFloat
tot_like
=
0.0
;
kaldi
::
int64
frame_count
=
0
;
int
num_success
=
0
,
num_fail
=
0
;
Timer
timer
;
for
(;
!
posterior_reader
.
Done
();
posterior_reader
.
Next
())
{
std
::
string
key
=
posterior_reader
.
Key
();
Matrix
<
BaseFloat
>
loglikes
(
posterior_reader
.
Value
());
KALDI_LOG
<<
key
<<
" "
<<
loglikes
.
NumRows
()
<<
" x "
<<
loglikes
.
NumCols
();
if
(
loglikes
.
NumRows
()
==
0
)
{
KALDI_WARN
<<
"Zero-length utterance: "
<<
key
;
num_fail
++
;
continue
;
}
SequentialBaseFloatMatrixReader
posterior_reader
(
posterior_rspecifier
);
std
::
ifstream
is_logprior
(
logprior_rxfilename
);
Vector
<
BaseFloat
>
logprior
;
logprior
.
Read
(
is_logprior
,
false
);
// It's important that we initialize decode_fst after loglikes_reader, as it
// can prevent crashes on systems installed without enough virtual memory.
// It has to do with what happens on UNIX systems if you call fork() on a
// large process: the page-table entries are duplicated, which requires a
// lot of virtual memory.
VectorFst
<
StdArc
>
*
decode_fst
=
fst
::
ReadFstKaldi
(
fst_in_filename
);
BaseFloat
tot_like
=
0.0
;
kaldi
::
int64
frame_count
=
0
;
int
num_success
=
0
,
num_fail
=
0
;
FasterDecoder
decoder
(
*
decode_fst
,
decoder_opts
);
Timer
timer
;
for
(;
!
posterior_reader
.
Done
();
posterior_reader
.
Next
())
{
std
::
string
key
=
posterior_reader
.
Key
();
Matrix
<
BaseFloat
>
loglikes
(
posterior_reader
.
Value
());
KALDI_LOG
<<
key
<<
" "
<<
loglikes
.
NumRows
()
<<
" x "
<<
loglikes
.
NumCols
();
if
(
loglikes
.
NumRows
()
==
0
)
{
KALDI_WARN
<<
"Zero-length utterance: "
<<
key
;
num_fail
++
;
continue
;
}
KALDI_ASSERT
(
loglikes
.
NumCols
()
==
logprior
.
Dim
());
loglikes
.
ApplyLog
();
loglikes
.
AddVecToRows
(
-
1.0
,
logprior
);
DecodableMatrixScaled
decodable
(
loglikes
,
acoustic_scale
);
decoder
.
Decode
(
&
decodable
);
VectorFst
<
LatticeArc
>
decoded
;
// linear FST.
if
((
allow_partial
||
decoder
.
ReachedFinal
())
&&
decoder
.
GetBestPath
(
&
decoded
))
{
num_success
++
;
if
(
!
decoder
.
ReachedFinal
())
KALDI_WARN
<<
"Decoder did not reach end-state, outputting partial "
"traceback."
;
std
::
vector
<
int32
>
alignment
;
std
::
vector
<
int32
>
words
;
LatticeWeight
weight
;
frame_count
+=
loglikes
.
NumRows
();
GetLinearSymbolSequence
(
decoded
,
&
alignment
,
&
words
,
&
weight
);
words_writer
.
Write
(
key
,
words
);
if
(
alignment_writer
.
IsOpen
())
alignment_writer
.
Write
(
key
,
alignment
);
if
(
word_syms
!=
NULL
)
{
std
::
string
res
;
for
(
size_t
i
=
0
;
i
<
words
.
size
();
i
++
)
{
std
::
string
s
=
word_syms
->
Find
(
words
[
i
]);
res
+=
s
;
if
(
s
==
""
)
KALDI_ERR
<<
"Word-id "
<<
words
[
i
]
<<
" not in symbol table."
;
std
::
cerr
<<
s
<<
' '
;
}
decoding_results
.
push_back
(
res
);
KALDI_ASSERT
(
loglikes
.
NumCols
()
==
logprior
.
Dim
());
loglikes
.
ApplyLog
();
loglikes
.
AddVecToRows
(
-
1.0
,
logprior
);
DecodableMatrixScaled
decodable
(
loglikes
,
acoustic_scale
);
decoder
->
Decode
(
&
decodable
);
VectorFst
<
LatticeArc
>
decoded
;
// linear FST.
if
((
allow_partial
||
decoder
->
ReachedFinal
())
&&
decoder
->
GetBestPath
(
&
decoded
))
{
num_success
++
;
if
(
!
decoder
->
ReachedFinal
())
KALDI_WARN
<<
"Decoder did not reach end-state, outputting partial "
"traceback."
;
std
::
vector
<
int32
>
alignment
;
std
::
vector
<
int32
>
words
;
LatticeWeight
weight
;
frame_count
+=
loglikes
.
NumRows
();
GetLinearSymbolSequence
(
decoded
,
&
alignment
,
&
words
,
&
weight
);
words_writer
->
Write
(
key
,
words
);
if
(
alignment_writer
->
IsOpen
())
alignment_writer
->
Write
(
key
,
alignment
);
if
(
word_syms
!=
NULL
)
{
std
::
string
res
;
for
(
size_t
i
=
0
;
i
<
words
.
size
();
i
++
)
{
std
::
string
s
=
word_syms
->
Find
(
words
[
i
]);
res
+=
s
;
if
(
s
==
""
)
KALDI_ERR
<<
"Word-id "
<<
words
[
i
]
<<
" not in symbol table."
;
std
::
cerr
<<
s
<<
' '
;
}
BaseFloat
like
=
-
weight
.
Value1
()
-
weight
.
Value2
();
tot_like
+=
like
;
KALDI_LOG
<<
"Log-like per frame for utterance "
<<
key
<<
" is "
<<
(
like
/
loglikes
.
NumRows
())
<<
" over "
<<
loglikes
.
NumRows
()
<<
" frames."
;
}
else
{
num_fail
++
;
KALDI_WARN
<<
"Did not successfully decode utterance "
<<
key
<<
", len = "
<<
loglikes
.
NumRows
();
decoding_results
.
push_back
(
res
);
}
BaseFloat
like
=
-
weight
.
Value1
()
-
weight
.
Value2
();
tot_like
+=
like
;
KALDI_LOG
<<
"Log-like per frame for utterance "
<<
key
<<
" is "
<<
(
like
/
loglikes
.
NumRows
())
<<
" over "
<<
loglikes
.
NumRows
()
<<
" frames."
;
}
else
{
num_fail
++
;
KALDI_WARN
<<
"Did not successfully decode utterance "
<<
key
<<
", len = "
<<
loglikes
.
NumRows
();
}
double
elapsed
=
timer
.
Elapsed
();
KALDI_LOG
<<
"Time taken [excluding initialization] "
<<
elapsed
<<
"s: real-time factor assuming 100 frames/sec is "
<<
(
elapsed
*
100.0
/
frame_count
);
KALDI_LOG
<<
"Done "
<<
num_success
<<
" utterances, failed for "
<<
num_fail
;
KALDI_LOG
<<
"Overall log-likelihood per frame is "
<<
(
tot_like
/
frame_count
)
<<
" over "
<<
frame_count
<<
" frames."
;
delete
word_syms
;
delete
decode_fst
;
}
catch
(
const
std
::
exception
&
e
)
{
std
::
cerr
<<
e
.
what
();
}
double
elapsed
=
timer
.
Elapsed
();
KALDI_LOG
<<
"Time taken [excluding initialization] "
<<
elapsed
<<
"s: real-time factor assuming 100 frames/sec is "
<<
(
elapsed
*
100.0
/
frame_count
);
KALDI_LOG
<<
"Done "
<<
num_success
<<
" utterances, failed for "
<<
num_fail
;
KALDI_LOG
<<
"Overall log-likelihood per frame is "
<<
(
tot_like
/
frame_count
)
<<
" over "
<<
frame_count
<<
" frames."
;
return
decoding_results
;
}
fluid/DeepASR/decoder/post_decode_faster.h
浏览文件 @
b3339631
...
...
@@ -14,10 +14,36 @@ limitations under the License. */
#include <string>
#include <vector>
#include "base/kaldi-common.h"
#include "base/timer.h"
#include "decoder/decodable-matrix.h"
#include "decoder/faster-decoder.h"
#include "fstext/fstext-lib.h"
#include "hmm/transition-model.h"
#include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
#include "tree/context-dep.h"
#include "util/common-utils.h"
std
::
vector
<
std
::
string
>
decode
(
std
::
string
word_syms_filename
,
std
::
string
fst_in_filename
,
std
::
string
logprior_rxfilename
,
std
::
string
posterior_respecifier
,
std
::
string
words_wspecifier
,
std
::
string
alignment_wspecifier
=
""
);
class
Decoder
{
public:
Decoder
(
std
::
string
word_syms_filename
,
std
::
string
fst_in_filename
,
std
::
string
logprior_rxfilename
);
~
Decoder
();
std
::
vector
<
std
::
string
>
decode
(
std
::
string
posterior_rspecifier
);
private:
fst
::
SymbolTable
*
word_syms
;
fst
::
VectorFst
<
fst
::
StdArc
>
*
decode_fst
;
kaldi
::
FasterDecoder
*
decoder
;
kaldi
::
Vector
<
kaldi
::
BaseFloat
>
logprior
;
kaldi
::
Int32VectorWriter
*
words_writer
;
kaldi
::
Int32VectorWriter
*
alignment_writer
;
bool
binary
;
kaldi
::
BaseFloat
acoustic_scale
;
bool
allow_partial
;
};
fluid/DeepASR/decoder/pybind.cc
浏览文件 @
b3339631
...
...
@@ -20,10 +20,12 @@ limitations under the License. */
namespace
py
=
pybind11
;
PYBIND11_MODULE
(
post_decode_faster
,
m
)
{
m
.
doc
()
=
"Decode function for Deep ASR model"
;
m
.
def
(
"decode"
,
&
decode
,
"Decode one input probability matrix "
"and return the transcription"
);
m
.
doc
()
=
"Decoder for Deep ASR model"
;
py
::
class_
<
Decoder
>
(
m
,
"Decoder"
)
.
def
(
py
::
init
<
std
::
string
,
std
::
string
,
std
::
string
>
())
.
def
(
"decode"
,
&
Decoder
::
decode
,
"Decode one input probability matrix "
"and return the transcription"
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录