未验证 提交 7f9b198d 编写于 作者: L LielinJiang 提交者: GitHub

Romove grid_sampler and refine example code (#27649)

* refine grid_sample and temporal_shift
上级 a01bc6b3
......@@ -13421,7 +13421,7 @@ def temporal_shift(x, seg_num, shift_ratio=0.25, name=None):
${comment}
Args:
x(Variable): ${x_comment}
x(Tensor): ${x_comment}
seg_num(int): ${seg_num_comment}
shift_ratio(float): ${shift_ratio_comment}
name(str, optional): For detailed information, please refer
......@@ -13429,7 +13429,7 @@ def temporal_shift(x, seg_num, shift_ratio=0.25, name=None):
None by default.
Returns:
out(Variable): The temporal shifting result is a tensor variable with the
out(Tensor): The temporal shifting result is a tensor with the
same shape and same data type as the input.
Raises:
......@@ -13438,9 +13438,11 @@ def temporal_shift(x, seg_num, shift_ratio=0.25, name=None):
Examples:
.. code-block:: python
import paddle.fluid as fluid
input = fluid.data(name='input', shape=[None,4,2,2], dtype='float32')
out = fluid.layers.temporal_shift(x=input, seg_num=2, shift_ratio=0.2)
import paddle
import paddle.nn.functional as F
input = paddle.randn([6, 4, 2, 2])
out = F.temporal_shift(x=input, seg_num=2, shift_ratio=0.2)
"""
helper = LayerHelper("temporal_shift", **locals())
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'temporal_shift')
......@@ -13452,6 +13454,10 @@ def temporal_shift(x, seg_num, shift_ratio=0.25, name=None):
if not isinstance(seg_num, int):
raise TypeError("seg_num must be int type.")
if in_dygraph_mode():
return core.ops.temporal_shift(x, 'seg_num', seg_num, 'shift_ratio',
shift_ratio)
helper.append_op(
type="temporal_shift",
inputs={"X": x},
......
......@@ -127,5 +127,15 @@ def load_tests(loader, standard_tests, pattern):
return suite
class TestGridSampleAPI(unittest.TestCase):
def test_errors(self):
with self.assertRaises(ValueError):
x = paddle.randn([1, 1, 3, 3])
F.grid_sample(x, 1.0)
with self.assertRaises(ValueError):
x = paddle.randn([1, 1, 3, 3])
F.grid_sample(1.0, x)
if __name__ == '__main__':
unittest.main()
......@@ -18,6 +18,7 @@ import unittest
import numpy as np
from op_test import OpTest
import paddle
from paddle.fluid import core
......@@ -77,5 +78,12 @@ class TestTemporalShift3(TestTemporalShift):
self.shift_ratio = 0.3
class TestTemporalShiftAPI(unittest.TestCase):
def test_api(self):
input = paddle.randn([6, 4, 2, 2])
out = paddle.nn.functional.temporal_shift(
x=input, seg_num=2, shift_ratio=0.2)
if __name__ == "__main__":
unittest.main()
......@@ -34,7 +34,6 @@ from ...fluid.layers import distribute_fpn_proposals #DEFINE_ALIAS
from ...fluid.layers import generate_mask_labels #DEFINE_ALIAS
from ...fluid.layers import generate_proposal_labels #DEFINE_ALIAS
from ...fluid.layers import generate_proposals #DEFINE_ALIAS
from ...fluid.layers import grid_sampler #DEFINE_ALIAS
from ...fluid.layers import image_resize #DEFINE_ALIAS
from ...fluid.layers import prior_box #DEFINE_ALIAS
from ...fluid.layers import prroi_pool #DEFINE_ALIAS
......@@ -74,7 +73,7 @@ __all__ = [
'generate_mask_labels',
'generate_proposal_labels',
'generate_proposals',
'grid_sampler',
'grid_sample',
'image_resize',
'image_resize_short',
# 'multi_box_head',
......@@ -205,25 +204,35 @@ def grid_sample(x,
data x and y is indexing the 3rd dimension (in height dimension),
finally results is the bilinear interpolation or nearest value of 4 nearest corner
points. The output tensor shape will be [N, C, H, W].
Step 1:
Get (x, y) grid coordinates and scale to [0, H-1/W-1].
.. code-block:: text
grid_x = 0.5 * (grid[:, :, :, 0] + 1) * (W - 1)
grid_y = 0.5 * (grid[:, :, :, 1] + 1) * (H - 1)
Step 2:
Indices input data X with grid (x, y) in each [H, W] area, and bilinear
interpolate point value by 4 nearest points or nearest interpolate point value
by nearest point.
.. code-block:: text
Step 1:
Get (x, y) grid coordinates and scale to [0, H-1/W-1].
.. code-block:: text
grid_x = 0.5 * (grid[:, :, :, 0] + 1) * (W - 1)
grid_y = 0.5 * (grid[:, :, :, 1] + 1) * (H - 1)
Step 2:
Indices input data X with grid (x, y) in each [H, W] area, and bilinear
interpolate point value by 4 nearest points or nearest interpolate point value
by nearest point.
wn ------- y_n ------- en
| | |
| d_n |
| | |
x_w --d_w-- grid--d_e-- x_e
| | |
| d_s |
| | |
ws ------- y_s ------- wn
wn ------- y_n ------- en
| | |
| d_n |
| | |
x_w --d_w-- grid--d_e-- x_e
| | |
| d_s |
| | |
ws ------- y_s ------- wn
For bilinear interpolation:
x_w = floor(x) // west side x coord
x_e = x_w + 1 // east side x coord
......@@ -237,8 +246,10 @@ def grid_sample(x,
en = X[:, :, y_n, x_e] // north-east point value
ws = X[:, :, y_s, x_w] // south-east point value
es = X[:, :, y_s, x_w] // north-east point value
output = wn * d_e * d_s + en * d_w * d_s
+ ws * d_e * d_n + es * d_w * d_n
+ ws * d_e * d_n + es * d_w * d_n
Args:
x(Tensor): The input tensor, which is a 4-d tensor with shape
[N, C, H, W], N is the batch size, C is the channel
......@@ -262,7 +273,9 @@ def grid_sample(x,
Tensor, The shape of output is [N, C, grid_H, grid_W] in which `grid_H` is the height of grid and `grid_W` is the width of grid. The data type is same as input tensor.
Examples:
.. code-block:: python
import paddle
import paddle.nn.functional as F
import numpy as np
......@@ -287,7 +300,7 @@ def grid_sample(x,
[ 0.7, 0.4],
[ 0.2, 0.8]]]]).astype("float64")
paddle.disable_static()
x = paddle.to_tensor(x)
grid = paddle.to_tensor(grid)
y_t = F.grid_sample(
......@@ -304,13 +317,10 @@ def grid_sample(x,
# [ 0.596 0.38 0.52 0.24 ]]]]
"""
helper = LayerHelper("grid_sample", **locals())
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'grid_sampler')
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'grid_sample')
check_variable_and_dtype(grid, 'grid', ['float32', 'float64'],
'grid_sampler')
if not isinstance(x, Variable):
raise ValueError("The x should be a Variable")
if not isinstance(grid, Variable):
raise ValueError("The grid should be a Variable")
'grid_sample')
_modes = ['bilinear', 'nearest']
_padding_modes = ['zeros', 'reflection', 'border']
if mode not in _modes:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册