habana_frameworks.mediapipe.fn.Clamp

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

Define graph call:
  • __call__(input, lower_bound, upper_bound)

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

  • (optional) lower_bound - 1D tensor of size 1, with lowerbound value in it. Supported dimensions: minimum = 1, maximum = 1. Supported data types: FLOAT16, FLOAT32, BFLOAT16.

  • (optional) upper_bound - 1D tensor of size 1, with upperbound value in it. Supported dimensions: minimum = 1, maximum = 1. Supported data types: FLOAT16, FLOAT32, BFLOAT16.

Description:

Clamps all elements in input into the range [lowerBound, upperBound].

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

upperBound

The maximum upperbound value for the input. Higher values than upperBound are clipped to upperBound.

  • Type: float

  • Default: 0.0

  • Optional: yes

lowerBound

The minimum lowerbound value for the input. Lower values than lowerBound are clipped to lowerBound.

  • Type: float

  • Default: 0.0

  • Optional: yes

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Supported data type:

    • FLOAT32

    • BFLOAT16

Note

  1. All input/output tensors must be of the same data type and must have the same dimensionality.

  2. This operator is agnostic to the data layout.

  3. lower_bound and upper_bound are optional tensors with lowerBound and upperBound values respectively.

  4. When lower_bound and upper_bound input tensors are not provided scalar params are used as lowerBound and upperBound.

  5. If the data type is BFLOAT16, then the params lowerBound and upperBound float value is converted inside the operator from float to BFLOAT16.

Example: Clamp Operator

The following code snippet shows usage of Clamp 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

# 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])

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

        self.clamp = fn.Clamp(upperBound=120.0,
                              lowerBound=50.0,
                              dtype=dt.FLOAT32)

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

    def definegraph(self):
        images, data = self.input()
        images = self.decode(images)
        images = self.pre_cast(images)
        images = self.clamp(images)
        images = self.transpose(images)
        return images, data

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 data
    print('data:\n', images)

if __name__ == "__main__":
    main()

The following is the output of Clamp operator:

data:
[[[[120. 113.  94.]
  [120. 112.  93.]
  [118. 110.  95.]

  ...

  [ 50.  50.  50.]
  [ 50.  51.  50.]
  [ 50.  53.  50.]]]]