affine_grid

paddle.nn.functional. affine_grid ( theta, out_shape, align_corners=True, name=None ) [source]

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.

Parameters
  • theta (Tensor) – The data type can be float32 or float64.

  • out_shape (Tensor | 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.

  • align_corners (bool) – Whether to align corners of target feature map and source feature map. Default: True.

  • 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 Name.

Returns

Tensor, 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

import paddle
import paddle.nn.functional as F
import numpy as np
# theta shape = [1, 2, 3]
theta = np.array([[[-0.7, -0.4, 0.3],
                   [ 0.6,  0.5, 1.5]]]).astype("float32")
theta_t = paddle.to_tensor(theta)
y_t = F.affine_grid(
        theta_t,
        [1, 2, 3, 3],
        align_corners=False)
print(y_t)

#[[[[ 1.0333333   0.76666665]
#   [ 0.76666665  1.0999999 ]
#   [ 0.5         1.4333333 ]]
#
#  [[ 0.5666667   1.1666666 ]
#   [ 0.3         1.5       ]
#   [ 0.03333333  1.8333334 ]]
#
#  [[ 0.10000002  1.5666667 ]
#   [-0.16666666  1.9000001 ]
#   [-0.43333334  2.2333333 ]]]]