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, CPU

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 dtype as dt
import numpy as np

# Create media pipeline derived class
class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, num_threads, dir):
        super(myMediaPipe, self).__init__(
            device,
            queue_depth,
            batch_size,
            num_threads,
            self.__class__.__name__)

        self.inp = fn.ReadNumpyDatasetFromDir(num_outputs=1,
                                              shuffle=False,
                                              dir=dir,
                                              pattern="inp_x_*.npy",
                                              dense=True,
                                              dtype=dt.UINT8,
                                              device=device)

        self.const = fn.Constant(constant=0.7,
                                dtype=dt.FLOAT32,
                                device=device)

        self.coin_flip = fn.CoinFlip(seed=100,
                                    device=device)

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

    def definegraph(self):
        inp = self.inp()
        probability = self.const()
        predicate = self.coin_flip(probability)
        out = self.random_flip(inp, predicate)
        return inp, predicate, out


def main():
    batch_size = 1
    queue_depth = 2
    num_threads = 1
    device = 'cpu'
    dir = '/path/to/numpy/files'

    # Create media pipeline object
    pipe = myMediaPipe(device, queue_depth, batch_size, num_threads, dir)

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

    # Run media pipeline
    inp, predicate, out = pipe.run()

    if (device == 'cpu'):
        # Copy data as numpy array
        inp = inp.as_nparray()
        predicate = predicate.as_nparray()
        out = out.as_nparray()
    else:
        # Copy data to host from device as numpy array
        inp = inp.as_cpu().as_nparray()
        predicate = predicate.as_cpu().as_nparray()
        out = out.as_cpu().as_nparray()

    print("\ninp tensor shape:", inp.shape)
    print("inp tensor dtype:", inp.dtype)
    print("inp tensor data:\n", inp)

    print("\npredicate tensor shape:", predicate.shape)
    print("predicate tensor dtype:", predicate.dtype)
    print("predicate tensor data:\n", predicate)

    print("\nout tensor shape:", out.shape)
    print("out tensor dtype:", out.dtype)
    print("out tensor data:\n", out)

    pipe.del_iter()

if __name__ == "__main__":
    main()

The following is the output for CoinFlip operator:

inp tensor shape: (1, 3, 2, 3)
inp tensor dtype: uint8
inp tensor data:
[[[[149 187 232]
  [160 201 202]]

  [[ 80 147 153]
  [199 174 158]]

  [[200 124 139]
  [  3 161 216]]]]

predicate tensor shape: (1,)
predicate tensor dtype: uint8
predicate tensor data:
[1]

out tensor shape: (1, 3, 2, 3)
out tensor dtype: uint8
out tensor data:
[[[[232 187 149]
  [202 201 160]]

  [[153 147  80]
  [158 174 199]]

  [[139 124 200]
  [216 161   3]]]]