Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
f95edc38
D
DeepSpeech
项目概览
PaddlePaddle
/
DeepSpeech
大约 2 年 前同步成功
通知
210
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看板
提交
f95edc38
编写于
9月 20, 2022
作者:
H
Hui Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
format
上级
322301a6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
102 addition
and
91 deletion
+102
-91
tests/unit/asr/reverse_pad_list.py
tests/unit/asr/reverse_pad_list.py
+102
-91
未找到文件。
tests/unit/asr/reverse_pad_list.py
浏览文件 @
f95edc38
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
paddle
import
paddle
import
numpy
as
np
import
unittest
# from paddlespeech.audio.utils.tensor_utils import reverse_pad_list
import
paddlespeech.s2t
# noqa: F401
import
paddlespeech.s2t
from
paddlespeech.audio.utils.tensor_utils
import
add_sos_eos
from
paddlespeech.audio.utils.tensor_utils
import
add_sos_eos
from
paddlespeech.audio.utils.tensor_utils
import
pad_sequence
from
paddlespeech.audio.utils.tensor_utils
import
pad_sequence
# from paddlespeech.audio.utils.tensor_utils import reverse_pad_list
def
reverse_pad_list
(
ys_pad
:
paddle
.
Tensor
,
def
reverse_pad_list
(
ys_pad
:
paddle
.
Tensor
,
ys_lens
:
paddle
.
Tensor
,
ys_lens
:
paddle
.
Tensor
,
pad_value
:
float
=-
1.0
)
->
paddle
.
Tensor
:
pad_value
:
float
=-
1.0
)
->
paddle
.
Tensor
:
...
@@ -33,95 +44,94 @@ def reverse_pad_list(ys_pad: paddle.Tensor,
...
@@ -33,95 +44,94 @@ def reverse_pad_list(ys_pad: paddle.Tensor,
for
y
,
i
in
zip
(
ys_pad
,
ys_lens
)],
True
,
pad_value
)
for
y
,
i
in
zip
(
ys_pad
,
ys_lens
)],
True
,
pad_value
)
return
r_ys_pad
return
r_ys_pad
def
naive_reverse_pad_list_with_sos_eos
(
r_hyps
,
r_hyps_lens
,
sos
=
5000
,
eos
=
5000
,
ignore_id
=-
1
):
def
naive_reverse_pad_list_with_sos_eos
(
r_hyps
,
r_hyps_lens
,
sos
=
5000
,
eos
=
5000
,
ignore_id
=-
1
):
r_hyps
=
reverse_pad_list
(
r_hyps
,
r_hyps_lens
,
float
(
ignore_id
))
r_hyps
=
reverse_pad_list
(
r_hyps
,
r_hyps_lens
,
float
(
ignore_id
))
r_hyps
,
_
=
add_sos_eos
(
r_hyps
,
sos
,
eos
,
ignore_id
)
r_hyps
,
_
=
add_sos_eos
(
r_hyps
,
sos
,
eos
,
ignore_id
)
return
r_hyps
return
r_hyps
def
reverse_pad_list_with_sos_eos
(
r_hyps
,
r_hyps_lens
,
sos
=
5000
,
eos
=
5000
,
ignore_id
=-
1
):
# >>> r_hyps = reverse_pad_list(r_hyps, r_hyps_lens, float(self.ignore_id))
def
reverse_pad_list_with_sos_eos
(
r_hyps
,
# >>> r_hyps, _ = add_sos_eos(r_hyps, self.sos, self.eos, self.ignore_id)
r_hyps_lens
,
max_len
=
paddle
.
max
(
r_hyps_lens
)
sos
=
5000
,
index_range
=
paddle
.
arange
(
0
,
max_len
,
1
)
eos
=
5000
,
seq_len_expand
=
r_hyps_lens
.
unsqueeze
(
1
)
ignore_id
=-
1
):
seq_mask
=
seq_len_expand
>
index_range
# (beam, max_len)
# >>> r_hyps = reverse_pad_list(r_hyps, r_hyps_lens, float(self.ignore_id))
# >>> r_hyps, _ = add_sos_eos(r_hyps, self.sos, self.eos, self.ignore_id)
index
=
(
seq_len_expand
-
1
)
-
index_range
# (beam, max_len)
max_len
=
paddle
.
max
(
r_hyps_lens
)
# >>> index
index_range
=
paddle
.
arange
(
0
,
max_len
,
1
)
# >>> tensor([[ 2, 1, 0],
seq_len_expand
=
r_hyps_lens
.
unsqueeze
(
1
)
# >>> [ 2, 1, 0],
seq_mask
=
seq_len_expand
>
index_range
# (beam, max_len)
# >>> [ 0, -1, -2]])
index
=
index
*
seq_mask
index
=
(
seq_len_expand
-
1
)
-
index_range
# (beam, max_len)
# >>> index
# >>> index
# >>> tensor([[ 2, 1, 0],
# >>> tensor([[2, 1, 0],
# >>> [ 2, 1, 0],
# >>> [2, 1, 0],
# >>> [ 0, -1, -2]])
# >>> [0, 0, 0]])
index
=
index
*
seq_mask
def
paddle_gather
(
x
,
dim
,
index
):
index_shape
=
index
.
shape
# >>> index
index_flatten
=
index
.
flatten
()
# >>> tensor([[2, 1, 0],
if
dim
<
0
:
# >>> [2, 1, 0],
dim
=
len
(
x
.
shape
)
+
dim
# >>> [0, 0, 0]])
nd_index
=
[]
def
paddle_gather
(
x
,
dim
,
index
):
for
k
in
range
(
len
(
x
.
shape
)):
index_shape
=
index
.
shape
if
k
==
dim
:
index_flatten
=
index
.
flatten
()
nd_index
.
append
(
index_flatten
)
if
dim
<
0
:
else
:
dim
=
len
(
x
.
shape
)
+
dim
reshape_shape
=
[
1
]
*
len
(
x
.
shape
)
nd_index
=
[]
reshape_shape
[
k
]
=
x
.
shape
[
k
]
for
k
in
range
(
len
(
x
.
shape
)):
x_arange
=
paddle
.
arange
(
x
.
shape
[
k
],
dtype
=
index
.
dtype
)
if
k
==
dim
:
x_arange
=
x_arange
.
reshape
(
reshape_shape
)
nd_index
.
append
(
index_flatten
)
dim_index
=
paddle
.
expand
(
x_arange
,
index_shape
).
flatten
()
else
:
nd_index
.
append
(
dim_index
)
reshape_shape
=
[
1
]
*
len
(
x
.
shape
)
ind2
=
paddle
.
transpose
(
paddle
.
stack
(
nd_index
),
reshape_shape
[
k
]
=
x
.
shape
[
k
]
[
1
,
0
]).
astype
(
"int64"
)
x_arange
=
paddle
.
arange
(
x
.
shape
[
k
],
dtype
=
index
.
dtype
)
paddle_out
=
paddle
.
gather_nd
(
x
,
ind2
).
reshape
(
index_shape
)
x_arange
=
x_arange
.
reshape
(
reshape_shape
)
return
paddle_out
dim_index
=
paddle
.
expand
(
x_arange
,
index_shape
).
flatten
()
nd_index
.
append
(
dim_index
)
r_hyps
=
paddle_gather
(
r_hyps
,
1
,
index
)
ind2
=
paddle
.
transpose
(
paddle
.
stack
(
nd_index
),
[
1
,
0
]).
astype
(
"int64"
)
# >>> r_hyps
paddle_out
=
paddle
.
gather_nd
(
x
,
ind2
).
reshape
(
index_shape
)
# >>> tensor([[3, 2, 1],
return
paddle_out
# >>> [4, 8, 9],
# >>> [2, 2, 2]])
r_hyps
=
paddle_gather
(
r_hyps
,
1
,
index
)
r_hyps
=
paddle
.
where
(
seq_mask
,
r_hyps
,
eos
)
# >>> r_hyps
# >>> r_hyps
# >>> tensor([[3, 2, 1],
# >>> tensor([[3, 2, 1],
# >>> [4, 8, 9],
# >>> [4, 8, 9],
# >>> [2, 2, 2]])
# >>> [2, eos, eos]])
r_hyps
=
paddle
.
where
(
seq_mask
,
r_hyps
,
eos
)
B
=
r_hyps
.
shape
[
0
]
# >>> r_hyps
_sos
=
paddle
.
ones
([
B
,
1
],
dtype
=
r_hyps
.
dtype
)
*
sos
# >>> tensor([[3, 2, 1],
# r_hyps = paddle.concat([hyps[:, 0:1], r_hyps], axis=1)
# >>> [4, 8, 9],
r_hyps
=
paddle
.
concat
([
_sos
,
r_hyps
],
axis
=
1
)
# >>> [2, eos, eos]])
# >>> r_hyps
B
=
r_hyps
.
shape
[
0
]
# >>> tensor([[sos, 3, 2, 1],
_sos
=
paddle
.
ones
([
B
,
1
],
dtype
=
r_hyps
.
dtype
)
*
sos
# >>> [sos, 4, 8, 9],
# r_hyps = paddle.concat([hyps[:, 0:1], r_hyps], axis=1)
# >>> [sos, 2, eos, eos]])
r_hyps
=
paddle
.
concat
([
_sos
,
r_hyps
],
axis
=
1
)
return
r_hyps
# >>> r_hyps
# >>> tensor([[sos, 3, 2, 1],
# >>> [sos, 4, 8, 9],
# >>> [sos, 2, eos, eos]])
return
r_hyps
class
TestU2Model
(
unittest
.
TestCase
):
class
TestU2Model
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
paddle
.
set_device
(
'cpu'
)
paddle
.
set_device
(
'cpu'
)
self
.
sos
=
5000
self
.
sos
=
5000
self
.
eos
=
5000
self
.
eos
=
5000
self
.
ignore_id
=-
1
self
.
ignore_id
=
-
1
self
.
reverse_hyps
=
paddle
.
to_tensor
(
self
.
reverse_hyps
=
paddle
.
to_tensor
([[
4
,
3
,
2
,
1
,
-
1
],
[[
4
,
3
,
2
,
1
,
-
1
],
[
5
,
4
,
3
,
2
,
1
]])
[
5
,
4
,
3
,
2
,
1
]]
)
self
.
reverse_hyps_sos_eos
=
paddle
.
to_tensor
(
self
.
reverse_hyps_sos_eos
=
paddle
.
to_tensor
(
[[
self
.
sos
,
4
,
3
,
2
,
1
,
self
.
eos
],
[[
self
.
sos
,
4
,
3
,
2
,
1
,
self
.
eos
],
[
self
.
sos
,
5
,
4
,
3
,
2
,
1
]])
[
self
.
sos
,
5
,
4
,
3
,
2
,
1
]]
)
self
.
hyps
=
paddle
.
to_tensor
(
[
[
1
,
2
,
3
,
4
,
-
1
],
[
1
,
2
,
3
,
4
,
5
]
]
)
self
.
hyps
=
paddle
.
to_tensor
([[
1
,
2
,
3
,
4
,
-
1
],
[
1
,
2
,
3
,
4
,
5
]])
self
.
hyps_lens
=
paddle
.
to_tensor
([
4
,
5
],
paddle
.
int32
)
self
.
hyps_lens
=
paddle
.
to_tensor
([
4
,
5
],
paddle
.
int32
)
...
@@ -130,16 +140,17 @@ class TestU2Model(unittest.TestCase):
...
@@ -130,16 +140,17 @@ class TestU2Model(unittest.TestCase):
self
.
assertSequenceEqual
(
r_hyps
.
tolist
(),
self
.
reverse_hyps
.
tolist
())
self
.
assertSequenceEqual
(
r_hyps
.
tolist
(),
self
.
reverse_hyps
.
tolist
())
def
test_naive_reverse_pad_list_with_sos_eos
(
self
):
def
test_naive_reverse_pad_list_with_sos_eos
(
self
):
r_hyps_sos_eos
=
naive_reverse_pad_list_with_sos_eos
(
self
.
hyps
,
self
.
hyps_lens
)
r_hyps_sos_eos
=
naive_reverse_pad_list_with_sos_eos
(
self
.
hyps
,
self
.
assertSequenceEqual
(
r_hyps_sos_eos
.
tolist
(),
self
.
reverse_hyps_sos_eos
.
tolist
())
self
.
hyps_lens
)
self
.
assertSequenceEqual
(
r_hyps_sos_eos
.
tolist
(),
self
.
reverse_hyps_sos_eos
.
tolist
())
def
test_static_reverse_pad_list_with_sos_eos
(
self
):
def
test_static_reverse_pad_list_with_sos_eos
(
self
):
r_hyps_sos_eos_static
=
reverse_pad_list_with_sos_eos
(
self
.
hyps
,
self
.
hyps_lens
)
r_hyps_sos_eos_static
=
reverse_pad_list_with_sos_eos
(
self
.
hyps
,
self
.
assertSequenceEqual
(
r_hyps_sos_eos_static
.
tolist
(),
self
.
reverse_hyps_sos_eos
.
tolist
())
self
.
hyps_lens
)
self
.
assertSequenceEqual
(
r_hyps_sos_eos_static
.
tolist
(),
self
.
reverse_hyps_sos_eos
.
tolist
())
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录