habana_frameworks.mediapipe.fn.CoinFlip

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

Define graph call:
  • __call__(input, seed)

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

  • (optional) seed - Seed to the random number generator. This is a scalar value. Supported dimensions: minimum = 1, maximum = 1. Supported data types: UINT32.

Description:

Outputs a tensor with Random numbers from a Bernoulli distribution. Bernoulli distribution is the discrete probability distribution of a random variable which takes the value 1 with probability p and the value 0 with probability q = 1 - p.

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

seed

Seed to the random number generator, only positive integer.

  • Type: int

  • Default: 100

  • Optional: yes

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Supported data types:

    • INT32

Note

  1. Input type BF16/F16 maps to output type INT16 and Input type FLOAT32 maps to Output type INT32.

  2. Seed can be given either as a static attribute or as a input tensor. If given as input tensor, the static parameter will be ignored.

Example: CoinFlip Operator

The following code snippet shows usage of CoinFlip operator. The predicate tensor generated by CoinFlip operator is fed to RandomFlip operator for flipping the images:

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

# 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.input_data = np.ones(shape=[batch_size],
                                  dtype=np.float32) * 0.3

        self.input_node = fn.MediaConst(data=self.input_data,
                                        shape=[batch_size],
                                        dtype='float32')

        self.random_bernoulli = fn.CoinFlip(seed=100,
                                            dtype=dt.INT32)

        self.random_flip = fn.RandomFlip(horizontal=1)

        # 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)
        input_node = self.input_node()
        predicate = self.random_bernoulli(input_node)
        images = self.random_flip(images, predicate)
        images = self.transpose(images)
        return images, labels, predicate

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, predicate = pipe.run()

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

    # Display images
    plt.figure(figsize=(10, 10))
    for i in range(batch_size):
        ax = plt.subplot(2, 3, i + 1)
        plt.imshow(images[i].astype("uint8"))
        title = "flipped" if predicate[i]  else "not flipped"
        plt.title(title)
        plt.axis("off")
    plt.show()

    print('predicate dtype:', predicate.dtype)
    print('predicate:\n', predicate)

if __name__ == "__main__":
    main()

The following is the output for CoinFlip operator:

predicate dtype: uint8
predicate:
 [1 1 0 1 1 0]