Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
186682fe
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
186682fe
编写于
4月 20, 2021
作者:
F
FNRE
提交者:
GitHub
4月 20, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add paddle.nn.unfold #32297 (#32298)
* add paddle.nn.unfold * update Parameters of Unfold
上级
e3489013
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
100 addition
and
0 deletion
+100
-0
python/paddle/fluid/tests/unittests/test_unfold_op.py
python/paddle/fluid/tests/unittests/test_unfold_op.py
+28
-0
python/paddle/nn/__init__.py
python/paddle/nn/__init__.py
+1
-0
python/paddle/nn/layer/common.py
python/paddle/nn/layer/common.py
+71
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_unfold_op.py
浏览文件 @
186682fe
...
...
@@ -18,6 +18,9 @@ import math
import
numpy
as
np
import
unittest
from
op_test
import
OpTest
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid
import
core
class
TestUnfoldOp
(
OpTest
):
...
...
@@ -98,5 +101,30 @@ class TestUnfoldOp(OpTest):
self
.
check_grad
([
'X'
],
'Y'
)
class
TestUnfoldAPI
(
TestUnfoldOp
):
"""
This is for test on paddle.nn.Unfold
"""
def
setUp
(
self
):
self
.
op_type
=
'unfold'
self
.
set_data
()
self
.
places
=
[
fluid
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
self
.
places
.
append
(
fluid
.
CUDAPlace
(
0
))
def
test_dygraph
(
self
):
for
place
in
self
.
places
:
with
fluid
.
dygraph
.
guard
(
place
):
input
=
fluid
.
dygraph
.
to_variable
(
self
.
inputs
[
'X'
])
m
=
paddle
.
nn
.
Unfold
(
**
self
.
attrs
)
m
.
eval
()
result
=
m
(
input
)
self
.
assertTrue
(
np
.
allclose
(
result
.
numpy
(),
self
.
outputs
[
'Y'
]))
def
test_info
(
self
):
str
(
paddle
.
nn
.
Unfold
(
**
self
.
attrs
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/nn/__init__.py
浏览文件 @
186682fe
...
...
@@ -83,6 +83,7 @@ from .layer.common import Dropout #DEFINE_ALIAS
from
.layer.common
import
Dropout2D
#DEFINE_ALIAS
from
.layer.common
import
Dropout3D
#DEFINE_ALIAS
from
.layer.common
import
AlphaDropout
#DEFINE_ALIAS
from
.layer.common
import
Unfold
#DEFINE_ALIAS
from
.layer.pooling
import
AvgPool1D
#DEFINE_ALIAS
from
.layer.pooling
import
AvgPool2D
#DEFINE_ALIAS
...
...
python/paddle/nn/layer/common.py
浏览文件 @
186682fe
...
...
@@ -35,6 +35,7 @@ __all__ = [
'Dropout3D'
,
'Bilinear'
,
'AlphaDropout'
,
'Unfold'
,
]
...
...
@@ -1380,3 +1381,73 @@ class Embedding(layers.Layer):
if
self
.
_name
is
not
None
:
main_str
+=
', name={_name}'
return
main_str
.
format
(
**
self
.
__dict__
)
class
Unfold
(
layers
.
Layer
):
"""
This op returns a col buffer of sliding local blocks of input x, also known
as im2col for batched 2D image tensors. For each block under the convolution filter,
all element will be rearranged as a column. While the convolution filter sliding over
the input feature map, a series of such columns will be formed.
For each input :math:`x` with shape [N, C, H, W], the output shape [N, Cout, Lout]
can be calculated as following.
See ``paddle.nn.functional.unfold`` for more details.
Parameters:
kernel_sizes(int|list): The size of convolution kernel, should be [k_h, k_w]
or an integer k treated as [k, k].
strides(int|list): The strides, should be [stride_h, stride_w]
or an integer stride treated as [sride, stride].
For default, strides will be [1, 1].
paddings(int|list): The paddings of each dimension, should be
[padding_top, padding_left, padding_bottom, padding_right]
or [padding_h, padding_w] or an integer padding.
If [padding_h, padding_w] was given, it will expanded to
[padding_h, padding_w, padding_h, padding_w]. If an integer
padding was given, [padding, padding, padding, padding] will
be used. For default, paddings will be [0, 0, 0, 0]
dilations(int|list): the dilations of convolution kernel, should be
[dilation_h, dilation_w], or an integer dilation treated as
[dilation, dilation]. For default, it will be [1, 1].
name(str, optional): The default value is None.
Normally there is no need for user to set this property.
For more information, please refer to :ref:`api_guide_Name`
Examples:
.. code-block:: python
import paddle
import paddle.nn as nn
x = paddle.randn((100,3,224,224))
unfold = nn.Unfold(kernel_sizes=[3, 3])
result = unfold(x)
print(result)
"""
def
__init__
(
self
,
kernel_sizes
,
dilations
=
1
,
paddings
=
0
,
strides
=
1
,
name
=
None
):
super
(
Unfold
,
self
).
__init__
()
self
.
kernel_sizes
=
kernel_sizes
self
.
dilations
=
dilations
self
.
paddings
=
paddings
self
.
strides
=
strides
self
.
name
=
name
def
forward
(
self
,
input
):
return
F
.
unfold
(
input
,
self
.
kernel_sizes
,
self
.
dilations
,
self
.
paddings
,
self
.
strides
,
self
.
name
)
def
extra_repr
(
self
):
name_str
=
', name={}'
.
format
(
self
.
name
)
if
self
.
name
else
''
return
'kernel_size={}, dilation={}, padding={}, stride={}{}'
.
\
format
(
self
.
kernel_sizes
,
self
.
dilations
,
self
.
paddings
,
self
.
strides
,
name_str
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录