Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
43aad989
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
43aad989
编写于
10月 18, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
deconv
上级
5ec55e79
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
66 addition
and
38 deletion
+66
-38
paddle/operators/deconv2d_op.cc
paddle/operators/deconv2d_op.cc
+2
-1
paddle/operators/deconv2d_op.cu
paddle/operators/deconv2d_op.cu
+3
-2
paddle/operators/deconv2d_op.h
paddle/operators/deconv2d_op.h
+61
-35
未找到文件。
paddle/operators/deconv2d_op.cc
浏览文件 @
43aad989
...
...
@@ -100,4 +100,5 @@ REGISTER_OP(deconv2d, ops::Deconv2DOp, ops::Deconv2DOpMaker, deconv2d_grad,
REGISTER_OP_CPU_KERNEL
(
deconv2d
,
ops
::
GemmDeconv2DKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
deconv2d_grad
,
ops
::
GemmConv2DKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
deconv2d_grad
,
ops
::
GemmDeconvGrad2DKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/deconv2d_op.cu
浏览文件 @
43aad989
...
...
@@ -18,6 +18,7 @@
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
deconv2d
,
ops
::
Gemm
ConvGrad
2DKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
deconv2d
,
ops
::
Gemm
Deconv
2DKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
deconv2d_grad
,
ops
::
GemmConv2DKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
deconv2d_grad
,
ops
::
GemmDeconvGrad2DKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/deconv2d_op.h
浏览文件 @
43aad989
...
...
@@ -80,10 +80,10 @@ class GemmDeconv2DKernel : public framework::OpKernel<T> {
col2im
;
// use col_shape in the im2col and col2im calculation
framework
::
DDim
col_shape
=
{
C
,
K_H
,
K_W
,
H
,
W
};
DDim
col_shape
=
{
C
,
K_H
,
K_W
,
H
,
W
};
// use col_matrix_shape in the gemm calculation
framework
::
DDim
col_matrix_shape
=
{
M
*
K_H
*
K_W
,
H
*
W
};
DDim
col_matrix_shape
=
{
M
*
K_H
*
K_W
,
H
*
W
};
Tensor
col
;
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
...
...
@@ -124,7 +124,6 @@ class GemmDeconv2DKernel : public framework::OpKernel<T> {
}
};
/*
template
<
typename
Place
,
typename
T
>
class
GemmDeconvGrad2DKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
...
...
@@ -143,8 +142,8 @@ class GemmDeconvGrad2DKernel : public framework::OpKernel<T> {
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Filter"
));
std
::
vector
<
int
>
strides
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"strides"
);
// no paddings and groups allowed in deconv
// Actually, no paddings and groups allowed in deconv
std
::
vector
<
int
>
paddings
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"paddings"
);
int
N
=
input
->
dims
()[
0
];
int
M
=
input
->
dims
()[
1
];
...
...
@@ -154,19 +153,23 @@ class GemmDeconvGrad2DKernel : public framework::OpKernel<T> {
int
K_H
=
filter
.
dims
()[
2
];
int
K_W
=
filter
.
dims
()[
3
];
int C = output
->dims()[1];
// output channels
int O_H = output->dims()[2];
int O_W = output->dims()[3];
int
C
=
output
_grad
->
dims
()[
1
];
// output channels
int
O_H
=
output
_grad
->
dims
()[
2
];
int
O_W
=
output
_grad
->
dims
()[
3
];
// Two functors required to get to the right shape
paddle
::
operators
::
math
::
Col2ImFunctor
<
paddle
::
operators
::
math
::
ColFormat
::
kCFO
,
Place
,
T
>
col2im
;
paddle
::
operators
::
math
::
Im2ColFunctor
<
paddle
::
operators
::
math
::
ColFormat
::
kCFO
,
Place
,
T
>
im2col
;
// use col_shape in the im2col and col2im calculation
framework::
DDim col_shape = {C, K_H, K_W, H, W};
DDim
col_shape
=
{
C
,
K_H
,
K_W
,
H
,
W
};
// use col_matrix_shape in the gemm calculation
framework::DDim col_matrix_shape = {M
* K_H * K_W, H * W};
DDim
col_matrix_shape
=
{
C
*
K_H
*
K_W
,
H
*
W
};
Tensor
col
;
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
...
...
@@ -179,37 +182,60 @@ class GemmDeconvGrad2DKernel : public framework::OpKernel<T> {
DDim
output_shape
=
{
C
,
O_H
,
O_W
};
DDim
input_matrix_shape
=
{
M
,
H
*
W
};
DDim filter_matrix_shape = {M, C* K_H * K_W};
DDim
filter_matrix_shape
=
{
M
,
C
*
K_H
*
K_W
};
filter
.
Resize
(
filter_matrix_shape
);
// deconvolution: gemm + col2im (similar to conv-backward on input)
output->mutable_data<T>(context.GetPlace());
auto t = framework::EigenVector<T>::Flatten(*output);
// deconvolution grad on input:
// im2col + gemm (similar to conv-forward)
// input need to compute gradient
if
(
input_grad
)
{
input_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
t
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
input_grad
);
t
.
device
(
context
.
GetEigenDevice
<
Place
>
())
=
t
.
constant
(
static_cast
<
T
>
(
0
));
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
// batch with size (M, H *
W)
Tensor input
_batch =
input->Slice<T>(i, i + 1).Resize(input_matrix
_shape);
// output size: (C, O_H, O_
W)
Tensor output
_batch =
output->Slice<T>(i, i + 1).Resize(output
_shape);
// filter size: (Co, Ci * Hf * Wf
)
// col_matrix = filter * input_batch
// of shape (C * K_H * K_W, H * W)
math::matmul<Place, T>(context.device_context(), filter, tru
e,
input_batch, false, T(1.0), &col_matrix
,
// batch with size (C, O_H * O_
W)
Tensor
output_grad
_batch
=
output_grad
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
output
_shape
);
// batch with size (M, H,
W)
Tensor
input_grad
_batch
=
input_grad
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
input_matrix
_shape
);
// im2col: (C * K_H * K_W, H * W
)
im2col
(
context
.
device_context
(),
output_grad_batch
,
col_matrix
,
strides
[
0
],
strides
[
1
],
paddings
[
0
],
paddings
[
1
]);
// gemm: dx = filter * dy
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter
,
fals
e
,
col_matrix
,
false
,
T
(
1.0
),
&
input_grad_batch
,
T
(
0.0
));
}
}
col2im(context.device_context(), output_batch, col_matrix, strides[0],
strides[1], 0, 0);
// filter gradient required
if
(
filter_grad
)
{
filter_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
Tensor
filter_grad_
=
*
filter_grad
;
filter_grad_
.
Resize
(
filter_matrix_shape
);
auto
t
=
framework
::
EigenVector
<
T
>::
Flatten
(
filter_grad_
);
t
.
device
(
context
.
GetEigenDevice
<
Place
>
())
=
t
.
constant
(
static_cast
<
T
>
(
0
));
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
// batch with size (C, O_H, O_W)
Tensor
output_grad_batch
=
output_grad
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
output_shape
);
// input batch
Tensor
in_batch
=
input
->
Slice
<
T
>
(
i
,
i
+
1
).
Resize
(
input_matrix_shape
);
// im2col: (C * K_H * K_W, H * W)
im2col
(
context
.
device_context
(),
output_grad_batch
,
col_matrix
,
strides
[
0
],
strides
[
1
],
paddings
[
0
],
paddings
[
1
]);
// gemm: d_filter = x * y_grad^T
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
in_batch
,
false
,
col_matrix
,
true
,
T
(
1.0
),
&
filter_grad
,
T
(
1.0
));
}
}
}
};
*/
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录