Skip to content

Commit e2c0713

Browse files
committed
follow comments
1 parent db569f2 commit e2c0713

File tree

6 files changed

+81
-78
lines changed

6 files changed

+81
-78
lines changed

paddle/cuda/include/hl_cnn.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extern void hl_expand_feature2col(
9191
* @param[in] paddingH padding height.
9292
* @param[in] paddingW padding width.
9393
* @param[out] tgtData output data.
94-
* @param[in] tgtStride output data stride.
94+
* @param[in] tgtStride stride between output data samples.
9595
*
9696
*/
9797
extern void hl_maxpool_forward(
@@ -125,7 +125,7 @@ extern void hl_maxpool_forward(
125125
* @param[in] paddingH padding height.
126126
* @param[in] paddingW padding width.
127127
* @param[out] targetGrad output grad.
128-
* @param[in] outStride output grad data stride.
128+
* @param[in] outStride stride between output data samples.
129129
*
130130
*/
131131
extern void hl_maxpool_backward(
@@ -157,7 +157,7 @@ extern void hl_maxpool_backward(
157157
* @param[in] paddingH padding height.
158158
* @param[in] paddingW padding width.
159159
* @param[out] tgtData output data.
160-
* @param[in] tgtStride output data stride.
160+
* @param[in] tgtStride stride between output data samples.
161161
*
162162
*/
163163
extern void hl_avgpool_forward(
@@ -189,7 +189,7 @@ extern void hl_avgpool_forward(
189189
* @param[in] scaleA scale.
190190
* @param[in] scaleB scale.
191191
* @param[out] backGrad output grad.
192-
* @param[in] outStride output grad data stride.
192+
* @param[in] outStride stride between output data samples.
193193
*
194194
*/
195195
extern void hl_avgpool_backward(

paddle/gserver/layers/PoolProjection.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ PoolProjection* PoolProjection::create(const ProjectionConfig& config,
3434
void MaxPoolProjection::forward() {
3535
MatrixPtr inputV = in_->value;
3636
MatrixPtr outV = out_->value;
37-
outV->maxPoolForward(*inputV, imgSizeY_, imgSize_, channels_,
38-
sizeX_, sizeY_, strideY_, stride_,
39-
outputY_, outputX_, confPaddingY_, confPadding_);
37+
outV->maxPoolForward(*inputV, imgSizeY_, imgSize_, channels_, sizeX_, sizeY_,
38+
strideY_, stride_, outputY_, outputX_, confPaddingY_,
39+
confPadding_);
4040
}
4141

4242
void MaxPoolProjection::backward(const UpdateCallback& callback) {
@@ -50,17 +50,16 @@ void MaxPoolProjection::backward(const UpdateCallback& callback) {
5050
return;
5151
}
5252
inputGrad->maxPoolBackward(*inputV, imgSizeY_, imgSize_, *outGrad, *outV,
53-
sizeX_, sizeY_,
54-
strideY_, stride_, outputY_, outputX_, 1, 1,
55-
confPaddingY_, confPadding_);
53+
sizeX_, sizeY_, strideY_, stride_, outputY_,
54+
outputX_, 1, 1, confPaddingY_, confPadding_);
5655
}
5756

5857
void AvgPoolProjection::forward() {
5958
MatrixPtr inputV = in_->value;
6059
MatrixPtr outV = out_->value;
61-
outV->avgPoolForward(*inputV, imgSizeY_, imgSize_, channels_,
62-
sizeX_, sizeY_, strideY_, stride_,
63-
outputY_, outputX_, confPaddingY_, confPadding_);
60+
outV->avgPoolForward(*inputV, imgSizeY_, imgSize_, channels_, sizeX_, sizeY_,
61+
strideY_, stride_, outputY_, outputX_, confPaddingY_,
62+
confPadding_);
6463
}
6564

6665
void AvgPoolProjection::backward(const UpdateCallback& callback) {
@@ -73,9 +72,8 @@ void AvgPoolProjection::backward(const UpdateCallback& callback) {
7372
return;
7473
}
7574

76-
inputGrad->avgPoolBackward(*outputGrad, imgSizeY_, imgSize_,
77-
sizeX_, sizeY_, strideY_, stride_,
78-
outputY_, outputX_, 1, 1,
75+
inputGrad->avgPoolBackward(*outputGrad, imgSizeY_, imgSize_, sizeX_, sizeY_,
76+
strideY_, stride_, outputY_, outputX_, 1, 1,
7977
confPaddingY_, confPadding_);
8078
}
8179
} // namespace paddle

paddle/gserver/layers/PoolProjection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License. */
1515
#pragma once
1616

1717
#include "Projection.h"
18+
#include "paddle/math/MathUtils.h"
1819

1920
namespace paddle {
2021

paddle/gserver/layers/SpatialPyramidPoolLayer.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,15 @@ ProjectionConfig SpatialPyramidPoolLayer::getConfig(size_t imgSizeW,
5656
size_t SpatialPyramidPoolLayer::getSize() {
5757
CHECK_EQ(inputLayers_.size(), 1UL);
5858
size_t layerSize = 0;
59+
const SppConfig& sppConf = config_.inputs(0).spp_conf();
5960
imgSizeH_ = inputLayers_[0]->getOutput().getFrameHeight();
6061
imgSizeW_ = inputLayers_[0]->getOutput().getFrameWidth();
62+
if (imgSizeH_ == 0) {
63+
imgSizeH_ = sppConf.has_img_size_y() ? sppConf.img_size_y() : imgSizeW_;
64+
}
65+
if (imgSizeW_ == 0) {
66+
imgSizeW_ = sppConf.img_size();
67+
}
6168

6269
size_t outputH = 1;
6370
size_t outputW = (std::pow(4, pyramidHeight_) - 1) / (4 - 1);
@@ -66,10 +73,10 @@ size_t SpatialPyramidPoolLayer::getSize() {
6673

6774
getOutput().setFrameHeight(outputH);
6875
getOutput().setFrameWidth(outputW);
76+
6977
return layerSize;
7078
}
7179

72-
7380
bool SpatialPyramidPoolLayer::init(const LayerMap& layerMap,
7481
const ParameterMap& parameterMap) {
7582
Layer::init(layerMap, parameterMap);
@@ -90,8 +97,8 @@ bool SpatialPyramidPoolLayer::init(const LayerMap& layerMap,
9097
size_t endCol = 0;
9198
for (size_t i = 0; i < pyramidHeight_; i++) {
9299
poolProjections_.emplace_back(PoolProjection::create(
93-
getConfig(imgSizeW_, imgSizeH_, channels_, i, poolType_),
94-
nullptr, useGpu_));
100+
getConfig(imgSizeW_, imgSizeH_, channels_, i, poolType_), nullptr,
101+
useGpu_));
95102
endCol += poolProjections_[i]->getOutputSize();
96103
projCol_.push_back(std::make_pair(startCol, endCol));
97104
startCol = endCol;
@@ -125,4 +132,3 @@ void SpatialPyramidPoolLayer::backward(const UpdateCallback& callback) {
125132
}
126133

127134
} // namespace paddle
128-

python/paddle/trainer_config_helpers/layers.py

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
'rank_cost', 'lambda_cost', 'huber_cost',
5757
'block_expand_layer',
5858
'maxout_layer', 'out_prod_layer', 'print_layer',
59-
# 'spp_layer',
59+
'spp_layer',
6060
]
6161

6262

@@ -112,7 +112,7 @@ class LayerType(object):
112112
LINEAR_COMBINATION_LAYER = "convex_comb"
113113
BLOCK_EXPAND = "blockexpand"
114114
MAXOUT = "maxout"
115-
# SPP_LAYER = "spp"
115+
SPP_LAYER = "spp"
116116

117117
PRINT_LAYER = "print"
118118

@@ -1711,60 +1711,60 @@ def img_pool_layer(input, pool_size, name=None,
17111711
num_filters=num_channels)
17121712

17131713

1714-
# @wrap_name_default("spp")
1715-
# @layer_support()
1716-
# def spp_layer(input, name=None, num_channels=None, pool_type=None,
1717-
# pyramid_height=None, img_width=None, layer_attr=None):
1718-
# pass
1719-
# """
1720-
# Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition.
1721-
# The details please refer to
1722-
# `Kaiming He's paper <https://arxiv.org/abs/1406.4729>`_.
1723-
1724-
# :param name: layer name.
1725-
# :type name: basestring
1726-
# :param input: layer's input.
1727-
# :type input: LayerOutput
1728-
# :param num_channels: number of input channel.
1729-
# :type num_channels: int
1730-
# :param pool_type: Pooling type. MaxPooling or AveragePooling. Default is MaxPooling.
1731-
# :type scale: BasePoolingType
1732-
# :param pyramid_height: pyramid height.
1733-
# :type pyramid_height: int
1734-
# :param img_width: the width of input feature map. If it is None, the input feature
1735-
# map should be square.
1736-
# :type img_width: int|None
1737-
# :param layer_attr: Extra Layer Attribute.
1738-
# :type layer_attr: ExtraLayerAttribute
1739-
# :return: LayerOutput object.
1740-
# :rtype: LayerOutput
1741-
# """
1742-
# if num_channels is None:
1743-
# assert input.num_filters is not None
1744-
# num_channels = input.num_filters
1745-
1746-
# if pool_type is None:
1747-
# pool_type = MaxPooling()
1748-
# elif isinstance(pool_type, AvgPooling):
1749-
# pool_type.name = 'avg'
1750-
1751-
# type_name = pool_type.name
1752-
# if (isinstance(pool_type, AvgPooling) or isinstance(pool_type, MaxPooling)):
1753-
# type_name += '-projection'
1754-
1755-
# Layer(
1756-
# name=name,
1757-
# type=LayerType.SPP_LAYER,
1758-
# inputs=Input(input.name,
1759-
# spp=SpatialPyramidPool(pool_type=type_name,
1760-
# channels=num_channels,
1761-
# pyramid_height=pyramid_height,
1762-
# img_width=img_width)
1763-
# ),
1764-
# **ExtraLayerAttribute.to_kwargs(layer_attr)
1765-
# )
1766-
# return LayerOutput(name, LayerType.SPP_LAYER, parents=[input],
1767-
# num_filters=num_channels)
1714+
@wrap_name_default("spp")
1715+
@layer_support()
1716+
def spp_layer(input, name=None, num_channels=None, pool_type=None,
1717+
pyramid_height=None, img_width=None, layer_attr=None):
1718+
pass
1719+
"""
1720+
Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition.
1721+
The details please refer to
1722+
`Kaiming He's paper <https://arxiv.org/abs/1406.4729>`_.
1723+
1724+
:param name: layer name.
1725+
:type name: basestring
1726+
:param input: layer's input.
1727+
:type input: LayerOutput
1728+
:param num_channels: number of input channel.
1729+
:type num_channels: int
1730+
:param pool_type: Pooling type. MaxPooling or AveragePooling. Default is MaxPooling.
1731+
:type scale: BasePoolingType
1732+
:param pyramid_height: pyramid height.
1733+
:type pyramid_height: int
1734+
:param img_width: the width of input feature map. If it is None, the input feature
1735+
map should be square.
1736+
:type img_width: int|None
1737+
:param layer_attr: Extra Layer Attribute.
1738+
:type layer_attr: ExtraLayerAttribute
1739+
:return: LayerOutput object.
1740+
:rtype: LayerOutput
1741+
"""
1742+
if num_channels is None:
1743+
assert input.num_filters is not None
1744+
num_channels = input.num_filters
1745+
1746+
if pool_type is None:
1747+
pool_type = MaxPooling()
1748+
elif isinstance(pool_type, AvgPooling):
1749+
pool_type.name = 'avg'
1750+
1751+
type_name = pool_type.name
1752+
if (isinstance(pool_type, AvgPooling) or isinstance(pool_type, MaxPooling)):
1753+
type_name += '-projection'
1754+
1755+
Layer(
1756+
name=name,
1757+
type=LayerType.SPP_LAYER,
1758+
inputs=Input(input.name,
1759+
spp=SpatialPyramidPool(pool_type=type_name,
1760+
channels=num_channels,
1761+
pyramid_height=pyramid_height,
1762+
img_width=img_width)
1763+
),
1764+
**ExtraLayerAttribute.to_kwargs(layer_attr)
1765+
)
1766+
return LayerOutput(name, LayerType.SPP_LAYER, parents=[input],
1767+
num_filters=num_channels)
17681768

17691769

17701770
def __img_norm_layer__(name, input, size, norm_type, scale, power,

python/paddle/trainer_config_helpers/tests/configs/generate_protostr.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ test_sequence_pooling test_lstmemory_layer test_grumemory_layer
1111
last_first_seq test_expand_layer test_ntm_layers test_hsigmoid
1212
img_layers util_layers simple_rnn_layers unused_layers test_cost_layers
1313
test_rnn_group shared_fc shared_lstm test_cost_layers_with_weight
14-
# test_maxout test_bi_grumemory math_ops test_spp_layer)
1514
test_maxout test_bi_grumemory math_ops test_spp_layer)
1615

1716

18-
1917
for conf in ${configs[*]}
2018
do
2119
echo "Generating " $conf

0 commit comments

Comments
 (0)