Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-380] count_include_pad argument for Avg Pooling #11021

Merged
merged 6 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp-package/scripts/OpWrapperGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def GetConvertEnumVariableToString(self, variable=''):

class Arg:
typeDict = {'boolean':'bool',\
'boolean or None':'dmlc::optional<bool>',\
'Shape(tuple)':'Shape',\
'Symbol':'Symbol',\
'NDArray':'Symbol',\
Expand Down
22 changes: 15 additions & 7 deletions python/mxnet/gluon/nn/conv_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def __init__(self, channels, kernel_size, strides=(1, 1, 1), padding=(0, 0, 0),
class _Pooling(HybridBlock):
"""Abstract class for different pooling layers."""
def __init__(self, pool_size, strides, padding, ceil_mode, global_pool,
pool_type, **kwargs):
pool_type, count_include_pad=None, **kwargs):
super(_Pooling, self).__init__(**kwargs)
if strides is None:
strides = pool_size
Expand All @@ -687,6 +687,8 @@ def __init__(self, pool_size, strides, padding, ceil_mode, global_pool,
'kernel': pool_size, 'stride': strides, 'pad': padding,
'global_pool': global_pool, 'pool_type': pool_type,
'pooling_convention': 'full' if ceil_mode else 'valid'}
if count_include_pad is not None:
self._kwargs['count_include_pad'] = count_include_pad

def _alias(self):
return 'pool'
Expand Down Expand Up @@ -863,6 +865,8 @@ class AvgPool1D(_Pooling):
respectively. padding is applied on 'W' dimension.
ceil_mode : bool, default False
When `True`, will use ceil instead of floor to compute the output shape.
count_include_pad : bool, default True
When 'False', will exclude padding elements when computing the average value.


Inputs:
Expand All @@ -879,13 +883,13 @@ class AvgPool1D(_Pooling):
equation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls add documentation for count_include_pad for gluon blocks

"""
def __init__(self, pool_size=2, strides=None, padding=0, layout='NCW',
ceil_mode=False, **kwargs):
ceil_mode=False, count_include_pad=True, **kwargs):
assert layout == 'NCW', "Only supports 'NCW' layout for now"
if isinstance(pool_size, numeric_types):
pool_size = (pool_size,)
assert len(pool_size) == 1, "pool_size must be a number or a list of 1 ints"
super(AvgPool1D, self).__init__(
pool_size, strides, padding, ceil_mode, False, 'avg', **kwargs)
pool_size, strides, padding, ceil_mode, False, 'avg', count_include_pad, **kwargs)


class AvgPool2D(_Pooling):
Expand All @@ -907,6 +911,8 @@ class AvgPool2D(_Pooling):
dimensions respectively. padding is applied on 'H' and 'W' dimension.
ceil_mode : bool, default False
When True, will use ceil instead of floor to compute the output shape.
count_include_pad : bool, default True
When 'False', will exclude padding elements when computing the average value.


Inputs:
Expand All @@ -926,13 +932,13 @@ class AvgPool2D(_Pooling):
equation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here and and for AvgPool3D

"""
def __init__(self, pool_size=(2, 2), strides=None, padding=0,
ceil_mode=False, layout='NCHW', **kwargs):
ceil_mode=False, layout='NCHW', count_include_pad=True, **kwargs):
assert layout == 'NCHW', "Only supports 'NCHW' layout for now"
if isinstance(pool_size, numeric_types):
pool_size = (pool_size,)*2
assert len(pool_size) == 2, "pool_size must be a number or a list of 2 ints"
super(AvgPool2D, self).__init__(
pool_size, strides, padding, ceil_mode, False, 'avg', **kwargs)
pool_size, strides, padding, ceil_mode, False, 'avg', count_include_pad, **kwargs)


class AvgPool3D(_Pooling):
Expand All @@ -955,6 +961,8 @@ class AvgPool3D(_Pooling):
dimension.
ceil_mode : bool, default False
When True, will use ceil instead of floor to compute the output shape.
count_include_pad : bool, default True
When 'False', will exclude padding elements when computing the average value.


Inputs:
Expand All @@ -975,13 +983,13 @@ class AvgPool3D(_Pooling):
equation.
"""
def __init__(self, pool_size=(2, 2, 2), strides=None, padding=0,
ceil_mode=False, layout='NCDHW', **kwargs):
ceil_mode=False, layout='NCDHW', count_include_pad=True, **kwargs):
assert layout == 'NCDHW', "Only supports 'NCDHW' layout for now"
if isinstance(pool_size, numeric_types):
pool_size = (pool_size,)*3
assert len(pool_size) == 3, "pool_size must be a number or a list of 3 ints"
super(AvgPool3D, self).__init__(
pool_size, strides, padding, ceil_mode, False, 'avg', **kwargs)
pool_size, strides, padding, ceil_mode, False, 'avg', count_include_pad, **kwargs)


class GlobalMaxPool1D(_Pooling):
Expand Down
8 changes: 6 additions & 2 deletions src/operator/nn/cudnn/cudnn_pooling-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ class CuDNNPoolingOp {
mode_ = CUDNN_POOLING_MAX;
break;
case pool_enum::kAvgPooling:
mode_ = CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING;
if (param_.count_include_pad.has_value() && !param_.count_include_pad.value()) {
mode_ = CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING;
} else {
mode_ = CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING;
}
break;
default:
LOG(FATAL) << "Not implmented";
Expand Down Expand Up @@ -263,7 +267,7 @@ class CuDNNPoolingOp {
&(pad_vec[0]),
&(stride_vec[0])));
#else
LOG(FATAL) << "3D pooling only support CUDNN v5 and abouve";
LOG(FATAL) << "3D pooling only support CUDNN v5 and above";
#endif
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/operator/nn/mkldnn/mkldnn_pooling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ mkldnn::algorithm GetMKLDNNPoolAlgo(const PoolingParam &param) {
return mkldnn::algorithm::pooling_max;
break;
case pool_enum::kAvgPooling:
return mkldnn::algorithm::pooling_avg_include_padding;
if (param.count_include_pad.has_value() && !param.count_include_pad.value()) {
return mkldnn::algorithm::pooling_avg_exclude_padding;
} else {
return mkldnn::algorithm::pooling_avg_include_padding;
}
break;
default:
LOG(FATAL) << "MKLDNN Pooling: Unknown pooling method.";
Expand Down
Loading