mindspore.ops.max_pool2d

View Source On Gitee
mindspore.ops.max_pool2d(x, kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)[source]

Performs a 2D max pooling on the input Tensor.

Typically, the input is a Tensor with shape (Nin,Cin,Hin,Win), outputs regional maximum in the (Hin,Win)-dimension. Given kernel_size ks=(hker,wker) and stride s=(s0,s1), the operation is as follows:

output(Ni,Cj,h,w)=maxm=0,,hker1maxn=0,,wker1input(Ni,Cj,s0×h+m,s1×w+n)
Parameters
  • x (Tensor) – Tensor of shape (Nin,Cin,Hin,Win) with data type of int8, int16, int32, int64, uint8, uint16, uint32, uint64, float16, float32 or float64 in CPU or GPU while that of uint16 in Ascend.

  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the maximum value and arg value, is an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both stride, or a tuple of two int numbers that represent height and width of movement respectively. Default: None , which indicates the moving step is kernel_size .

  • padding (Union[int, tuple[int]]) – An int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 0 .

  • dilation (Union[int, tuple[int]]) – Control the stride of elements in the kernel. Default: 1 .

  • return_indices (bool) – Whether to output the indices of max value. Default: False .

  • ceil_mode (bool) – Whether to use ceil instead of floor to calculate output shape. Default: False .

Returns

If return_indices is False , return a Tensor output, else return a tuple (output, argmax).

  • output (Tensor) - Maxpooling result, with shape (Nout,Cout,Hout,Wout). It has the same data type as x.

Hout=Hin+2padding[0]dilation[0]×(kernel_size[0]1)1stride[0]+1
Wout=Win+2padding[1]dilation[1]×(kernel_size[1]1)1stride[1]+1
  • argmax (Tensor) - Index corresponding to the maximum value. In CPU and GPU, data type is int64 while that is uint16 in Ascend. It will be return only when return_indices is True.

Raises
  • TypeError – If x is not a Tensor.

  • ValueError – If length of shape of x is not equal to 4.

  • TypeError – If kernel_size , stride , padding or dilation is not int or tuple.

  • ValueError – If kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If padding is more than half of kernel_size.

  • TypeError – If ceil_mode is not bool.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.arange(20 * 16 * 50 * 32).reshape((20, 16, 50, 32)), mindspore.float32)
>>> output_tensor, argmax = ops.max_pool2d(x, kernel_size=(3, 2), stride=(2, 1), return_indices=True)
>>> print(output_tensor.shape)
(20, 16, 24, 31)
>>> print(argmax.shape)
(20, 16, 24, 31)