habana_frameworks.mediapipe.fn.Transpose
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)],
|
tensorDim |
Number of valid entries in the permutation array.
|
dtype |
Output data type.
|
Note
Input/output tensors must be of the same datatype.
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
import numpy as np
import os
g_display_timeout = os.getenv("DISPLAY_TIMEOUT") or 5
# Create MediaPipe derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, batch_size, num_threads, op_device, dir, img_h, img_w):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
num_threads,
self.__class__.__name__)
self.input = fn.ReadImageDatasetFromDir(shuffle=False,
dir=dir,
format="jpg",
device="cpu")
# WHCN
self.decode = fn.ImageDecoder(device="hpu",
output_format=it.RGB_I,
resize=[img_w, img_h])
# WHCN -> CWHN
self.transpose = fn.Transpose(permutation=[0, 2, 1, 3],
tensorDim=4,
dtype=dt.UINT8,
device=op_device)
def definegraph(self):
imgs, lbls = self.input()
imgs = self.decode(imgs)
transp_imgs = self.transpose(imgs)
return transp_imgs, imgs, lbls
def display_images(images, batch_size, cols):
rows = (batch_size + 1) // cols
plt.figure(figsize=(10, 10))
for i in range(batch_size):
ax = plt.subplot(rows, cols, i + 1)
plt.imshow(images[i])
plt.axis("off")
plt.show(block=False)
plt.pause(g_display_timeout)
plt.close()
def run(device, op_device):
batch_size = 6
queue_depth = 2
num_threads = 1
img_width = 200
img_height = 200
base_dir = os.environ['DATASET_DIR']
dir = base_dir + "/img_data/"
columns = 3
# Create MediaPipe object
pipe = myMediaPipe(device, queue_depth, batch_size,
num_threads, op_device, dir,
img_height, img_width)
# Build MediaPipe
pipe.build()
# Initialize MediaPipe iterator
pipe.iter_init()
# Run MediaPipe
transp_imgs, imgs, lbls = pipe.run()
def as_cpu(tensor):
if (callable(getattr(tensor, "as_cpu", None))):
tensor = tensor.as_cpu()
return tensor
# Copy data to host from device as numpy array
transp_imgs = as_cpu(transp_imgs).as_nparray()
imgs = as_cpu(imgs).as_nparray()
lbls = as_cpu(lbls).as_nparray()
del pipe
# Display shape
print('input shape', imgs.shape)
print('output shape', transp_imgs.shape)
display_images(transp_imgs, batch_size, columns)
return imgs, transp_imgs
def compare_ref(inp, out):
ref = np.transpose(inp, [0, 2, 1, 3])
if np.array_equal(ref, out) == False:
raise ValueError(f"Mismatch w.r.t ref")
if __name__ == "__main__":
dev_opdev = {'mixed': ['hpu'],
'legacy': ['hpu']}
for dev in dev_opdev.keys():
for op_dev in dev_opdev[dev]:
inp, out = run(dev, op_dev)
compare_ref(inp, out)
Transposed Output Images 1
- 1
Licensed under a CC BY SA 4.0 license. The images used here are taken from https://data.caltech.edu/records/mzrjq-6wc02.
Images shape before transpose operation: NCHW
image shape: (6, 3, 200, 200)
Images shape after transpose operation: NHWC
image shape: (6, 200, 200, 3)