Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
25a3d2d7
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看板
提交
25a3d2d7
编写于
11月 22, 2017
作者:
G
gongweibao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix by comments
上级
e11d4424
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
123 addition
and
77 deletion
+123
-77
paddle/operators/block_expand_op.cc
paddle/operators/block_expand_op.cc
+2
-1
paddle/operators/block_expand_op.h
paddle/operators/block_expand_op.h
+14
-8
python/paddle/v2/fluid/tests/test_block_expand_op.py
python/paddle/v2/fluid/tests/test_block_expand_op.py
+107
-68
未找到文件。
paddle/operators/block_expand_op.cc
浏览文件 @
25a3d2d7
...
@@ -30,7 +30,8 @@ class BlockExpandOp : public framework::OperatorWithKernel {
...
@@ -30,7 +30,8 @@ class BlockExpandOp : public framework::OperatorWithKernel {
"Output of BlockExpandOp op should not be null."
);
"Output of BlockExpandOp op should not be null."
);
auto
in_dim
=
ctx
->
GetInputDim
(
"X"
);
auto
in_dim
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
in_dim
.
size
(),
4
,
"Input format must be NCHW."
);
PADDLE_ENFORCE_EQ
(
in_dim
.
size
(),
4
,
"Input(X) format must be 4D tensor, eg., NCHW."
);
PADDLE_ENFORCE_GE
(
in_dim
[
0
],
1
,
"Input batchsize must >= 1."
);
PADDLE_ENFORCE_GE
(
in_dim
[
0
],
1
,
"Input batchsize must >= 1."
);
int
block_height
=
ctx
->
Attrs
().
Get
<
int
>
(
"blockHeight"
);
int
block_height
=
ctx
->
Attrs
().
Get
<
int
>
(
"blockHeight"
);
...
...
paddle/operators/block_expand_op.h
浏览文件 @
25a3d2d7
...
@@ -68,13 +68,16 @@ class BlockExpandKernel : public framework::OpKernel<T> {
...
@@ -68,13 +68,16 @@ class BlockExpandKernel : public framework::OpKernel<T> {
img_height
,
img_width
,
block_height
,
block_width
,
stride_height
,
img_height
,
img_width
,
block_height
,
block_width
,
stride_height
,
stride_width
,
padding_height
,
padding_width
,
outputHeight
,
outputWidth
);
stride_width
,
padding_height
,
padding_width
,
outputHeight
,
outputWidth
);
std
::
vector
<
int
>
stride
({
stride_height
,
stride_width
});
std
::
vector
<
int
>
padding
({
padding_height
,
padding_width
});
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
Tensor
src
=
in
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
({
C
,
img_height
,
img_width
});
Tensor
src
=
in
->
Slice
(
i
,
i
+
1
).
Resize
({
C
,
img_height
,
img_width
});
Tensor
dst
=
out
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
Tensor
dst
=
out
->
Slice
(
i
,
i
+
1
).
Resize
(
{
outputHeight
,
outputWidth
,
C
,
block_height
,
block_width
});
{
outputHeight
,
outputWidth
,
C
,
block_height
,
block_width
});
math
::
Im2ColFunctor
<
math
::
ColFormat
::
kOCF
,
Place
,
T
>
f
;
math
::
Im2ColFunctor
<
math
::
ColFormat
::
kOCF
,
Place
,
T
>
f
;
f
(
ctx
.
device_context
(),
src
,
dst
,
stride_height
,
stride_width
,
f
(
ctx
.
device_context
(),
src
,
stride
,
padding
,
&
dst
);
padding_height
,
padding_width
);
}
}
}
}
};
};
...
@@ -112,13 +115,16 @@ class BlockExpandGradKernel : public framework::OpKernel<T> {
...
@@ -112,13 +115,16 @@ class BlockExpandGradKernel : public framework::OpKernel<T> {
img_height
,
img_width
,
block_height
,
block_width
,
stride_height
,
img_height
,
img_width
,
block_height
,
block_width
,
stride_height
,
stride_width
,
padding_height
,
padding_width
,
outputHeight
,
outputWidth
);
stride_width
,
padding_height
,
padding_width
,
outputHeight
,
outputWidth
);
std
::
vector
<
int
>
stride
({
stride_height
,
stride_width
});
std
::
vector
<
int
>
padding
({
padding_height
,
padding_width
});
// std::vector<int> stride({stride_height, stride_width});
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
Tensor
dst
=
d_x
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
({
C
,
img_height
,
img_width
});
Tensor
dst
=
d_x
->
Slice
(
i
,
i
+
1
).
Resize
({
C
,
img_height
,
img_width
});
Tensor
src
=
d_out
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
Tensor
src
=
d_out
->
Slice
(
i
,
i
+
1
).
Resize
(
{
outputHeight
,
outputWidth
,
C
,
block_height
,
block_width
});
{
outputHeight
,
outputWidth
,
C
,
block_height
,
block_width
});
math
::
Col2ImFunctor
<
math
::
ColFormat
::
kOCF
,
Place
,
T
>
f
;
math
::
Col2ImFunctor
<
math
::
ColFormat
::
kOCF
,
Place
,
T
>
f
;
f
(
ctx
.
device_context
(),
dst
,
src
,
stride_height
,
stride_width
,
f
(
ctx
.
device_context
(),
dst
,
stride
,
padding
,
&
src
);
padding_height
,
padding_width
);
}
}
}
}
};
};
...
...
python/paddle/v2/fluid/tests/test_block_expand_op.py
浏览文件 @
25a3d2d7
...
@@ -4,27 +4,27 @@ from op_test import OpTest
...
@@ -4,27 +4,27 @@ from op_test import OpTest
def
get_output_shape
(
attrs
,
x
):
def
get_output_shape
(
attrs
,
x
):
img
H
eight
=
x
.
shape
[
1
]
img
_h
eight
=
x
.
shape
[
1
]
img
W
idth
=
x
.
shape
[
2
]
img
_w
idth
=
x
.
shape
[
2
]
padding
H
eight
=
attrs
[
'paddingHeight'
]
padding
_h
eight
=
attrs
[
'paddingHeight'
]
padding
W
idth
=
attrs
[
'paddingWidth'
]
padding
_w
idth
=
attrs
[
'paddingWidth'
]
block
H
eight
=
attrs
[
'blockHeight'
]
block
_h
eight
=
attrs
[
'blockHeight'
]
block
W
idth
=
attrs
[
'blockWidth'
]
block
_w
idth
=
attrs
[
'blockWidth'
]
stride
H
eight
=
attrs
[
'strideHeight'
]
stride
_h
eight
=
attrs
[
'strideHeight'
]
stride
W
idth
=
attrs
[
'strideWidth'
]
stride
_w
idth
=
attrs
[
'strideWidth'
]
output
H
eight
=
\
output
_h
eight
=
\
1
+
\
1
+
\
(
img
Height
+
2
*
paddingHeight
-
blockHeight
+
strideH
eight
-
1
)
/
\
(
img
_height
+
2
*
padding_height
-
block_height
+
stride_h
eight
-
1
)
/
\
strideHeight
strideHeight
output
W
idth
=
\
output
_w
idth
=
\
1
+
\
1
+
\
(
img
Width
+
2
*
paddingWidth
-
blockWidth
+
strideW
idth
-
1
)
/
\
(
img
_width
+
2
*
padding_width
-
block_width
+
stride_w
idth
-
1
)
/
\
stride
W
idth
stride
_w
idth
return
output
Height
,
outputW
idth
return
output
_height
,
output_w
idth
def
im2col
(
attrs
,
im
,
col
):
def
im2col
(
attrs
,
im
,
col
):
...
@@ -34,38 +34,39 @@ def im2col(attrs, im, col):
...
@@ -34,38 +34,39 @@ def im2col(attrs, im, col):
{outputHeight, outputWidth, inputChannels, filterHeight, filterWidth}
{outputHeight, outputWidth, inputChannels, filterHeight, filterWidth}
"""
"""
input_channels
=
im
.
shape
[
0
]
input_channels
=
im
.
shape
[
0
]
input
H
eight
=
im
.
shape
[
1
]
input
_h
eight
=
im
.
shape
[
1
]
input
W
idth
=
im
.
shape
[
2
]
input
_w
idth
=
im
.
shape
[
2
]
output
H
eight
=
col
.
shape
[
0
]
output
_h
eight
=
col
.
shape
[
0
]
output
W
idth
=
col
.
shape
[
1
]
output
_w
idth
=
col
.
shape
[
1
]
filter
H
eight
=
col
.
shape
[
3
]
filter
_h
eight
=
col
.
shape
[
3
]
filter
W
idth
=
col
.
shape
[
4
]
filter
_w
idth
=
col
.
shape
[
4
]
stride
H
eight
=
attrs
[
'strideHeight'
]
stride
_h
eight
=
attrs
[
'strideHeight'
]
stride
W
idth
=
attrs
[
'strideWidth'
]
stride
_w
idth
=
attrs
[
'strideWidth'
]
padding
H
eight
=
attrs
[
'paddingHeight'
]
padding
_h
eight
=
attrs
[
'paddingHeight'
]
padding
W
idth
=
attrs
[
'paddingWidth'
]
padding
_w
idth
=
attrs
[
'paddingWidth'
]
for
col_row_idx
in
range
(
0
,
output
H
eight
):
for
col_row_idx
in
range
(
0
,
output
_h
eight
):
for
col_col_idx
in
range
(
0
,
output
W
idth
):
for
col_col_idx
in
range
(
0
,
output
_w
idth
):
for
channel
in
range
(
0
,
input_channels
):
for
channel
in
range
(
0
,
input_channels
):
for
filter_row_idx
in
range
(
0
,
filter
H
eight
):
for
filter_row_idx
in
range
(
0
,
filter
_h
eight
):
for
filter_col_idx
in
range
(
0
,
filter
W
idth
):
for
filter_col_idx
in
range
(
0
,
filter
_w
idth
):
im_row_offset
=
col_row_idx
*
stride
H
eight
\
im_row_offset
=
col_row_idx
*
stride
_h
eight
\
+
filter_row_idx
-
padding
H
eight
+
filter_row_idx
-
padding
_h
eight
im_col_offset
=
col_col_idx
*
stride
W
idth
\
im_col_offset
=
col_col_idx
*
stride
_w
idth
\
+
filter_col_idx
-
padding
W
idth
+
filter_col_idx
-
padding
_w
idth
if
(
im_row_offset
<
0
or
im_row_offset
>=
inputHeight
or
if
(
im_row_offset
<
0
or
im_row_offset
>=
input_height
or
im_col_offset
<
0
or
im_col_offset
<
0
or
im_col_offset
>=
input
W
idth
):
im_col_offset
>=
input
_w
idth
):
col
[
col_row_idx
][
col_col_idx
][
channel
][
\
col
[
col_row_idx
][
col_col_idx
][
channel
][
\
filter_row_idx
][
filter_col_idx
]
=
0.0
filter_row_idx
][
filter_col_idx
]
=
0.0
else
:
else
:
im_offset
=
(
channel
*
input
H
eight
+
im_row_offset
\
im_offset
=
(
channel
*
input
_h
eight
+
im_row_offset
\
)
*
input
W
idth
+
im_col_offset
)
*
input
_w
idth
+
im_col_offset
col
[
col_row_idx
][
col_col_idx
][
channel
][
\
col
[
col_row_idx
][
col_col_idx
][
channel
][
\
filter_row_idx
][
filter_col_idx
]
=
im
[
channel
][
\
filter_row_idx
][
filter_col_idx
]
=
im
[
channel
][
\
...
@@ -76,55 +77,55 @@ def col2img(attrs, col, img):
...
@@ -76,55 +77,55 @@ def col2img(attrs, col, img):
"""
"""
img: {CHW}
img: {CHW}
col:
col:
{output
H
eight, outputWidth, inputChannels, filterHeight, filterWidth}
{output
_h
eight, outputWidth, inputChannels, filterHeight, filterWidth}
"""
"""
input_channels
=
im
.
shape
[
0
]
input_channels
=
im
.
shape
[
0
]
input
H
eight
=
im
.
shape
[
1
]
input
_h
eight
=
im
.
shape
[
1
]
input
W
idth
=
im
.
shape
[
2
]
input
_w
idth
=
im
.
shape
[
2
]
output
H
eight
=
col
.
shape
[
0
]
output
_h
eight
=
col
.
shape
[
0
]
output
W
idth
=
col
.
shape
[
1
]
output
_w
idth
=
col
.
shape
[
1
]
filter
H
eight
=
col
.
shape
[
3
]
filter
_h
eight
=
col
.
shape
[
3
]
filter
W
idth
=
col
.
shape
[
4
]
filter
_w
idth
=
col
.
shape
[
4
]
stride
H
eight
=
attrs
[
'strideHeight'
]
stride
_h
eight
=
attrs
[
'strideHeight'
]
stride
W
idth
=
attrs
[
'strideWidth'
]
stride
_w
idth
=
attrs
[
'strideWidth'
]
padding
H
eight
=
attrs
[
'paddingHeight'
]
padding
_h
eight
=
attrs
[
'paddingHeight'
]
padding
W
idth
=
attrs
[
'paddingWidth'
]
padding
_w
idth
=
attrs
[
'paddingWidth'
]
for
col_row_idx
in
range
(
0
,
output
H
eight
):
for
col_row_idx
in
range
(
0
,
output
_h
eight
):
for
col_col_idx
in
range
(
0
,
output
W
idth
):
for
col_col_idx
in
range
(
0
,
output
_w
idth
):
for
channel
in
range
(
0
,
input_channels
):
for
channel
in
range
(
0
,
input_channels
):
for
filter_row_idx
in
range
(
0
,
filter
H
eight
):
for
filter_row_idx
in
range
(
0
,
filter
_h
eight
):
for
filter_col_idx
in
range
(
0
,
filter
W
idth
):
for
filter_col_idx
in
range
(
0
,
filter
_w
idth
):
im_row_offset
=
\
im_row_offset
=
\
col_row_idx
*
stride
Height
+
filter_row_idx
-
paddingH
eight
col_row_idx
*
stride
_height
+
filter_row_idx
-
padding_h
eight
im_col_offset
=
\
im_col_offset
=
\
col_col_idx
*
stride
Width
+
filter_col_idx
-
paddingW
idth
col_col_idx
*
stride
_width
+
filter_col_idx
-
padding_w
idth
if
(
im_row_offset
>=
0
and
if
(
im_row_offset
>=
0
and
im_row_offset
<
input
H
eight
and
im_row_offset
<
input
_h
eight
and
im_col_offset
>=
0
and
im_col_offset
>=
0
and
im_col_offset
<
input
W
idth
):
im_col_offset
<
input
_w
idth
):
im
[
channel
][
im_row_offset
][
im_col_offset
]
=
\
im
[
channel
][
im_row_offset
][
im_col_offset
]
=
\
col
[
col_row_idx
][
col_col_idx
][
channel
][
filter_row_idx
][
filter_col_idx
]
col
[
col_row_idx
][
col_col_idx
][
channel
][
filter_row_idx
][
filter_col_idx
]
class
TestBlockExpandOp
(
OpTest
):
def
get_input_data
(
C
,
H
,
W
):
def
get_input_data
(
self
,
C
,
H
,
W
):
x
=
np
.
random
.
uniform
(
0.1
,
1
,
[
C
,
H
,
W
]).
astype
(
"float32"
)
x
=
np
.
random
.
uniform
(
0.1
,
1
,
[
C
,
H
,
W
]).
astype
(
"float32"
)
for
c
in
range
(
0
,
C
):
for
c
in
range
(
0
,
C
):
for
h
in
range
(
0
,
H
):
for
h
in
range
(
0
,
H
):
for
w
in
range
(
0
,
W
):
for
w
in
range
(
0
,
W
):
#x[c][h][w] = c * H * W + h *W + w
#x[c][h][w] = c * H * W + h *W + w
x
[
c
][
h
][
w
]
=
0.2
+
0.01
*
(
c
*
H
*
W
+
h
*
W
+
w
)
x
[
c
][
h
][
w
]
=
0.2
+
0.01
*
(
c
*
H
*
W
+
h
*
W
+
w
)
return
x
return
x
class
TestBlockExpandOp
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
C
=
3
C
=
3
H
=
4
H
=
4
W
=
4
W
=
4
x
=
self
.
get_input_data
(
C
,
H
,
W
)
x
=
get_input_data
(
C
,
H
,
W
)
#print x
attrs
=
{
attrs
=
{
'blockHeight'
:
2
,
'blockHeight'
:
2
,
...
@@ -135,9 +136,47 @@ class TestBlockExpandOp(OpTest):
...
@@ -135,9 +136,47 @@ class TestBlockExpandOp(OpTest):
'paddingWidth'
:
1
,
'paddingWidth'
:
1
,
}
}
outputHeight
,
outputWidth
=
get_output_shape
(
attrs
,
x
)
output_height
,
output_width
=
get_output_shape
(
attrs
,
x
)
out
=
np
.
random
.
uniform
(
0.1
,
1
,
\
[
output_height
,
output_width
,
x
.
shape
[
0
],
\
attrs
[
'blockHeight'
],
attrs
[
'blockWidth'
]]).
astype
(
"float32"
)
self
.
op_type
=
"block_expand"
self
.
inputs
=
{
'X'
:
x
.
reshape
(
1
,
C
,
H
,
W
)}
self
.
attrs
=
attrs
im2col
(
attrs
,
x
,
out
)
self
.
outputs
=
{
'Out'
:
out
.
reshape
(
1
,
output_height
,
output_width
,
x
.
shape
[
0
],
\
attrs
[
'blockHeight'
],
attrs
[
'blockWidth'
])
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad_normal
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestBlockExpandOp2
(
OpTest
):
def
setUp
(
self
):
C
=
3
H
=
4
W
=
5
x
=
get_input_data
(
C
,
H
,
W
)
attrs
=
{
'blockHeight'
:
2
,
'blockWidth'
:
1
,
'strideHeight'
:
2
,
'strideWidth'
:
1
,
'paddingHeight'
:
2
,
'paddingWidth'
:
1
,
}
output_height
,
output_width
=
get_output_shape
(
attrs
,
x
)
out
=
np
.
random
.
uniform
(
0.1
,
1
,
\
out
=
np
.
random
.
uniform
(
0.1
,
1
,
\
[
output
Height
,
outputW
idth
,
x
.
shape
[
0
],
\
[
output
_height
,
output_w
idth
,
x
.
shape
[
0
],
\
attrs
[
'blockHeight'
],
attrs
[
'blockWidth'
]]).
astype
(
"float32"
)
attrs
[
'blockHeight'
],
attrs
[
'blockWidth'
]]).
astype
(
"float32"
)
self
.
op_type
=
"block_expand"
self
.
op_type
=
"block_expand"
...
@@ -146,7 +185,7 @@ class TestBlockExpandOp(OpTest):
...
@@ -146,7 +185,7 @@ class TestBlockExpandOp(OpTest):
im2col
(
attrs
,
x
,
out
)
im2col
(
attrs
,
x
,
out
)
self
.
outputs
=
{
self
.
outputs
=
{
'Out'
:
out
.
reshape
(
1
,
output
Height
,
outputW
idth
,
x
.
shape
[
0
],
\
'Out'
:
out
.
reshape
(
1
,
output
_height
,
output_w
idth
,
x
.
shape
[
0
],
\
attrs
[
'blockHeight'
],
attrs
[
'blockWidth'
])
attrs
[
'blockHeight'
],
attrs
[
'blockWidth'
])
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录