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

remove pool3d from fluid (#48455)

* remove pool3d
上级 7078c1e1
...@@ -71,7 +71,6 @@ __all__ = [ ...@@ -71,7 +71,6 @@ __all__ = [
'conv2d', 'conv2d',
'softmax', 'softmax',
'pool2d', 'pool2d',
'pool3d',
'batch_norm', 'batch_norm',
'reduce_mean', 'reduce_mean',
'reduce_all', 'reduce_all',
...@@ -1895,246 +1894,6 @@ def pool2d( ...@@ -1895,246 +1894,6 @@ def pool2d(
return pool_out return pool_out
@templatedoc()
def pool3d(
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="NCDHW",
):
"""
${comment}
Args:
input (Variable): The input tensor of pooling operator, which is a 5-D tensor with
shape [N, C, D, H, W]. The format of
input tensor is `"NCDHW"` or `"NDHWC"`, where `N` is batch size, `C` is
the number of channels, `D` is the depth of the feature,
`H` is the height of the feature, and `W` is the width
of the feature.
pool_size (int|list|tuple): The pool kernel size. If pool kernel size
is a tuple or list, it must contain three integers,
(pool_size_Depth, pool_size_Height, pool_size_Width).
Otherwise, the pool kernel size will be the cube of an int.
pool_type (string): ${pooling_type_comment}
pool_stride (string|int|list|tuple)): The pool padding. If `pool_padding` is a string, either 'VALID' or
'SAME' which is the padding algorithm. If pool stride size is a tuple or list,
it must contain three integers, `[stride_Depth, stride_Height, stride_Width]`.
Otherwise, the pool stride size will be a cube of an int.
pool_padding (int|list|tuple): The pool padding size. If pool padding size is a tuple or list,
it could be in three forms: `[pad_depth, pad_height, pad_width]` or
`[pad_depth_front, pad_depth_back, pad_height_top, pad_height_bottom, pad_width_left, pad_width_right]`,
and when `data_format` is `"NCDHW"`, `pool_padding` can be in the form
`[[0,0], [0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]`.
when `data_format` is `"NDHWC"`, `pool_padding` can be in the form
`[[0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]`.
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: `"NCDHW"`, `"NDHWC"`.
The default is `"NCDHW"`. When it is `"NCDHW"`, the data is stored in the order of:
`[batch_size, input_channels, input_depth, 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 "NCDHW" or "NDHWC".
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, 32], dtype='float32')
# max pool3d
pool3d = fluid.layers.pool3d(
input = data,
pool_size = 2,
pool_type = "max",
pool_stride = 1,
global_pooling=False)
# average pool3d
pool3d = fluid.layers.pool3d(
input = data,
pool_size = 2,
pool_type = "avg",
pool_stride = 1,
global_pooling=False)
# global average pool3d
pool3d = fluid.layers.pool3d(
input = data,
pool_size = 2,
pool_type = "avg",
pool_stride = 1,
global_pooling=True)
# example 1:
# Attr(pool_padding) is a list with 6 elements, Attr(data_format) is "NCDHW".
out_1 = fluid.layers.pool3d(
input = data,
pool_size = 2,
pool_type = "avg",
pool_stride = 1,
pool_padding = [1, 2, 1, 0, 1, 2],
global_pooling = False,
data_format = "NCDHW")
# example 2:
# Attr(pool_padding) is a string, Attr(data_format) is "NCDHW".
out_2 = fluid.layers.pool3d(
input = data,
pool_size = 3,
pool_type = "avg",
pool_stride = 1,
pool_padding = "VALID",
global_pooling = False,
data_format = "NCDHW")
"""
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 Attr(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 ["NCDHW", "NDHWC"]:
raise ValueError(
"Attr(data_format) should be 'NCDHW' or 'NDHWC'. Received "
"Attr(data_format): %s" % str(data_format)
)
pool_size = utils.convert_to_list(pool_size, 3, 'pool_size')
pool_stride = utils.convert_to_list(pool_stride, 3, 'pool_stride')
def update_padding(padding, data_format):
def is_list_or_tuple(ele):
if isinstance(ele, (list, tuple)):
return True
return False
if is_list_or_tuple(padding) and len(padding) == 5:
if is_list_or_tuple(padding[0]) and (data_format == "NCDHW"):
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:5]
padding = [ele for a_list in padding for ele in a_list]
elif is_list_or_tuple(padding[0]) and (data_format == "NDHWC"):
if not (padding[0] == [0, 0] and padding[4] == [0, 0]):
raise ValueError(
"Non-zero pool_padding(%s) in the batch or channel dimensions "
"is not supported." % str(padding)
)
padding = padding[1:4]
padding = [ele for a_list in padding for ele in a_list]
padding = utils.convert_to_list(padding, 6, 'padding')
if utils._is_symmetric_padding(padding, 3):
padding = [padding[0], padding[2], padding[4]]
elif is_list_or_tuple(padding) and len(padding) == 6:
padding = utils.convert_to_list(padding, 6, 'padding')
if utils._is_symmetric_padding(padding, 3):
padding = [padding[0], padding[2], padding[4]]
else:
padding = utils.convert_to_list(padding, 3, '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, 0]
if ceil_mode != False:
raise ValueError(
"When Attr(pool_padding) is \"VALID\", ceil_mode must be False. "
"Received ceil_mode: True."
)
elif pool_padding == "SAME":
padding_algorithm = "SAME"
pool_padding = [0, 0, 0]
pool_padding = update_padding(pool_padding, data_format)
op_type = "pool3d"
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
def batch_norm( def batch_norm(
input, input,
act=None, act=None,
......
...@@ -37,7 +37,6 @@ class TensorRTPool3dTest(InferencePassTest): ...@@ -37,7 +37,6 @@ class TensorRTPool3dTest(InferencePassTest):
self.pool_type = 'max' self.pool_type = 'max'
self.pool_stride = 1 self.pool_stride = 1
self.pool_padding = 0 self.pool_padding = 0
self.global_pooling = False
self.ceil_mode = False self.ceil_mode = False
self.exclusive = False self.exclusive = False
self.enable_trt = True self.enable_trt = True
...@@ -64,16 +63,23 @@ class TensorRTPool3dTest(InferencePassTest): ...@@ -64,16 +63,23 @@ class TensorRTPool3dTest(InferencePassTest):
shape=[-1, self.channel, self.depth, self.height, self.width], shape=[-1, self.channel, self.depth, self.height, self.width],
dtype='float32', dtype='float32',
) )
pool_out = fluid.layers.pool3d( if self.pool_type == "max":
input=data, pool_out = paddle.nn.functional.max_pool3d(
pool_size=self.pool_size, x=data,
pool_type=self.pool_type, kernel_size=self.pool_size,
pool_stride=self.pool_stride, stride=self.pool_stride,
pool_padding=self.pool_padding, padding=self.pool_padding,
global_pooling=self.global_pooling, ceil_mode=self.ceil_mode,
ceil_mode=self.ceil_mode, )
exclusive=self.exclusive, else:
) pool_out = paddle.nn.functional.avg_pool3d(
x=data,
kernel_size=self.pool_size,
stride=self.pool_stride,
padding=self.pool_padding,
ceil_mode=self.ceil_mode,
exclusive=self.exclusive,
)
# out = fluid.layers.batch_norm(pool_out, is_test=True) # out = fluid.layers.batch_norm(pool_out, is_test=True)
self.fetch_list = [pool_out] self.fetch_list = [pool_out]
...@@ -158,62 +164,6 @@ class TensorRTAvgPool3dTest(TensorRTPool3dTest): ...@@ -158,62 +164,6 @@ class TensorRTAvgPool3dTest(TensorRTPool3dTest):
self.pool_type = 'avg' self.pool_type = 'avg'
self.pool_stride = 1 self.pool_stride = 1
self.pool_padding = 0 self.pool_padding = 0
self.global_pooling = False
self.ceil_mode = False
self.exclusive = False
class TensorRTGlobalPool3dTest(TensorRTPool3dTest):
def set_extra_config(self):
self.pool_size = 2
self.pool_type = 'max'
self.pool_stride = 1
self.pool_padding = 0
self.global_pooling = True
self.ceil_mode = False
self.exclusive = False
class TensorRTCeilPool3dTest(TensorRTPool3dTest):
def set_extra_config(self):
self.pool_size = 2
self.pool_type = 'max'
self.pool_stride = 1
self.pool_padding = 0
self.global_pooling = False
self.ceil_mode = True
self.exclusive = False
class TensorRTExclusivePool3dTest(TensorRTPool3dTest):
def set_extra_config(self):
self.pool_size = 2
self.pool_type = 'max'
self.pool_stride = 1
self.pool_padding = 0
self.global_pooling = False
self.ceil_mode = False
self.exclusive = True
class TensorRTSamePaddingPool3dTest(InferencePassTest):
def set_extra_config(self):
self.pool_size = 2
self.pool_type = 'max'
self.pool_stride = 1
self.pool_padding = 'SAME'
self.global_pooling = False
self.ceil_mode = False
self.exclusive = False
class TensorRTValidPaddingPool3dTest(InferencePassTest):
def set_extra_config(self):
self.pool_size = 2
self.pool_type = 'max'
self.pool_stride = 1
self.pool_padding = 'VALID'
self.global_pooling = False
self.ceil_mode = False self.ceil_mode = False
self.exclusive = False self.exclusive = False
......
...@@ -3178,20 +3178,6 @@ class TestBook(LayerTest): ...@@ -3178,20 +3178,6 @@ class TestBook(LayerTest):
x, pool_size=[5, 3], pool_stride=[1, 2], pool_padding=(2, 1) x, pool_size=[5, 3], pool_stride=[1, 2], pool_padding=(2, 1)
) )
def make_pool3d(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(
name='x', shape=[3, 244, 244, 244], dtype='float32'
)
return layers.pool3d(
x,
pool_size=[5, 3, 2],
pool_stride=[1, 2, 3],
pool_padding=(2, 1, 1),
)
def make_lstm_unit(self): def make_lstm_unit(self):
with program_guard( with program_guard(
fluid.default_main_program(), fluid.default_startup_program() fluid.default_main_program(), fluid.default_startup_program()
......
...@@ -18,7 +18,6 @@ import numpy as np ...@@ -18,7 +18,6 @@ import numpy as np
import paddle import paddle
import paddle.fluid.core as core import paddle.fluid.core as core
from op_test import OpTest from op_test import OpTest
import paddle.fluid as fluid
def adaptive_start_index(index, input_size, output_size): def adaptive_start_index(index, input_size, output_size):
...@@ -1027,292 +1026,5 @@ create_test_cudnn_padding_VALID_class(TestCase4_channel_last) ...@@ -1027,292 +1026,5 @@ create_test_cudnn_padding_VALID_class(TestCase4_channel_last)
create_test_cudnn_padding_VALID_class(TestCase5_channel_last) create_test_cudnn_padding_VALID_class(TestCase5_channel_last)
# test API
class TestPool3DAPI(unittest.TestCase):
def test_api(self):
x_NDHWC = np.random.random([2, 5, 5, 5, 3]).astype("float32")
x_NCDHW = np.random.random([2, 3, 5, 5, 5]).astype("float32")
input_NDHWC = fluid.layers.data(
name="input_NDHWC",
shape=[2, 5, 5, 5, 3],
append_batch_size=False,
dtype="float32",
)
input_NCDHW = fluid.layers.data(
name="input_NCDHW",
shape=[2, 3, 5, 5, 5],
append_batch_size=False,
dtype="float32",
)
ksize = [3, 3, 3]
out_1 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1, 1],
use_cudnn=False,
data_format="NDHWC",
)
out_2 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="avg",
pool_padding=[[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]],
use_cudnn=False,
data_format="NDHWC",
)
out_3 = fluid.layers.pool3d(
input=input_NCDHW,
pool_size=ksize,
pool_type="avg",
pool_padding=[[0, 0], [0, 0], [1, 1], [1, 1], [1, 1]],
use_cudnn=False,
data_format="NCDHW",
)
out_4 = fluid.layers.pool3d(
input=input_NCDHW,
pool_size=ksize,
pool_type="avg",
pool_padding=[1, 2, 1, 0, 0, 1],
use_cudnn=False,
data_format="NCDHW",
)
# test VALID
out_5 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="avg",
pool_padding="VALID",
use_cudnn=False,
data_format="NDHWC",
)
out_6 = fluid.layers.pool3d(
input=input_NCDHW,
pool_size=ksize,
pool_type="avg",
pool_padding="VALID",
use_cudnn=False,
data_format="NCDHW",
)
# test SAME
out_7 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_stride=[1, 1, 2],
pool_type="avg",
pool_padding="SAME",
use_cudnn=False,
data_format="NDHWC",
)
out_8 = fluid.layers.pool3d(
input=input_NCDHW,
pool_size=[4, 4, 4],
pool_type="avg",
pool_padding="SAME",
use_cudnn=False,
data_format="NCDHW",
)
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_NDHWC": x_NDHWC, "input_NCDHW": x_NCDHW},
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8],
)
assert np.allclose(
res_1,
pool3D_forward_naive(
x=x_NDHWC,
ksize=ksize,
pool_type="max",
strides=[1, 1, 1],
paddings=[1, 1, 1],
data_format="NDHWC",
),
)
assert np.allclose(
res_2,
pool3D_forward_naive(
x=x_NDHWC,
ksize=ksize,
pool_type="avg",
strides=[1, 1, 1],
paddings=[1, 1, 1, 1, 1, 1],
data_format="NDHWC",
),
)
assert np.allclose(
res_3,
pool3D_forward_naive(
x=x_NCDHW,
ksize=ksize,
pool_type="avg",
strides=[1, 1, 1],
paddings=[1, 1, 1, 1, 1, 1],
data_format="NCDHW",
),
rtol=0.07,
atol=1e-05,
)
assert np.allclose(
res_4,
pool3D_forward_naive(
x=x_NCDHW,
ksize=ksize,
pool_type="avg",
strides=[1, 1, 1],
paddings=[1, 2, 1, 0, 0, 1],
data_format="NCDHW",
),
rtol=0.07,
atol=1e-05,
)
# VALID
assert np.allclose(
res_5,
pool3D_forward_naive(
x=x_NDHWC,
ksize=ksize,
pool_type="avg",
strides=[1, 1, 1],
paddings=[10, 20],
padding_algorithm="VALID",
data_format="NDHWC",
),
)
assert np.allclose(
res_6,
pool3D_forward_naive(
x=x_NCDHW,
ksize=ksize,
pool_type="avg",
strides=[1, 1, 1],
paddings=[10, 20],
padding_algorithm="VALID",
data_format="NCDHW",
),
rtol=0.07,
atol=1e-05,
)
# SAME
assert np.allclose(
res_7,
pool3D_forward_naive(
x=x_NDHWC,
ksize=ksize,
pool_type="avg",
strides=[1, 1, 2],
paddings=[10, 20],
padding_algorithm="SAME",
data_format="NDHWC",
),
)
assert np.allclose(
res_8,
pool3D_forward_naive(
x=x_NCDHW,
ksize=[4, 4, 4],
pool_type="avg",
strides=[1, 1, 1],
paddings=[10, 20],
padding_algorithm="SAME",
data_format="NCDHW",
),
rtol=0.07,
atol=1e-05,
)
class TestPool3DAPI_Error(unittest.TestCase):
def test_api(self):
input_NDHWC = fluid.layers.data(
name="input_NDHWC",
shape=[2, 5, 5, 5, 3],
append_batch_size=False,
dtype="float32",
)
ksize = [3, 3, 3]
# cudnn type error
def run_1():
out_1 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1, 1],
use_cudnn=[0],
data_format="NDHWC",
)
self.assertRaises(TypeError, run_1)
# data_format value error
def run_2():
out_2 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[1, 1, 1],
use_cudnn=False,
data_format="NDHWCC",
)
self.assertRaises(ValueError, run_2)
# padding str value error
def run_3():
out_3 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALIDSAME",
use_cudnn=False,
data_format="NDHWC",
)
self.assertRaises(ValueError, run_3)
# padding str valid and ceil_mode value error
def run_4():
out_4 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="max",
pool_padding="VALID",
use_cudnn=False,
ceil_mode=True,
data_format="NDHWC",
)
self.assertRaises(ValueError, run_4)
# padding with 8 ele. value error
def run_5():
out_5 = fluid.layers.pool3d(
input=input_NDHWC,
pool_size=ksize,
pool_type="max",
pool_padding=[[1, 1], [0, 0], [0, 0], [1, 1], [1, 1]],
use_cudnn=False,
data_format="NDHWC",
)
self.assertRaises(ValueError, run_5)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册