habana_frameworks.mediapipe.fn.RandomUniform

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

Define graph call:
  • __call__(seed)

Parameter:
  • (optional) seed - 1D tensor of size 1. Supported dimensions: minimum = 1, maximum = 1. Supported data types: INT32.

Description:

Generate random numbers from a uniform distribution.

Supported backend:
  • HPU, CPU

Keyword Arguments

kwargs

Description

low

Lower bound.

  • Type: float

  • Default: 0.0

  • Optional: yes

high

Upper bound.

  • Type: float

  • Default: 1.0

  • Optional: yes

seed

Seed to the random number generator.

  • Type: int

  • Default: 0.0

  • Optional: yes

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: FLOAT32

  • Optional: yes

  • Supported data types:

    • BFLOAT16

    • FLOAT32

Note

  1. If seed tensor is provided, then no need to initialize seed value in operator arguments.

  2. Output tensor supported with minimum rank = 1 and maximum rank = 1.

Example: RandomUniform Operator

The following code snippet shows usage of RandomUniform operator. Random data is generated as noise by RandomUniform operator and added to the image:

from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import dtype as dt

# 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.FLOAT32,
                                              device=device)

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

        self.random_uniform = fn.RandomUniform(seed=123456,
                                              low=0.1,
                                              high=0.3,
                                              dtype=dt.FLOAT32,
                                              device=device)

        self.mul = fn.Mult(dtype=dt.FLOAT32,
                          device=device)

    def definegraph(self):
        inp = self.inp()
        noise = self.random_uniform()
        out = self.mul(inp, noise)
        return inp, noise, 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, noise, out = pipe.run()

    if (device == 'cpu'):
        # Copy data as numpy array
        inp = inp.as_nparray()
        noise = noise.as_nparray()
        out = out.as_nparray()
    else:
        # Copy data to host from device as numpy array
        inp = inp.as_cpu().as_nparray()
        noise = noise.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("\nnoise tensor shape:", noise.shape)
    print("noise tensor dtype:", noise.dtype)
    print("noise tensor data:\n", noise)

    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 of RandomUniform operator:

inp tensor shape: (1, 3, 2, 3)
inp tensor dtype: float32
inp tensor data:
[[[[182. 227. 113.]
  [175. 128. 253.]]

  [[ 58. 140. 136.]
  [ 86.  80. 111.]]

  [[175. 196. 178.]
  [ 20. 163. 108.]]]]

noise tensor shape: (1,)
noise tensor dtype: float32
noise tensor data:
[0.19644217]

out tensor shape: (1, 3, 2, 3)
out tensor dtype: float32
out tensor data:
[[[[35.752476  44.592373  22.197966 ]
  [34.37738   25.144598  49.69987  ]]

  [[11.393646  27.501904  26.716135 ]
  [16.894028  15.715374  21.80508  ]]

  [[34.37738   38.502666  34.966705 ]
  [ 3.9288435 32.020073  21.215755 ]]]]