Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
7eeaae16
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
1 年多 前同步成功
通知
696
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7eeaae16
编写于
10月 19, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
deconv
上级
c33575a5
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
109 addition
and
7 deletion
+109
-7
paddle/operators/deconv2d_op.h
paddle/operators/deconv2d_op.h
+8
-7
python/paddle/v2/framework/tests/test_deconv_op.py
python/paddle/v2/framework/tests/test_deconv_op.py
+101
-0
未找到文件。
paddle/operators/deconv2d_op.h
浏览文件 @
7eeaae16
...
@@ -14,6 +14,7 @@ limitations under the License. */
...
@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#pragma once
#include "glog/logging.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/im2col.h"
#include "paddle/operators/math/im2col.h"
...
@@ -117,8 +118,7 @@ class GemmDeconv2DKernel : public framework::OpKernel<T> {
...
@@ -117,8 +118,7 @@ class GemmDeconv2DKernel : public framework::OpKernel<T> {
// of shape (C * K_H * K_W, H * W)
// of shape (C * K_H * K_W, H * W)
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter
,
true
,
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter
,
true
,
input_batch
,
false
,
T
(
1.0
),
&
col_matrix
,
T
(
0.0
));
input_batch
,
false
,
T
(
1.0
),
&
col_matrix
,
T
(
0.0
));
col2im
(
context
.
device_context
(),
output_batch
,
col
,
strides
[
0
],
col2im
(
context
.
device_context
(),
output_batch
,
col_matrix
,
strides
[
0
],
strides
[
1
],
0
,
0
);
strides
[
1
],
0
,
0
);
}
}
}
}
...
@@ -203,8 +203,8 @@ class GemmDeconvGrad2DKernel : public framework::OpKernel<T> {
...
@@ -203,8 +203,8 @@ class GemmDeconvGrad2DKernel : public framework::OpKernel<T> {
input_grad
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
input_matrix_shape
);
input_grad
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
input_matrix_shape
);
// im2col: dy from (C, O_H, O_W) -> (C * K_H * K_W, H * W)
// im2col: dy from (C, O_H, O_W) -> (C * K_H * K_W, H * W)
im2col
(
context
.
device_context
(),
output_grad_batch
,
col
_matrix
,
im2col
(
context
.
device_context
(),
output_grad_batch
,
col
,
strides
[
0
]
,
strides
[
0
],
strides
[
1
],
paddings
[
0
],
paddings
[
1
]);
strides
[
1
],
paddings
[
0
],
paddings
[
1
]);
// gemm: dx = filter * dy
// gemm: dx = filter * dy
// (M, C * K_H * K_W) * (C * K_H * K_W, H * W) -> (M, C, H)
// (M, C * K_H * K_W) * (C * K_H * K_W, H * W) -> (M, C, H)
...
@@ -234,13 +234,14 @@ class GemmDeconvGrad2DKernel : public framework::OpKernel<T> {
...
@@ -234,13 +234,14 @@ class GemmDeconvGrad2DKernel : public framework::OpKernel<T> {
Tensor
in_batch
=
input
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
input_matrix_shape
);
Tensor
in_batch
=
input
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
input_matrix_shape
);
// im2col: (C * H * W, K_H * K_W)
// im2col: (C * H * W, K_H * K_W)
im2col
(
context
.
device_context
(),
output_grad_batch
,
col
_matrix_f
,
im2col
(
context
.
device_context
(),
output_grad_batch
,
col
,
strides
[
0
]
,
strides
[
0
],
strides
[
1
],
paddings
[
0
],
paddings
[
1
]);
strides
[
1
],
paddings
[
0
],
paddings
[
1
]);
// gemm: d_filter = x * y_grad^T
// gemm: d_filter = x * y_grad^T
// (M, C * H * W) * (K_H * K_W, C * H * W) -> (M, C, H)
// (M, C * H * W) * (K_H * K_W, C * H * W) -> (M, C, H)
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
in_batch
,
false
,
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
in_batch
,
false
,
col_matrix
,
true
,
T
(
1.0
),
&
filter_grad_
,
T
(
1.0
));
col_matrix_f
,
true
,
T
(
1.0
),
&
filter_grad_
,
T
(
1.0
));
}
}
}
}
}
}
...
...
python/paddle/v2/framework/tests/test_deconv_op.py
0 → 100644
浏览文件 @
7eeaae16
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
def
deconv2d_forward_naive
(
input_
,
filter_
,
deconv_param
):
# [2, 3, 5, 5]
in_n
,
in_c
,
in_h
,
in_w
=
input_
.
shape
# [3, 6, 3, 3]
f_c
,
out_c
,
f_h
,
f_w
=
filter_
.
shape
assert
in_c
==
f_c
stride
,
pad
=
deconv_param
[
'stride'
],
deconv_param
[
'pad'
]
out_h
=
(
in_h
-
1
)
*
stride
[
0
]
+
f_h
out_w
=
(
in_w
-
1
)
*
stride
[
1
]
+
f_w
out
=
np
.
zeros
((
in_n
,
out_c
,
out_h
,
out_w
))
for
n
in
range
(
in_n
):
for
i
in
range
(
in_h
):
for
j
in
range
(
in_w
):
input_masked
=
input_
[
n
,
:,
i
,
j
]
# (c)
input_masked
=
np
.
reshape
(
input_masked
,
(
in_c
,
1
,
1
))
input_masked
=
np
.
tile
(
input_masked
,
(
1
,
f_h
,
f_w
))
for
k
in
range
(
out_c
):
tmp_out
=
np
.
sum
(
input_masked
*
filter_
[:,
k
,
:,
:],
axis
=
0
)
i1
,
i2
=
i
*
stride
[
0
],
i
*
stride
[
0
]
+
f_h
j1
,
j2
=
j
*
stride
[
0
],
j
*
stride
[
0
]
+
f_w
out
[
n
,
k
,
i1
:
i2
,
j1
:
j2
]
+=
tmp_out
return
out
class
TestDeconv2dOp
(
OpTest
):
def
setUp
(
self
):
# init as deconv
self
.
init_op_type
()
# [2, 3, 5, 5] -> kernel [3, 6, 3, 3] -> output [2, 6, 7, 7]
self
.
init_test_case
()
deconv2d_param
=
{
'stride'
:
self
.
stride
,
'pad'
:
self
.
pad
}
input_
=
np
.
random
.
random
(
self
.
input_size
).
astype
(
"float32"
)
filter_
=
np
.
random
.
random
(
self
.
filter_size
).
astype
(
"float32"
)
output
=
deconv2d_forward_naive
(
input_
,
filter_
,
deconv2d_param
)
# print 'deconv output py', output, output.shape
self
.
inputs
=
{
'Input'
:
input_
,
'Filter'
:
filter_
}
self
.
attrs
=
{
'strides'
:
self
.
stride
,
'paddings'
:
self
.
pad
,
# 'dilations': self.dilations
}
self
.
outputs
=
{
'Output'
:
output
}
def
test_check_output
(
self
):
print
'check output here'
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
(
set
([
'Input'
,
'Filter'
]),
'Output'
,
max_relative_error
=
0.05
)
def
test_check_grad_no_filter
(
self
):
self
.
check_grad
(
[
'Input'
],
'Output'
,
max_relative_error
=
0.05
,
no_grad_set
=
set
([
'Filter'
]))
def
test_check_grad_no_input
(
self
):
self
.
check_grad
(
[
'Filter'
],
'Output'
,
max_relative_error
=
0.05
,
no_grad_set
=
set
([
'Input'
]))
def
init_test_case
(
self
):
self
.
pad
=
[
0
,
0
]
self
.
stride
=
[
1
,
1
]
self
.
dilations
=
[
1
,
1
]
self
.
input_size
=
[
2
,
3
,
5
,
5
]
# NCHW
f_c
=
self
.
input_size
[
1
]
self
.
filter_size
=
[
f_c
,
6
,
3
,
3
]
def
init_op_type
(
self
):
self
.
op_type
=
"deconv2d"
"""
class TestCudnn(TestConv2dOp):
def init_group(self):
self.groups = 1
def init_op_type(self):
self.op_type = "conv_cudnn"
"""
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录