diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index bec47d9227e1acde4ef9780548a48ab58b8d1f8c..b3908e3c8ebc1923f431ad7b7e05f2fdf9c03592 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -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}, diff --git a/python/paddle/fluid/tests/unittests/test_grid_sample_function.py b/python/paddle/fluid/tests/unittests/test_grid_sample_function.py index ea94a8ba69a784efb1a2a12f6f251316553cab50..9ad0309a70e31cb83ff7bfeeef6e10460e116e5c 100644 --- a/python/paddle/fluid/tests/unittests/test_grid_sample_function.py +++ b/python/paddle/fluid/tests/unittests/test_grid_sample_function.py @@ -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() diff --git a/python/paddle/fluid/tests/unittests/test_temporal_shift_op.py b/python/paddle/fluid/tests/unittests/test_temporal_shift_op.py index f800f7b2ca857c7cf507ee2c4f3184dbd1ff5974..1fbc0fc4604c2c790271254a961999ef0190e2df 100644 --- a/python/paddle/fluid/tests/unittests/test_temporal_shift_op.py +++ b/python/paddle/fluid/tests/unittests/test_temporal_shift_op.py @@ -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() diff --git a/python/paddle/nn/functional/vision.py b/python/paddle/nn/functional/vision.py index a74a98d5ed45b9f613b0f2f6d5f04544ffae3d2a..7f86e56df1b54f62149a9af628e8bb2e783df4ed 100644 --- a/python/paddle/nn/functional/vision.py +++ b/python/paddle/nn/functional/vision.py @@ -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: