crop

paddle. crop ( x, shape=None, offsets=None, name=None ) [source]

Crop input into output, as specified by offsets and shape.

* Case 1 (input is a 2-D Tensor):
    Input:
        X.shape = [3, 5]
        X.data = [[0, 1, 2, 0, 0],
                  [0, 3, 4, 0, 0],
                  [0, 0, 0, 0, 0]]
    Parameters:
        shape = [2, 2]
        offsets = [0, 1]
    Output:
        Out.shape = [2, 2]
        Out.data = [[1, 2],
                    [3, 4]]
* Case 2 (input is a 3-D Tensor):
    Input:
        X.shape = [2, 3, 4]
        X.data =  [[[0, 1, 2, 3],
                    [0, 5, 6, 7],
                    [0, 0, 0, 0]],
                   [[0, 3, 4, 5],
                    [0, 6, 7, 8],
                    [0, 0, 0, 0]]]
    Parameters:
        shape = [2, 2, -1]
        offsets = [0, 0, 1]
    Output:
        Out.shape = [2, 2, 3]
        Out.data  = [[[1, 2, 3],
                      [5, 6, 7]],
                     [[3, 4, 5],
                      [6, 7, 8]]]
Parameters
  • x (Tensor) – 1-D to 6-D Tensor, the data type is float32, float64, int32 or int64.

  • shape (list|tuple|Tensor, optional) – The output shape is specified by shape. Its data type is int32. If a list/tuple, it’s length must be the same as the dimension size of x. If a Tensor, it should be a 1-D Tensor. When it is a list, each element can be an integer or a Tensor of shape: [1]. If Variable contained, it is suitable for the case that the shape may be changed each iteration.

  • offsets (list|tuple|Variable, optional) – Specifies the cropping offsets at each dimension. Its data type is int32. If a list/tuple, it’s length must be the same as the dimension size of x. If a Tensor, it should be a 1-D Tensor. When it is a list, each element can be an integer or a Tensor of shape: [1]. If Variable contained, it is suitable for the case that the offsets may be changed each iteration. Default: None, the offsets are 0 at each dimension.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor, The cropped Tensor has same data type with x.

Examples

>>> import paddle
>>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> # x.shape = [3, 3]
>>> # x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

>>> # shape can be a 1-D Tensor or list or tuple.
>>> shape = paddle.to_tensor([2, 2], dtype='int32')
>>> # shape = [2, 2]
>>> # shape = (2, 2)
>>> out = paddle.crop(x, shape)
>>> # out.shape = [2, 2]
>>> # out = [[1,2], [4,5]]

>>> # offsets can be a 1-D Tensor or list or tuple.
>>> offsets = paddle.to_tensor([0, 1], dtype='int32')
>>> # offsets = [1, 0]
>>> # offsets = (1, 1)
>>> out = paddle.crop(x, shape, offsets)
>>> # out.shape = [2, 2]
>>> # if offsets = [0, 0], out = [[1,2], [4,5]]
>>> # if offsets = [0, 1], out = [[2,3], [5,6]]
>>> # if offsets = [1, 0], out = [[4,5], [7,8]]
>>> # if offsets = [1, 1], out = [[5,6], [8,9]]