Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ad50fa71
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ad50fa71
编写于
2月 25, 2021
作者:
L
littletomatodonkey
提交者:
GitHub
2月 25, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add int pad support for Pad1D/2D/3D (#31209)
* add int pad support for Pad1D/2D/3D * fix type * fix format
上级
d18c5e47
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
50 addition
and
7 deletion
+50
-7
python/paddle/fluid/tests/unittests/test_pad3d_op.py
python/paddle/fluid/tests/unittests/test_pad3d_op.py
+33
-0
python/paddle/nn/layer/common.py
python/paddle/nn/layer/common.py
+17
-7
未找到文件。
python/paddle/fluid/tests/unittests/test_pad3d_op.py
浏览文件 @
ad50fa71
...
...
@@ -467,12 +467,15 @@ class TestPad1dAPI(unittest.TestCase):
for
place
in
self
.
places
:
input_shape
=
(
3
,
4
,
5
)
pad
=
[
1
,
2
]
pad_int
=
1
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
pad_reflection
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"reflect"
)
pad_replication
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"replicate"
)
pad_constant
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"constant"
,
value
=
value
)
pad_constant_int
=
nn
.
Pad1D
(
padding
=
pad_int
,
mode
=
"constant"
,
value
=
value
)
pad_circular
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"circular"
)
data
=
paddle
.
to_tensor
(
input_data
)
...
...
@@ -492,6 +495,14 @@ class TestPad1dAPI(unittest.TestCase):
input_data
,
pad
,
"constant"
,
value
=
value
,
data_format
=
"NCL"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_constant_int
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
[
pad_int
]
*
2
,
"constant"
,
value
=
value
,
data_format
=
"NCL"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_circular
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
pad
,
"circular"
,
value
=
value
,
data_format
=
"NCL"
)
...
...
@@ -541,12 +552,15 @@ class TestPad2dAPI(unittest.TestCase):
for
place
in
self
.
places
:
input_shape
=
(
3
,
4
,
5
,
6
)
pad
=
[
1
,
2
,
2
,
1
]
pad_int
=
1
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
pad_reflection
=
nn
.
Pad2D
(
padding
=
pad
,
mode
=
"reflect"
)
pad_replication
=
nn
.
Pad2D
(
padding
=
pad
,
mode
=
"replicate"
)
pad_constant
=
nn
.
Pad2D
(
padding
=
pad
,
mode
=
"constant"
,
value
=
value
)
pad_constant_int
=
nn
.
Pad2D
(
padding
=
pad_int
,
mode
=
"constant"
,
value
=
value
)
pad_circular
=
nn
.
Pad2D
(
padding
=
pad
,
mode
=
"circular"
)
data
=
paddle
.
to_tensor
(
input_data
)
...
...
@@ -566,6 +580,14 @@ class TestPad2dAPI(unittest.TestCase):
input_data
,
pad
,
"constant"
,
value
=
value
,
data_format
=
"NCHW"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_constant_int
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
[
pad_int
]
*
4
,
"constant"
,
value
=
value
,
data_format
=
"NCHW"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_circular
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
pad
,
"circular"
,
data_format
=
"NCHW"
)
...
...
@@ -617,12 +639,15 @@ class TestPad3dAPI(unittest.TestCase):
for
place
in
self
.
places
:
input_shape
=
(
3
,
4
,
5
,
6
,
7
)
pad
=
[
1
,
2
,
2
,
1
,
1
,
0
]
pad_int
=
1
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
pad_reflection
=
nn
.
Pad3D
(
padding
=
pad
,
mode
=
"reflect"
)
pad_replication
=
nn
.
Pad3D
(
padding
=
pad
,
mode
=
"replicate"
)
pad_constant
=
nn
.
Pad3D
(
padding
=
pad
,
mode
=
"constant"
,
value
=
value
)
pad_constant_int
=
nn
.
Pad3D
(
padding
=
pad_int
,
mode
=
"constant"
,
value
=
value
)
pad_circular
=
nn
.
Pad3D
(
padding
=
pad
,
mode
=
"circular"
)
data
=
paddle
.
to_tensor
(
input_data
)
...
...
@@ -642,6 +667,14 @@ class TestPad3dAPI(unittest.TestCase):
input_data
,
pad
,
"constant"
,
value
=
value
,
data_format
=
"NCDHW"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_constant_int
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
[
pad_int
]
*
6
,
"constant"
,
value
=
value
,
data_format
=
"NCDHW"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_circular
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
pad
,
"circular"
,
data_format
=
"NCDHW"
)
...
...
python/paddle/nn/layer/common.py
浏览文件 @
ad50fa71
...
...
@@ -38,6 +38,13 @@ __all__ = [
]
def
_npairs
(
x
,
n
):
if
isinstance
(
x
,
(
paddle
.
Tensor
,
list
)):
return
x
x
=
[
x
]
*
(
n
*
2
)
return
x
class
Linear
(
layers
.
Layer
):
r
"""
...
...
@@ -915,7 +922,8 @@ class Pad1D(layers.Layer):
If mode is 'reflect', pad[0] and pad[1] must be no greater than width-1.
Parameters:
padding (Tensor | List[int32]): The padding size with data type int32. [len(padding)/2] dimensions
padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the
same padding in both dimensions. Else [len(padding)/2] dimensions
of input will be padded. The pad has the form (pad_left, pad_right).
mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'.
When in 'constant' mode, this op uses a constant value to pad the input tensor.
...
...
@@ -968,7 +976,7 @@ class Pad1D(layers.Layer):
data_format
=
"NCL"
,
name
=
None
):
super
(
Pad1D
,
self
).
__init__
()
self
.
_pad
=
padding
self
.
_pad
=
_npairs
(
padding
,
1
)
self
.
_mode
=
mode
self
.
_value
=
value
self
.
_data_format
=
data_format
...
...
@@ -996,8 +1004,9 @@ class Pad2D(layers.Layer):
than width-1. The height dimension has the same condition.
Parameters:
padding (Tensor | List[int32]): The padding size with data type int32. [len(padding)/2] dimensions
of input will be padded. The pad has the form (pad_left, pad_right, pad_top, pad_bottom).
padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the
same padding in all dimensions. Else [len(padding)/2] dimensions of input will be padded.
The pad has the form (pad_left, pad_right, pad_top, pad_bottom).
mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'.
When in 'constant' mode, this op uses a constant value to pad the input tensor.
When in 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
...
...
@@ -1051,7 +1060,7 @@ class Pad2D(layers.Layer):
data_format
=
"NCHW"
,
name
=
None
):
super
(
Pad2D
,
self
).
__init__
()
self
.
_pad
=
padding
self
.
_pad
=
_npairs
(
padding
,
2
)
self
.
_mode
=
mode
self
.
_value
=
value
self
.
_data_format
=
data_format
...
...
@@ -1079,7 +1088,8 @@ class Pad3D(layers.Layer):
than width-1. The height and depth dimension has the same condition.
Parameters:
padding (Tensor | List[int32]): The padding size with data type int32. [len(padding)/2] dimensions
padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the
same padding in all dimensions. Else [len(padding)/2] dimensions
of input will be padded. The pad has the form (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back).
mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'.
When in 'constant' mode, this op uses a constant value to pad the input tensor.
...
...
@@ -1134,7 +1144,7 @@ class Pad3D(layers.Layer):
data_format
=
"NCDHW"
,
name
=
None
):
super
(
Pad3D
,
self
).
__init__
()
self
.
_pad
=
padding
self
.
_pad
=
_npairs
(
padding
,
3
)
self
.
_mode
=
mode
self
.
_value
=
value
self
.
_data_format
=
data_format
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录