habana_frameworks.mediapipe.fn.Constant

Class:
  • habana_frameworks.mediapipe.fn.Constant(**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:

The output tensor is filled with the data from the input tensor. The shape of the output tensor is determined by the shape of the input tensor.

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Supported data types:

    • INT8

    • UINT8

    • BFLOAT16

    • FLOAT32

Note

  1. The input and the output tensors must be of the same data type.

  2. The input tensor should be contiguous.

Example: Constant Operator

The following code snippet shows usage of Constant 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, img_c):
        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])

        self.constant = fn.Constant(dtype=dt.UINT8);

        # 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)
        const_data = self.constant(images)
        return const_data, labels

def main():
    batch_size = 6
    img_width = 200
    img_height = 200
    img_channel = 3
    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, img_channel)

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

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

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

    # Display shape, dtype, data
    print('constant tensor shape:', const_data.shape)
    print('constant tensor dtype:', const_data.dtype)
    print('data:\n', const_data)

if __name__ == "__main__":
    main()

The following is the output of Constant operator:

constant tensor shape: (6, 3, 200, 200)
constant tensor dtype: uint8
data:
[[[[122 121 118 ... 109 108 109]
  [119 118 115 ... 113 110 106]
  [121 120 117 ... 110 110 108]

  ...

  [ 78  75  75 ...  39  44  46]
  [ 73  72  74 ...  44  46  48]
  [ 79  77  78 ...  44  46  48]]]]