Skip to content

Commit eaf3dec

Browse files
committed
follow comments
1 parent 70e0468 commit eaf3dec

File tree

6 files changed

+98
-47
lines changed

6 files changed

+98
-47
lines changed

paddle/gserver/layers/PoolProjection.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,46 @@ namespace paddle {
1818

1919
REGISTER_PROJECTION_CREATE_FUNC(pool, &PoolProjection::create);
2020

21+
PoolProjection::PoolProjection(const ProjectionConfig& config,
22+
ParameterPtr parameter, bool useGpu)
23+
: Projection(config, parameter, useGpu) {
24+
const PoolConfig& conf = config_.pool_conf();
25+
poolType_ = conf.pool_type();
26+
channels_ = conf.channels();
27+
sizeX_ = conf.size_x();
28+
stride_ = conf.stride();
29+
outputX_ = conf.output_x();
30+
imgSize_ = conf.img_size();
31+
confPadding_ = conf.padding();
32+
33+
sizeY_ = conf.has_size_y() ? conf.size_y() : conf.size_x();
34+
imgSizeY_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
35+
strideY_ = conf.has_stride_y() ? conf.stride_y() : conf.stride();
36+
confPaddingY_ = conf.has_padding_y() ? conf.padding_y() : conf.padding();
37+
outputY_ = conf.has_output_y() ? conf.output_y() : conf.output_x();
38+
}
39+
40+
size_t PoolProjection::getSize() {
41+
imgSizeY_ = in_->getFrameHeight();
42+
imgSize_ = in_->getFrameWidth();
43+
const PoolConfig& conf = config_.pool_conf();
44+
if (imgSizeY_ == 0) {
45+
imgSizeY_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
46+
}
47+
if (imgSize_ == 0) {
48+
imgSize_ = conf.img_size();
49+
}
50+
outputY_ = outputSize(imgSizeY_, sizeY_, confPaddingY_, strideY_,
51+
/* caffeMode */ false);
52+
outputX_ = outputSize(imgSize_, sizeX_, confPadding_, stride_,
53+
/* caffeMode */ false);
54+
55+
const_cast<Argument*>(out_)->setFrameHeight(outputY_);
56+
const_cast<Argument*>(out_)->setFrameWidth(outputX_);
57+
58+
return outputY_ * outputX_ * channels_;
59+
}
60+
2161
PoolProjection* PoolProjection::create(const ProjectionConfig& config,
2262
ParameterPtr parameter, bool useGpu) {
2363
const std::string& pool = config.pool_conf().pool_type();

paddle/gserver/layers/PoolProjection.h

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,22 @@ class PoolProjection : public Projection {
3131

3232
public:
3333
PoolProjection(const ProjectionConfig& config, ParameterPtr parameter,
34-
bool useGpu)
35-
: Projection(config, parameter, useGpu) {
36-
const PoolConfig& conf = config_.pool_conf();
37-
poolType_ = conf.pool_type();
38-
channels_ = conf.channels();
39-
sizeX_ = conf.size_x();
40-
stride_ = conf.stride();
41-
outputX_ = conf.output_x();
42-
imgSize_ = conf.img_size();
43-
confPadding_ = conf.padding();
34+
bool useGpu);
4435

45-
sizeY_ = conf.has_size_y() ? conf.size_y() : conf.size_x();
46-
imgSizeY_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
47-
strideY_ = conf.has_stride_y() ? conf.stride_y() : conf.stride();
48-
confPaddingY_ = conf.has_padding_y() ? conf.padding_y() : conf.padding();
49-
outputY_ = conf.has_output_y() ? conf.output_y() : conf.output_x();
50-
}
5136
static PoolProjection* create(const ProjectionConfig& config,
5237
ParameterPtr parameter, bool useGpu);
53-
const std::string& getPoolType() const { return poolType_; }
54-
size_t getSize() {
55-
imgSizeY_ = in_->getFrameHeight();
56-
imgSize_ = in_->getFrameWidth();
57-
const PoolConfig& conf = config_.pool_conf();
58-
if (imgSizeY_ == 0) {
59-
imgSizeY_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
60-
}
61-
if (imgSize_ == 0) {
62-
imgSize_ = conf.img_size();
63-
}
64-
outputY_ = outputSize(imgSizeY_, sizeY_, confPaddingY_, strideY_,
65-
/* caffeMode */ false);
66-
outputX_ = outputSize(imgSize_, sizeX_, confPadding_, stride_,
67-
/* caffeMode */ false);
6838

69-
const_cast<Argument*>(out_)->setFrameHeight(outputY_);
70-
const_cast<Argument*>(out_)->setFrameWidth(outputX_);
39+
const std::string& getPoolType() const { return poolType_; }
7140

72-
return outputY_ * outputX_ * channels_;
73-
}
41+
size_t getSize();
7442
};
7543

7644
class MaxPoolProjection : public PoolProjection {
7745
public:
7846
MaxPoolProjection(const ProjectionConfig& config, ParameterPtr parameter,
7947
bool useGpu)
8048
: PoolProjection(config, parameter, useGpu) {}
49+
8150
virtual void forward();
8251
virtual void backward(const UpdateCallback& callback = nullptr);
8352
};
@@ -87,6 +56,7 @@ class AvgPoolProjection : public PoolProjection {
8756
AvgPoolProjection(const ProjectionConfig& config, ParameterPtr parameter,
8857
bool useGpu)
8958
: PoolProjection(config, parameter, useGpu) {}
59+
9060
virtual void forward();
9161
virtual void backward(const UpdateCallback& callback = nullptr);
9262
};

paddle/gserver/layers/PoolProjectionLayer.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
1615
#pragma once
1716

17+
#include <vector>
1818
#include "PoolLayer.h"
1919
#include "PoolProjection.h"
2020
#include "paddle/math/Matrix.h"
21-
#include <vector>
2221

2322
namespace paddle {
2423
/**
@@ -32,15 +31,16 @@ class PoolProjectionLayer : public PoolLayer {
3231
ProjectionConfig projectionConfig_;
3332

3433
public:
35-
size_t getSize();
36-
virtual void forward(PassType passType);
37-
virtual void backward(const UpdateCallback& callback = nullptr);
38-
explicit PoolProjectionLayer(const LayerConfig& config)
39-
: PoolLayer(config) {
34+
explicit PoolProjectionLayer(const LayerConfig& config) : PoolLayer(config) {
4035
PoolConfig* conf = projectionConfig_.mutable_pool_conf();
4136
*conf = config_.inputs(0).pool_conf();
42-
poolProjection_.reset(PoolProjection::create(projectionConfig_, nullptr,
43-
useGpu_));
37+
poolProjection_.reset(
38+
PoolProjection::create(projectionConfig_, nullptr, useGpu_));
4439
}
40+
41+
size_t getSize();
42+
43+
virtual void forward(PassType passType);
44+
virtual void backward(const UpdateCallback& callback = nullptr);
4545
};
4646
} // namespace paddle

paddle/gserver/layers/SpatialPyramidPoolLayer.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
1615
#pragma once
1716

1817
#include "Layer.h"
1918
#include "PoolProjection.h"
20-
#include "paddle/utils/Logging.h"
2119
#include "paddle/math/MathUtils.h"
20+
#include "paddle/utils/Logging.h"
2221

2322
namespace paddle {
23+
/**
24+
* @brief A layer for spatial pyramid pooling on the input image by taking
25+
* the max, average, etc. within regions, so that the result vector of
26+
* different sized images are of the same size.
27+
*/
2428

2529
class SpatialPyramidPoolLayer : public Layer {
2630
protected:
@@ -36,12 +40,15 @@ class SpatialPyramidPoolLayer : public Layer {
3640

3741
public:
3842
explicit SpatialPyramidPoolLayer(const LayerConfig& config) : Layer(config) {}
43+
3944
~SpatialPyramidPoolLayer() {}
4045

4146
virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
47+
4248
ProjectionConfig getConfig(size_t sizeX_, size_t sizeY_, size_t channels,
4349
size_t pyamidLevel_, std::string& poolType_);
4450
size_t getSize();
51+
4552
virtual void forward(PassType passType);
4653
virtual void backward(const UpdateCallback& callback = nullptr);
4754
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
type: "nn"
2+
layers {
3+
name: "data"
4+
type: "data"
5+
size: 3200
6+
active_type: ""
7+
}
8+
layers {
9+
name: "__spp_0__"
10+
type: "spp"
11+
size: 80
12+
active_type: ""
13+
inputs {
14+
input_layer_name: "data"
15+
spp_conf {
16+
pool_type: "max-projection"
17+
pyramid_height: 2
18+
channels: 16
19+
img_size: 10
20+
img_size_y: 20
21+
}
22+
}
23+
}
24+
input_layer_names: "data"
25+
output_layer_names: "__spp_0__"
26+
sub_models {
27+
name: "root"
28+
layer_names: "data"
29+
layer_names: "__spp_0__"
30+
input_layer_names: "data"
31+
output_layer_names: "__spp_0__"
32+
is_recurrent_layer_group: false
33+
}
34+

python/paddle/trainer_config_helpers/tests/configs/test_spp_layer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from paddle.trainer_config_helpers import *
22

33
settings(
4-
batch_size=100,
4+
batch_size=100,
55
learning_rate=1e-5
66
)
77

0 commit comments

Comments
 (0)