forked from mindspore-Ecosystem/mindspore
!43432 fractional_max_pool_2d_3d
Merge pull request !43432 from yide12/fractionalmaxpool
This commit is contained in:
commit
1ed689f087
|
@ -194,6 +194,8 @@ Dropout层
|
|||
mindspore.nn.AvgPool1d
|
||||
mindspore.nn.AvgPool2d
|
||||
mindspore.nn.AvgPool3d
|
||||
mindspore.nn.FractionalMaxPool2d
|
||||
mindspore.nn.FractionalMaxPool3d
|
||||
mindspore.nn.MaxPool1d
|
||||
mindspore.nn.MaxPool2d
|
||||
mindspore.nn.MaxPool3d
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
mindspore.nn.FractionalMaxPool2d
|
||||
================================
|
||||
|
||||
.. py:class:: mindspore.nn.FractionalMaxPool2d(kernel_size, output_size=None, output_ratio=None, return_indices=False, _random_samples=None)
|
||||
|
||||
对输入的多维数据进行二维的分数最大池化运算。
|
||||
|
||||
对多个输入平面组成的输入上应用2D分数最大池化。在 :math:`(kH_{in}, kW_{in})` 区域上应用最大池化操作,由输出shape决定随机步长。对于任何输入shape,指定输出shape为 :math:`(H, W)` 。输出特征的数量等于输入平面的数量。
|
||||
在一个输入Tensor上应用2D fractional max pooling,可被视为组成一个2D平面。
|
||||
|
||||
分数最大池化的详细描述在 `Fractional Max-Pooling <https://arxiv.org/pdf/1412.6071>`_ 。
|
||||
|
||||
参数:
|
||||
- **kernel_size** (Union[int, tuple[int]]) - 指定池化核尺寸大小,如果为整数,则代表池化核的高和宽。如果为tuple,其值必须包含两个整数值分别表示池化核的高和宽。
|
||||
- **output_size** (Union[int, tuple[int]]) - 目标输出shape。如果是整数,则表示输出目标的高和宽。如果是tuple,其值必须包含两个整数值分别表示目标输出的高和宽。默认值是 `None` 。
|
||||
- **output_ratio** (Union[float, tuple[float]]) - 目标输出shape与输入shape的比率。通过输入shape和 `output_ratio` 确定输出shape。支持数据类型:float16、float32、double,数值介于0到1之间。默认值是 `None` 。
|
||||
- **return_indices** (bool) - 如果为 `True` ,返回分数最大池化的最大值的的索引值。默认值是 `False` 。
|
||||
- **_random_samples** (Tensor) - 3D张量,分数最大池化的随机步长。支持的数据类型:float16、float32、double。数值介于0到1之间。shape为 :math:`(N, C, 2)` 的Tensor。默认值是 `None` 。
|
||||
|
||||
输入:
|
||||
- **input_x** (Tensor) - shape为 :math:`(N, C, H_{in}, W_{in})` 的Tensor。支持的数据类型,float16、float32、float64、int32和int64。
|
||||
|
||||
输出:
|
||||
- **y** (Tensor) - 数据类型和输入相同,shape是 :math:`(N, C, output\underline{~}shape{H}, output\underline{~}shape{W})`。
|
||||
- **argmax** (Tensor) - 输出的索引,是一个张量。shape和输出 `y` 一致,数据类型是int64。仅当 `return_indices` 为True时,输出最大池化的索引值。
|
||||
|
||||
异常:
|
||||
- **TypeError** - `input_x` 不是float16、float32、float64、int32或int64。
|
||||
- **TypeError** - `_random_samples` 不是float16、float32或float64。
|
||||
- **ValueError** - `kernel_size` 不是整数并且不是长度为2的元组。
|
||||
- **ValueError** - `output_shape` 不是整数并且不是长度为2的元组。
|
||||
- **ValueError** - `kernel_size`, `output_shape` 与-1的和大于 `input_x` 的对应维度的量。
|
||||
- **ValueError** - `_random_samples` 维度不是3。
|
||||
- **ValueError** - `output_size` 和 `output_ratio` 同时为 `None` 。
|
||||
- **ValueError** - `input_x` 和 `_random_samples` 的第一维度大小不相等。
|
||||
- **ValueError** - `input_x` 和 `_random_samples` 第二维度大小不相等。
|
||||
- **ValueError** - `_random_samples` 第三维度大小不是2。
|
|
@ -0,0 +1,41 @@
|
|||
mindspore.nn.FractionalMaxPool3d
|
||||
================================
|
||||
|
||||
.. py:class:: mindspore.nn.FractionalMaxPool3d(kernel_size, output_size=None, output_ratio=None, return_indices=False, _random_samples=None)
|
||||
|
||||
对输入的多维数据进行三维的分数最大池化运算。
|
||||
|
||||
对多个输入平面组成的输入上应用3D分数最大池化。在 :math:`(kD_{in}, kH_{in}, kW_{in})` 区域上应用最大池化操作,由输出shape决定随机步长。输出特征的数量等于输入平面的数量。
|
||||
|
||||
分数最大池化的详细描述在 `Fractional MaxPooling by Ben Graham <https://arxiv.org/abs/1412.6071>`_ 。
|
||||
|
||||
输入输出的数据格式可以是"NCDHW"。其中,"N"是批次大小,"C"是通道数,"D"是特征深度,"H"是特征高度,"W"是特征宽度。
|
||||
|
||||
参数:
|
||||
- **kernel_size** (Union[float, tuple[int]]) - 指定池化核尺寸大小,如果为整数,则代表池化核的深、高和宽。如果为tuple,其值必须包含三个整数值分别表示池化核的深、高和宽。
|
||||
- **output_size** (Union[int, tuple[int]]) - 目标输出大小。如果是整数,则表示输出目标的深、高和宽。如果是tuple,其值必须包含三个整数值分别表示目标输出的深、高和宽。默认值是 `None` 。
|
||||
- **output_ratio** (Union[float, tuple[float]]) - 目标输出shape与输入shape的比率。通过输入shape和 `output_ratio` 确定输出shape。支持数据类型:float16、float32、double,数值介于0到1之间。默认值是 `None` 。
|
||||
- **return_indices** (bool) - 如果为 `True` ,返回分数最大池化的最大值的的索引值。默认值是 `False` 。
|
||||
- **random_samples** (Tensor) - 随机步长。支持的数据类型:float16、float32、double。shape为 :math:`(N, C, 3)` 的Tensor。数值介于0到1之间。默认值是 `None` 。
|
||||
|
||||
输入:
|
||||
- **input_x** (Tensor) - 4维或5维的张量,支持的数据类型:float16、float32、double、int32、int64。支持shape为 :math:`(N, C, D_{in}, H_{in}, W_{in})` 。
|
||||
|
||||
输出:
|
||||
- **y** (Tensor) - 3D分数最大池化的输出,是一个张量。数据类型和输入相同,shape是 :math:`(N, C, output\underline{~}shape{D}, output\underline{~}shape{H}, output\underline{~}shape{W})` 。
|
||||
- **argmax** (Tensor) - 仅当 `return_indices` 为True时,输出最大池化的索引值。shape和输出 `y` 一致。
|
||||
|
||||
异常:
|
||||
- **TypeError** - `input_x` 不是4维或5维张量。
|
||||
- **TypeError** - `random_samples` 不是3维张量。
|
||||
- **TypeError** - `x` 数据类型不是float16、float32、double、int32、int64。
|
||||
- **TypeError** - `random_samples` 数据类型不是float16、float32、double。
|
||||
- **TypeError** - `argmax` 数据类型不是int32、int64。
|
||||
- **ValueError** - `output_shape` 不是长度为3的元组。
|
||||
- **ValueError** - `kernal_size` 不是长度为3的元组。
|
||||
- **ValueError** - `output_shape` 和 `kernel_size` 不是正数。
|
||||
- **ValueError** - `output_size` 和 `output_ratio` 同时为 `None` 。
|
||||
- **ValueError** - `data_format` 数据格式不是 `NCDHW` 。
|
||||
- **ValueError** - `input_x` 和 `random_samples` 的第一维度大小不相等。
|
||||
- **ValueError** - `input_x` 和 `random_samples` 第二维度大小不相等。
|
||||
- **ValueError** - `random_samples` 第三维度大小不是3。
|
|
@ -194,6 +194,8 @@ Pooling Layer
|
|||
mindspore.nn.AvgPool1d
|
||||
mindspore.nn.AvgPool2d
|
||||
mindspore.nn.AvgPool3d
|
||||
mindspore.nn.FractionalMaxPool2d
|
||||
mindspore.nn.FractionalMaxPool3d
|
||||
mindspore.nn.MaxPool1d
|
||||
mindspore.nn.MaxPool2d
|
||||
mindspore.nn.MaxPool3d
|
||||
|
|
|
@ -24,11 +24,13 @@ import mindspore.context as context
|
|||
from mindspore.common import dtype as mstype
|
||||
from mindspore.ops.operations.nn_ops import AdaptiveMaxPool2D
|
||||
from mindspore.ops.operations.nn_ops import AdaptiveMaxPool3D, AdaptiveAvgPool3D
|
||||
from mindspore.ops.operations.nn_ops import FractionalMaxPoolWithFixedKsize, FractionalMaxPool3DWithFixedKsize
|
||||
from mindspore.ops.operations.nn_ops import MaxPool3DWithArgmax
|
||||
from mindspore.nn.cell import Cell
|
||||
|
||||
__all__ = ['AvgPool3d', 'MaxPool3d', 'AvgPool2d', 'MaxPool2d', 'AvgPool1d', 'MaxPool1d', 'AdaptiveAvgPool1d',
|
||||
'AdaptiveMaxPool1d', 'AdaptiveMaxPool2d', 'AdaptiveMaxPool3d', 'AdaptiveAvgPool2d', 'AdaptiveAvgPool3d']
|
||||
__all__ = ['AvgPool3d', 'MaxPool3d', 'AvgPool2d', 'MaxPool2d', 'AvgPool1d', 'MaxPool1d', 'FractionalMaxPool2d',
|
||||
'FractionalMaxPool3d', 'AdaptiveAvgPool1d', 'AdaptiveMaxPool1d', 'AdaptiveMaxPool2d', 'AdaptiveMaxPool3d',
|
||||
'AdaptiveAvgPool2d', 'AdaptiveAvgPool3d']
|
||||
|
||||
|
||||
class _PoolNd(Cell):
|
||||
|
@ -1071,3 +1073,257 @@ class AdaptiveMaxPool3d(Cell):
|
|||
if self.return_indices:
|
||||
return output
|
||||
return output[0]
|
||||
|
||||
|
||||
class FractionalMaxPool2d(Cell):
|
||||
r"""
|
||||
2D fractional max pooling operation for temporal data.
|
||||
|
||||
Applies a 2D fractional max pooling to an input signal composed of multiple input planes.
|
||||
The max-pooling operation is applied in kH × kW regions by a stochastic step size determined by
|
||||
the target output size. For any input size, the size of the specified output is H x W. The number
|
||||
of output features is equal to the number of input planes.
|
||||
|
||||
Fractional MaxPooling is described in the paper `Fractional Max-Pooling <https://arxiv.org/pdf/1412.6071>`_.
|
||||
|
||||
Args:
|
||||
kernel_size (Union[int, tuple[int]]): The size of kernel window used to take the maximum value.
|
||||
The target `kernel_size` is H x W. `kernel_size` can be a tuple, or a single K for K x K.
|
||||
specifying the window size (H, W) of the input tensor.
|
||||
output_size (Union[int, tuple[int]]): The target output size is H x W.
|
||||
`output_size` can be a tuple, or a single H for H x H.
|
||||
specifying the size (H, W) of the output tensor.
|
||||
Default: None.
|
||||
output_ratio (Union[float, tuple]): The target `output_ratio` is H x W.
|
||||
`output_ratio` can be a tuple, or a single H for H x H.
|
||||
Specifying the size of the output tensor by using a ratio of the input size.
|
||||
Data type : float16, float32, double, and value is between (0, 1).
|
||||
Default: None.
|
||||
return_indices (bool): If `return_indices` is True, the indices of max value would be output.
|
||||
Default: False.
|
||||
_random_samples (Tensor): The random step of FractionalMaxPool2d, which is a 3D tensor.
|
||||
Tensor of data type : float16, float32, double, and value is between (0, 1).
|
||||
Supported shape :math:`(N, C, 2)`.
|
||||
Default: None.
|
||||
|
||||
Inputs:
|
||||
- **input_x** (Tensor) - Tensor of shape :math:`(N, C, H_{in}, W_{in})`,
|
||||
with float16, float32, float64, int32, int64 data type.
|
||||
|
||||
Outputs:
|
||||
- **y** (Tensor) - Has the same type as the `input_x`.
|
||||
Has the shape :math:`(N, C, output\underline{~}shape{H}, output\underline{~}shape{W})`.
|
||||
|
||||
- **argmax** (Tensor) - The indices along with the outputs, which is a Tensor, with the same shape as the
|
||||
`y` and int64 data type. It will output only when `return_indices` is True.
|
||||
|
||||
Raises:
|
||||
TypeError: If data type of `input_x` is not one of the following: float16, float32, float64, int32, int64.
|
||||
TypeError: If data type of `_random_samples` is not one of the following: float16, float32, float64.
|
||||
ValueError: If `kernel_size` is not a number and `kernel_size` is not a tuple of length 2.
|
||||
ValueError: If `output_size` is not a number and `output_size` is not a tuple of length 2.
|
||||
ValueError: If the sum of `kernel_size` , `output_size` and -1 is larger than the corresponding
|
||||
dimension of `input_x`.
|
||||
ValueError: If the dimension of `_random_samples` is not 3.
|
||||
ValueError: if `output_size` and `output_ratio` are None at the same time.
|
||||
ValueError: If the first dimension size of `input_x` and `_random_samples` is not equal.
|
||||
ValueError: If the second dimension size of `input_x` and `_random_samples` is not equal.
|
||||
ValueError: If the third dimension size of `_random_samples` is not 2.
|
||||
|
||||
Supported Platforms:
|
||||
``CPU``
|
||||
|
||||
Examples:
|
||||
>>> # the kernel_size is an int number and the output_size is a tuple.
|
||||
>>> import numpy as np
|
||||
>>> from mindspore import nn
|
||||
>>> from mindspore import Tensor
|
||||
>>> import mindspore.common.dtype as mstype
|
||||
>>> input_x = Tensor(np.array([0.3220, 0.9545, 0.7879, 0.0975, 0.3698,
|
||||
... 0.5135, 0.5740, 0.3435, 0.1895, 0.8764,
|
||||
... 0.9581, 0.4760, 0.9014, 0.8522, 0.3664,
|
||||
... 0.4980, 0.9673, 0.9879, 0.6988, 0.9022,
|
||||
... 0.9304, 0.1558, 0.0153, 0.1559, 0.9852]).reshape([1, 1, 5, 5]), mstype.float32)
|
||||
>>> _random_samples = Tensor(np.array([[[0.8, 0.8]]]), mstype.float32)
|
||||
>>> net = nn.FractionalMaxPool2d(kernel_size=2, output_size=(2, 2), _random_samples=_random_samples,
|
||||
... return_indices=True)
|
||||
>>> y, argmax = net(input_x)
|
||||
>>> print(y)
|
||||
Tensor(shape=[1, 1, 2, 2], dtype=Float32, value=
|
||||
[[[[9.54500020e-001, 8.76399994e-001],
|
||||
[9.67299998e-001, 9.85199988e-001]]]])
|
||||
>>> print(argmax)
|
||||
Tensor(shape=[1, 1, 2, 2], dtype=Int64, value=
|
||||
[[[[ 1, 9],
|
||||
[16, 24]]]])
|
||||
>>> net = nn.FractionalMaxPool2d(kernel_size=2, output_ratio=(0.5, 0.5), _random_samples=_random_samples,
|
||||
... return_indices=True)
|
||||
>>> y, argmax = net(input_x)
|
||||
>>> print(y)
|
||||
Tensor(shape=[1, 1, 2, 2], dtype=Float32, value=
|
||||
[[[[9.54500020e-001, 8.76399994e-001],
|
||||
[9.67299998e-001, 9.85199988e-001]]]])
|
||||
>>> print(argmax)
|
||||
Tensor(shape=[1, 1, 2, 2], dtype=Int64, value=
|
||||
[[[[ 1, 9],
|
||||
[16, 24]]]])
|
||||
"""
|
||||
|
||||
def __init__(self, kernel_size, output_size=None, output_ratio=None, return_indices=False, _random_samples=None):
|
||||
"""Initialize FractionalMaxPool2d."""
|
||||
super(FractionalMaxPool2d, self).__init__()
|
||||
self.return_indices = return_indices
|
||||
self.output_ratio = None
|
||||
if _random_samples is None:
|
||||
_random_samples = Tensor(np.array([[[0, 0]]]), mstype.float32)
|
||||
self.random_samples = _random_samples
|
||||
if output_ratio is not None:
|
||||
if isinstance(output_ratio, float):
|
||||
output_ratio = (output_ratio, output_ratio)
|
||||
validator.check_float_range(output_ratio[0], 0.0, 1.0, Rel.INC_RIGHT)
|
||||
validator.check_float_range(output_ratio[1], 0.0, 1.0, Rel.INC_RIGHT)
|
||||
self.kernel_size = kernel_size
|
||||
self.output_ratio = output_ratio
|
||||
elif output_size is not None:
|
||||
self.fractional_max_pool2d = FractionalMaxPoolWithFixedKsize(kernel_size, output_size)
|
||||
else:
|
||||
raise ValueError("'output_size' and 'output_ratio' can not be None at the same time.")
|
||||
|
||||
def construct(self, x):
|
||||
if self.output_ratio is not None:
|
||||
output_size = (int(x.shape[-2] * self.output_ratio[0]), int(x.shape[-1] * self.output_ratio[1]))
|
||||
fractional_max_pool2d = FractionalMaxPoolWithFixedKsize(self.kernel_size, output_size)
|
||||
output = fractional_max_pool2d(x, self.random_samples)
|
||||
if self.return_indices:
|
||||
return output
|
||||
return output[0]
|
||||
output = self.fractional_max_pool2d(x, self.random_samples)
|
||||
if self.return_indices:
|
||||
return output
|
||||
return output[0]
|
||||
|
||||
|
||||
class FractionalMaxPool3d(Cell):
|
||||
r"""
|
||||
3D fractional max pooling operation for temporal data.
|
||||
|
||||
This operator applies a 3D fractional max pooling over an input signal composed of several input planes.
|
||||
The max-pooling operation is applied in kD x kH x kW regions by a stochastic step size determined
|
||||
by the target output size.The number of output features is equal to the number of input planes.
|
||||
|
||||
Refer to the paper `Fractional MaxPooling by Ben Graham <https://arxiv.org/abs/1412.6071>`_ for more details.
|
||||
|
||||
The input and output data format can be "NCDHW". N is the batch size, C is the number of channels,
|
||||
D the feature depth, H is the feature height, and W is the feature width.
|
||||
|
||||
Args:
|
||||
kernel_size (Union[float, tuple]): The target `kernel_size` is D x H x W.
|
||||
`kernel_size` can be a tuple, or a single K for K x K x K.
|
||||
specifying the window size (D, H, W) of the input tensor.
|
||||
output_size (Union[int, tuple]): The target `output_size` is D x H x W.
|
||||
`output_size` can be a tuple, or a single H for H x H x H.
|
||||
Specifying the size (D, H, W) of the output tensor.
|
||||
Default: None.
|
||||
output_ratio (Union[float, tuple]): The target `output_ratio` is D x H x W.
|
||||
`output_ratio` can be a tuple, or a single H for H x H x H.
|
||||
Specifying the size of the output tensor by using a ratio of the input size.
|
||||
Data type : float16, float32, double, and value is between (0, 1).
|
||||
Default: None.
|
||||
return_indices (bool): If `return_indices` is True, the indices of max value would be output.
|
||||
Default: False.
|
||||
_random_samples (Tensor): The random step of FractionalMaxPool3d, which is a 3D tensor.
|
||||
Tensor of data type : float16, float32, double, and value is between (0, 1).
|
||||
Supported shape :math:`(N, C, 3)`
|
||||
|
||||
Inputs:
|
||||
- **imput_x** (Tensor) - The input of FractionalMaxPool3d, which is a 4D or 5D tensor.
|
||||
Tensor of data type : float16, float32, double, int32, int64.
|
||||
Supported shape :math:`(N, C, D_{in}, H_{in}, W_{in})` .
|
||||
|
||||
Outputs:
|
||||
- **y** (Tensor) - A tensor, the output of FractionalMaxPool3d.
|
||||
Has the same data type with `imput_x`.
|
||||
Tensor of shape :math:`(N, C, D_{out}, H_{out}, W_{out})` .
|
||||
|
||||
- **argmax** (Tensor) - The indices along with the outputs, which is a Tensor, with the same shape as the
|
||||
`y` and int32 data type. It will output only when `return_indices` is True.
|
||||
|
||||
Raises:
|
||||
TypeError: If `input_x` is not a 4D or 5D tensor.
|
||||
TypeError: If `_random_samples` is not a 3D tensor.
|
||||
TypeError: If data type of `imput_x` is not float16, float32, double, int32, int64.
|
||||
TypeError: If dtype of `_random_samples` is not float16, float32, double.
|
||||
TypeError: If dtype of `argmax` is not int32, int64.
|
||||
ValueError: If `output_size` is a tuple and if `output_size` length is not 3.
|
||||
ValueError: If `kernel_size` is a tuple and if `kernel_size` length is not 3.
|
||||
ValueError: If numbers in `output_size` or `kernel_size` is not positive.
|
||||
ValueError: if `output_size` and `output_ratio` are None at the same time.
|
||||
ValueError: If the first dimension size of `input_x` and `_random_samples` is not equal.
|
||||
ValueError: If the second dimension size of `input_x` and `_random_samples` is not equal.
|
||||
ValueError: If the third dimension size of `_random_samples` is not 3.
|
||||
|
||||
Supported Platforms:
|
||||
``GPU`` ``CPU``
|
||||
|
||||
Examples:
|
||||
>>> import numpy as np
|
||||
>>> from mindspore import nn
|
||||
>>> from mindspore import Tensor
|
||||
>>> import mindspore.common.dtype as mstype
|
||||
>>> x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
|
||||
... .reshape([1, 1, 2, 2, 4]), mstype.float32)
|
||||
>>> _random_samples = Tensor(np.array([0.7, 0.7, 0.7]).reshape([1, 1, 3]), mstype.float32)
|
||||
>>> net = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), output_size=(1, 1, 3),
|
||||
... _random_samples=_random_samples, return_indices=True)
|
||||
>>> output, argmax = net(x)
|
||||
>>> print(output)
|
||||
Tensor(shape=[1, 1, 1, 1, 3], dtype=Float32, value=
|
||||
[[[[[1.30000000e+001, 1.40000000e+001, 1.60000000e+001]]]]])
|
||||
>>> print(argmax)
|
||||
Tensor(shape=[1, 1, 1, 1, 3], dtype=Int64, value=
|
||||
[[[[[12, 13, 15]]]]])
|
||||
>>> net = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), output_ratio=(0.5, 0.5, 0.5),
|
||||
... _random_samples=_random_samples, return_indices=True)
|
||||
>>> output, argmax = net(x)
|
||||
>>> print(output)
|
||||
Tensor(shape=[1, 1, 1, 1, 2], dtype=Float32, value=
|
||||
[[[[[1.30000000e+001, 1.60000000e+001]]]]])
|
||||
>>> print(argmax)
|
||||
Tensor(shape=[1, 1, 1, 1, 2], dtype=Int64, value=
|
||||
[[[[[12, 15]]]]])
|
||||
"""
|
||||
|
||||
def __init__(self, kernel_size, output_size=None, output_ratio=None, return_indices=False, _random_samples=None):
|
||||
"""Initialize FractionalMaxPool3d."""
|
||||
super(FractionalMaxPool3d, self).__init__()
|
||||
self.return_indices = return_indices
|
||||
self.output_ratio = None
|
||||
if _random_samples is None:
|
||||
_random_samples = Tensor(np.array([0, 0, 0]).reshape([1, 1, 3]), mstype.float32)
|
||||
self.random_samples = _random_samples
|
||||
if output_ratio is not None:
|
||||
if isinstance(output_ratio, float):
|
||||
output_ratio = (output_ratio, output_ratio, output_ratio)
|
||||
validator.check_float_range(output_ratio[0], 0.0, 1.0, Rel.INC_RIGHT)
|
||||
validator.check_float_range(output_ratio[1], 0.0, 1.0, Rel.INC_RIGHT)
|
||||
validator.check_float_range(output_ratio[2], 0.0, 1.0, Rel.INC_RIGHT)
|
||||
self.kernel_size = kernel_size
|
||||
self.output_ratio = output_ratio
|
||||
elif output_size is not None:
|
||||
self.fractional_max_pool3d = FractionalMaxPool3DWithFixedKsize(kernel_size, output_size)
|
||||
else:
|
||||
raise ValueError("'output_size' and 'output_ratio' can not be None at the same time.")
|
||||
|
||||
def construct(self, x):
|
||||
if self.output_ratio:
|
||||
output_size = (int(x.shape[-3] * self.output_ratio[0]), int(x.shape[-2] * self.output_ratio[1]),
|
||||
int(x.shape[-1] * self.output_ratio[2]))
|
||||
fractional_max_pool3d = FractionalMaxPool3DWithFixedKsize(self.kernel_size, output_size)
|
||||
output = fractional_max_pool3d(x, self.random_samples)
|
||||
if self.return_indices:
|
||||
return output
|
||||
return output[0]
|
||||
output = self.fractional_max_pool3d(x, self.random_samples)
|
||||
if self.return_indices:
|
||||
return output
|
||||
return output[0]
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
# Copyright 2022 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
import mindspore.common.dtype as mstype
|
||||
import mindspore as ms
|
||||
|
||||
|
||||
class FractionalMaxPool2dNet(nn.Cell):
|
||||
"""FractionalMaxPool2d"""
|
||||
|
||||
def __init__(self):
|
||||
super(FractionalMaxPool2dNet, self).__init__()
|
||||
_random_samples = Tensor(np.array([[[0.8, 0.8]]]), mstype.float32)
|
||||
self.pool1 = nn.FractionalMaxPool2d(kernel_size=2, output_size=(2, 2), _random_samples=_random_samples,
|
||||
return_indices=True)
|
||||
self.pool2 = nn.FractionalMaxPool2d(kernel_size=2, output_ratio=(0.5, 0.5), _random_samples=_random_samples,
|
||||
return_indices=True)
|
||||
|
||||
def construct(self, x):
|
||||
output1 = self.pool1(x)
|
||||
output2 = self.pool2(x)
|
||||
return output1, output2
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.platform_arm_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
|
||||
def test_fractional_maxpool2d_normal(mode):
|
||||
"""
|
||||
Feature: FractionalMaxPool2d
|
||||
Description: Verify the result of FractionalMaxPool2d
|
||||
Expectation: success
|
||||
"""
|
||||
ms.set_context(mode=mode)
|
||||
net = FractionalMaxPool2dNet()
|
||||
input_x = Tensor(np.random.rand(25).reshape([1, 1, 5, 5]), mstype.float32)
|
||||
output1, output2 = net(input_x)
|
||||
assert output1[0].shape == output1[1].shape == (1, 1, 2, 2)
|
||||
assert output2[0].shape == output2[1].shape == (1, 1, 2, 2)
|
||||
input_x = Tensor([[[[5.58954370e-001, 6.63938331e-001, 6.21228504e-001, 2.42979444e-001, 3.76893662e-001],
|
||||
[1.81983045e-003, 3.52343421e-001, 4.62048613e-001, 1.10343760e-001, 1.39571702e-001],
|
||||
[4.99799584e-001, 4.64907907e-001, 6.20357162e-001, 3.59420753e-001, 1.26215309e-001],
|
||||
[7.71829579e-002, 4.58553624e-001, 3.58015698e-001, 3.53923170e-001, 1.75972716e-001],
|
||||
[5.65106732e-001, 6.46603699e-001, 6.05013040e-001, 3.82114821e-001, 4.62306777e-003]]]],
|
||||
mstype.float32)
|
||||
output1, output2 = net(input_x)
|
||||
expect_output_y = np.array([[[[6.63938344e-001, 3.76893669e-001],
|
||||
[6.46603703e-001, 3.82114828e-001]]]])
|
||||
expect_output_argmax = np.array([[[[1, 4],
|
||||
[21, 23]]]])
|
||||
assert np.allclose(output1[0].asnumpy(), expect_output_y)
|
||||
assert np.allclose(output1[1].asnumpy(), expect_output_argmax)
|
||||
assert np.allclose(output2[0].asnumpy(), expect_output_y)
|
||||
assert np.allclose(output2[1].asnumpy(), expect_output_argmax)
|
||||
|
||||
|
||||
class FractionalMaxPool3dNet(nn.Cell):
|
||||
"""FractionalMaxPool3d"""
|
||||
|
||||
def __init__(self):
|
||||
super(FractionalMaxPool3dNet, self).__init__()
|
||||
_random_samples = Tensor(np.array([0.7, 0.7, 0.7]).reshape([1, 1, 3]), mstype.float32)
|
||||
self.pool1 = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), _random_samples=_random_samples,
|
||||
output_size=(1, 1, 2), return_indices=True)
|
||||
self.pool2 = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), output_ratio=(0.5, 0.5, 0.5),
|
||||
_random_samples=_random_samples, return_indices=True)
|
||||
|
||||
def construct(self, x):
|
||||
output1 = self.pool1(x)
|
||||
output2 = self.pool2(x)
|
||||
return output1, output2
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.platform_arm_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
|
||||
def test_fractional_maxpool3d_normal(mode):
|
||||
"""
|
||||
Feature: Test FractioanlMaxPool3d
|
||||
Description: Test the functionality of FractionalMaxPool3d
|
||||
Expectation: Success
|
||||
"""
|
||||
ms.set_context(mode=mode)
|
||||
input_x = Tensor(np.random.rand(16).reshape([1, 1, 2, 2, 4]), mstype.float32)
|
||||
net = FractionalMaxPool3dNet()
|
||||
output1, output2 = net(input_x)
|
||||
assert output1[0].shape == output1[1].shape == (1, 1, 1, 1, 2)
|
||||
assert output2[0].shape == output2[1].shape == (1, 1, 1, 1, 2)
|
||||
input_x = Tensor([[[[[5.76273143e-001, 7.97047436e-001, 5.05385816e-001, 7.98332036e-001],
|
||||
[5.79880655e-001, 9.75979388e-001, 3.17571498e-002, 8.08261558e-002]],
|
||||
[[3.82758647e-001, 7.09801614e-001, 4.39641386e-001, 5.71077049e-001],
|
||||
[9.16305065e-001, 3.71438652e-001, 6.52868748e-001, 6.91260636e-001]]]]], mstype.float32)
|
||||
output1, output2 = net(input_x)
|
||||
expect_output_y = np.array([[[[[9.16305065e-001, 6.91260636e-001]]]]])
|
||||
expect_output_argmax = np.array([[[[[12, 15]]]]])
|
||||
assert np.allclose(output1[0].asnumpy(), expect_output_y)
|
||||
assert np.allclose(output1[1].asnumpy(), expect_output_argmax)
|
||||
assert np.allclose(output2[0].asnumpy(), expect_output_y)
|
||||
assert np.allclose(output2[1].asnumpy(), expect_output_argmax)
|
|
@ -0,0 +1,67 @@
|
|||
# Copyright 2022 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
import mindspore.common.dtype as mstype
|
||||
import mindspore as ms
|
||||
|
||||
|
||||
class FractionalMaxPool3dNet(nn.Cell):
|
||||
"""FractionalMaxPool3d"""
|
||||
|
||||
def __init__(self):
|
||||
super(FractionalMaxPool3dNet, self).__init__()
|
||||
_random_samples = Tensor(np.array([0.7, 0.7, 0.7]).reshape([1, 1, 3]), mstype.float32)
|
||||
self.pool1 = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), _random_samples=_random_samples,
|
||||
output_size=(1, 1, 2), return_indices=True)
|
||||
self.pool2 = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), output_ratio=(0.5, 0.5, 0.5),
|
||||
_random_samples=_random_samples, return_indices=True)
|
||||
|
||||
def construct(self, x):
|
||||
output1 = self.pool1(x)
|
||||
output2 = self.pool2(x)
|
||||
return output1, output2
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
|
||||
def test_fractional_maxpool3d_normal(mode):
|
||||
"""
|
||||
Feature: Test FractioanlMaxPool3d
|
||||
Description: Test the functionality of FractionalMaxPool3d
|
||||
Expectation: Success
|
||||
"""
|
||||
ms.set_context(mode=mode)
|
||||
input_x = Tensor(np.random.rand(16).reshape([1, 1, 2, 2, 4]), mstype.float32)
|
||||
net = FractionalMaxPool3dNet()
|
||||
output1, output2 = net(input_x)
|
||||
assert output1[0].shape == output1[1].shape == (1, 1, 1, 1, 2)
|
||||
assert output2[0].shape == output2[1].shape == (1, 1, 1, 1, 2)
|
||||
input_x = Tensor([[[[[5.76273143e-001, 7.97047436e-001, 5.05385816e-001, 7.98332036e-001],
|
||||
[5.79880655e-001, 9.75979388e-001, 3.17571498e-002, 8.08261558e-002]],
|
||||
[[3.82758647e-001, 7.09801614e-001, 4.39641386e-001, 5.71077049e-001],
|
||||
[9.16305065e-001, 3.71438652e-001, 6.52868748e-001, 6.91260636e-001]]]]], mstype.float32)
|
||||
output1, output2 = net(input_x)
|
||||
expect_output_y = np.array([[[[[9.16305065e-001, 6.91260636e-001]]]]])
|
||||
expect_output_argmax = np.array([[[[[12, 15]]]]])
|
||||
assert np.allclose(output1[0].asnumpy(), expect_output_y)
|
||||
assert np.allclose(output1[1].asnumpy(), expect_output_argmax)
|
||||
assert np.allclose(output2[0].asnumpy(), expect_output_y)
|
||||
assert np.allclose(output2[1].asnumpy(), expect_output_argmax)
|
|
@ -0,0 +1,84 @@
|
|||
# Copyright 2022 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
"""
|
||||
test fractional maxpooling api
|
||||
"""
|
||||
import numpy as np
|
||||
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.common.api import _cell_graph_executor
|
||||
import mindspore.common.dtype as mstype
|
||||
|
||||
|
||||
class FractionalMaxPool2dNet(nn.Cell):
|
||||
"""FractionalMaxPool2d"""
|
||||
|
||||
def __init__(self):
|
||||
super(FractionalMaxPool2dNet, self).__init__()
|
||||
_random_samples = Tensor(np.array([[[0.8, 0.8]]]), mstype.float32)
|
||||
self.pool1 = nn.FractionalMaxPool2d(kernel_size=2, output_size=(2, 2), _random_samples=_random_samples,
|
||||
return_indices=True)
|
||||
self.pool2 = nn.FractionalMaxPool2d(kernel_size=2, output_ratio=(0.5, 0.5), _random_samples=_random_samples,
|
||||
return_indices=True)
|
||||
|
||||
def construct(self, x):
|
||||
output1 = self.pool1(x)
|
||||
output2 = self.pool2(x)
|
||||
return output1, output2
|
||||
|
||||
|
||||
def test_compile_fractional_maxpool2d():
|
||||
"""
|
||||
Feature: Test FractioanlMaxPool2d
|
||||
Description: Test the functionality of FractionalMaxPool2d
|
||||
Expectation: Success
|
||||
"""
|
||||
input_x = Tensor(np.array([0.3220, 0.9545, 0.7879, 0.0975, 0.3698,
|
||||
0.5135, 0.5740, 0.3435, 0.1895, 0.8764,
|
||||
0.9581, 0.4760, 0.9014, 0.8522, 0.3664,
|
||||
0.4980, 0.9673, 0.9879, 0.6988, 0.9022,
|
||||
0.9304, 0.1558, 0.0153, 0.1559, 0.9852]).reshape([1, 1, 5, 5]), mstype.float32)
|
||||
net = FractionalMaxPool2dNet()
|
||||
_cell_graph_executor.compile(net, input_x)
|
||||
|
||||
|
||||
class FractionalMaxPool3dNet(nn.Cell):
|
||||
"""FractionalMaxPool3d"""
|
||||
|
||||
def __init__(self):
|
||||
super(FractionalMaxPool3dNet, self).__init__()
|
||||
_random_samples = Tensor(np.array([0.7, 0.7, 0.7]).reshape([1, 1, 3]), mstype.float32)
|
||||
self.pool1 = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), output_size=(1, 1, 2),
|
||||
_random_samples=_random_samples, return_indices=True)
|
||||
self.pool2 = nn.FractionalMaxPool3d(kernel_size=(1.0, 1.0, 1.0), output_ratio=(0.5, 0.5, 0.5),
|
||||
_random_samples=_random_samples, return_indices=True)
|
||||
|
||||
def construct(self, x):
|
||||
output1 = self.pool1(x)
|
||||
output2 = self.pool2(x)
|
||||
return output1, output2
|
||||
|
||||
|
||||
def test_compile_fractional_maxpool3d():
|
||||
"""
|
||||
Feature: Test FractioanlMaxPool3d
|
||||
Description: Test the functionality of FractionalMaxPool3d
|
||||
Expectation: Success
|
||||
"""
|
||||
input_x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
|
||||
.reshape([1, 1, 2, 2, 4]), mstype.float32)
|
||||
net = FractionalMaxPool3dNet()
|
||||
_cell_graph_executor.compile(net, input_x)
|
Loading…
Reference in New Issue