Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
兽拳
Paddle
提交
9a74c448
P
Paddle
项目概览
兽拳
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9a74c448
编写于
10月 29, 2018
作者:
J
JiabinYang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test=develop
上级
6e361542
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
55 addition
and
55 deletion
+55
-55
paddle/fluid/operators/space_to_depth_op.cc
paddle/fluid/operators/space_to_depth_op.cc
+17
-17
paddle/fluid/operators/space_to_depth_op.h
paddle/fluid/operators/space_to_depth_op.h
+13
-13
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+11
-11
python/paddle/fluid/tests/unittests/test_space_to_depth_op.py
...on/paddle/fluid/tests/unittests/test_space_to_depth_op.py
+14
-14
未找到文件。
paddle/fluid/operators/space_to_depth_op.cc
浏览文件 @
9a74c448
...
...
@@ -31,31 +31,31 @@ class SpaceToDepthOp : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
4
,
"input should be a 4D tensor"
);
auto
stride
=
ctx
->
Attrs
().
Get
<
int64_t
>
(
"strid
e"
);
auto
blocksize
=
ctx
->
Attrs
().
Get
<
int64_t
>
(
"blocksiz
e"
);
PADDLE_ENFORCE_GT
(
stride
,
1
,
"The strid
e should be Greater than 1"
);
PADDLE_ENFORCE_GT
(
blocksize
,
1
,
"The blocksiz
e should be Greater than 1"
);
PADDLE_ENFORCE_GT
(
x_dims
[
1
],
0
,
"input channel should be Greater than 0"
);
PADDLE_ENFORCE_GT
(
x_dims
[
2
],
0
,
"input Height should be Greater than 0"
);
PADDLE_ENFORCE_GT
(
x_dims
[
3
],
0
,
"input Width should be Greater than 0"
);
PADDLE_ENFORCE_EQ
(
x_dims
[
1
]
%
(
stride
*
strid
e
),
0
,
PADDLE_ENFORCE_EQ
(
x_dims
[
1
]
%
(
blocksize
*
blocksiz
e
),
0
,
"input channel should be divisible of the square of "
"SpaceToDepthOp
strid
e"
);
PADDLE_ENFORCE_EQ
(
x_dims
[
2
]
%
(
strid
e
),
0
,
"SpaceToDepthOp
blocksiz
e"
);
PADDLE_ENFORCE_EQ
(
x_dims
[
2
]
%
(
blocksiz
e
),
0
,
"input Height should be divisible of the square of "
"SpaceToDepthOp
strid
e"
);
PADDLE_ENFORCE_EQ
(
x_dims
[
3
]
%
(
strid
e
),
0
,
"SpaceToDepthOp
blocksiz
e"
);
PADDLE_ENFORCE_EQ
(
x_dims
[
3
]
%
(
blocksiz
e
),
0
,
"input Width should be divisible of the square of "
"SpaceToDepthOp
strid
e"
);
"SpaceToDepthOp
blocksiz
e"
);
VLOG
(
3
)
<<
"SpaceToDepthOp operator x.shape="
<<
x_dims
<<
"Attribute
stride"
<<
strid
e
<<
std
::
endl
;
<<
"Attribute
blocksize"
<<
blocksiz
e
<<
std
::
endl
;
std
::
vector
<
int64_t
>
output_shape
(
4
,
0
);
// [B,C,H,W]
output_shape
[
0
]
=
x_dims
[
0
];
output_shape
[
1
]
=
x_dims
[
1
]
*
stride
*
strid
e
;
output_shape
[
2
]
=
x_dims
[
2
]
/
strid
e
;
output_shape
[
3
]
=
x_dims
[
3
]
/
strid
e
;
output_shape
[
1
]
=
x_dims
[
1
]
*
blocksize
*
blocksiz
e
;
output_shape
[
2
]
=
x_dims
[
2
]
/
blocksiz
e
;
output_shape
[
3
]
=
x_dims
[
3
]
/
blocksiz
e
;
auto
out_dims
=
framework
::
make_ddim
(
output_shape
);
...
...
@@ -80,20 +80,20 @@ class SpaceToDepthOpMaker : public framework::OpProtoAndCheckerMaker {
"(Tensor), The output should be a 4D tensor B * C2 * W2 * H2 of "
"SpaceToDepthOp operator."
);
AddAttr
<
int64_t
>
(
"
strid
e"
,
"(int64_t, default 2)
strid
e used to do change Space To Depth."
)
"
blocksiz
e"
,
"(int64_t, default 2)
blocksiz
e used to do change Space To Depth."
)
.
SetDefault
(
2
)
.
GreaterThan
(
1
);
AddComment
(
R"DOC(
reorg operator used in Yolo v2.
The equation is: C2 = C1/
stride * stride, W2 = W1 ∗ stride + offset % stride, H2 = H1 ∗ stride + offset / strid
e,
The equation is: C2 = C1/
blocksize * blocksize, W2 = W1 ∗ blocksize + offset % blocksize, H2 = H1 ∗ blocksize + offset / blocksiz
e,
Reshape Input(X) into the shape according to Attr(
strid
e). The
Reshape Input(X) into the shape according to Attr(
blocksiz
e). The
data in Input(X) are unchanged.
Examples:
1. Given a 4-D tensor Input(X) with a shape [128, 2048, 26, 26], and the
strid
e is 2, the reorg operator will transform Input(X)
1. Given a 4-D tensor Input(X) with a shape [128, 2048, 26, 26], and the
blocksiz
e is 2, the reorg operator will transform Input(X)
into a 4-D tensor with shape [128, 2048, 13, 13] and leaving Input(X)'s data unchanged.
)DOC"
);
...
...
paddle/fluid/operators/space_to_depth_op.h
浏览文件 @
9a74c448
...
...
@@ -25,19 +25,19 @@ template <typename T>
class
space_to_depth_compute
{
public:
HOSTDEVICE
space_to_depth_compute
(
const
T
*
x
,
int64_t
w
,
int64_t
h
,
int64_t
c
,
int64_t
batch
,
int64_t
strid
e
,
int64_t
batch
,
int64_t
blocksiz
e
,
int64_t
forward
,
T
*
out
)
:
x_
(
x
),
w_
(
w
),
h_
(
h
),
c_
(
c
),
batch_
(
batch
),
stride_
(
strid
e
),
blocksize_
(
blocksiz
e
),
forward_
(
forward
),
out_
(
out
)
{}
HOSTDEVICE
void
operator
()(
int64_t
in_index
)
{
int64_t
out_c
=
c_
/
(
stride_
*
strid
e_
);
int64_t
out_c
=
c_
/
(
blocksize_
*
blocksiz
e_
);
// calculate each dim position with index of tensor
int64_t
b
=
in_index
/
(
c_
*
h_
*
w_
);
int64_t
k
=
(
in_index
%
(
c_
*
h_
*
w_
))
/
(
h_
*
w_
);
...
...
@@ -46,10 +46,10 @@ class space_to_depth_compute {
int64_t
c2
=
k
%
out_c
;
int64_t
offset
=
k
/
out_c
;
int64_t
w2
=
i
*
stride_
+
offset
%
strid
e_
;
int64_t
h2
=
j
*
stride_
+
offset
/
strid
e_
;
int64_t
w2
=
i
*
blocksize_
+
offset
%
blocksiz
e_
;
int64_t
h2
=
j
*
blocksize_
+
offset
/
blocksiz
e_
;
int64_t
out_index
=
w2
+
w_
*
stride_
*
(
h2
+
h_
*
strid
e_
*
(
c2
+
out_c
*
b
));
w2
+
w_
*
blocksize_
*
(
h2
+
h_
*
blocksiz
e_
*
(
c2
+
out_c
*
b
));
if
(
forward_
)
out_
[
out_index
]
=
x_
[
in_index
];
else
...
...
@@ -58,7 +58,7 @@ class space_to_depth_compute {
private:
const
T
*
x_
;
int64_t
w_
,
h_
,
c_
,
batch_
,
strid
e_
,
forward_
;
int64_t
w_
,
h_
,
c_
,
batch_
,
blocksiz
e_
,
forward_
;
T
*
out_
;
};
...
...
@@ -68,7 +68,7 @@ class SpaceToDepthKernel : public framework::OpKernel<T> {
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
out
=
context
.
Output
<
framework
::
LoDTensor
>
(
"Out"
);
auto
*
x
=
context
.
Input
<
framework
::
LoDTensor
>
(
"X"
);
auto
stride
=
context
.
Attr
<
int64_t
>
(
"strid
e"
);
auto
blocksize
=
context
.
Attr
<
int64_t
>
(
"blocksiz
e"
);
auto
in_dims
=
x
->
dims
();
out
->
mutable_data
(
context
.
GetPlace
(),
x
->
type
());
...
...
@@ -83,8 +83,8 @@ class SpaceToDepthKernel : public framework::OpKernel<T> {
auto
*
x_data
=
x
->
data
<
T
>
();
auto
*
out_data
=
out
->
data
<
T
>
();
paddle
::
operators
::
space_to_depth_compute
<
T
>
computer
(
x_data
,
W
,
H
,
C
,
B
,
strid
e
,
1
,
out_data
);
paddle
::
operators
::
space_to_depth_compute
<
T
>
computer
(
x_data
,
W
,
H
,
C
,
B
,
blocksiz
e
,
1
,
out_data
);
for_range
(
computer
);
out
->
Resize
(
out_dims
);
...
...
@@ -99,7 +99,7 @@ class SpaceToDepthGradKernel : public framework::OpKernel<T> {
context
.
Input
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
d_x
=
context
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
stride
=
context
.
Attr
<
int64_t
>
(
"strid
e"
);
auto
blocksize
=
context
.
Attr
<
int64_t
>
(
"blocksiz
e"
);
auto
in_dims
=
d_x
->
dims
();
d_x
->
mutable_data
(
context
.
GetPlace
(),
d_out
->
type
());
...
...
@@ -115,8 +115,8 @@ class SpaceToDepthGradKernel : public framework::OpKernel<T> {
auto
*
dx_data
=
d_x
->
data
<
T
>
();
auto
*
dout_data
=
d_out
->
data
<
T
>
();
paddle
::
operators
::
space_to_depth_compute
<
T
>
computer
(
dout_data
,
W
,
H
,
C
,
B
,
strid
e
,
0
,
dx_data
);
paddle
::
operators
::
space_to_depth_compute
<
T
>
computer
(
dout_data
,
W
,
H
,
C
,
B
,
blocksiz
e
,
0
,
dx_data
);
for_range
(
computer
);
d_x
->
Resize
(
in_dims
);
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
9a74c448
...
...
@@ -7485,29 +7485,29 @@ def maxout(x, groups, name=None):
return
out
def
space_to_depth
(
x
,
strid
e
,
name
=
None
):
def
space_to_depth
(
x
,
blocksiz
e
,
name
=
None
):
"""
Gives a
stride to space_to_depth the input LoDtensor
Gives a
blocksize to space_to_depth the input LoDtensor with Layout: [batch, channel, height, width]
R
earranges blocks of spatial data, into depth. More specifically, this op outputs a copy of the
This op r
earranges blocks of spatial data, into depth. More specifically, this op outputs a copy of the
input LoDtensor where values from the height and width dimensions are moved to the channel dimension.
The attr
strid
e indicates the input block size.
The attr
blocksiz
e indicates the input block size.
space_to_depth will reorgnize the elements of input with shape[batch, channel, height, width] according
to
stride to construct output with shape [batch, channel * stride * stride, height/stride, width/strid
e]:
to
blocksize to construct output with shape [batch, channel * blocksize * blocksize, height/blocksize, width/blocksiz
e]:
space_to_depth is used to This operation is useful for resizing the activations between convolutions
(but keeping all data)
Args:
x(variable): The input LoDtensor.
stride(variable): The strid
e to select the element on each feature map
blocksize(variable): The blocksiz
e to select the element on each feature map
Returns:
Variable: The output LoDtensor.
Raises:
TypeError:
strid
e type must be a long.
TypeError:
blocksiz
e type must be a long.
Examples:
.. code-block:: python
...
...
@@ -7515,13 +7515,13 @@ def space_to_depth(x, stride, name=None):
data = fluid.layers.data(
name='data', shape=[1, 4, 2, 2], dtype='float32')
space_to_depthed = fluid.layers.space_to_depth(
x=data,
strid
e=2)
x=data,
blocksiz
e=2)
"""
helper
=
LayerHelper
(
"space_to_depth"
,
**
locals
())
if
not
(
isinstance
(
strid
e
,
int
)):
raise
ValueError
(
"
strid
e must be a python Int"
)
if
not
(
isinstance
(
blocksiz
e
,
int
)):
raise
ValueError
(
"
blocksiz
e must be a python Int"
)
if
name
is
None
:
out
=
helper
.
create_variable_for_type_inference
(
...
...
@@ -7533,7 +7533,7 @@ def space_to_depth(x, stride, name=None):
helper
.
append_op
(
type
=
"space_to_depth"
,
inputs
=
{
"X"
:
x
},
attrs
=
{
"
stride"
:
strid
e
},
attrs
=
{
"
blocksize"
:
blocksiz
e
},
outputs
=
{
"Out"
:
out
})
return
out
...
...
python/paddle/fluid/tests/unittests/test_space_to_depth_op.py
浏览文件 @
9a74c448
...
...
@@ -21,8 +21,8 @@ from op_test import OpTest
class
TestSpaceToDepthOp
(
OpTest
):
@
staticmethod
def
helper
(
in_
,
width
,
height
,
channel
,
batch
,
strid
e
,
forward
,
out_
):
channel_out
=
channel
//
(
stride
*
strid
e
)
def
helper
(
in_
,
width
,
height
,
channel
,
batch
,
blocksiz
e
,
forward
,
out_
):
channel_out
=
channel
//
(
blocksize
*
blocksiz
e
)
for
b
in
range
(
batch
):
for
k
in
range
(
channel
):
for
j
in
range
(
height
):
...
...
@@ -30,10 +30,10 @@ class TestSpaceToDepthOp(OpTest):
in_index
=
i
+
width
*
(
j
+
height
*
(
k
+
channel
*
b
))
channel2
=
k
%
channel_out
offset
=
k
//
channel_out
width2
=
i
*
stride
+
offset
%
strid
e
height2
=
j
*
stride
+
offset
//
strid
e
out_index
=
width2
+
width
*
strid
e
*
(
height2
+
height
*
strid
e
*
width2
=
i
*
blocksize
+
offset
%
blocksiz
e
height2
=
j
*
blocksize
+
offset
//
blocksiz
e
out_index
=
width2
+
width
*
blocksiz
e
*
(
height2
+
height
*
blocksiz
e
*
(
channel2
+
channel_out
*
b
))
if
forward
:
out_
[
out_index
]
=
in_
[
in_index
]
...
...
@@ -46,10 +46,10 @@ class TestSpaceToDepthOp(OpTest):
self
.
op_type
=
"space_to_depth"
self
.
inputs
=
{
"X"
:
self
.
x
}
self
.
helper
(
self
.
x_1d
,
self
.
x
.
shape
[
3
],
self
.
x
.
shape
[
2
],
self
.
x
.
shape
[
1
],
self
.
x
.
shape
[
0
],
self
.
stride
,
self
.
forward
,
self
.
out_1d
)
self
.
x
.
shape
[
1
],
self
.
x
.
shape
[
0
],
self
.
blocksize
,
self
.
forward
,
self
.
out_1d
)
self
.
out
=
np
.
reshape
(
self
.
out_1d
,
self
.
infered_shape
)
self
.
attrs
=
{
"
stride"
:
self
.
strid
e
}
self
.
attrs
=
{
"
blocksize"
:
self
.
blocksiz
e
}
self
.
outputs
=
{
"Out"
:
self
.
out
}
def
init_data
(
self
):
...
...
@@ -57,7 +57,7 @@ class TestSpaceToDepthOp(OpTest):
self
.
infered_shape
=
(
32
,
48
,
3
,
3
)
self
.
one_d_len
=
32
*
48
*
3
*
3
self
.
strid
e
=
2
self
.
blocksiz
e
=
2
self
.
x
=
np
.
random
.
random
(
self
.
ori_shape
).
astype
(
'float32'
)
self
.
x_1d
=
np
.
reshape
(
self
.
x
,
self
.
one_d_len
)
self
.
out
=
np
.
zeros
(
self
.
infered_shape
).
astype
(
'float32'
)
...
...
@@ -81,7 +81,7 @@ class TestSpaceToDepthOpBasic(TestSpaceToDepthOp):
self
.
infered_shape
=
(
32
,
32
,
3
,
3
)
self
.
one_d_len
=
32
*
32
*
3
*
3
self
.
strid
e
=
2
self
.
blocksiz
e
=
2
self
.
x
=
np
.
random
.
random
(
self
.
ori_shape
).
astype
(
'float32'
)
self
.
x_1d
=
np
.
reshape
(
self
.
x
,
self
.
one_d_len
)
self
.
out
=
np
.
zeros
(
self
.
infered_shape
).
astype
(
'float32'
)
...
...
@@ -95,7 +95,7 @@ class TestSpaceToDepthOpDoubleBasic(TestSpaceToDepthOp):
self
.
infered_shape
=
(
32
,
32
,
3
,
3
)
self
.
one_d_len
=
32
*
32
*
3
*
3
self
.
strid
e
=
2
self
.
blocksiz
e
=
2
self
.
x
=
np
.
random
.
random
(
self
.
ori_shape
).
astype
(
'float64'
)
self
.
x_1d
=
np
.
reshape
(
self
.
x
,
self
.
one_d_len
)
self
.
out
=
np
.
zeros
(
self
.
infered_shape
).
astype
(
'float64'
)
...
...
@@ -109,7 +109,7 @@ class TestSpaceToDepthOpWithStride3(TestSpaceToDepthOp):
self
.
infered_shape
=
(
32
,
81
,
2
,
2
)
self
.
one_d_len
=
32
*
81
*
2
*
2
self
.
strid
e
=
3
self
.
blocksiz
e
=
3
self
.
x
=
np
.
random
.
random
(
self
.
ori_shape
).
astype
(
'float32'
)
self
.
x_1d
=
np
.
reshape
(
self
.
x
,
self
.
one_d_len
)
self
.
out
=
np
.
zeros
(
self
.
infered_shape
).
astype
(
'float32'
)
...
...
@@ -123,7 +123,7 @@ class TestSpaceToDepthOpWithNotSquare(TestSpaceToDepthOp):
self
.
infered_shape
=
(
32
,
81
,
3
,
2
)
self
.
one_d_len
=
32
*
81
*
3
*
2
self
.
strid
e
=
3
self
.
blocksiz
e
=
3
self
.
x
=
np
.
random
.
random
(
self
.
ori_shape
).
astype
(
'float32'
)
self
.
x_1d
=
np
.
reshape
(
self
.
x
,
self
.
one_d_len
)
self
.
out
=
np
.
zeros
(
self
.
infered_shape
).
astype
(
'float32'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录