Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
aa2cd2ce
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
aa2cd2ce
编写于
11月 01, 2016
作者:
W
wangyang59
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor ExpandConvTransLayer to share codes with ExpandConvLayer
上级
bda259bb
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
83 addition
and
88 deletion
+83
-88
paddle/gserver/layers/ConvBaseLayer.cpp
paddle/gserver/layers/ConvBaseLayer.cpp
+7
-0
paddle/gserver/layers/ConvBaseLayer.h
paddle/gserver/layers/ConvBaseLayer.h
+67
-0
paddle/gserver/layers/ExpandConvLayer.cpp
paddle/gserver/layers/ExpandConvLayer.cpp
+0
-18
paddle/gserver/layers/ExpandConvLayer.h
paddle/gserver/layers/ExpandConvLayer.h
+1
-18
paddle/gserver/layers/ExpandConvTransLayer.cpp
paddle/gserver/layers/ExpandConvTransLayer.cpp
+4
-36
paddle/gserver/layers/ExpandConvTransLayer.h
paddle/gserver/layers/ExpandConvTransLayer.h
+4
-16
未找到文件。
paddle/gserver/layers/ConvBaseLayer.cpp
浏览文件 @
aa2cd2ce
...
@@ -21,6 +21,12 @@ bool ConvBaseLayer::init(const LayerMap& layerMap,
...
@@ -21,6 +21,12 @@ bool ConvBaseLayer::init(const LayerMap& layerMap,
/* Initialize the basic parent class */
/* Initialize the basic parent class */
Layer
::
init
(
layerMap
,
parameterMap
);
Layer
::
init
(
layerMap
,
parameterMap
);
if
(
config_
.
type
()
==
"exconv"
||
config_
.
type
()
==
"cudnn_conv"
)
{
isConv_
=
true
;
}
else
{
isConv_
=
false
;
}
/* Initialize the convolutional layer parameter */
/* Initialize the convolutional layer parameter */
numFilters_
=
config_
.
num_filters
();
numFilters_
=
config_
.
num_filters
();
sharedBiases_
=
config_
.
shared_biases
();
sharedBiases_
=
config_
.
shared_biases
();
...
@@ -88,6 +94,7 @@ size_t ConvBaseLayer::calOutputSize() {
...
@@ -88,6 +94,7 @@ size_t ConvBaseLayer::calOutputSize() {
getOutput
().
setFrameWidth
(
outputW_
[
0
]);
getOutput
().
setFrameWidth
(
outputW_
[
0
]);
layerSize
=
outputH_
[
0
]
*
outputW_
[
0
]
*
size_t
(
numFilters_
);
layerSize
=
outputH_
[
0
]
*
outputW_
[
0
]
*
size_t
(
numFilters_
);
return
layerSize
;
return
layerSize
;
}
}
}
// namespace paddle
}
// namespace paddle
paddle/gserver/layers/ConvBaseLayer.h
浏览文件 @
aa2cd2ce
...
@@ -28,6 +28,9 @@ class ConvBaseLayer : public Layer {
...
@@ -28,6 +28,9 @@ class ConvBaseLayer : public Layer {
protected:
protected:
typedef
std
::
vector
<
int
>
IntV
;
typedef
std
::
vector
<
int
>
IntV
;
/// True if it's convolution layer, false if it's deconv layer
bool
isConv_
;
/// The number of filters.
/// The number of filters.
int
numFilters_
;
int
numFilters_
;
/// The x dimension of the padding.
/// The x dimension of the padding.
...
@@ -75,6 +78,13 @@ protected:
...
@@ -75,6 +78,13 @@ protected:
/// of output size.
/// of output size.
bool
caffeMode_
;
bool
caffeMode_
;
/*The expandInput_ and transOutValue_ are used for CPU expand conv calc*/
/// Expand one sample at a time. shape:
/// (numChannels * filterPixels_, outputSizeH * outputSizeW)
MatrixPtr
expandInput_
;
/// The transpose of output, which is an auxiliary matrix.
MatrixPtr
transOutValue_
;
public:
public:
explicit
ConvBaseLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
explicit
ConvBaseLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
...
@@ -88,6 +98,63 @@ public:
...
@@ -88,6 +98,63 @@ public:
virtual
size_t
calOutputSize
();
virtual
size_t
calOutputSize
();
Weight
&
getWeight
(
int
idx
)
{
return
*
weights_
[
idx
];
}
Weight
&
getWeight
(
int
idx
)
{
return
*
weights_
[
idx
];
}
/**
* Calculate output size based on caffeMode_.
* - input(+padding): 0123456789
* - imageSize(+padding) = 10;
* - filterSize = 3;
* - stride = 2;
* - caffeMode_ is true:
- output: (012), (234), (456), (678)
- outputSize = 4;
* - caffeMode_ is false:
* - output: (012), (234), (456), (678), (9)
* - outputSize = 5;
*/
int
outputSize
(
int
imageSize
,
int
filterSize
,
int
padding
,
int
stride
)
{
int
outputSize
;
if
(
!
caffeMode_
)
{
outputSize
=
(
imageSize
-
filterSize
+
2
*
padding
+
stride
-
1
)
/
stride
+
1
;
}
else
{
outputSize
=
(
imageSize
-
filterSize
+
2
*
padding
)
/
stride
+
1
;
}
CHECK_GE
(
outputSize
,
1
);
return
outputSize
;
}
int
imageSize
(
int
outputSize
,
int
filterSize
,
int
padding
,
int
stride
)
{
int
imageSize
;
if
(
!
caffeMode_
)
{
imageSize
=
(
outputSize
-
1
)
*
stride
+
filterSize
-
2
*
padding
-
stride
+
1
;
}
else
{
imageSize
=
(
outputSize
-
1
)
*
stride
+
filterSize
-
2
*
padding
;
}
CHECK_GE
(
imageSize
,
1
);
return
imageSize
;
}
/**
* Create or resize expandInput_.
*/
void
resetExpandInput
(
size_t
height
,
size_t
width
);
/**
* Create or resize transOutValue_.
*/
void
resetConvOutput
(
size_t
batchSize
,
int
inIdx
);
/**
* Add shared bias.
*/
void
addSharedBias
();
/**
* Add unshared bias.
*/
void
addUnsharedBias
();
};
};
}
// namespace paddle
}
// namespace paddle
paddle/gserver/layers/ExpandConvLayer.cpp
浏览文件 @
aa2cd2ce
...
@@ -63,14 +63,6 @@ size_t ExpandConvLayer::getOutputSize() {
...
@@ -63,14 +63,6 @@ size_t ExpandConvLayer::getOutputSize() {
return
layerSize
;
return
layerSize
;
}
}
void
ExpandConvLayer
::
resetExpandInput
(
size_t
height
,
size_t
width
)
{
Matrix
::
resizeOrCreate
(
expandInput_
,
height
,
width
,
false
,
useGpu_
);
}
void
ExpandConvLayer
::
resetConvOutput
(
size_t
batchSize
,
int
inIdx
)
{
Matrix
::
resizeOrCreate
(
transOutValue_
,
batchSize
*
numFilters_
,
subN_
[
inIdx
],
false
,
useGpu_
);
}
void
ExpandConvLayer
::
expandOneFrame
(
MatrixPtr
image
,
size_t
startIdx
,
void
ExpandConvLayer
::
expandOneFrame
(
MatrixPtr
image
,
size_t
startIdx
,
int
inIdx
)
{
int
inIdx
)
{
...
@@ -135,17 +127,7 @@ void ExpandConvLayer::addSharedBias() {
...
@@ -135,17 +127,7 @@ void ExpandConvLayer::addSharedBias() {
transOutValue_
->
reshape
(
mapW
,
mapH
);
transOutValue_
->
reshape
(
mapW
,
mapH
);
transOutValue_
->
transpose
(
out
,
false
);
// false means no memory allocation
transOutValue_
->
transpose
(
out
,
false
);
// false means no memory allocation
out
->
clear
();
bias
->
clear
();
}
void
ExpandConvLayer
::
addUnsharedBias
()
{
MatrixPtr
outValue
=
getOutputValue
();
MatrixPtr
bias
=
Matrix
::
create
(
biases_
->
getW
()
->
getData
(),
1
,
biases_
->
getW
()
->
getElementCnt
(),
false
,
useGpu_
);
outValue
->
addBias
(
*
bias
,
1.0
f
);
}
void
ExpandConvLayer
::
forward
(
PassType
passType
)
{
void
ExpandConvLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
Layer
::
forward
(
passType
);
...
...
paddle/gserver/layers/ExpandConvLayer.h
浏览文件 @
aa2cd2ce
...
@@ -43,6 +43,7 @@ protected:
...
@@ -43,6 +43,7 @@ protected:
/// The transpose of output, which is an auxiliary matrix.
/// The transpose of output, which is an auxiliary matrix.
MatrixPtr
transOutValue_
;
MatrixPtr
transOutValue_
;
public:
public:
explicit
ExpandConvLayer
(
const
LayerConfig
&
config
)
:
ConvBaseLayer
(
config
)
{}
explicit
ExpandConvLayer
(
const
LayerConfig
&
config
)
:
ConvBaseLayer
(
config
)
{}
...
@@ -52,16 +53,6 @@ public:
...
@@ -52,16 +53,6 @@ public:
size_t
getOutputSize
();
size_t
getOutputSize
();
/**
* Create or resize expandInput_.
*/
void
resetExpandInput
(
size_t
height
,
size_t
width
);
/**
* Create or resize transOutValue_.
*/
void
resetConvOutput
(
size_t
batchSize
,
int
inIdx
);
/**
/**
* Expand one input sample.
* Expand one input sample.
*/
*/
...
@@ -72,15 +63,7 @@ public:
...
@@ -72,15 +63,7 @@ public:
*/
*/
void
expandFwdOnce
(
MatrixPtr
image
,
int
inIdx
,
int
startIdx
);
void
expandFwdOnce
(
MatrixPtr
image
,
int
inIdx
,
int
startIdx
);
/**
* Add shared bias.
*/
void
addSharedBias
();
/**
* Add unshared bias.
*/
void
addUnsharedBias
();
void
forward
(
PassType
passType
);
void
forward
(
PassType
passType
);
void
bpropSharedBias
(
MatrixPtr
biases
,
MatrixPtr
v
);
void
bpropSharedBias
(
MatrixPtr
biases
,
MatrixPtr
v
);
void
bpropBiases
(
MatrixPtr
v
);
void
bpropBiases
(
MatrixPtr
v
);
...
...
paddle/gserver/layers/ExpandConvTransLayer.cpp
浏览文件 @
aa2cd2ce
...
@@ -29,14 +29,14 @@ REGISTER_LAYER(exconvt, ExpandConvTransLayer);
...
@@ -29,14 +29,14 @@ REGISTER_LAYER(exconvt, ExpandConvTransLayer);
bool
ExpandConvTransLayer
::
init
(
const
LayerMap
&
layerMap
,
bool
ExpandConvTransLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
const
ParameterMap
&
parameterMap
)
{
/* Initialize the basic convolutional parent class */
/* Initialize the basic convolutional parent class */
Conv
Trans
BaseLayer
::
init
(
layerMap
,
parameterMap
);
ConvBaseLayer
::
init
(
layerMap
,
parameterMap
);
/* Initialize the projection */
/* Initialize the projection */
for
(
auto
&
inputConfig
:
config_
.
inputs
())
{
for
(
auto
&
inputConfig
:
config_
.
inputs
())
{
const
ConvConfig
&
conf
=
inputConfig
.
conv_conf
();
const
ConvConfig
&
conf
=
inputConfig
.
conv_conf
();
subM_
.
push_back
(
conf
.
channels
()
/
conf
.
groups
());
subM_
.
push_back
(
conf
.
channels
()
/
conf
.
groups
());
subN_
.
push_back
(
conf
.
output_x
()
*
conf
.
output_x
());
subN_
.
push_back
(
conf
.
output_x
()
*
conf
.
output_x
());
subK_
.
push_back
(
channel
_
*
conf
.
filter_size
()
*
conf
.
filter_size
()
/
subK_
.
push_back
(
numFilters
_
*
conf
.
filter_size
()
*
conf
.
filter_size
()
/
conf
.
groups
());
conf
.
groups
());
/* Consistent caffe mode for multiple input */
/* Consistent caffe mode for multiple input */
caffeMode_
=
conf
.
caffe_mode
();
caffeMode_
=
conf
.
caffe_mode
();
...
@@ -65,8 +65,8 @@ size_t ExpandConvTransLayer::getSize() {
...
@@ -65,8 +65,8 @@ size_t ExpandConvTransLayer::getSize() {
imageSize
(
outputW_
[
i
],
filterSize_
[
i
],
padding_
[
i
],
stride_
[
i
]));
imageSize
(
outputW_
[
i
],
filterSize_
[
i
],
padding_
[
i
],
stride_
[
i
]));
subN_
.
push_back
(
outputH_
[
i
]
*
outputW_
[
i
]);
subN_
.
push_back
(
outputH_
[
i
]
*
outputW_
[
i
]);
CHECK
(
layerSize
==
0
||
CHECK
(
layerSize
==
0
||
imgSizeH_
[
i
]
*
imgSizeW_
[
i
]
*
(
size_t
)
channel
_
==
layerSize
);
imgSizeH_
[
i
]
*
imgSizeW_
[
i
]
*
(
size_t
)
numFilters
_
==
layerSize
);
layerSize
=
imgSizeH_
[
i
]
*
imgSizeW_
[
i
]
*
channel
_
;
layerSize
=
imgSizeH_
[
i
]
*
imgSizeW_
[
i
]
*
numFilters
_
;
}
}
getOutput
().
setFrameHeight
(
imgSizeH_
[
0
]);
getOutput
().
setFrameHeight
(
imgSizeH_
[
0
]);
getOutput
().
setFrameWidth
(
imgSizeW_
[
0
]);
getOutput
().
setFrameWidth
(
imgSizeW_
[
0
]);
...
@@ -83,38 +83,6 @@ void ExpandConvTransLayer::resetExpandInput(size_t height, size_t width) {
...
@@ -83,38 +83,6 @@ void ExpandConvTransLayer::resetExpandInput(size_t height, size_t width) {
}*/
}*/
void
ExpandConvTransLayer
::
addSharedBias
()
{
size_t
mapW
=
getSize
()
/
channel_
;
size_t
mapH
=
getOutputValue
()
->
getElementCnt
()
/
mapW
;
MatrixPtr
out
=
Matrix
::
create
(
getOutputValue
()
->
getData
(),
mapH
,
mapW
,
false
,
useGpu_
);
Matrix
::
resizeOrCreate
(
transOutValue_
,
mapW
,
mapH
,
false
,
useGpu_
);
out
->
transpose
(
transOutValue_
,
false
);
// false means no memory allocation
transOutValue_
->
reshape
(
transOutValue_
->
getElementCnt
()
/
channel_
,
channel_
);
MatrixPtr
bias
=
Matrix
::
create
(
biases_
->
getW
()
->
getData
(),
1
,
biases_
->
getW
()
->
getElementCnt
(),
false
,
useGpu_
);
transOutValue_
->
addBias
(
*
bias
,
1.0
f
);
transOutValue_
->
reshape
(
mapW
,
mapH
);
transOutValue_
->
transpose
(
out
,
false
);
// false means no memory allocation
out
->
clear
();
bias
->
clear
();
}
void
ExpandConvTransLayer
::
addUnsharedBias
()
{
MatrixPtr
outValue
=
getOutputValue
();
MatrixPtr
bias
=
Matrix
::
create
(
biases_
->
getW
()
->
getData
(),
1
,
biases_
->
getW
()
->
getElementCnt
(),
false
,
useGpu_
);
outValue
->
addBias
(
*
bias
,
1.0
f
);
}
void
ExpandConvTransLayer
::
expandOneFrame
(
MatrixPtr
image
,
size_t
startIdx
,
void
ExpandConvTransLayer
::
expandOneFrame
(
MatrixPtr
image
,
size_t
startIdx
,
int
inIdx
)
{
int
inIdx
)
{
...
...
paddle/gserver/layers/ExpandConvTransLayer.h
浏览文件 @
aa2cd2ce
...
@@ -15,7 +15,7 @@ limitations under the License. */
...
@@ -15,7 +15,7 @@ limitations under the License. */
#pragma once
#pragma once
#include "Conv
Trans
BaseLayer.h"
#include "ConvBaseLayer.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/Matrix.h"
#include <vector>
#include <vector>
...
@@ -28,7 +28,7 @@ namespace paddle {
...
@@ -28,7 +28,7 @@ namespace paddle {
*
*
* The config file api is img_convTrans_layer.
* The config file api is img_convTrans_layer.
*/
*/
class
ExpandConvTransLayer
:
public
Conv
Trans
BaseLayer
{
class
ExpandConvTransLayer
:
public
ConvBaseLayer
{
protected:
protected:
/// For expand convolution.
/// For expand convolution.
/// subM_ = numFilters_ / groups_.
/// subM_ = numFilters_ / groups_.
...
@@ -45,15 +45,11 @@ protected:
...
@@ -45,15 +45,11 @@ protected:
IntV
outputH_
;
IntV
outputH_
;
/// The spatial dimensions of width of output feature map.
/// The spatial dimensions of width of output feature map.
IntV
outputW_
;
IntV
outputW_
;
/// Expand one sample at a time. shape:
/// (numChannels * filterPixels_, outputSizeH * outputSizeW)
MatrixPtr
expandInput_
;
/// The transpose of output, which is an auxiliary matrix.
MatrixPtr
transOutValue_
;
public:
public:
explicit
ExpandConvTransLayer
(
const
LayerConfig
&
config
)
:
explicit
ExpandConvTransLayer
(
const
LayerConfig
&
config
)
:
Conv
Trans
BaseLayer
(
config
)
{}
ConvBaseLayer
(
config
)
{}
~
ExpandConvTransLayer
()
{}
~
ExpandConvTransLayer
()
{}
...
@@ -86,15 +82,7 @@ public:
...
@@ -86,15 +82,7 @@ public:
*/
*/
void
shrinkFwd
(
MatrixPtr
output
,
int
inpIdx
);
void
shrinkFwd
(
MatrixPtr
output
,
int
inpIdx
);
/**
* Add shared bias.
*/
void
addSharedBias
();
/**
* Add unshared bias.
*/
void
addUnsharedBias
();
void
forward
(
PassType
passType
);
void
forward
(
PassType
passType
);
void
bpropSharedBias
(
MatrixPtr
biases
,
MatrixPtr
v
);
void
bpropSharedBias
(
MatrixPtr
biases
,
MatrixPtr
v
);
void
bpropBiases
(
MatrixPtr
v
);
void
bpropBiases
(
MatrixPtr
v
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录