Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
4b3f6c61
D
DeepSpeech
项目概览
PaddlePaddle
/
DeepSpeech
12 个月 前同步成功
通知
204
Star
8425
Fork
1598
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
245
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DeepSpeech
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
245
Issue
245
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
4b3f6c61
编写于
5月 18, 2022
作者:
H
Hui Zhang
提交者:
GitHub
5月 18, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1913 from Honei/timelimit
[asr][server]add asr time limt configuration
上级
38eb3361
b1ef4349
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
112 addition
and
82 deletion
+112
-82
paddlespeech/cli/asr/infer.py
paddlespeech/cli/asr/infer.py
+20
-3
paddlespeech/s2t/modules/decoder.py
paddlespeech/s2t/modules/decoder.py
+17
-16
paddlespeech/s2t/modules/embedding.py
paddlespeech/s2t/modules/embedding.py
+7
-2
paddlespeech/s2t/modules/encoder.py
paddlespeech/s2t/modules/encoder.py
+48
-46
paddlespeech/server/engine/asr/python/asr_engine.py
paddlespeech/server/engine/asr/python/asr_engine.py
+20
-15
未找到文件。
paddlespeech/cli/asr/infer.py
浏览文件 @
4b3f6c61
...
...
@@ -187,6 +187,7 @@ class ASRExecutor(BaseExecutor):
vocab
=
self
.
config
.
vocab_filepath
,
spm_model_prefix
=
self
.
config
.
spm_model_prefix
)
self
.
config
.
decode
.
decoding_method
=
decode_method
else
:
raise
Exception
(
"wrong type"
)
model_name
=
model_type
[:
model_type
.
rindex
(
...
...
@@ -201,6 +202,21 @@ class ASRExecutor(BaseExecutor):
model_dict
=
paddle
.
load
(
self
.
ckpt_path
)
self
.
model
.
set_state_dict
(
model_dict
)
# compute the max len limit
if
"conformer"
in
model_type
or
"transformer"
in
model_type
or
"wenetspeech"
in
model_type
:
# in transformer like model, we may use the subsample rate cnn network
subsample_rate
=
self
.
model
.
subsampling_rate
()
frame_shift_ms
=
self
.
config
.
preprocess_config
.
process
[
0
][
'n_shift'
]
/
self
.
config
.
preprocess_config
.
process
[
0
][
'fs'
]
max_len
=
self
.
model
.
encoder
.
embed
.
pos_enc
.
max_len
if
self
.
config
.
encoder_conf
.
get
(
"max_len"
,
None
):
max_len
=
self
.
config
.
encoder_conf
.
max_len
self
.
max_len
=
frame_shift_ms
*
max_len
*
subsample_rate
logger
.
info
(
f
"The asr server limit max duration len:
{
self
.
max_len
}
"
)
def
preprocess
(
self
,
model_type
:
str
,
input
:
Union
[
str
,
os
.
PathLike
]):
"""
Input preprocess and return paddle.Tensor stored in self.input.
...
...
@@ -352,9 +368,10 @@ class ASRExecutor(BaseExecutor):
audio
,
audio_sample_rate
=
soundfile
.
read
(
audio_file
,
dtype
=
"int16"
,
always_2d
=
True
)
audio_duration
=
audio
.
shape
[
0
]
/
audio_sample_rate
max_duration
=
50.0
if
audio_duration
>=
max_duration
:
logger
.
error
(
"Please input audio file less then 50 seconds.
\n
"
)
if
audio_duration
>
self
.
max_len
:
logger
.
error
(
f
"Please input audio file less then
{
self
.
max_len
}
seconds.
\n
"
)
return
False
except
Exception
as
e
:
logger
.
exception
(
e
)
...
...
paddlespeech/s2t/modules/decoder.py
浏览文件 @
4b3f6c61
...
...
@@ -62,21 +62,21 @@ class TransformerDecoder(BatchScorerInterface, nn.Layer):
False: x -> x + att(x)
"""
def
__init__
(
self
,
vocab
_size
:
int
,
encoder_output_size
:
int
,
attention_heads
:
int
=
4
,
linear_units
:
int
=
2048
,
num_blocks
:
int
=
6
,
dropout_rate
:
float
=
0.1
,
positional_dropout_rate
:
float
=
0.1
,
self
_attention_dropout_rate
:
float
=
0.0
,
src_attention_dropout_rate
:
float
=
0.0
,
input_layer
:
str
=
"embed"
,
use_output_layer
:
bool
=
True
,
normalize_before
:
bool
=
Tru
e
,
concat_after
:
bool
=
False
,
):
def
__init__
(
self
,
vocab_size
:
int
,
encoder_output
_size
:
int
,
attention_heads
:
int
=
4
,
linear_units
:
int
=
2048
,
num_blocks
:
int
=
6
,
dropout_rate
:
float
=
0.1
,
positional_
dropout_rate
:
float
=
0.1
,
self_attention_dropout_rate
:
float
=
0.0
,
src
_attention_dropout_rate
:
float
=
0.0
,
input_layer
:
str
=
"embed"
,
use_output_layer
:
bool
=
True
,
normalize_before
:
bool
=
True
,
concat_after
:
bool
=
Fals
e
,
max_len
:
int
=
5000
):
assert
check_argument_types
()
...
...
@@ -87,7 +87,8 @@ class TransformerDecoder(BatchScorerInterface, nn.Layer):
if
input_layer
==
"embed"
:
self
.
embed
=
nn
.
Sequential
(
Embedding
(
vocab_size
,
attention_dim
),
PositionalEncoding
(
attention_dim
,
positional_dropout_rate
),
)
PositionalEncoding
(
attention_dim
,
positional_dropout_rate
,
max_len
=
max_len
),
)
else
:
raise
ValueError
(
f
"only 'embed' is supported:
{
input_layer
}
"
)
...
...
paddlespeech/s2t/modules/embedding.py
浏览文件 @
4b3f6c61
...
...
@@ -112,7 +112,9 @@ class PositionalEncoding(nn.Layer, PositionalEncodingInterface):
paddle.Tensor: for compatibility to RelPositionalEncoding, (batch=1, time, ...)
"""
T
=
x
.
shape
[
1
]
assert
offset
+
x
.
shape
[
1
]
<
self
.
max_len
assert
offset
+
x
.
shape
[
1
]
<
self
.
max_len
,
"offset: {} + x.shape[1]: {} is larger than the max_len: {}"
.
format
(
offset
,
x
.
shape
[
1
],
self
.
max_len
)
#TODO(Hui Zhang): using T = x.size(1), __getitem__ not support Tensor
pos_emb
=
self
.
pe
[:,
offset
:
offset
+
T
]
x
=
x
*
self
.
xscale
+
pos_emb
...
...
@@ -148,6 +150,7 @@ class RelPositionalEncoding(PositionalEncoding):
max_len (int, optional): [Maximum input length.]. Defaults to 5000.
"""
super
().
__init__
(
d_model
,
dropout_rate
,
max_len
,
reverse
=
True
)
logger
.
info
(
f
"max len:
{
max_len
}
"
)
def
forward
(
self
,
x
:
paddle
.
Tensor
,
offset
:
int
=
0
)
->
Tuple
[
paddle
.
Tensor
,
paddle
.
Tensor
]:
...
...
@@ -158,7 +161,9 @@ class RelPositionalEncoding(PositionalEncoding):
paddle.Tensor: Encoded tensor (batch, time, `*`).
paddle.Tensor: Positional embedding tensor (1, time, `*`).
"""
assert
offset
+
x
.
shape
[
1
]
<
self
.
max_len
assert
offset
+
x
.
shape
[
1
]
<
self
.
max_len
,
"offset: {} + x.shape[1]: {} is larger than the max_len: {}"
.
format
(
offset
,
x
.
shape
[
1
],
self
.
max_len
)
x
=
x
*
self
.
xscale
#TODO(Hui Zhang): using x.size(1), __getitem__ not support Tensor
pos_emb
=
self
.
pe
[:,
offset
:
offset
+
x
.
shape
[
1
]]
...
...
paddlespeech/s2t/modules/encoder.py
浏览文件 @
4b3f6c61
...
...
@@ -47,24 +47,24 @@ __all__ = ["BaseEncoder", 'TransformerEncoder', "ConformerEncoder"]
class
BaseEncoder
(
nn
.
Layer
):
def
__init__
(
self
,
input_size
:
int
,
output_size
:
int
=
256
,
attention_heads
:
int
=
4
,
linear_units
:
int
=
2048
,
num_blocks
:
int
=
6
,
dropout_rate
:
float
=
0.1
,
positional_dropout_rate
:
float
=
0.1
,
attention_dropout_rate
:
float
=
0.0
,
input_layer
:
str
=
"conv2d
"
,
pos_enc_layer_type
:
str
=
"abs_pos"
,
normalize_before
:
bool
=
Tru
e
,
concat_after
:
bool
=
False
,
static_chunk_size
:
int
=
0
,
use_dynamic_chunk
:
bool
=
Fals
e
,
global_cmvn
:
paddle
.
nn
.
Layer
=
Non
e
,
use_dynamic_left_chunk
:
bool
=
False
,
):
def
__init__
(
self
,
input_size
:
int
,
output_size
:
int
=
256
,
attention_heads
:
int
=
4
,
linear_units
:
int
=
2048
,
num_blocks
:
int
=
6
,
dropout_rate
:
float
=
0.1
,
positional_
dropout_rate
:
float
=
0.1
,
attention_dropout_rate
:
float
=
0.0
,
input_layer
:
str
=
"conv2d"
,
pos_enc_layer_type
:
str
=
"abs_pos
"
,
normalize_before
:
bool
=
True
,
concat_after
:
bool
=
Fals
e
,
static_chunk_size
:
int
=
0
,
use_dynamic_chunk
:
bool
=
False
,
global_cmvn
:
paddle
.
nn
.
Layer
=
Non
e
,
use_dynamic_left_chunk
:
bool
=
Fals
e
,
max_len
:
int
=
5000
):
"""
Args:
input_size (int): input dim, d_feature
...
...
@@ -127,7 +127,9 @@ class BaseEncoder(nn.Layer):
odim
=
output_size
,
dropout_rate
=
dropout_rate
,
pos_enc_class
=
pos_enc_class
(
d_model
=
output_size
,
dropout_rate
=
positional_dropout_rate
),
)
d_model
=
output_size
,
dropout_rate
=
positional_dropout_rate
,
max_len
=
max_len
),
)
self
.
normalize_before
=
normalize_before
self
.
after_norm
=
LayerNorm
(
output_size
,
epsilon
=
1e-12
)
...
...
@@ -415,32 +417,32 @@ class TransformerEncoder(BaseEncoder):
class
ConformerEncoder
(
BaseEncoder
):
"""Conformer encoder module."""
def
__init__
(
self
,
input_size
:
int
,
output_size
:
int
=
256
,
attention_heads
:
int
=
4
,
linear_units
:
int
=
2048
,
num_blocks
:
int
=
6
,
dropout_rate
:
float
=
0.1
,
positional_dropout_rate
:
float
=
0.1
,
attention_dropout_rate
:
float
=
0.0
,
input_layer
:
str
=
"conv2d
"
,
pos_enc_layer_type
:
str
=
"rel_pos"
,
normalize_before
:
bool
=
Tru
e
,
concat_after
:
bool
=
False
,
static_chunk_size
:
int
=
0
,
use_dynamic_chunk
:
bool
=
Fals
e
,
global_cmvn
:
nn
.
Layer
=
Non
e
,
use_dynamic_left_chunk
:
bool
=
False
,
positionwise_conv_kernel_size
:
int
=
1
,
macaron_style
:
bool
=
True
,
selfattention_layer_type
:
str
=
"rel_selfattn
"
,
activation_type
:
str
=
"swish"
,
use_cnn_module
:
bool
=
True
,
cnn_module_kernel
:
int
=
15
,
causal
:
bool
=
False
,
cnn_module_norm
:
str
=
"batch_norm"
,
):
def
__init__
(
self
,
input_size
:
int
,
output_size
:
int
=
256
,
attention_heads
:
int
=
4
,
linear_units
:
int
=
2048
,
num_blocks
:
int
=
6
,
dropout_rate
:
float
=
0.1
,
positional_
dropout_rate
:
float
=
0.1
,
attention_dropout_rate
:
float
=
0.0
,
input_layer
:
str
=
"conv2d"
,
pos_enc_layer_type
:
str
=
"rel_pos
"
,
normalize_before
:
bool
=
True
,
concat_after
:
bool
=
Fals
e
,
static_chunk_size
:
int
=
0
,
use_dynamic_chunk
:
bool
=
False
,
global_cmvn
:
nn
.
Layer
=
Non
e
,
use_dynamic_left_chunk
:
bool
=
Fals
e
,
positionwise_conv_kernel_size
:
int
=
1
,
macaron_style
:
bool
=
True
,
selfattention_layer_type
:
str
=
"rel_selfattn"
,
activation_type
:
str
=
"swish
"
,
use_cnn_module
:
bool
=
True
,
cnn_module_kernel
:
int
=
15
,
causal
:
bool
=
False
,
cnn_module_norm
:
str
=
"batch_norm"
,
max_len
:
int
=
5000
):
"""Construct ConformerEncoder
Args:
input_size to use_dynamic_chunk, see in BaseEncoder
...
...
@@ -464,7 +466,7 @@ class ConformerEncoder(BaseEncoder):
attention_dropout_rate
,
input_layer
,
pos_enc_layer_type
,
normalize_before
,
concat_after
,
static_chunk_size
,
use_dynamic_chunk
,
global_cmvn
,
use_dynamic_left_chunk
)
use_dynamic_left_chunk
,
max_len
)
activation
=
get_activation
(
activation_type
)
# self-attention module definition
...
...
paddlespeech/server/engine/asr/python/asr_engine.py
浏览文件 @
4b3f6c61
...
...
@@ -78,21 +78,26 @@ class ASREngine(BaseEngine):
Args:
audio_data (bytes): base64.b64decode
"""
if
self
.
executor
.
_check
(
io
.
BytesIO
(
audio_data
),
self
.
config
.
sample_rate
,
self
.
config
.
force_yes
):
logger
.
info
(
"start run asr engine"
)
self
.
executor
.
preprocess
(
self
.
config
.
model
,
io
.
BytesIO
(
audio_data
))
st
=
time
.
time
()
self
.
executor
.
infer
(
self
.
config
.
model
)
infer_time
=
time
.
time
()
-
st
self
.
output
=
self
.
executor
.
postprocess
()
# Retrieve result of asr.
else
:
logger
.
info
(
"file check failed!"
)
self
.
output
=
None
logger
.
info
(
"inference time: {}"
.
format
(
infer_time
))
logger
.
info
(
"asr engine type: python"
)
try
:
if
self
.
executor
.
_check
(
io
.
BytesIO
(
audio_data
),
self
.
config
.
sample_rate
,
self
.
config
.
force_yes
):
logger
.
info
(
"start run asr engine"
)
self
.
executor
.
preprocess
(
self
.
config
.
model
,
io
.
BytesIO
(
audio_data
))
st
=
time
.
time
()
self
.
executor
.
infer
(
self
.
config
.
model
)
infer_time
=
time
.
time
()
-
st
self
.
output
=
self
.
executor
.
postprocess
(
)
# Retrieve result of asr.
else
:
logger
.
info
(
"file check failed!"
)
self
.
output
=
None
logger
.
info
(
"inference time: {}"
.
format
(
infer_time
))
logger
.
info
(
"asr engine type: python"
)
except
Exception
as
e
:
logger
.
info
(
e
)
def
postprocess
(
self
):
"""postprocess
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录