Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
eea0097d
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看板
提交
eea0097d
编写于
3月 09, 2017
作者:
G
gaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
NormalizeLayer for SSD
上级
515543ab
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
254 addition
and
13 deletion
+254
-13
paddle/gserver/layers/NormalizeLayer.cpp
paddle/gserver/layers/NormalizeLayer.cpp
+182
-0
paddle/gserver/layers/PriorBox.cpp
paddle/gserver/layers/PriorBox.cpp
+17
-13
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+14
-0
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+10
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+31
-0
未找到文件。
paddle/gserver/layers/NormalizeLayer.cpp
0 → 100644
浏览文件 @
eea0097d
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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 "Layer.h"
#include "paddle/math/BaseMatrix.h"
#include "paddle/math/Matrix.h"
namespace
paddle
{
/**
* This layer applys normalize across the channels of each sample to a
* conv layer's output and scale the output by a group of trainable factors
* which dimensions equal to the channel's number.
* - Input: One and only one input layer are accepted. The input layer must be
* be a data output layer.
* - Output: The normalized data of the input data.
* Reference:
* Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed,
* Cheng-Yang Fu, Alexander C. Berg. SSD: Single Shot MultiBox Detector
*/
class
NormalizeLayer
:
public
Layer
{
public:
explicit
NormalizeLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
);
void
forward
(
PassType
passType
);
void
backward
(
const
UpdateCallback
&
callback
);
protected:
size_t
channels_
;
std
::
unique_ptr
<
Weight
>
scale_
;
MatrixPtr
scaleDiff_
;
MatrixPtr
normBuffer_
;
MatrixPtr
dataBuffer_
;
MatrixPtr
channelBuffer_
;
MatrixPtr
spatialBuffer_
;
MatrixPtr
sampleBuffer_
;
};
REGISTER_LAYER
(
normalize
,
NormalizeLayer
);
bool
NormalizeLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
Layer
::
init
(
layerMap
,
parameterMap
);
CHECK
(
parameters_
[
0
]);
channels_
=
config_
.
num_filters
();
scale_
.
reset
(
new
Weight
(
channels_
,
1
,
parameters_
[
0
]));
return
true
;
}
void
NormalizeLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
auto
in
=
getInput
(
0
);
MatrixPtr
inV
=
getInputValue
(
0
);
size_t
batchSize
=
inV
->
getHeight
();
size_t
dataDim
=
inV
->
getWidth
();
CHECK_EQ
(
getSize
(),
dataDim
);
reserveOutput
(
batchSize
,
dataDim
);
MatrixPtr
outV
=
getOutputValue
();
size_t
spatialDim
=
dataDim
/
channels_
;
Matrix
::
resizeOrCreate
(
dataBuffer_
,
batchSize
,
dataDim
,
false
,
useGpu_
);
Matrix
::
resizeOrCreate
(
spatialBuffer_
,
1
,
spatialDim
,
false
,
useGpu_
);
Matrix
::
resizeOrCreate
(
channelBuffer_
,
channels_
,
1
,
false
,
useGpu_
);
Matrix
::
resizeOrCreate
(
sampleBuffer_
,
channels_
,
spatialDim
,
false
,
useGpu_
);
Matrix
::
resizeOrCreate
(
normBuffer_
,
batchSize
,
spatialDim
,
false
,
useGpu_
);
normBuffer_
->
zeroMem
();
spatialBuffer_
->
zeroMem
();
sampleBuffer_
->
zeroMem
();
dataBuffer_
->
zeroMem
();
// add eps to avoid overflow
normBuffer_
->
addScalar
(
*
normBuffer_
,
1e-6
);
channelBuffer_
->
resetOne
();
inV
->
square2
(
*
dataBuffer_
);
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
spatialBuffer_
->
zeroMem
();
MatrixPtr
inTmp
=
Matrix
::
create
(
inV
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
MatrixPtr
dataTmp
=
Matrix
::
create
(
dataBuffer_
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
MatrixPtr
outTmp
=
Matrix
::
create
(
outV
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
MatrixPtr
normTmp
=
Matrix
::
create
(
normBuffer_
->
getData
()
+
i
*
spatialDim
,
1
,
spatialDim
,
false
,
useGpu_
);
// compute norm.
spatialBuffer_
->
sumCols
(
*
dataTmp
,
1
,
1
);
spatialBuffer_
->
sqrt2
(
*
spatialBuffer_
);
normTmp
->
copyFrom
(
*
spatialBuffer_
);
sampleBuffer_
->
mul
(
*
channelBuffer_
,
*
spatialBuffer_
,
1.
,
0.
);
sampleBuffer_
->
dotDiv
(
*
inTmp
,
*
sampleBuffer_
);
outTmp
->
copyFrom
(
*
sampleBuffer_
);
// scale the layer.
spatialBuffer_
->
resetOne
();
sampleBuffer_
->
mul
(
*
scale_
->
getW
(),
*
spatialBuffer_
,
1.
,
0.
);
outTmp
->
dotMul
(
*
outTmp
,
*
sampleBuffer_
);
}
}
void
NormalizeLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
MatrixPtr
inG
=
getInputGrad
(
0
);
MatrixPtr
inV
=
getInputValue
(
0
);
MatrixPtr
outG
=
getOutputGrad
();
MatrixPtr
outV
=
getOutputValue
();
auto
in
=
getInput
(
0
);
size_t
batchSize
=
inG
->
getHeight
();
size_t
dataDim
=
inG
->
getWidth
();
size_t
spatialDim
=
dataDim
/
channels_
;
bool
syncFlag
=
hl_get_sync_flag
();
dataBuffer_
->
dotMul
(
*
outG
,
*
outV
);
Matrix
::
resizeOrCreate
(
scaleDiff_
,
channels_
,
1
,
false
,
useGpu_
);
scaleDiff_
->
zeroMem
();
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
spatialBuffer_
->
zeroMem
();
channelBuffer_
->
zeroMem
();
// propagate to param.
MatrixPtr
dataBufferTmp
=
Matrix
::
create
(
dataBuffer_
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
const
MatrixPtr
inValueTmp
=
Matrix
::
create
(
inV
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
const
MatrixPtr
outGradTmp
=
Matrix
::
create
(
outG
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
MatrixPtr
inGradTmp
=
Matrix
::
create
(
inG
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
const
MatrixPtr
normTmp
=
Matrix
::
create
(
normBuffer_
->
getData
()
+
i
*
spatialDim
,
1
,
spatialDim
,
false
,
useGpu_
);
channelBuffer_
->
sumRows
(
*
dataBufferTmp
,
1
,
1
);
channelBuffer_
->
dotDiv
(
*
channelBuffer_
,
*
(
scale_
->
getW
()));
// store a / scale[i] in scaleDiff_ temporary
scaleDiff_
->
add
(
*
channelBuffer_
,
1.
);
sampleBuffer_
->
dotMul
(
*
inValueTmp
,
*
outGradTmp
);
spatialBuffer_
->
sumCols
(
*
sampleBuffer_
,
1.
,
1.
);
// scale the grad
channelBuffer_
->
resetOne
();
sampleBuffer_
->
mul
(
*
channelBuffer_
,
*
spatialBuffer_
,
1.
,
0.
);
inGradTmp
->
dotMul
(
*
inValueTmp
,
*
sampleBuffer_
);
// divide by square of norm
spatialBuffer_
->
dotMul
(
*
normTmp
,
*
normTmp
);
sampleBuffer_
->
mul
(
*
channelBuffer_
,
*
spatialBuffer_
,
1.
,
0.
);
inGradTmp
->
dotDiv
(
*
inGradTmp
,
*
sampleBuffer_
);
// subtract
inGradTmp
->
add
(
*
outGradTmp
,
-
1
,
1
);
// divide by norm
sampleBuffer_
->
mul
(
*
channelBuffer_
,
*
normTmp
,
1.
,
0.
);
inGradTmp
->
dotDiv
(
*
inGradTmp
,
*
sampleBuffer_
);
// scale the diff
spatialBuffer_
->
resetOne
();
sampleBuffer_
->
mul
(
*
scale_
->
getW
(),
*
spatialBuffer_
,
1.
,
0.
);
inGradTmp
->
dotMul
(
*
inGradTmp
,
*
sampleBuffer_
);
}
// updata scale
if
(
scale_
->
getWGrad
())
scale_
->
getWGrad
()
->
copyFrom
(
*
scaleDiff_
);
hl_set_sync_flag
(
false
);
hl_set_sync_flag
(
syncFlag
);
scale_
->
getParameterPtr
()
->
incUpdate
(
callback
);
}
}
// namespace paddle
paddle/gserver/layers/PriorBox.cpp
浏览文件 @
eea0097d
...
...
@@ -45,27 +45,32 @@ protected:
MatrixPtr
buffer_
;
};
REGISTER_LAYER
(
priorbox
,
PriorBoxLayer
);
bool
PriorBoxLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
Layer
::
init
(
layerMap
,
parameterMap
);
auto
pbConf
=
config_
.
inputs
(
0
).
priorbox_conf
();
std
::
vector
<
real
>
tmp
;
aspectRatio_
.
push_back
(
1.
);
std
::
copy
(
pbConf
.
min_size
().
begin
(),
pbConf
.
min_size
().
end
(),
std
::
back_inserter
(
minSize_
));
std
::
copy
(
pbConf
.
max_size
().
begin
(),
pbConf
.
max_size
().
end
(),
std
::
back_inserter
(
maxSize_
));
std
::
copy
(
pbConf
.
aspect_ratio
().
begin
(),
pbConf
.
aspect_ratio
().
end
(),
std
::
back_inserter
(
aspectRatio_
));
std
::
copy
(
pbConf
.
variance
().
begin
(),
pbConf
.
variance
().
end
(),
std
::
back_inserter
(
variance_
));
std
::
copy
(
pbConf
.
aspect_ratio
().
begin
(),
pbConf
.
aspect_ratio
().
end
(),
std
::
back_inserter
(
tmp
));
// flip
int
inputRatioLength
=
aspectRatio_
.
size
();
for
(
int
index
=
0
;
index
<
inputRatioLength
;
index
++
)
aspectRatio_
.
push_back
(
1
/
aspectRatio_
[
index
]);
aspectRatio_
.
push_back
(
1.
);
int
inputRatioLength
=
tmp
.
size
();
for
(
int
index
=
0
;
index
<
inputRatioLength
;
index
++
)
{
aspectRatio_
.
push_back
(
tmp
[
index
]);
aspectRatio_
.
push_back
(
1
/
tmp
[
index
]);
}
numPriors_
=
aspectRatio_
.
size
();
if
(
maxSize_
.
size
()
>
0
)
numPriors_
++
;
return
true
;
...
...
@@ -94,12 +99,12 @@ void PriorBoxLayer::forward(PassType passType) {
for
(
int
w
=
0
;
w
<
layerWidth
;
++
w
)
{
real
centerX
=
(
w
+
0.5
)
*
stepW
;
real
centerY
=
(
h
+
0.5
)
*
stepH
;
int
minSize
=
0
;
real
minSize
=
0
;
for
(
size_t
s
=
0
;
s
<
minSize_
.
size
();
s
++
)
{
// first prior.
minSize
=
minSize_
[
s
];
int
boxWidth
=
minSize
;
int
boxHeight
=
minSize
;
real
boxWidth
=
minSize
;
real
boxHeight
=
minSize
;
// xmin, ymin, xmax, ymax.
tmpPtr
[
idx
++
]
=
(
centerX
-
boxWidth
/
2.
)
/
imageWidth
;
tmpPtr
[
idx
++
]
=
(
centerY
-
boxHeight
/
2.
)
/
imageHeight
;
...
...
@@ -112,7 +117,7 @@ void PriorBoxLayer::forward(PassType passType) {
CHECK_EQ
(
minSize_
.
size
(),
maxSize_
.
size
());
// second prior.
for
(
size_t
s
=
0
;
s
<
maxSize_
.
size
();
s
++
)
{
int
maxSize
=
maxSize_
[
s
];
real
maxSize
=
maxSize_
[
s
];
boxWidth
=
boxHeight
=
sqrt
(
minSize
*
maxSize
);
tmpPtr
[
idx
++
]
=
(
centerX
-
boxWidth
/
2.
)
/
imageWidth
;
tmpPtr
[
idx
++
]
=
(
centerY
-
boxHeight
/
2.
)
/
imageHeight
;
...
...
@@ -145,6 +150,5 @@ void PriorBoxLayer::forward(PassType passType) {
MatrixPtr
outV
=
getOutputValue
();
outV
->
copyFrom
(
buffer_
->
data_
,
dim
*
2
);
}
REGISTER_LAYER
(
priorbox
,
PriorBoxLayer
);
}
// namespace paddle
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
eea0097d
...
...
@@ -1623,6 +1623,20 @@ TEST(Layer, PadLayer) {
}
}
TEST
(
Layer
,
NormalizeLayer
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"normalize"
);
config
.
layerConfig
.
set_size
(
100
);
config
.
layerConfig
.
set_num_filters
(
10
);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
100
,
10
});
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"normalize"
,
10
,
false
,
useGpu
,
false
,
5
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
testing
::
InitGoogleTest
(
&
argc
,
argv
);
initMain
(
argc
,
argv
);
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
eea0097d
...
...
@@ -1619,6 +1619,16 @@ class PriorBoxLayer(LayerBase):
self
.
config
.
size
=
size
@
config_layer
(
'normalize'
)
class
NormalizeLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
size
,
num_filters
,
**
xargs
):
super
(
NormalizeLayer
,
self
).
__init__
(
name
,
'normalize'
,
0
,
inputs
,
**
xargs
)
self
.
config
.
size
=
size
self
.
config
.
num_filters
=
num_filters
self
.
create_input_parameter
(
0
,
num_filters
,
[
num_filters
,
1
])
@
config_layer
(
'data'
)
class
DataLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
size
,
height
=
None
,
width
=
None
,
device
=
None
):
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
eea0097d
...
...
@@ -111,6 +111,7 @@ __all__ = [
'out_prod_layer'
,
'print_layer'
,
'priorbox_layer'
,
'normalize_layer'
,
'spp_layer'
,
'pad_layer'
,
'eos_layer'
,
...
...
@@ -184,6 +185,7 @@ class LayerType(object):
PRINT_LAYER
=
"print"
PRIORBOX_LAYER
=
"priorbox"
NORMALIZE_LAYER
=
"normalize"
CTC_LAYER
=
"ctc"
WARP_CTC_LAYER
=
"warp_ctc"
...
...
@@ -998,6 +1000,35 @@ def priorbox_layer(input,
size
=
size
)
@
wrap_name_default
(
"normalize"
)
def
normalize_layer
(
input
,
name
=
None
,
param_attr
=
None
):
"""
Normalize a layer's output. This layer is necessary for ssd.
This layer applys normalize across the channels of each sample to
a conv layer's output and scale the output by a group of trainable
factors which dimensions equal to the channel's number.
:param name: The Layer Name.
:type name: basestring
:param input: The input layer.
:type input: LayerOutput
:param param_attr: The Parameter Attribute|list.
:type param_attr: ParameterAttribute
:return: LayerOutput
"""
Layer
(
name
=
name
,
type
=
LayerType
.
NORMALIZE_LAYER
,
inputs
=
[
Input
(
input
.
name
,
**
param_attr
.
attr
)],
size
=
input
.
size
,
num_filters
=
input
.
num_filters
)
return
LayerOutput
(
name
,
LayerType
.
NORMALIZE_LAYER
,
parents
=
input
,
num_filters
=
input
.
num_filters
,
size
=
input
.
size
)
@
wrap_name_default
(
"seq_pooling"
)
@
wrap_bias_attr_default
(
has_bias
=
False
)
@
wrap_param_default
([
'pooling_type'
],
default_factory
=
lambda
_
:
MaxPooling
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录