Rate This Document
Findability
Accuracy
Completeness
Readability

permute

Usage

Converts tensor dimensions.

Interface

1
torchvision.ops.permute(dims:List[int]).contiguous()

Parameters

Parameter

Description

Value Range

Input/Output

dims

Sequence, which specifies the dimension order for the transpose.

[2, 0, 1]

Input

Return Values

  • Success: KP_PT_STS_NO_ERR
  • Failure: an error code

Error Codes

Error Code

Description

KP_PT_STS_NULL_PTR_ERR

An error occurs in pointer memory allocation.

KP_PT_PERMUTE_MAP_WRN

The dims value is not supported.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import numpy as np
import torch
from torchvision import transforms, ops
# Create a 5x5 image.
src = np.array([[1, 0, 0, 0, 0],
               [0, 1, 1, 1, 0],
               [0, 1, 0, 1, 0],
               [0, 1, 1, 1, 0],
               [1, 0, 0, 0, 0]], dtype=np.float32)  

# Convert the NumPy array into a tensor.
src_tensor = torch.from_numpy(src)  

# Define the dimension rearrangement sequence: dims = [1, 0]

# Use torchvision.ops.permute to rearrange image dimensions.
permute_op = ops.Permute(dims)
permuted_tensor = permute_op(src_tensor)

print(permuted_tensor)

Output:

1
2
3
4
5
tensor([[1., 0., 0., 0., 1.],
        [0., 1., 1., 1., 0.],
        [0., 1., 0., 1., 0.],
        [0., 1., 1., 1., 0.],
        [0., 0., 0., 0., 0.]])