未验证 提交 2ce1f04d 编写于 作者: C ccrrong 提交者: GitHub

remove pool2d from fluid (#48512)

* remove pool2d
上级 73bff10f
......@@ -81,12 +81,12 @@ def residual_block(num, quant_skip_pattern=None):
hidden = paddle.matmul(hidden, matmul_weight, True, True)
if quant_skip_pattern:
with fluid.name_scope(quant_skip_pattern):
pool = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='avg', pool_stride=2
pool = paddle.nn.functional.avg_pool2d(
x=hidden, kernel_size=2, stride=2
)
else:
pool = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='avg', pool_stride=2
pool = paddle.nn.functional.avg_pool2d(
x=hidden, kernel_size=2, stride=2
)
fc = fluid.layers.fc(input=pool, size=10)
loss = paddle.nn.functional.cross_entropy(
......@@ -733,11 +733,11 @@ def quant_dequant_residual_block(num, quant_skip_pattern=None):
hidden = paddle.matmul(hidden, data2, True, True)
if isinstance(quant_skip_pattern, str):
with fluid.name_scope(quant_skip_pattern):
pool1 = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='avg', pool_stride=2
pool1 = paddle.nn.functional.avg_pool2d(
x=hidden, kernel_size=2, stride=2
)
pool2 = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='max', pool_stride=2
pool2 = paddle.nn.functional.max_pool2d(
x=hidden, kernel_size=2, stride=2
)
pool_add = paddle.nn.functional.relu(paddle.add(x=pool1, y=pool2))
elif isinstance(quant_skip_pattern, list):
......@@ -745,20 +745,20 @@ def quant_dequant_residual_block(num, quant_skip_pattern=None):
len(quant_skip_pattern) > 1
), 'test config error: the len of quant_skip_pattern list should be greater than 1.'
with fluid.name_scope(quant_skip_pattern[0]):
pool1 = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='avg', pool_stride=2
pool1 = paddle.nn.functional.avg_pool2d(
x=hidden, kernel_size=2, stride=2
)
pool2 = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='max', pool_stride=2
pool2 = paddle.nn.functional.max_pool2d(
x=hidden, kernel_size=2, stride=2
)
with fluid.name_scope(quant_skip_pattern[1]):
pool_add = paddle.nn.functional.relu(paddle.add(x=pool1, y=pool2))
else:
pool1 = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='avg', pool_stride=2
pool1 = paddle.nn.functional.avg_pool2d(
x=hidden, kernel_size=2, stride=2
)
pool2 = fluid.layers.pool2d(
input=hidden, pool_size=2, pool_type='max', pool_stride=2
pool2 = paddle.nn.functional.max_pool2d(
x=hidden, kernel_size=2, stride=2
)
pool_add = paddle.nn.functional.relu(paddle.add(x=pool1, y=pool2))
fc = fluid.layers.fc(input=pool_add, size=10)
......
......@@ -69,9 +69,7 @@ def resnet_cifar10(input, depth=32):
res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)
res2 = layer_warp(basicblock, res1, 16, 32, n, 2)
res3 = layer_warp(basicblock, res2, 32, 64, n, 2)
pool = fluid.layers.pool2d(
input=res3, pool_size=8, pool_type='avg', pool_stride=1
)
pool = paddle.nn.functional.avg_pool2d(x=res3, kernel_size=8, stride=1)
return pool
......
......@@ -88,9 +88,7 @@ def resnet_cifar10(input, depth=32):
res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)
res2 = layer_warp(basicblock, res1, 16, 32, n, 2)
res3 = layer_warp(basicblock, res2, 32, 64, n, 2)
pool = fluid.layers.pool2d(
input=res3, pool_size=8, pool_type='avg', pool_stride=1
)
pool = paddle.nn.functional.avg_pool2d(x=res3, kernel_size=8, stride=1)
return pool
......
......@@ -68,7 +68,6 @@ __all__ = [
'linear_chain_crf',
'crf_decoding',
'conv2d',
'pool2d',
'dropout',
'split',
'l2_normalize',
......@@ -1428,249 +1427,6 @@ def conv2d(
return helper.append_activation(pre_act)
@templatedoc()
def pool2d(
input,
pool_size=-1,
pool_type="max",
pool_stride=1,
pool_padding=0,
global_pooling=False,
use_cudnn=True,
ceil_mode=False,
name=None,
exclusive=True,
data_format="NCHW",
):
"""
${comment}
Args:
input (Variable): The input tensor of pooling operator which is a 4-D tensor with
shape [N, C, H, W]. The format of input tensor is `"NCHW"` or
`"NHWC"`, where `N` is batch size, `C` is the number of channels,
`H` is the height of the feature, and `W` is the width of the
feature. The data type if float32 or float64.
pool_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
it must contain two integers, (pool_size_Height, pool_size_Width).
Otherwise, the pool kernel size will be a square of an int.
pool_type: ${pooling_type_comment}
pool_stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,
it must contain two integers, (pool_stride_Height, pool_stride_Width).
Otherwise, the pool stride size will be a square of an int.
pool_padding (string|int|list|tuple): The pool padding. If `pool_padding` is a string, either 'VALID' or
'SAME' which is the padding algorithm. If pool padding size is a tuple or list,
it could be in three forms: `[pad_height, pad_width]` or
`[pad_height_top, pad_height_bottom, pad_width_left, pad_width_right]`, and when `data_format` is `"NCHW"`,
`pool_padding` can be in the form `[[0,0], [0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]`.
when `data_format` is `"NHWC"`, `pool_padding` can be in the form
`[[0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]`.
Otherwise, the pool padding size will be a square of an int.
global_pooling (bool): ${global_pooling_comment}
use_cudnn (bool): ${use_cudnn_comment}
ceil_mode (bool): ${ceil_mode_comment}
name(str, optional): For detailed information, please refer
to :ref:`api_guide_Name`. Usually name is no need to set and
None by default.
exclusive (bool): Whether to exclude padding points in average pooling
mode, default is `true`.
data_format (string): The data format of the input and output data. An optional string from: `"NCHW"`, `"NHWC"`.
The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
`[batch_size, input_channels, input_height, input_width]`.
Returns:
Variable: The output tensor of pooling result. The data type is same as input tensor.
Raises:
ValueError: If `pool_type` is not "max" nor "avg".
ValueError: If `global_pooling` is False and `pool_size` is -1.
TypeError: If `use_cudnn` is not a bool value.
ValueError: If `data_format` is not "NCHW" or "NHWC".
ValueError: If `pool_padding` is a string, but not "SAME" or "VALID".
ValueError: If `pool_padding` is "VALID", but `ceil_mode` is True.
ValueError: If `pool_padding` is a list or tuple, but the elements in the batch or channel dimensions are non-zero.
ShapeError: If the input is not a 4-D or 5-D Tensor.
ShapeError: If the dimension of input minus the size of `pool_stride` is not 2.
ShapeError: If the size of `pool_size` and `pool_stride` is not equal.
ShapeError: If the output's shape calculated is not greater than 0.
Examples:
.. code-block:: python
import paddle.fluid as fluid
import paddle
paddle.enable_static()
data = fluid.data(name='data', shape=[None, 3, 32, 32], dtype='float32')
# max pool2d
pool2d = fluid.layers.pool2d(
input = data,
pool_size = 2,
pool_type = "max",
pool_stride = 1,
global_pooling=False)
# average pool2d
pool2d = fluid.layers.pool2d(
input = data,
pool_size = 2,
pool_type = "avg",
pool_stride = 1,
global_pooling=False)
# global average pool2d
pool2d = fluid.layers.pool2d(
input = data,
pool_size = 2,
pool_type = "avg",
pool_stride = 1,
global_pooling=True)
# Attr(pool_padding) is a list with 4 elements, Attr(data_format) is "NCHW".
out_1 = fluid.layers.pool2d(
input = data,
pool_size = 3,
pool_type = "avg",
pool_stride = 1,
pool_padding = [1, 2, 1, 0],
data_format = "NCHW")
# Attr(pool_padding) is a string, Attr(data_format) is "NCHW".
out_2 = fluid.layers.pool2d(
input = data,
pool_size = 3,
pool_type = "avg",
pool_stride = 1,
pool_padding = "VALID",
data_format = "NCHW")
"""
if pool_type not in ["max", "avg"]:
raise ValueError(
"Unknown Attr(pool_type): '%s'. It can only be 'max' or 'avg'.",
str(pool_type),
)
if global_pooling is False and pool_size == -1:
raise ValueError(
"When Attr(global_pooling) is False, Attr(pool_size) must be passed "
"and be a valid value. Received pool_size: %s." % str(pool_size)
)
if not isinstance(use_cudnn, bool):
raise TypeError(
"Attr(use_cudnn) should be True or False. Received "
"Attr(use_cudnn): %s." % str(use_cudnn)
)
if data_format not in ["NCHW", "NHWC"]:
raise ValueError(
"Attr(data_format) should be 'NCHW' or 'NHWC'. Received "
"Attr(data_format): %s." % str(data_format)
)
pool_size = utils.convert_to_list(pool_size, 2, 'pool_size')
pool_stride = utils.convert_to_list(pool_stride, 2, 'pool_stride')
def update_padding(padding, data_format):
def is_list_or_tuple(ele):
if isinstance(ele, list) or isinstance(ele, tuple):
return True
return False
if is_list_or_tuple(padding) and len(padding) == 4:
if is_list_or_tuple(padding[0]) and (data_format == "NCHW"):
if not (padding[0] == [0, 0] and padding[1] == [0, 0]):
raise ValueError(
"Non-zero pool_padding(%s) in the batch or channel dimensions "
"is not supported." % str(padding)
)
padding = padding[2:4]
padding = [ele for a_list in padding for ele in a_list]
elif is_list_or_tuple(padding[0]) and (data_format == "NHWC"):
if not (padding[0] == [0, 0] and padding[3] == [0, 0]):
raise ValueError(
"Non-zero pool_padding(%s) in the batch or channel dimensions "
"is not supported." % str(padding)
)
padding = padding[1:3]
padding = [ele for a_list in padding for ele in a_list]
padding = utils.convert_to_list(padding, 4, 'padding')
if utils._is_symmetric_padding(padding, 2):
padding = [padding[0], padding[2]]
else:
padding = utils.convert_to_list(padding, 2, 'padding')
return padding
padding_algorithm = "EXPLICIT"
if isinstance(pool_padding, str):
pool_padding = pool_padding.upper()
if pool_padding not in ["SAME", "VALID"]:
raise ValueError(
"Unknown Attr(pool_padding): '%s'. It can only be 'SAME' or 'VALID'."
% str(pool_padding)
)
if pool_padding == "VALID":
padding_algorithm = "VALID"
pool_padding = [0, 0]
if ceil_mode is not False:
raise ValueError(
"When Attr(pool_padding) is \"VALID\", Attr(ceil_mode) must be False. "
"Received ceil_mode: True."
)
elif pool_padding == "SAME":
padding_algorithm = "SAME"
pool_padding = [0, 0]
pool_padding = update_padding(pool_padding, data_format)
if in_dygraph_mode():
input = input._use_gpudnn(use_cudnn)
return _C_ops.pool2d(
input,
pool_size,
pool_stride,
pool_padding,
ceil_mode,
exclusive,
data_format,
pool_type,
global_pooling,
False,
padding_algorithm,
)
op_type = 'pool2d'
helper = LayerHelper(op_type, **locals())
dtype = helper.input_dtype()
pool_out = helper.create_variable_for_type_inference(dtype)
helper.append_op(
type=op_type,
inputs={"X": input},
outputs={"Out": pool_out},
attrs={
"pooling_type": pool_type,
"ksize": pool_size,
"global_pooling": global_pooling,
"strides": pool_stride,
"paddings": pool_padding,
"padding_algorithm": padding_algorithm,
"use_cudnn": use_cudnn,
"ceil_mode": ceil_mode,
"use_mkldnn": False,
"exclusive": exclusive,
"data_format": data_format,
},
)
return pool_out
@templatedoc()
def layer_norm(
input,
......
......@@ -132,15 +132,19 @@ def simple_img_conv_pool(
act=act,
use_cudnn=use_cudnn,
)
pool_out = layers.pool2d(
input=conv_out,
pool_size=pool_size,
pool_type=pool_type,
pool_stride=pool_stride,
pool_padding=pool_padding,
global_pooling=global_pooling,
use_cudnn=use_cudnn,
if pool_type == 'max':
pool_out = paddle.nn.functional.max_pool2d(
x=conv_out,
kernel_size=pool_size,
stride=pool_stride,
padding=pool_padding,
)
else:
pool_out = paddle.nn.functional.avg_pool2d(
x=conv_out,
kernel_size=pool_size,
stride=pool_stride,
padding=pool_padding,
)
return pool_out
......@@ -258,12 +262,17 @@ def img_conv_group(
if abs(drop_rate) > 1e-5:
tmp = layers.dropout(x=tmp, dropout_prob=drop_rate)
pool_out = layers.pool2d(
input=tmp,
pool_size=pool_size,
pool_type=pool_type,
pool_stride=pool_stride,
use_cudnn=use_cudnn,
if pool_type == 'max':
pool_out = paddle.nn.functional.max_pool2d(
x=tmp,
kernel_size=pool_size,
stride=pool_stride,
)
else:
pool_out = paddle.nn.functional.avg_pool2d(
x=tmp,
kernel_size=pool_size,
stride=pool_stride,
)
return pool_out
......
......@@ -68,9 +68,7 @@ def resnet_cifar10(input, depth=32):
res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)
res2 = layer_warp(basicblock, res1, 16, 32, n, 2)
res3 = layer_warp(basicblock, res2, 32, 64, n, 2)
pool = fluid.layers.pool2d(
input=res3, pool_size=8, pool_type='avg', pool_stride=1
)
pool = paddle.nn.functional.avg_pool2d(x=res3, kernel_size=8, stride=1)
return pool
......
......@@ -59,12 +59,11 @@ class SE_ResNeXt:
conv = self.conv_bn_layer(
input=input, num_filters=64, filter_size=7, stride=2, act='relu'
)
conv = fluid.layers.pool2d(
input=conv,
pool_size=3,
pool_stride=2,
pool_padding=1,
pool_type='max',
conv = paddle.nn.functional.max_pool2d(
x=conv,
kernel_size=3,
stride=2,
padding=1,
)
elif layers == 101:
cardinality = 32
......@@ -75,12 +74,11 @@ class SE_ResNeXt:
conv = self.conv_bn_layer(
input=input, num_filters=64, filter_size=7, stride=2, act='relu'
)
conv = fluid.layers.pool2d(
input=conv,
pool_size=3,
pool_stride=2,
pool_padding=1,
pool_type='max',
conv = paddle.nn.functional.max_pool2d(
x=conv,
kernel_size=3,
stride=2,
padding=1,
)
elif layers == 152:
cardinality = 64
......@@ -97,12 +95,11 @@ class SE_ResNeXt:
conv = self.conv_bn_layer(
input=conv, num_filters=128, filter_size=3, stride=1, act='relu'
)
conv = fluid.layers.pool2d(
input=conv,
pool_size=3,
pool_stride=2,
pool_padding=1,
pool_type='max',
conv = paddle.nn.functional.max_pool2d(
x=conv,
kernel_size=3,
stride=2,
padding=1,
)
for block in range(len(depth)):
......@@ -115,9 +112,7 @@ class SE_ResNeXt:
reduction_ratio=reduction_ratio,
)
pool = fluid.layers.pool2d(
input=conv, pool_size=7, pool_type='avg', global_pooling=True
)
pool = paddle.nn.functional.adaptive_avg_pool2d(x=conv, output_size=1)
drop = fluid.layers.dropout(x=pool, dropout_prob=0.2)
stdv = 1.0 / math.sqrt(drop.shape[1] * 1.0)
out = fluid.layers.fc(
......@@ -185,9 +180,7 @@ class SE_ResNeXt:
return paddle.static.nn.batch_norm(input=conv, act=act)
def squeeze_excitation(self, input, num_channels, reduction_ratio):
pool = fluid.layers.pool2d(
input=input, pool_size=0, pool_type='avg', global_pooling=True
)
pool = paddle.nn.functional.adaptive_avg_pool2d(x=input, output_size=1)
stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
squeeze = fluid.layers.fc(
input=pool,
......
......@@ -41,11 +41,9 @@ class TestBase(IPUOpTest):
def set_op_attrs(self):
self.attrs = {
"pool_size": 3,
"pool_type": 'avg',
"pool_stride": 1,
"pool_padding": 0,
"global_pooling": False,
"kernel_size": 3,
"stride": 1,
"padding": 0,
"ceil_mode": False,
"exclusive": True,
"data_format": 'NCHW',
......@@ -56,7 +54,7 @@ class TestBase(IPUOpTest):
x = paddle.static.data(
name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32'
)
out = paddle.fluid.layers.pool2d(x, **self.attrs)
out = paddle.nn.functional.avg_pool2d(x, **self.attrs)
self.fetch_list = [out.name]
def run_model(self, exec_mode):
......@@ -73,58 +71,52 @@ class TestBase(IPUOpTest):
class TestCase1(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_size'] = 3
self.attrs['kernel_size'] = 3
class TestCase1_2(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_size'] = [3, 1]
self.attrs['kernel_size'] = [3, 1]
class TestCase2(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_stride'] = 2
self.attrs['stride'] = 2
class TestCase2_2(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_stride'] = [2, 1]
self.attrs['stride'] = [2, 1]
class TestCase3(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_padding'] = [1, 1]
self.attrs['padding'] = [1, 1]
class TestCase3_2(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_padding'] = [1, 1, 2, 2]
self.attrs['padding'] = [1, 1, 2, 2]
@unittest.skip('the results has a positional offset')
class TestCase3_3(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_padding'] = [1, 2, 1, 1]
self.attrs['padding'] = [1, 2, 1, 1]
@unittest.skip('paddle output has nan')
class TestCase3_4(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['pool_size'] = 1
self.attrs['pool_padding'] = 1
class TestCase4(TestBase):
def set_attrs(self):
super().set_attrs()
self.attrs['global_pooling'] = True
self.attrs['size'] = 1
self.attrs['padding'] = 1
class TestCase5(TestBase):
......
......@@ -41,13 +41,10 @@ class TestBase(IPUOpTest):
def set_op_attrs(self):
self.attrs = {
"pool_size": 3,
"pool_type": 'max',
"pool_stride": 1,
"pool_padding": 0,
"global_pooling": False,
"kernel_size": 3,
"stride": 1,
"padding": 0,
"ceil_mode": False,
"exclusive": True,
"data_format": 'NCHW',
}
......@@ -56,7 +53,7 @@ class TestBase(IPUOpTest):
x = paddle.static.data(
name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32'
)
out = paddle.fluid.layers.pool2d(x, **self.attrs)
out = paddle.nn.functional.max_pool2d(x, **self.attrs)
self.fetch_list = [out.name]
def run_model(self, exec_mode):
......@@ -73,57 +70,51 @@ class TestBase(IPUOpTest):
class TestCase1(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_size'] = 3
self.attrs['kernel_size'] = 3
class TestCase1_2(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_size'] = [3, 1]
self.attrs['kernel_size'] = [3, 1]
class TestCase2(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_stride'] = 2
self.attrs['stride'] = 2
class TestCase2_2(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_stride'] = [2, 1]
self.attrs['stride'] = [2, 1]
class TestCase3(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_padding'] = [1, 1]
self.attrs['padding'] = [1, 1]
class TestCase3_2(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_padding'] = [1, 1, 2, 2]
self.attrs['padding'] = [1, 1, 2, 2]
@unittest.skip('auto_pad is not currently supported')
class TestCase3_3(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_padding'] = 'VALID'
self.attrs['padding'] = 'VALID'
@unittest.skip('auto_pad is not currently supported')
class TestCase3_4(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['pool_padding'] = 'SAME'
class TestCase4(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['global_pooling'] = True
self.attrs['padding'] = 'SAME'
class TestCase5(TestBase):
......@@ -132,11 +123,5 @@ class TestCase5(TestBase):
self.attrs['ceil_mode'] = True
class TestCase6(TestBase):
def set_op_attrs(self):
super().set_op_attrs()
self.attrs['exclusive'] = False
if __name__ == "__main__":
unittest.main()
......@@ -20,6 +20,7 @@ import unittest
import numpy as np
from inference_pass_test import InferencePassTest
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.static.nn as nn
......@@ -63,17 +64,25 @@ class TensorRTPoolTest(InferencePassTest):
shape=[-1, self.channel, self.height, self.width],
dtype='float32',
)
pool_out = fluid.layers.pool2d(
input=data,
pool_size=self.pool_size,
pool_type=self.pool_type,
pool_stride=self.pool_stride,
pool_padding=self.pool_padding,
global_pooling=self.global_pooling,
if self.pool_type == 'max':
pool_out = paddle.nn.functional.max_pool2d(
x=data,
kernel_size=self.pool_size,
stride=self.pool_stride,
padding=self.pool_padding,
ceil_mode=self.ceil_mode,
)
else:
pool_out = paddle.nn.functional.avg_pool2d(
x=data,
kernel_size=self.pool_size,
stride=self.pool_stride,
padding=self.pool_padding,
ceil_mode=self.ceil_mode,
exclusive=self.exclusive,
)
out = nn.batch_norm(pool_out, is_test=True)
self.fetch_list = [out]
def check_output(self):
......
......@@ -734,86 +734,76 @@ class TestPool2DAPI(unittest.TestCase):
)
ksize = [3, 3]
out_1 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1],
out_1 = paddle.nn.functional.max_pool2d(
x=input_NHWC,
kernel_size=ksize,
padding=[1, 1],
data_format="NHWC",
)
out_2 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="avg",
pool_padding=[[0, 0], [1, 1], [1, 1], [0, 0]],
out_2 = paddle.nn.functional.avg_pool2d(
x=input_NHWC,
kernel_size=ksize,
padding=[[0, 0], [1, 1], [1, 1], [0, 0]],
data_format="NHWC",
)
out_3 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=ksize,
pool_type="avg",
pool_padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
out_3 = paddle.nn.functional.avg_pool2d(
x=input_NCHW,
kernel_size=ksize,
padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
data_format="NCHW",
)
out_4 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=ksize,
pool_type="avg",
pool_padding=[1, 2, 1, 0],
out_4 = paddle.nn.functional.avg_pool2d(
x=input_NCHW,
kernel_size=ksize,
padding=[1, 2, 1, 0],
data_format="NCHW",
)
# test VALID
out_5 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=ksize,
pool_type="avg",
pool_padding="VALID",
out_5 = paddle.nn.functional.avg_pool2d(
x=input_NCHW,
kernel_size=ksize,
padding="VALID",
data_format="NCHW",
)
out_6 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALID",
out_6 = paddle.nn.functional.avg_pool2d(
x=input_NHWC,
kernel_size=ksize,
padding="VALID",
data_format="NHWC",
)
# test SAME
out_7 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=[4, 4],
pool_type="avg",
pool_padding="SAME",
out_7 = paddle.nn.functional.avg_pool2d(
x=input_NCHW,
kernel_size=[4, 4],
padding="SAME",
data_format="NCHW",
)
out_8 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=[4, 4],
pool_type="max",
pool_padding="SAME",
out_8 = paddle.nn.functional.avg_pool2d(
x=input_NHWC,
kernel_size=[4, 4],
padding="SAME",
data_format="NHWC",
)
# test negetive
out_9 = fluid.layers.pool2d(
input=input_NHWC_negetive,
pool_size=ksize,
pool_type="avg",
pool_padding=[0, 0],
out_9 = paddle.nn.functional.avg_pool2d(
x=input_NHWC_negetive,
kernel_size=ksize,
padding=[0, 0],
data_format="NHWC",
)
assert out_9.shape == (2, -1, 3, 3)
out_10 = fluid.layers.pool2d(
input=input_NCHW_negetive,
pool_size=ksize,
pool_type="avg",
pool_padding=[0, 0],
out_10 = paddle.nn.functional.avg_pool2d(
x=input_NCHW_negetive,
kernel_size=ksize,
padding=[0, 0],
data_format="NCHW",
)
assert out_10.shape == (2, 3, -1, -1)
......@@ -950,11 +940,10 @@ class TestPool2DAPI_Error(unittest.TestCase):
# data_format value error
def run_2():
out_2 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1],
out_2 = paddle.nn.functional.max_pool2d(
x=input_NHWC,
kernel_size=ksize,
padding=[1, 1],
data_format="NHWCC",
)
......@@ -962,11 +951,10 @@ class TestPool2DAPI_Error(unittest.TestCase):
# padding str value error
def run_3():
out_3 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALIDSAME",
out_3 = paddle.nn.functional.max_pool2d(
x=input_NHWC,
kernel_size=ksize,
padding="VALIDSAME",
data_format="NHWC",
)
......@@ -974,11 +962,10 @@ class TestPool2DAPI_Error(unittest.TestCase):
# padding str valid and ceil_mode value error
def run_4():
out_4 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALID",
out_4 = paddle.nn.functional.max_pool2d(
x=input_NHWC,
kernel_size=ksize,
padding="VALID",
ceil_mode=True,
data_format="NHWC",
)
......@@ -987,11 +974,10 @@ class TestPool2DAPI_Error(unittest.TestCase):
# padding with 8 ele. value error
def run_5():
out_5 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[[1, 1], [0, 0], [0, 0], [1, 1]],
out_5 = paddle.nn.functional.max_pool2d(
x=input_NHWC,
kernel_size=ksize,
padding=[[1, 1], [0, 0], [0, 0], [1, 1]],
data_format="NHWC",
)
......
......@@ -141,8 +141,8 @@ def SE_ResNeXt50Small(use_feed):
conv = conv_bn_layer(
input=conv, num_filters=16, filter_size=3, stride=1, act='relu'
)
conv = fluid.layers.pool2d(
input=conv, pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
conv = paddle.nn.functional.max_pool2d(
x=conv, kernel_size=3, stride=2, padding=1
)
cardinality = 32
......
......@@ -2121,8 +2121,8 @@ class TestBook(LayerTest):
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x', shape=[3, 224, 224], dtype='float32')
return layers.pool2d(
x, pool_size=[5, 3], pool_stride=[1, 2], pool_padding=(2, 1)
return paddle.nn.functional.max_pool2d(
x, kernel_size=[5, 3], stride=[1, 2], padding=(2, 1)
)
def make_pool2d_infershape(self):
......@@ -2133,8 +2133,8 @@ class TestBook(LayerTest):
x = paddle.nn.functional.affine_grid(
theta, out_shape=[2, 3, 244, 244]
)
return layers.pool2d(
x, pool_size=[5, 3], pool_stride=[1, 2], pool_padding=(2, 1)
return paddle.nn.functional.max_pool2d(
x, kernel_size=[5, 3], stride=[1, 2], padding=(2, 1)
)
def make_lstm_unit(self):
......
......@@ -431,7 +431,7 @@ class TestAvgPool2DDoubleGradCheckCase1(unittest.TestCase):
)
input_NCHW.persistable = True
y = layers.pool2d(input_NCHW, pool_size=2, pool_type="avg")
y = paddle.nn.functional.avg_pool2d(input_NCHW, kernel_size=2)
x_arr = np.random.uniform(-1, 1, [2, 3, 5, 5]).astype(np.float32)
gradient_checker.double_grad_check(
......@@ -533,7 +533,6 @@ class TestAvgPool2DDoubleGradCheckCase4(unittest.TestCase):
)
input_NCHW.persistable = True
y = layers.pool2d(input_NCHW, pool_size=[4, 4], pool_type="avg")
y = paddle.nn.functional.avg_pool2d(input_NCHW, kernel_size=[4, 4])
x_arr = np.random.uniform(-1, 1, [2, 3, 5, 5]).astype(np.float32)
......
......@@ -27,10 +27,10 @@ from paddle.fluid import compiler
def Lenet(data, class_dim):
conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None)
bn1 = paddle.static.nn.batch_norm(conv1, act='relu')
pool1 = fluid.layers.pool2d(bn1, 2, 'max', 2)
pool1 = paddle.nn.functional.max_pool2d(bn1, 2, 2)
conv2 = fluid.layers.conv2d(pool1, 16, 5, 1, act=None)
bn2 = paddle.static.nn.batch_norm(conv2, act='relu')
pool2 = fluid.layers.pool2d(bn2, 2, 'max', 2)
pool2 = paddle.nn.functional.max_pool2d(bn2, 2, 2)
fc1 = fluid.layers.fc(pool2, size=50, act='relu')
fc2 = fluid.layers.fc(fc1, size=class_dim, act='softmax')
......
......@@ -16,7 +16,6 @@ import unittest
import numpy as np
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.tests.unittests.op_test import OpTest
......@@ -1133,331 +1132,5 @@ create_test_padding_SAME_class(TestCase1_strides)
create_test_cudnn_padding_SAME_class(TestCase1_strides)
# ----- test API
class TestPool2DAPI(unittest.TestCase):
def test_api(self):
x_NHWC = np.random.random([2, 5, 5, 3]).astype("float32")
x_NCHW = np.random.random([2, 3, 5, 5]).astype("float32")
input_NHWC = fluid.layers.data(
name="input_NHWC",
shape=[2, 5, 5, 3],
append_batch_size=False,
dtype="float32",
)
input_NCHW = fluid.layers.data(
name="input_NCHW",
shape=[2, 3, 5, 5],
append_batch_size=False,
dtype="float32",
)
input_NHWC_negetive = fluid.layers.data(
name="input_NHWC_negetive",
shape=[2, -1, 5, 3],
append_batch_size=False,
dtype="float32",
)
input_NCHW_negetive = fluid.layers.data(
name="input_NCHW_negetive",
shape=[2, 3, -1, -1],
append_batch_size=False,
dtype="float32",
)
ksize = [3, 3]
out_1 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1],
use_cudnn=False,
data_format="NHWC",
)
out_2 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="avg",
pool_padding=[[0, 0], [1, 1], [1, 1], [0, 0]],
use_cudnn=False,
data_format="NHWC",
)
out_3 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=ksize,
pool_type="avg",
pool_padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
use_cudnn=False,
data_format="NCHW",
)
out_4 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=ksize,
pool_type="avg",
pool_padding=[1, 2, 1, 0],
use_cudnn=False,
data_format="NCHW",
)
# test VALID
out_5 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=ksize,
pool_type="avg",
pool_padding="VALID",
use_cudnn=False,
data_format="NCHW",
)
out_6 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALID",
use_cudnn=False,
data_format="NHWC",
)
# test SAME
out_7 = fluid.layers.pool2d(
input=input_NCHW,
pool_size=[4, 4],
pool_type="avg",
pool_padding="SAME",
use_cudnn=False,
data_format="NCHW",
)
out_8 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=[4, 4],
pool_type="max",
pool_padding="SAME",
use_cudnn=False,
data_format="NHWC",
)
# test negetive
out_9 = fluid.layers.pool2d(
input=input_NHWC_negetive,
pool_size=ksize,
pool_type="avg",
pool_padding=[0, 0],
use_cudnn=False,
data_format="NHWC",
)
assert out_9.shape == (2, -1, 3, 3)
out_10 = fluid.layers.pool2d(
input=input_NCHW_negetive,
pool_size=ksize,
pool_type="avg",
pool_padding=[0, 0],
use_cudnn=False,
data_format="NCHW",
)
assert out_10.shape == (2, 3, -1, -1)
exe = fluid.Executor(place=fluid.CPUPlace())
[res_1, res_2, res_3, res_4, res_5, res_6, res_7, res_8] = exe.run(
fluid.default_main_program(),
feed={
"input_NHWC": x_NHWC,
"input_NCHW": x_NCHW,
"input_NHWC_negetive": x_NHWC,
"input_NCHW_negetive": x_NCHW,
},
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8],
)
assert np.allclose(
res_1,
pool2D_forward_naive(
x=x_NHWC,
ksize=ksize,
pool_type="max",
strides=[1, 1],
paddings=[1, 1],
data_format="NHWC",
),
)
assert np.allclose(
res_2,
pool2D_forward_naive(
x=x_NHWC,
ksize=ksize,
pool_type="avg",
strides=[1, 1],
paddings=[1, 1, 1, 1],
data_format="NHWC",
),
)
assert np.allclose(
res_3,
pool2D_forward_naive(
x=x_NCHW,
ksize=ksize,
pool_type="avg",
strides=[1, 1],
paddings=[1, 1, 1, 1],
data_format="NCHW",
),
rtol=0.07,
atol=1e-05,
)
assert np.allclose(
res_4,
pool2D_forward_naive(
x=x_NCHW,
ksize=ksize,
pool_type="avg",
strides=[1, 1],
paddings=[1, 2, 1, 0],
data_format="NCHW",
),
rtol=0.07,
atol=1e-05,
)
# VALID
assert np.allclose(
res_5,
pool2D_forward_naive(
x=x_NCHW,
ksize=ksize,
pool_type="avg",
strides=[1, 1],
paddings=[10, 20], # any ele is ok
padding_algorithm="VALID",
data_format="NCHW",
),
rtol=0.07,
atol=1e-05,
)
assert np.allclose(
res_6,
pool2D_forward_naive(
x=x_NHWC,
ksize=ksize,
pool_type="max",
strides=[1, 1],
paddings=[10, 20],
padding_algorithm="VALID",
data_format="NHWC",
),
)
# SAME
assert np.allclose(
res_7,
pool2D_forward_naive(
x=x_NCHW,
ksize=[4, 4],
pool_type="avg",
strides=[1, 1],
paddings=[10, 20],
padding_algorithm="SAME",
data_format="NCHW",
),
rtol=0.07,
atol=1e-05,
)
assert np.allclose(
res_8,
pool2D_forward_naive(
x=x_NHWC,
ksize=[4, 4],
pool_type="max",
strides=[1, 1],
paddings=[10, 20],
padding_algorithm="SAME",
data_format="NHWC",
),
)
class TestPool2DAPI_Error(unittest.TestCase):
def test_api(self):
input_NHWC = fluid.layers.data(
name="input_NHWC",
shape=[2, 5, 5, 3],
append_batch_size=False,
dtype="float32",
)
ksize = [3, 3]
# cudnn type error
def run_1():
out_1 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1],
use_cudnn=[0],
data_format="NHWC",
)
self.assertRaises(TypeError, run_1)
# data_format value error
def run_2():
out_2 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1],
use_cudnn=False,
data_format="NHWCC",
)
self.assertRaises(ValueError, run_2)
# padding str value error
def run_3():
out_3 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALIDSAME",
use_cudnn=False,
data_format="NHWC",
)
self.assertRaises(ValueError, run_3)
# padding str valid and ceil_mode value error
def run_4():
out_4 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALID",
use_cudnn=False,
ceil_mode=True,
data_format="NHWC",
)
self.assertRaises(ValueError, run_4)
# padding with 8 ele. value error
def run_5():
out_5 = fluid.layers.pool2d(
input=input_NHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[[1, 1], [0, 0], [0, 0], [1, 1]],
use_cudnn=False,
data_format="NHWC",
)
self.assertRaises(ValueError, run_5)
if __name__ == '__main__':
unittest.main()
......@@ -1427,11 +1427,13 @@ def max_pool2d(
)
dtype = helper.input_dtype(input_param_name='x')
pool_out = helper.create_variable_for_type_inference(dtype)
if return_mask:
mask = helper.create_variable_for_type_inference("int32")
outputs = {"Out": pool_out, "Mask": mask}
helper.append_op(
type=op_type,
type="max_pool2d_with_index",
inputs={"X": x},
outputs=outputs,
attrs={
......@@ -1448,8 +1450,30 @@ def max_pool2d(
"data_format": data_format,
},
)
return (pool_out, mask)
return (pool_out, mask) if return_mask else pool_out
else:
outputs = {"Out": pool_out}
helper.append_op(
type="pool2d",
inputs={"X": x},
outputs=outputs,
attrs={
"pooling_type": 'max',
"ksize": kernel_size,
"global_pooling": False,
"strides": stride,
"paddings": padding,
"padding_algorithm": padding_algorithm,
"use_cudnn": True,
"ceil_mode": ceil_mode,
"use_mkldnn": False,
"exclusive": True,
"data_format": data_format,
},
)
return pool_out
def max_pool3d(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册