diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 41766c6651aadf629da97e2ba805b75af3dbc9b6..1e740ca967343f16f7d76ec6ae67da5f7d00c4e8 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -177,7 +177,6 @@ __all__ = [ 'mul', 'maxout', 'space_to_depth', - 'affine_grid', 'affine_channel', 'similarity_focus', 'hash', @@ -9693,90 +9692,6 @@ def crop_tensor(x, shape=None, offsets=None, name=None): return out -def affine_grid(theta, out_shape, name=None): - """ - :alias_main: paddle.nn.functional.affine_grid - :alias: paddle.nn.functional.affine_grid,paddle.nn.functional.vision.affine_grid - :old_api: paddle.fluid.layers.affine_grid - - It generates a grid of (x,y) coordinates using the parameters of - the affine transformation that correspond to a set of points where - the input feature map should be sampled to produce the transformed - output feature map. - - Args: - theta (Variable) - A Tensor with shape [N, 2, 3]. It contains a batch of affine transform parameters. - The data type can be float32 or float64. - out_shape (Variable | list | tuple): The shape of target output with format [batch_size, channel, height, width]. - ``out_shape`` can be a Tensor or a list or tuple. The data - type must be int32. - name(str|None): 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: - Variable: A Tensor with shape [batch_size, H, W, 2] while 'H' and 'W' are the height and width of feature map in affine transformation. The data type is the same as `theta`. - - Raises: - ValueError: If the type of arguments is not supported. - - Examples: - - .. code-block:: python - - import paddle.fluid as fluid - import numpy as np - place = fluid.CPUPlace() - theta = fluid.data(name="x", shape=[None, 2, 3], dtype="float32") - out_shape = fluid.data(name="y", shape=[4], dtype="int32") - grid_0 = fluid.layers.affine_grid(theta, out_shape) - grid_1 = fluid.layers.affine_grid(theta, [5, 3, 28, 28]) - batch_size=2 - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - output= exe.run(feed={"x": np.random.rand(batch_size,2,3).astype("float32"), - "y": np.array([5, 3, 28, 28]).astype("int32")}, - fetch_list=[grid_0.name, grid_1.name]) - print(output[0]) - print(output[1]) - """ - helper = LayerHelper('affine_grid') - - check_variable_and_dtype( - theta, 'theta', ['float32', 'float64'], 'affine_grid' - ) - - if not ( - isinstance(out_shape, list) - or isinstance(out_shape, tuple) - or isinstance(out_shape, Variable) - ): - raise ValueError("The out_shape should be a list, tuple or Variable.") - - if not isinstance(theta, Variable): - raise ValueError("The theta should be a Variable.") - - out = helper.create_variable_for_type_inference(theta.dtype) - ipts = {'Theta': theta} - attrs = {} - if isinstance(out_shape, Variable): - ipts['OutputShape'] = out_shape - check_variable_and_dtype( - out_shape, 'out_shape', ['int32'], 'affine_grid' - ) - else: - attrs['output_shape'] = out_shape - if core.is_compiled_with_rocm(): - # ROCM platform do not have MIOPEN kernel for affine_grid - attrs['use_cudnn'] = False - - helper.append_op( - type='affine_grid', - inputs=ipts, - outputs={'Output': out}, - attrs=None if len(attrs) == 0 else attrs, - ) - return out - - def pad2d( input, paddings=[0, 0, 0, 0], diff --git a/python/paddle/fluid/tests/unittests/test_affine_grid_function.py b/python/paddle/fluid/tests/unittests/test_affine_grid_function.py index cc8371ae82109cc71e35b23173b275c8c58f6dc3..420b6e61ca26653dc7ba5aa04e5c8a6b3bfd3771 100644 --- a/python/paddle/fluid/tests/unittests/test_affine_grid_function.py +++ b/python/paddle/fluid/tests/unittests/test_affine_grid_function.py @@ -52,7 +52,9 @@ class AffineGridTestCase(unittest.TestCase): theta_var = fluid.data( "input", self.theta_shape, dtype=self.dtype ) - y_var = fluid.layers.affine_grid(theta_var, self.output_shape) + y_var = paddle.nn.functional.affine_grid( + theta_var, self.output_shape + ) feed_dict = {"input": self.theta} exe = fluid.Executor(place) exe.run(start) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 7b7dfd399120fcf7be14ff62dd6be53ee8145663..2f08a12b4b4ff592b9dd68e5a30ba7b541b1a2a7 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -3278,7 +3278,9 @@ class TestBook(LayerTest): fluid.default_main_program(), fluid.default_startup_program() ): theta = self._get_data("theta", shape=[2, 3], dtype='float32') - x = fluid.layers.affine_grid(theta, out_shape=[2, 3, 244, 244]) + x = paddle.nn.functional.affine_grid( + theta, out_shape=[2, 3, 244, 244] + ) return layers.pool2d( x, pool_size=[5, 3], pool_stride=[1, 2], pool_padding=(2, 1) ) @@ -4175,8 +4177,8 @@ class TestBook(LayerTest): theta = layers.data(name="theta", shape=[2, 3], dtype="float32") out_shape = layers.data(name="out_shape", shape=[-1], dtype="int32") - data_0 = layers.affine_grid(theta, out_shape) - data_1 = layers.affine_grid(theta, [5, 3, 28, 28]) + data_0 = paddle.nn.functional.affine_grid(theta, out_shape) + data_1 = paddle.nn.functional.affine_grid(theta, [5, 3, 28, 28]) self.assertIsNotNone(data_0) self.assertIsNotNone(data_1)