habana_frameworks.mediapipe.fn.Cast

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

Define graph call:
  • __call__(input)

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

Description:

This operator changes the data type of the input tensor.

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

round_mode

Rounding mode selection.

  • Type: CastF32RoundMode_t

  • Default: 0

  • Optional: yes

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Supported data types:

    • INT8

    • UINT8

    • INT32

    • UINT32

    • BFLOAT16

    • FLOAT32

Note

  1. Input and output tensors must have the different data type. Self cast is not allowed.

  2. Cast between all supported data types is allowed.

Example: Cast Operator

The following code snippet shows usage of Cast operator, decoder output is casted to FLOAT32. Print of output image array shows float numbers.

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

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

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

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

        self.cast = fn.Cast(dtype=dt.FLOAT32)

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

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

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

    # 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 data
    print(images)

if __name__ == "__main__":
    main()

The following is the output of Cast operator:

[[[[122. 113.  94.]
  [121. 112.  93.]
  [119. 111.  94.]
  ...
  [108.  99. 114.]
  [108.  99. 114.]
  [109. 100. 115.]]

  ...

  [[101. 125.  78.]
  [100. 123.  78.]
  [ 97. 120.  78.]

  ...

  [ 41.  50.  44.]
  [ 42.  51.  46.]
  [ 45.  54.  49.]]]]