Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2ed84a67
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
2ed84a67
编写于
10月 17, 2020
作者:
L
littletomatodonkey
提交者:
GitHub
10月 17, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add API for pad op. (#27943)
* add pad apis * rm pad2d test_layer * fix code example
上级
3718b2e7
变更
7
展开全部
隐藏空白更改
内联
并排
Showing
7 changed file
with
119 addition
and
776 deletion
+119
-776
python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py
...addle/fluid/tests/unittests/test_bilinear_interp_v2_op.py
+0
-14
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+0
-17
python/paddle/fluid/tests/unittests/test_nearest_interp_v2_op.py
...paddle/fluid/tests/unittests/test_nearest_interp_v2_op.py
+0
-14
python/paddle/fluid/tests/unittests/test_pad3d_op.py
python/paddle/fluid/tests/unittests/test_pad3d_op.py
+39
-49
python/paddle/nn/__init__.py
python/paddle/nn/__init__.py
+5
-11
python/paddle/nn/layer/__init__.py
python/paddle/nn/layer/__init__.py
+2
-11
python/paddle/nn/layer/common.py
python/paddle/nn/layer/common.py
+73
-660
未找到文件。
python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py
浏览文件 @
2ed84a67
...
...
@@ -606,20 +606,6 @@ class TestBilinearInterpOpAPI(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
res
,
expect_res
))
class
TestUpsampleBilinear2dInterpOpAPI2_0
(
unittest
.
TestCase
):
def
test_case
(
self
):
# dygraph
x_data
=
np
.
random
.
random
((
1
,
3
,
6
,
6
)).
astype
(
"float32"
)
upsample
=
paddle
.
nn
.
UpsamplingBilinear2d
(
scale_factor
=
[
2
,
2
])
with
fluid
.
dygraph
.
guard
():
x
=
fluid
.
dygraph
.
to_variable
(
x_data
)
interp
=
upsample
(
x
)
expect
=
bilinear_interp_np
(
x_data
,
out_h
=
12
,
out_w
=
12
,
align_corners
=
True
)
self
.
assertTrue
(
np
.
allclose
(
interp
.
numpy
(),
expect
))
class
TestBilinearInterpOpAPI_dy
(
unittest
.
TestCase
):
def
test_case
(
self
):
import
paddle
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
2ed84a67
...
...
@@ -316,23 +316,6 @@ class TestLayer(LayerTest):
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret_value
))
def
test_pad2d
(
self
):
with
self
.
static_graph
():
t
=
layers
.
data
(
name
=
't'
,
shape
=
[
-
1
,
3
,
5
,
5
],
dtype
=
'float32'
)
ret
=
layers
.
pad2d
(
t
,
paddings
=
[
1
,
1
,
1
,
1
])
static_ret
=
self
.
get_static_graph_result
(
feed
=
{
't'
:
np
.
ones
(
[
3
,
3
,
5
,
5
],
dtype
=
'float32'
)},
fetch_list
=
[
ret
])[
0
]
with
self
.
dynamic_graph
():
t
=
np
.
ones
([
3
,
3
,
5
,
5
],
dtype
=
'float32'
)
my_pad2d
=
paddle
.
nn
.
layer
.
Pad2D
(
paddings
=
1
)
dy_ret
=
my_pad2d
(
base
.
to_variable
(
t
))
dy_ret_value
=
dy_ret
.
numpy
()
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret_value
))
def
test_matmul
(
self
):
with
self
.
static_graph
():
t
=
layers
.
data
(
name
=
't'
,
shape
=
[
3
,
3
],
dtype
=
'float32'
)
...
...
python/paddle/fluid/tests/unittests/test_nearest_interp_v2_op.py
浏览文件 @
2ed84a67
...
...
@@ -526,20 +526,6 @@ class TestNearestAPI(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
results
[
i
+
1
],
expect_res
))
class
TestUpsampleNearest2dInterpOpAPI2_0
(
unittest
.
TestCase
):
def
test_case
(
self
):
# dygraph
x_data
=
np
.
random
.
random
((
1
,
3
,
6
,
6
)).
astype
(
"float32"
)
upsample
=
paddle
.
nn
.
UpsamplingNearest2d
(
scale_factor
=
[
2
,
2
])
with
fluid
.
dygraph
.
guard
():
x
=
fluid
.
dygraph
.
to_variable
(
x_data
)
interp
=
upsample
(
x
)
expect
=
nearest_neighbor_interp_np
(
x_data
,
out_h
=
12
,
out_w
=
12
,
align_corners
=
False
)
self
.
assertTrue
(
np
.
allclose
(
interp
.
numpy
(),
expect
))
class
TestNearestInterpException
(
unittest
.
TestCase
):
def
test_exception
(
self
):
input
=
fluid
.
data
(
name
=
"input"
,
shape
=
[
1
,
3
,
6
,
6
],
dtype
=
"float32"
)
...
...
python/paddle/fluid/tests/unittests/test_pad3d_op.py
浏览文件 @
2ed84a67
...
...
@@ -314,7 +314,6 @@ class TestPadAPI(unittest.TestCase):
def
test_dygraph_1
(
self
):
paddle
.
disable_static
()
input_shape
=
(
1
,
2
,
3
,
4
,
5
)
pad
=
[
1
,
2
,
1
,
1
,
3
,
4
]
mode
=
"constant"
...
...
@@ -342,7 +341,6 @@ class TestPadAPI(unittest.TestCase):
def
test_dygraph_2
(
self
):
paddle
.
disable_static
()
input_shape
=
(
2
,
3
,
4
,
5
)
pad
=
[
1
,
1
,
3
,
4
]
mode
=
"constant"
...
...
@@ -370,38 +368,8 @@ class TestPadAPI(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
def
test_dygraph_2
(
self
):
paddle
.
disable_static
()
input_shape
=
(
2
,
3
,
4
,
5
)
pad
=
[
1
,
1
,
3
,
4
]
mode
=
"constant"
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
np_out1
=
self
.
_get_numpy_out
(
input_data
,
pad
,
mode
,
value
,
data_format
=
"NCHW"
)
np_out2
=
self
.
_get_numpy_out
(
input_data
,
pad
,
mode
,
value
,
data_format
=
"NHWC"
)
tensor_data
=
paddle
.
to_tensor
(
input_data
)
tensor_pad
=
paddle
.
to_tensor
(
pad
,
dtype
=
"int32"
)
y1
=
F
.
pad
(
tensor_data
,
pad
=
tensor_pad
,
mode
=
mode
,
value
=
value
,
data_format
=
"NCHW"
)
y2
=
F
.
pad
(
tensor_data
,
pad
=
tensor_pad
,
mode
=
mode
,
value
=
value
,
data_format
=
"NHWC"
)
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
def
test_dygraph_3
(
self
):
paddle
.
disable_static
()
input_shape
=
(
3
,
4
,
5
)
pad
=
[
3
,
4
]
mode
=
"constant"
...
...
@@ -455,6 +423,8 @@ class TestPad1dAPI(unittest.TestCase):
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
mode
)
elif
mode
==
"replicate"
:
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
"edge"
)
elif
mode
==
"circular"
:
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
"wrap"
)
return
out
...
...
@@ -471,9 +441,10 @@ class TestPad1dAPI(unittest.TestCase):
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
pad_reflection
=
nn
.
ReflectionPad1d
(
padding
=
pad
)
pad_replication
=
nn
.
ReplicationPad1d
(
padding
=
pad
)
pad_constant
=
nn
.
ConstantPad1d
(
padding
=
pad
,
value
=
value
)
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_circular
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"circular"
)
data
=
paddle
.
to_tensor
(
input_data
)
...
...
@@ -492,6 +463,11 @@ class TestPad1dAPI(unittest.TestCase):
input_data
,
pad
,
"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"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
class
TestPad2dAPI
(
unittest
.
TestCase
):
def
_get_numpy_out
(
self
,
...
...
@@ -521,6 +497,8 @@ class TestPad2dAPI(unittest.TestCase):
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
mode
)
elif
mode
==
"replicate"
:
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
"edge"
)
elif
mode
==
"circular"
:
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
"wrap"
)
return
out
...
...
@@ -537,10 +515,10 @@ class TestPad2dAPI(unittest.TestCase):
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
pad_reflection
=
nn
.
ReflectionPad2d
(
padding
=
pad
)
pad_replication
=
nn
.
ReplicationPad2d
(
padding
=
pad
)
pad_constant
=
nn
.
ConstantPad2d
(
padding
=
pad
,
value
=
value
)
pad_
zero
=
nn
.
ZeroPad2d
(
padding
=
pad
)
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_
circular
=
nn
.
Pad2D
(
padding
=
pad
,
mode
=
"circular"
)
data
=
paddle
.
to_tensor
(
input_data
)
...
...
@@ -559,9 +537,9 @@ class TestPad2dAPI(unittest.TestCase):
input_data
,
pad
,
"constant"
,
value
=
value
,
data_format
=
"NCHW"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_
zero
(
data
)
output
=
pad_
circular
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
pad
,
"c
onstant"
,
value
=
0
,
data_format
=
"NCHW"
)
input_data
,
pad
,
"c
ircular"
,
data_format
=
"NCHW"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
...
...
@@ -595,6 +573,8 @@ class TestPad3dAPI(unittest.TestCase):
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
mode
)
elif
mode
==
"replicate"
:
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
"edge"
)
elif
mode
==
"circular"
:
out
=
np
.
pad
(
input_data
,
pad
,
mode
=
"wrap"
)
return
out
...
...
@@ -611,11 +591,18 @@ class TestPad3dAPI(unittest.TestCase):
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
pad_replication
=
nn
.
ReplicationPad3d
(
padding
=
pad
)
pad_constant
=
nn
.
ConstantPad3d
(
padding
=
pad
,
value
=
value
)
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_circular
=
nn
.
Pad3D
(
padding
=
pad
,
mode
=
"circular"
)
data
=
paddle
.
to_tensor
(
input_data
)
output
=
pad_reflection
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
pad
,
"reflect"
,
data_format
=
"NCDHW"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
output
=
pad_replication
(
data
)
np_out
=
self
.
_get_numpy_out
(
input_data
,
pad
,
"replicate"
,
data_format
=
"NCDHW"
)
...
...
@@ -626,6 +613,11 @@ class TestPad3dAPI(unittest.TestCase):
input_data
,
pad
,
"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"
)
self
.
assertTrue
(
np
.
allclose
(
output
.
numpy
(),
np_out
))
class
TestPad3dOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
...
...
@@ -673,32 +665,30 @@ class TestPad3dOpError(unittest.TestCase):
class
TestPadDataformatError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
def
test_ncl
():
paddle
.
disable_static
(
paddle
.
CPUPlace
())
input_shape
=
(
1
,
2
,
3
,
4
)
pad
=
paddle
.
to_tensor
(
np
.
array
([
2
,
1
,
2
,
1
]).
astype
(
'int32'
))
data
=
np
.
arange
(
np
.
prod
(
input_shape
),
dtype
=
np
.
float64
).
reshape
(
input_shape
)
+
1
my_pad
=
nn
.
ReplicationPad1d
(
padding
=
pad
,
data_format
=
"NCL"
)
my_pad
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"replicate"
,
data_format
=
"NCL"
)
data
=
paddle
.
to_tensor
(
data
)
result
=
my_pad
(
data
)
def
test_nchw
():
paddle
.
disable_static
(
paddle
.
CPUPlace
())
input_shape
=
(
1
,
2
,
4
)
pad
=
paddle
.
to_tensor
(
np
.
array
([
2
,
1
,
2
,
1
]).
astype
(
'int32'
))
data
=
np
.
arange
(
np
.
prod
(
input_shape
),
dtype
=
np
.
float64
).
reshape
(
input_shape
)
+
1
my_pad
=
nn
.
ReplicationPad1d
(
padding
=
pad
,
data_format
=
"NCHW"
)
my_pad
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"replicate"
,
data_format
=
"NCHW"
)
data
=
paddle
.
to_tensor
(
data
)
result
=
my_pad
(
data
)
def
test_ncdhw
():
paddle
.
disable_static
(
paddle
.
CPUPlace
())
input_shape
=
(
1
,
2
,
3
,
4
)
pad
=
paddle
.
to_tensor
(
np
.
array
([
2
,
1
,
2
,
1
]).
astype
(
'int32'
))
data
=
np
.
arange
(
np
.
prod
(
input_shape
),
dtype
=
np
.
float64
).
reshape
(
input_shape
)
+
1
my_pad
=
nn
.
ReplicationPad1d
(
padding
=
pad
,
data_format
=
"NCDHW"
)
my_pad
=
nn
.
Pad1D
(
padding
=
pad
,
mode
=
"replicate"
,
data_format
=
"NCDHW"
)
data
=
paddle
.
to_tensor
(
data
)
result
=
my_pad
(
data
)
...
...
python/paddle/nn/__init__.py
浏览文件 @
2ed84a67
...
...
@@ -71,22 +71,16 @@ from .layer.activation import Tanhshrink #DEFINE_ALIAS
from
.layer.activation
import
ThresholdedReLU
#DEFINE_ALIAS
from
.layer.activation
import
LogSoftmax
#DEFINE_ALIAS
from
.layer.activation
import
Maxout
#DEFINE_ALIAS
from
.layer.common
import
ReflectionPad1d
#DEFINE_ALIAS
from
.layer.common
import
ReplicationPad1d
#DEFINE_ALIAS
from
.layer.common
import
ConstantPad1d
#DEFINE_ALIAS
from
.layer.common
import
ReflectionPad2d
#DEFINE_ALIAS
from
.layer.common
import
ReplicationPad2d
#DEFINE_ALIAS
from
.layer.common
import
ConstantPad2d
#DEFINE_ALIAS
from
.layer.common
import
ZeroPad2d
#DEFINE_ALIAS
from
.layer.common
import
ReplicationPad3d
#DEFINE_ALIAS
from
.layer.common
import
ConstantPad3d
#DEFINE_ALIAS
from
.layer.common
import
BilinearTensorProduct
#DEFINE_ALIAS
from
.layer.common
import
Pool2D
#DEFINE_ALIAS
from
.layer.common
import
Pad1D
#DEFINE_ALIAS
from
.layer.common
import
Pad2D
#DEFINE_ALIAS
from
.layer.common
import
Pad3D
#DEFINE_ALIAS
from
.layer.common
import
CosineSimilarity
#DEFINE_ALIAS
from
.layer.common
import
Embedding
#DEFINE_ALIAS
from
.layer.common
import
Linear
#DEFINE_ALIAS
from
.layer.common
import
Flatten
#DEFINE_ALIAS
from
.layer.common
import
Upsample
#DEFINE_ALIAS
from
.layer.common
import
UpsamplingNearest2d
#DEFINE_ALIAS
from
.layer.common
import
UpsamplingBilinear2d
#DEFINE_ALIAS
from
.layer.common
import
Bilinear
#DEFINE_ALIAS
from
.layer.common
import
Dropout
#DEFINE_ALIAS
from
.layer.common
import
Dropout2d
#DEFINE_ALIAS
...
...
python/paddle/nn/layer/__init__.py
浏览文件 @
2ed84a67
...
...
@@ -44,23 +44,14 @@ from .activation import LogSoftmax #DEFINE_ALIAS
from
.common
import
BilinearTensorProduct
#DEFINE_ALIAS
from
.common
import
Bilinear
#DEFINE_ALIAS
from
.common
import
Pool2D
#DEFINE_ALIAS
from
.common
import
Pad1D
#DEFINE_ALIAS
from
.common
import
Pad2D
#DEFINE_ALIAS
from
.common
import
ReflectionPad1d
#DEFINE_ALIAS
from
.common
import
ReplicationPad1d
#DEFINE_ALIAS
from
.common
import
ConstantPad1d
#DEFINE_ALIAS
from
.common
import
ReflectionPad2d
#DEFINE_ALIAS
from
.common
import
ReplicationPad2d
#DEFINE_ALIAS
from
.common
import
ConstantPad2d
#DEFINE_ALIAS
from
.common
import
ZeroPad2d
#DEFINE_ALIAS
from
.common
import
ReplicationPad3d
#DEFINE_ALIAS
from
.common
import
ConstantPad3d
#DEFINE_ALIAS
from
.common
import
Pad3D
#DEFINE_ALIAS
from
.common
import
CosineSimilarity
#DEFINE_ALIAS
from
.common
import
Embedding
#DEFINE_ALIAS
from
.common
import
Linear
#DEFINE_ALIAS
from
.common
import
Flatten
#DEFINE_ALIAS
from
.common
import
Upsample
#DEFINE_ALIAS
from
.common
import
UpsamplingNearest2d
#DEFINE_ALIAS
from
.common
import
UpsamplingBilinear2d
#DEFINE_ALIAS
from
.common
import
Dropout
#DEFINE_ALIAS
from
.common
import
Dropout2d
#DEFINE_ALIAS
from
.common
import
Dropout3d
#DEFINE_ALIAS
...
...
python/paddle/nn/layer/common.py
浏览文件 @
2ed84a67
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录