未验证 提交 9cc0e726 编写于 作者: X xiaoting 提交者: GitHub

Fix interpolate doc (#29104)

* fix interpolate example, test=develop;test=document_fix

* fix format, test=develop, test=document_fix

* update upsample doc, test=develop, test=document_fix
上级 9b39af3f
......@@ -209,8 +209,8 @@ def interpolate(x,
size (list|tuple|Tensor|None): Output shape of image resize
layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
Default: None. If a list, each element can be an integer or a Tensor Variable of shape: [1].
If a Tensor Variable, its dimensions size should be a 1.
Default: None. If a list, each element can be an integer or a Tensor of shape: [1].
If a Tensor, its dimensions size should be a 1.
scale_factor (float|Tensor|list|tuple|None): The multiplier for the input height or width. At
least one of :attr:`size` or :attr:`scale_factor` must be set.
And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if it is either a list or a tuple or a Tensor.
......@@ -258,7 +258,6 @@ def interpolate(x,
import paddle
import numpy as np
import paddle.nn.functional as F
paddle.disable_static()
# given out size
input_data = np.random.rand(2,3,6,10).astype("float32")
......@@ -641,8 +640,8 @@ def upsample(x,
size (list|tuple|Tensor|None): Output shape of image resize
layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
Default: None. If a list, each element can be an integer or a Tensor Variable of shape: [1].
If a Tensor Variable, its dimensions size should be a 1.
Default: None. If a list, each element can be an integer or a Tensor of shape: [1].
If a Tensor , its dimensions size should be a 1.
scale_factor (float|Tensor|list|tuple|None): The multiplier for the input height or width. At
least one of :attr:`size` or :attr:`scale_factor` must be set.
And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if
......@@ -689,10 +688,10 @@ def upsample(x,
import paddle
import numpy as np
import paddle.nn.functional as F
paddle.disable_static()
input_data = np.random.rand(2,3,6,10).astype("float32")
input = paddle.to_tensor(input_data)
output = F.upsample(input=input, size=[12,12])
output = F.upsample(x=input, size=[12,12])
print(output.shape)
# [2L, 3L, 12L, 12L]
......
......@@ -56,6 +56,9 @@ from .common import Dropout #DEFINE_ALIAS
from .common import Dropout2D #DEFINE_ALIAS
from .common import Dropout3D #DEFINE_ALIAS
from .common import AlphaDropout #DEFINE_ALIAS
from .common import Upsample #DEFINE_ALIAS
from .common import UpsamplingBilinear2D #DEFINE_ALIAS
from .common import UpsamplingNearest2D #DEFINE_ALIAS
from .pooling import AvgPool1D #DEFINE_ALIAS
from .pooling import AvgPool2D #DEFINE_ALIAS
from .pooling import AvgPool3D #DEFINE_ALIAS
......
......@@ -30,6 +30,8 @@ __all__ = [
'Pad1D',
'Pad2D',
'Pad3D',
'UpsamplingNearest2D',
'UpsamplingBilinear2D',
'CosineSimilarity',
'Dropout',
'Dropout2D',
......@@ -289,8 +291,8 @@ class Upsample(layers.Layer):
size (list|tuple|Tensor|None): Output shape of image resize
layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
Default: None. If a list, each element can be an integer or a Tensor Variable of shape: [1].
If a Tensor Variable, its dimensions size should be a 1.
Default: None. If a list, each element can be an integer or a Tensor of shape: [1].
If a Tensor , its dimensions size should be a 1.
scale_factor (float|Tensor|list|tuple|None): The multiplier for the input height or width. At
least one of :attr:`size` or :attr:`scale_factor` must be set.
And :attr:`size` has a higher priority than :attr:`scale_factor`. Has to match input size if it is either a list or a tuple or a Tensor.
......@@ -337,7 +339,6 @@ class Upsample(layers.Layer):
import paddle
import paddle.nn as nn
import numpy as np
paddle.disable_static()
input_data = np.random.rand(2,3,6,10).astype("float32")
upsample_out = paddle.nn.Upsample(size=[12,12])
......@@ -380,6 +381,159 @@ class Upsample(layers.Layer):
return out
class UpsamplingNearest2D(layers.Layer):
"""
This op upsamples a batch of images, using nearest neighbours' pixel values.
The input must be a 4-D Tensor of the shape (num_batches, channels, in_h, in_w),
where in_w is width of the input tensor, in_h is the height of the input tensor.
And the upsampling only applies on the two dimensions(height and width).
Nearest neighbor interpolation is to perform nearest neighbor interpolation
in both the 3rd dimension(in height direction) and the 4th dimension(in width
direction) on input tensor.
For details of nearest neighbor interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation.
Parameters:
x (Tensor): 4-D Tensor, its data type is float32, float64, or uint8,
its data format is specified by :attr:`data_format`.
size (list|tuple|Tensor|None): Output shape of image resize
layer, the shape is (out_h, out_w) when input is a 4-D Tensor.
Default: None. If a list, each element can be an integer or a Tensor of shape: [1].
If a Tensor , its dimensions size should be a 1.
scale_factor (float|int|list|tuple|Tensor|None): The multiplier for the input height or width. At
least one of :attr:`size` or :attr:`scale_factor` must be set.
And :attr:`size` has a higher priority than :attr:`scale_factor`.
Has to match input size if it is either a list or a tuple or a Tensor.
Default: None.
data_format (str, optional): Specify the data format of the input, and the data format of the output
will be consistent with that of the input. An optional string from:`NCW`, `NWC`, `"NCHW"`, `"NHWC"`, `"NCDHW"`,
`"NDHWC"`. The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
`[batch_size, input_channels, input_height, input_width]`. When it is `"NCHW"`, the data is stored
in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
name(str, optional): The default value is None.
Normally there is no need for user to set this property.
For more information, please refer to :ref:`api_guide_Name`
Returns:
A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels),
Examples:
.. code-block:: python
import paddle
import paddle.nn as nn
input_data = paddle.rand(2,3,6,10).astype("float32")
upsample_out = paddle.nn.UpsamplingNearest2D(size=[12,12])
input = paddle.to_tensor(input_data)
output = upsample_out(x=input)
print(output.shape)
# [2L, 3L, 12L, 12L]
"""
def __init__(self,
size=None,
scale_factor=None,
data_format='NCHW',
name=None):
super(UpsamplingNearest2D, self).__init__()
self.size = size
self.scale_factor = scale_factor
self.data_format = data_format
self.name = name
def forward(self, x):
out = F.interpolate(
x,
size=self.size,
scale_factor=self.scale_factor,
mode='nearest',
align_corners=False,
align_mode=0,
data_format=self.data_format,
name=self.name)
return out
class UpsamplingBilinear2D(layers.Layer):
"""
This op upsamples a batch of images, using bilinear' pixel values.
The input must be a 4-D Tensor of the shape (num_batches, channels, in_h, in_w),
where in_w is width of the input tensor, in_h is the height of the input tensor.
And the upsampling only applies on the two dimensions(height and width).
Bilinear interpolation is an extension of linear interpolation for
interpolating functions of two variables (e.g. H-direction and
W-direction in this op) on a rectilinear 2D grid. The key idea is
to perform linear interpolation first in one direction, and then
again in the other direction.
For details of bilinear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Bilinear_interpolation.
Parameters:
x (Tensor): 4-D Tensor, its data type is float32, float64, or uint8,
its data format is specified by :attr:`data_format`.
size (list|tuple|Tensor|None): Output shape of image resize
layer, the shape is (out_h, out_w) when input is a 4-D Tensor.
Default: None. If a list, each element can be an integer or a Tensor of shape: [1].
If a Tensor , its dimensions size should be a 1.
scale_factor (float|int|list|tuple|Tensor|None): The multiplier for the input height or width. At
least one of :attr:`size` or :attr:`scale_factor` must be set.
And :attr:`size` has a higher priority than :attr:`scale_factor`.
Has to match input size if it is either a list or a tuple or a Tensor.
Default: None.
data_format (str, optional): Specify the data format of the input, and the data format of the output
will be consistent with that of the input. An optional string from:`NCW`, `NWC`, `"NCHW"`, `"NHWC"`, `"NCDHW"`,
`"NDHWC"`. The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
`[batch_size, input_channels, input_height, input_width]`. When it is `"NCHW"`, the data is stored
in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
name(str, optional): The default value is None.
Normally there is no need for user to set this property.
For more information, please refer to :ref:`api_guide_Name`
Returns:
A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels),
Examples:
.. code-block:: python
import paddle
import paddle.nn as nn
input_data = paddle.rand(2,3,6,10).astype("float32")
upsample_out = paddle.nn.UpsamplingBilinear2D(size=[12,12])
input = paddle.to_tensor(input_data)
output = upsample_out(x=input)
print(output.shape)
# [2L, 3L, 12L, 12L]
"""
def __init__(self,
size=None,
scale_factor=None,
data_format='NCHW',
name=None):
super(UpsamplingBilinear2D, self).__init__()
self.size = size
self.scale_factor = scale_factor
self.data_format = data_format
self.name = name
def forward(self, x):
out = F.interpolate(
x,
size=self.size,
scale_factor=self.scale_factor,
mode='bilinear',
align_corners=True,
align_mode=0,
data_format=self.data_format,
name=self.name)
return out
class Bilinear(layers.Layer):
r"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册