提交 6b61a096 编写于 作者: D dangqingqing

Optional padding mode, namely ceil or floor, ceil by default.

上级 c8817a19
...@@ -73,10 +73,6 @@ void PadGrad<DEVICE_TYPE_CPU>(real* inGrad, ...@@ -73,10 +73,6 @@ void PadGrad<DEVICE_TYPE_CPU>(real* inGrad,
} }
} }
/**
* \param inputs[0] input value.
* \param outputs[0] output value.
*/
template <DeviceType Device> template <DeviceType Device>
class PadFunc : public FunctionBase { class PadFunc : public FunctionBase {
public: public:
...@@ -89,6 +85,10 @@ public: ...@@ -89,6 +85,10 @@ public:
padw1_ = config.get<int>("padw1"); padw1_ = config.get<int>("padw1");
} }
/**
* \param inputs[0] input value.
* \param outputs[0] output value.
*/
void calc(const Arguments& inputs, void calc(const Arguments& inputs,
const Arguments& outputs, const Arguments& outputs,
const Arguments& inouts) override { const Arguments& inouts) override {
...@@ -124,10 +124,6 @@ private: ...@@ -124,10 +124,6 @@ private:
int padw1_; int padw1_;
}; };
/**
* \param inputs[0] input grad.
* \param outputs[0] output grad.
*/
template <DeviceType Device> template <DeviceType Device>
class PadGradFunc : public FunctionBase { class PadGradFunc : public FunctionBase {
public: public:
...@@ -140,6 +136,10 @@ public: ...@@ -140,6 +136,10 @@ public:
padw1_ = config.get<int>("padw1"); padw1_ = config.get<int>("padw1");
} }
/**
* \param inputs[0] output grad.
* \param inouts[0] input grad.
*/
void calc(const Arguments& inputs, void calc(const Arguments& inputs,
const Arguments& outputs, const Arguments& outputs,
const Arguments& inouts) override { const Arguments& inouts) override {
......
...@@ -43,28 +43,30 @@ TEST(Pad, real) { ...@@ -43,28 +43,30 @@ TEST(Pad, real) {
} }
} }
// TEST(PadGrad, real) { TEST(PadGrad, real) {
// for (size_t numSamples : {5, 32}) { for (size_t numSamples : {5, 32}) {
// for (size_t channels : {1, 5, 32}) { for (size_t channels : {1, 5, 32}) {
// for (size_t imgSizeH : {5, 33, 100}) { for (size_t imgSizeH : {5, 33, 100}) {
// for (size_t imgSizeW : {5, 32, 96}) { for (size_t imgSizeW : {5, 32, 96}) {
// VLOG(3) << " numSamples=" << numSamples << " channels=" << channels VLOG(3) << " numSamples=" << numSamples << " channels=" << channels
// << " imgSizeH=" << imgSizeH << " imgSizeW=" << imgSizeW; << " imgSizeH=" << imgSizeH << " imgSizeW=" << imgSizeW;
//
// FunctionCompare compare("PadGrad", FunctionCompare compare("PadGrad",
// FuncConfig() FuncConfig()
// .set("padc0", 2).set("padc1", 3) .set("padc0", 2)
// .set("padh0", 1).set("padh1", 2) .set("padc1", 3)
// .set("padw0", 3).set("padw1", 2)); .set("padh0", 1)
// Dims inDims{numSamples, channels, imgSizeH, imgSizeW}; .set("padh1", 2)
// Dims outDims{numSamples, channels + 5, imgSizeH + 3, imgSizeW + 5}; .set("padw0", 3)
// compare.cmpWithArg({Tensor(nullptr, inDims)}, .set("padw1", 2));
// {Tensor(nullptr, outDims)}, Dims inDims{numSamples, channels, imgSizeH, imgSizeW};
// {}); Dims outDims{numSamples, channels + 5, imgSizeH + 3, imgSizeW + 5};
// } compare.cmpWithArg(
// } {Tensor(nullptr, inDims)}, {}, {Tensor(nullptr, outDims)});
// } }
// } }
//} }
}
}
} // namespace paddle } // namespace paddle
...@@ -1109,7 +1109,7 @@ def parse_bilinear(bilinear, input_layer_name, bilinear_conf): ...@@ -1109,7 +1109,7 @@ def parse_bilinear(bilinear, input_layer_name, bilinear_conf):
bilinear_conf.out_size_y = bilinear.out_size_y bilinear_conf.out_size_y = bilinear.out_size_y
def parse_pool(pool, input_layer_name, pool_conf): def parse_pool(pool, input_layer_name, pool_conf, ceil_mode):
pool_conf.pool_type = pool.pool_type pool_conf.pool_type = pool.pool_type
config_assert(pool.pool_type in [ config_assert(pool.pool_type in [
'max-projection', 'avg-projection', 'cudnn-max-pool', 'cudnn-avg-pool' 'max-projection', 'avg-projection', 'cudnn-max-pool', 'cudnn-avg-pool'
...@@ -1134,10 +1134,10 @@ def parse_pool(pool, input_layer_name, pool_conf): ...@@ -1134,10 +1134,10 @@ def parse_pool(pool, input_layer_name, pool_conf):
pool_conf.padding_y = default(pool.padding_y, pool_conf.padding) pool_conf.padding_y = default(pool.padding_y, pool_conf.padding)
pool_conf.output_x = cnn_output_size(pool_conf.img_size, pool_conf.size_x, pool_conf.output_x = cnn_output_size(pool_conf.img_size, pool_conf.size_x,
pool_conf.padding, pool_conf.stride, pool_conf.padding, pool_conf.stride,
False) not ceil_mode)
pool_conf.output_y = cnn_output_size(pool_conf.img_size_y, pool_conf.size_y, pool_conf.output_y = cnn_output_size(pool_conf.img_size_y, pool_conf.size_y,
pool_conf.padding_y, pool_conf.padding_y,
pool_conf.stride_y, False) pool_conf.stride_y, not ceil_mode)
def parse_spp(spp, input_layer_name, spp_conf): def parse_spp(spp, input_layer_name, spp_conf):
...@@ -1810,9 +1810,8 @@ class ConvTransLayer(ConvTransLayerBase): ...@@ -1810,9 +1810,8 @@ class ConvTransLayer(ConvTransLayerBase):
@config_layer('norm') @config_layer('norm')
class NormLayer(LayerBase): class NormLayer(LayerBase):
def __init__(self, name, inputs, device=None): def __init__(self, name, inputs, **xargs):
super(NormLayer, self).__init__( super(NormLayer, self).__init__(name, 'norm', 0, inputs=inputs, **xargs)
name, 'norm', 0, inputs=inputs, device=device)
for input_index in xrange(len(self.inputs)): for input_index in xrange(len(self.inputs)):
input_layer = self.get_input_layer(input_index) input_layer = self.get_input_layer(input_index)
norm_conf = self.config.inputs[input_index].norm_conf norm_conf = self.config.inputs[input_index].norm_conf
...@@ -1824,23 +1823,22 @@ class NormLayer(LayerBase): ...@@ -1824,23 +1823,22 @@ class NormLayer(LayerBase):
@config_layer('pool') @config_layer('pool')
class PoolLayer(LayerBase): class PoolLayer(LayerBase):
def __init__(self, name, inputs, device=None): def __init__(self, name, inputs, ceil_mode=True, **xargs):
super(PoolLayer, self).__init__( super(PoolLayer, self).__init__(name, 'pool', 0, inputs=inputs, **xargs)
name, 'pool', 0, inputs=inputs, device=device)
for input_index in xrange(len(self.inputs)): for input_index in xrange(len(self.inputs)):
input_layer = self.get_input_layer(input_index) input_layer = self.get_input_layer(input_index)
pool_conf = self.config.inputs[input_index].pool_conf pool_conf = self.config.inputs[input_index].pool_conf
parse_pool(self.inputs[input_index].pool, input_layer.name, parse_pool(self.inputs[input_index].pool, input_layer.name,
pool_conf) pool_conf, ceil_mode)
self.set_cnn_layer(name, pool_conf.output_y, pool_conf.output_x, self.set_cnn_layer(name, pool_conf.output_y, pool_conf.output_x,
pool_conf.channels) pool_conf.channels)
@config_layer('spp') @config_layer('spp')
class SpatialPyramidPoolLayer(LayerBase): class SpatialPyramidPoolLayer(LayerBase):
def __init__(self, name, inputs, device=None): def __init__(self, name, inputs, **xargs):
super(SpatialPyramidPoolLayer, self).__init__( super(SpatialPyramidPoolLayer, self).__init__(
name, 'spp', 0, inputs=inputs, device=device) name, 'spp', 0, inputs=inputs, **xargs)
for input_index in xrange(len(self.inputs)): for input_index in xrange(len(self.inputs)):
input_layer = self.get_input_layer(input_index) input_layer = self.get_input_layer(input_index)
spp_conf = self.config.inputs[input_index].spp_conf spp_conf = self.config.inputs[input_index].spp_conf
...@@ -1877,7 +1875,6 @@ class BatchNormLayer(LayerBase): ...@@ -1877,7 +1875,6 @@ class BatchNormLayer(LayerBase):
inputs, inputs,
active_type="linear", active_type="linear",
bias=True, bias=True,
device=None,
use_global_stats=True, use_global_stats=True,
moving_average_fraction=0.9, moving_average_fraction=0.9,
batch_norm_type=None, batch_norm_type=None,
...@@ -1919,7 +1916,6 @@ class BatchNormLayer(LayerBase): ...@@ -1919,7 +1916,6 @@ class BatchNormLayer(LayerBase):
0, 0,
active_type=active_type, active_type=active_type,
inputs=inputs, inputs=inputs,
device=device,
**xargs) **xargs)
if use_global_stats is not None: if use_global_stats is not None:
...@@ -1953,9 +1949,9 @@ class BatchNormLayer(LayerBase): ...@@ -1953,9 +1949,9 @@ class BatchNormLayer(LayerBase):
@config_layer('trans') @config_layer('trans')
class TransLayer(LayerBase): class TransLayer(LayerBase):
def __init__(self, name, inputs, device=None): def __init__(self, name, inputs, **xargs):
super(TransLayer, self).__init__( super(TransLayer, self).__init__(
name, 'trans', 0, inputs=inputs, device=device) name, 'trans', 0, inputs=inputs, **xargs)
config_assert( config_assert(
len(self.inputs) == 1, len(self.inputs) == 1,
'TransLayer must have one and only one input') 'TransLayer must have one and only one input')
...@@ -1964,9 +1960,9 @@ class TransLayer(LayerBase): ...@@ -1964,9 +1960,9 @@ class TransLayer(LayerBase):
@config_layer('resize') @config_layer('resize')
class ResizeLayer(LayerBase): class ResizeLayer(LayerBase):
def __init__(self, name, size, inputs, device=None): def __init__(self, name, size, inputs, **xargs):
super(ResizeLayer, self).__init__( super(ResizeLayer, self).__init__(
name, 'resize', size=size, inputs=inputs, device=device) name, 'resize', size=size, inputs=inputs, **xargs)
config_assert( config_assert(
len(self.inputs) == 1, len(self.inputs) == 1,
'ResizeLayer must have one and only one input') 'ResizeLayer must have one and only one input')
...@@ -1974,9 +1970,9 @@ class ResizeLayer(LayerBase): ...@@ -1974,9 +1970,9 @@ class ResizeLayer(LayerBase):
@config_layer('blockexpand') @config_layer('blockexpand')
class BlockExpandLayer(LayerBase): class BlockExpandLayer(LayerBase):
def __init__(self, name, inputs, device=None): def __init__(self, name, inputs, **xargs):
super(BlockExpandLayer, self).__init__( super(BlockExpandLayer, self).__init__(
name, 'blockexpand', 0, inputs=inputs, device=device) name, 'blockexpand', 0, inputs=inputs, **xargs)
for input_index in xrange(len(self.inputs)): for input_index in xrange(len(self.inputs)):
input_layer = self.get_input_layer(input_index) input_layer = self.get_input_layer(input_index)
parse_block_expand( parse_block_expand(
......
...@@ -1980,7 +1980,8 @@ def img_pool_layer(input, ...@@ -1980,7 +1980,8 @@ def img_pool_layer(input,
layer_attr=None, layer_attr=None,
pool_size_y=None, pool_size_y=None,
stride_y=None, stride_y=None,
padding_y=None): padding_y=None,
ceil_mode=True):
""" """
Image pooling Layer. Image pooling Layer.
...@@ -2011,6 +2012,23 @@ def img_pool_layer(input, ...@@ -2011,6 +2012,23 @@ def img_pool_layer(input,
:type stride_y: int|None :type stride_y: int|None
:param layer_attr: Extra Layer attribute. :param layer_attr: Extra Layer attribute.
:type layer_attr: ExtraLayerAttribute :type layer_attr: ExtraLayerAttribute
:param ceil_mode: Wether to use ceil mode to calculate output height and with.
Defalut is True. If set false, Otherwise use floor.
- ceil_mode=True:
.. math::
w = 1 + int(ceil(input_width + 2 * padding - pool_size) / float(stride))
h = 1 + int(ceil(input_height + 2 * padding_y - pool_size_y) / float(stride_y))
- ceil_mode=False:
.. math::
w = 1 + int(floor(input_width + 2 * padding - pool_size) / float(stride))
h = 1 + int(floor(input_height + 2 * padding_y - pool_size_y) / float(stride_y))
:type ceil_mode: bool
:return: LayerOutput object. :return: LayerOutput object.
:rtype: LayerOutput :rtype: LayerOutput
""" """
...@@ -2048,6 +2066,7 @@ def img_pool_layer(input, ...@@ -2048,6 +2066,7 @@ def img_pool_layer(input,
stride_y=stride_y, stride_y=stride_y,
padding_y=padding_y)) padding_y=padding_y))
], ],
ceil_mode=ceil_mode,
**ExtraLayerAttribute.to_kwargs(layer_attr)) **ExtraLayerAttribute.to_kwargs(layer_attr))
return LayerOutput( return LayerOutput(
name, name,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册