Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13391,15 +13391,15 @@ 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
to :ref:`api_guide_Name`. Usually name is no need to set and
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:
Expand All @@ -13408,9 +13408,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')
Expand All @@ -13422,6 +13424,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},
Expand Down
10 changes: 10 additions & 0 deletions python/paddle/fluid/tests/unittests/test_grid_sample_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
8 changes: 8 additions & 0 deletions python/paddle/fluid/tests/unittests/test_temporal_shift_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np
from op_test import OpTest

import paddle
from paddle.fluid import core


Expand Down Expand Up @@ -77,5 +78,12 @@ def initTestCase(self):
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()
14 changes: 5 additions & 9 deletions python/paddle/nn/functional/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
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
Expand Down Expand Up @@ -74,7 +73,7 @@
'generate_mask_labels',
'generate_proposal_labels',
'generate_proposals',
'grid_sampler',
'grid_sample',
'image_resize',
'image_resize_short',
# 'multi_box_head',
Expand Down Expand Up @@ -287,7 +286,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(
Expand All @@ -304,13 +303,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:
Expand Down