Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
07cde439
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
07cde439
编写于
6月 21, 2017
作者:
H
hedaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reconstruction of GemmConv Based on new im2col.
上级
eb0c7e5e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
137 deletion
+48
-137
paddle/function/GemmConvOp.cpp
paddle/function/GemmConvOp.cpp
+48
-137
未找到文件。
paddle/function/GemmConvOp.cpp
浏览文件 @
07cde439
...
...
@@ -12,101 +12,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "
Gemm
ConvOp.h"
#include "ConvOp.h"
#include "GemmFunctor.h"
#include "Im2Col.h"
#include "paddle/math/MemoryHandle.h"
namespace
paddle
{
/*
* imData = [input_channels, input_height, input_width]
* colData = [input_channels, filter_height, filter_width,
* output_height, output_width]
*/
template
<
class
T
>
class
Im2ColFunctor
<
DEVICE_TYPE_CPU
,
T
>
{
public:
void
operator
()(
const
T
*
imData
,
int
inputChannels
,
int
inputHeight
,
int
inputWidth
,
int
filterHeight
,
int
filterWidth
,
int
strideHeight
,
int
strideWidth
,
int
paddingHeight
,
int
paddingWidth
,
int
outputHeight
,
int
outputWidth
,
T
*
colData
)
{
int
channelsCol
=
inputChannels
*
filterHeight
*
filterWidth
;
for
(
int
c
=
0
;
c
<
channelsCol
;
++
c
)
{
int
wOffset
=
c
%
filterWidth
;
int
hOffset
=
(
c
/
filterWidth
)
%
filterHeight
;
int
c_im
=
c
/
filterWidth
/
filterHeight
;
for
(
int
h
=
0
;
h
<
outputHeight
;
++
h
)
{
for
(
int
w
=
0
;
w
<
outputWidth
;
++
w
)
{
int
imRowIdx
=
h
*
strideHeight
+
hOffset
;
int
imColIdx
=
w
*
strideWidth
+
wOffset
;
if
((
imRowIdx
-
paddingHeight
)
<
0
||
(
imRowIdx
-
paddingHeight
)
>=
inputHeight
||
(
imColIdx
-
paddingWidth
)
<
0
||
(
imColIdx
-
paddingWidth
)
>=
inputWidth
)
{
colData
[(
c
*
outputHeight
+
h
)
*
outputWidth
+
w
]
=
T
(
0
);
}
else
{
imRowIdx
+=
c_im
*
inputHeight
-
paddingHeight
;
imColIdx
-=
paddingWidth
;
colData
[(
c
*
outputHeight
+
h
)
*
outputWidth
+
w
]
=
imData
[
imRowIdx
*
inputWidth
+
imColIdx
];
}
}
}
}
}
};
template
<
class
T
>
class
Col2ImFunctor
<
DEVICE_TYPE_CPU
,
T
>
{
public:
void
operator
()(
const
T
*
colData
,
int
inputChannels
,
int
inputHeight
,
int
inputWidth
,
int
filterHeight
,
int
filterWidth
,
int
strideHeight
,
int
strideWidth
,
int
paddingHeight
,
int
paddingWidth
,
int
outputHeight
,
int
outputWidth
,
T
*
imData
)
{
int
channelsCol
=
inputChannels
*
filterHeight
*
filterWidth
;
for
(
int
c
=
0
;
c
<
channelsCol
;
++
c
)
{
int
wOffset
=
c
%
filterWidth
;
int
hOffset
=
(
c
/
filterWidth
)
%
filterHeight
;
int
c_im
=
c
/
filterWidth
/
filterHeight
;
for
(
int
h
=
0
;
h
<
outputHeight
;
++
h
)
{
for
(
int
w
=
0
;
w
<
outputWidth
;
++
w
)
{
int
imRowIdx
=
h
*
strideHeight
+
hOffset
;
int
imColIdx
=
w
*
strideWidth
+
wOffset
;
if
((
imRowIdx
-
paddingHeight
)
>=
0
&&
(
imRowIdx
-
paddingHeight
)
<
inputHeight
&&
(
imColIdx
-
paddingWidth
)
>=
0
&&
(
imColIdx
-
paddingWidth
)
<
inputWidth
)
{
imRowIdx
+=
c_im
*
inputHeight
-
paddingHeight
;
imColIdx
-=
paddingWidth
;
imData
[
imRowIdx
*
inputWidth
+
imColIdx
]
+=
colData
[(
c
*
outputHeight
+
h
)
*
outputWidth
+
w
];
}
}
}
}
}
};
/*
* \brief Forward calculation of convolution.
*/
...
...
@@ -155,15 +67,20 @@ public:
real
*
inputData
=
inputs
[
0
].
data
<
real
>
();
real
*
filterData
=
inputs
[
1
].
data
<
real
>
();
real
*
outputData
=
outputs
[
0
].
data
<
real
>
();
size_t
size
=
inputChannels
/
groups_
*
filterHeight
*
filterWidth
*
outputHeight
*
outputWidth
;
resizeBuffer
<
Device
>
(
size
);
TensorShape
imShape
=
TensorShape
({
inputChannels
/
groups_
,
inputHeight
,
inputWidth
});
TensorShape
colShape
=
TensorShape
({
inputChannels
/
groups_
,
filterHeight
,
filterWidth
,
outputHeight
,
outputWidth
});
resizeBuffer
<
Device
>
(
colShape
.
getElements
());
real
*
colData
=
reinterpret_cast
<
real
*>
(
memory_
->
getBuf
());
Im2ColFunctor
<
Device
,
real
>
im2col
;
Im2ColFunctor
<
kCFO
,
Device
,
real
>
im2col
;
GemmFunctor
<
Device
,
real
>
gemm
;
size_t
inputOffset
=
(
inputChannels
/
groups_
)
*
inputHeight
*
inputWidth
;
size_t
inputOffset
=
imShape
.
getElements
()
;
size_t
outputOffset
=
(
outputChannels
/
groups_
)
*
outputHeight
*
outputWidth
;
size_t
filterOffset
=
filter
.
getElements
()
/
groups_
;
...
...
@@ -171,18 +88,13 @@ public:
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
for
(
size_t
g
=
0
;
g
<
groups_
;
g
++
)
{
im2col
(
inputData
+
g
*
inputOffset
,
inputChannels
/
groups_
,
inputHeight
,
inputWidth
,
filterHeight
,
filterWidth
,
imShape
,
colData
,
colShape
,
strideH
(),
strideW
(),
paddingH
(),
paddingW
(),
outputHeight
,
outputWidth
,
colData
);
paddingW
());
int
M
=
outputChannels
/
groups_
;
int
N
=
outputHeight
*
outputWidth
;
...
...
@@ -249,15 +161,20 @@ public:
real
*
outputGrad
=
inputs
[
0
].
data
<
real
>
();
real
*
filterData
=
inputs
[
1
].
data
<
real
>
();
real
*
inputGrad
=
outputs
[
0
].
data
<
real
>
();
size_t
size
=
inputChannels
/
groups_
*
filterHeight
*
filterWidth
*
outputHeight
*
outputWidth
;
resizeBuffer
<
Device
>
(
size
);
TensorShape
imShape
=
TensorShape
({
inputChannels
/
groups_
,
inputHeight
,
inputWidth
});
TensorShape
colShape
=
TensorShape
({
inputChannels
/
groups_
,
filterHeight
,
filterWidth
,
outputHeight
,
outputWidth
});
resizeBuffer
<
Device
>
(
colShape
.
getElements
());
real
*
colData
=
reinterpret_cast
<
real
*>
(
memory_
->
getBuf
());
Col2ImFunctor
<
Device
,
real
>
col2im
;
Col2ImFunctor
<
kCFO
,
Device
,
real
>
col2im
;
GemmFunctor
<
Device
,
real
>
gemm
;
size_t
inputOffset
=
(
inputChannels
/
groups_
)
*
inputHeight
*
inputWidth
;
size_t
inputOffset
=
imShape
.
getElements
()
;
size_t
outputOffset
=
(
outputChannels
/
groups_
)
*
outputHeight
*
outputWidth
;
size_t
filterOffset
=
filter
.
getElements
()
/
groups_
;
...
...
@@ -280,20 +197,14 @@ public:
0.0
f
,
colData
,
N
);
col2im
(
colData
,
inputChannels
/
groups_
,
inputHeight
,
inputWidth
,
filterHeight
,
filterWidth
,
col2im
(
inputGrad
+
g
*
inputOffset
,
imShape
,
colData
,
colShape
,
strideH
(),
strideW
(),
paddingH
(),
paddingW
(),
outputHeight
,
outputWidth
,
inputGrad
+
g
*
inputOffset
);
paddingW
());
}
inputGrad
+=
inputChannels
*
inputHeight
*
inputWidth
;
outputGrad
+=
outputChannels
*
outputHeight
*
outputWidth
;
...
...
@@ -347,33 +258,33 @@ public:
real
*
outputGrad
=
inputs
[
0
].
data
<
real
>
();
real
*
inputData
=
inputs
[
1
].
data
<
real
>
();
real
*
filterGrad
=
outputs
[
0
].
data
<
real
>
();
size_t
size
=
inputChannels
/
groups_
*
filterHeight
*
filterWidth
*
outputHeight
*
outputWidth
;
resizeBuffer
<
Device
>
(
size
);
TensorShape
imShape
=
TensorShape
({
inputChannels
/
groups_
,
inputHeight
,
inputWidth
});
TensorShape
colShape
=
TensorShape
({
inputChannels
/
groups_
,
filterHeight
,
filterWidth
,
outputHeight
,
outputWidth
});
resizeBuffer
<
Device
>
(
colShape
.
getElements
());
real
*
colData
=
reinterpret_cast
<
real
*>
(
memory_
->
getBuf
());
Im2ColFunctor
<
Device
,
real
>
im2col
;
Im2ColFunctor
<
kCFO
,
Device
,
real
>
im2col
;
GemmFunctor
<
Device
,
real
>
gemm
;
size_t
inputOffset
=
(
inputChannels
/
groups_
)
*
inputHeight
*
inputWidth
;
size_t
inputOffset
=
imShape
.
getElements
()
;
size_t
outputOffset
=
(
outputChannels
/
groups_
)
*
outputHeight
*
outputWidth
;
size_t
filterOffset
=
filter
.
getElements
()
/
groups_
;
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
for
(
size_t
g
=
0
;
g
<
groups_
;
g
++
)
{
im2col
(
inputData
+
g
*
inputOffset
,
inputChannels
/
groups_
,
inputHeight
,
inputWidth
,
filterHeight
,
filterWidth
,
imShape
,
colData
,
colShape
,
strideH
(),
strideW
(),
paddingH
(),
paddingW
(),
outputHeight
,
outputWidth
,
colData
);
paddingW
());
int
M
=
outputChannels
/
groups_
;
int
K
=
outputHeight
*
outputWidth
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录