habana_frameworks.mediapipe.fn.Sub

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

Define graph call:
  • __call__(input1, input2)

Parameter:
  • input1 - First input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: INT16, INT32, FLOAT16, BFLOAT16, FLOAT32.

  • input2 - Second input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: INT16, INT32, FLOAT16, BFLOAT16, FLOAT32.

Description:

The resulting tensor is formed from the difference of the two operands element-wise. This operator performs element-wise subtraction and supports Broadcasting. Computes output as: output = (input1 - input2), element-wise.

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: FLOAT32

  • Optional: yes

  • Supported data types:

    • INT32

    • BFLOAT16

    • FLOAT32

Note

  1. All input/output tensors must be of the same data type and must have the same dimensionality except in broadcast support where dimensionality can be different.

  2. This operator is agnostic to the data layout.

Example: Sub Operator

Ther following code snippet shows usage of Sub 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
from habana_frameworks.mediapipe.operators.cpu_nodes.cpu_nodes import media_function
import numpy as np


class random_noise_generator(media_function):
    def __init__(self, params):
        self.priv_params = params['priv_params']
        self.batch_size = self.priv_params['batch_size']
        self.channel = self.priv_params['channel']
        self.width = self.priv_params['width']
        self.height = self.priv_params['height']
        np.random.seed(0)

    def __call__(self):  # NCHW
        return np.random.rand(self.batch_size, self.channel, self.height, self.width)

# 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(dir=dir,
                                                format="jpg",
                                                shuffle=False,
                                                seed=0,
                                                label_dtype=dt.UINT32)

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

        priv_params = {}
        priv_params['batch_size'] = batch_size
        priv_params['channel'] = img_c
        priv_params['width'] = img_w
        priv_params['height'] = img_h

        self.input_node = fn.MediaFunc(func=random_noise_generator, shape=[
                                      img_w, img_h, img_c, batch_size], dtype=dt.FLOAT32, priv_params=priv_params)

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

        self.sub = fn.Sub(dtype=dt.FLOAT32)

    def definegraph(self):
        images, labels = self.input()
        images = self.decode(images)
        images = self.cast(images)
        images2 = self.input_node()
        images = self.sub(images, images2)
        return images, labels


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

    # 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
    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('data:', images)


if __name__ == "__main__":
    main()

The following is the output of Sub operator:

data: [[[[-6.13178052e+13  1.19237793e+02  3.76695680e+09 ...  1.07167763e+02
    -8.20546000e+06  1.08049873e+02]
  [-8.35879552e+08  1.16205544e+02  1.15000000e+02 ...  1.11642883e+02
    -8.52577571e+21  1.04282791e+02]
  [ 1.21000008e+02  1.18344101e+02  1.17000000e+02 ...  1.08131767e+02
    -7.20720036e+11  1.06134789e+02]
  ...
  ...
  [-2.49562923e+17  7.33680038e+01 -3.07828832e+33 ...  3.72368698e+01
    4.51390026e+23  4.42827835e+01]
  [ 1.05408803e+24  7.02920532e+01  1.83894963e+35 ...  4.22932549e+01
    4.60000000e+01  4.61360626e+01]
  [ 7.90000000e+01  7.53213501e+01  2.01966875e+05 ...  4.22142677e+01
    4.60000000e+01  4.65988808e+01]]]]