habana_frameworks.mediapipe.fn.Transpose

Class:
  • habana_frameworks.mediapipe.fn.Transpose(**kwargs)

Define graph call:
  • __call__(input)

Parameter:
  • input - Input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: INT8, UINT8, BFLOAT16, FLOAT32.

Description:

Produces a transposed tensor of the input tensor along multiple axes. See https://github.com/onnx/onnx/blob/rel-1.9.0/docs/Operators.md#Transpose.

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

permutation

Each location in the permutation array defines to which dimension this location will be transposed to, permutation[rank(Input tensor)],

  • Type: list[int]

  • Default: [0, 1, 2, 3, 4]

  • Optional: yes

tensorDim

Number of valid entries in the permutation array.

  • Type: int

  • Default: 5

  • Optional: no

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Supported data types:

    • INT8

    • UINT8

    • BFLOAT16

    • FLOAT32

Note

  1. Input/output tensors must be of the same datatype.

  2. Input/output tensor must have same number of dimensions and total elements.

Example: Transpose Operator

The following code snippet shows usage of Transpose operator:

from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import imgtype as it
from habana_frameworks.mediapipe.media_types import dtype as dt
import matplotlib.pyplot as plt

# Create media pipeline derived class
class myMediaPipe(MediaPipe):
    def __init__(self, device, dir, queue_depth, batch_size, img_h, img_w):
        super(
            myMediaPipe,
            self).__init__(
            device,
            queue_depth,
            batch_size,
            self.__class__.__name__)

        self.input = fn.ReadImageDatasetFromDir(shuffle=False,
                                                dir=dir,
                                                format="jpg")

        # WHCN
        self.decode = fn.ImageDecoder(device="hpu",
                                      output_format=it.RGB_P,
                                      resize=[img_w, img_h])

        # WHCN -> CWHN
        self.transpose = fn.Transpose(permutation=[2, 0, 1, 3],
                                      tensorDim=4,
                                      dtype=dt.UINT8)

    def definegraph(self):
        images, labels = self.input()
        images = self.decode(images)
        images = self.transpose(images)
        return images, labels

def main():
    batch_size = 6
    img_width = 200
    img_height = 200
    img_dir = "/path/to/images"
    queue_depth = 2

    # Create media pipeline object
    pipe = myMediaPipe('hpu', img_dir, queue_depth, batch_size,
                        img_height, img_width)

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

    # Run media pipeline
    images, labels = pipe.run()

    # Copy data to host from device as numpy array
    images = images.as_cpu().as_nparray()
    labels = labels.as_cpu().as_nparray()

    # Display shape
    print(images.shape)

if __name__ == "__main__":
    main()

Images shape before transpose operation: NCHW

image shape: (6, 3, 200, 200)

Images shape after transpose operation: NHWC

image shape: (6, 200, 200, 3)