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
import os


# Create MediaPipe derived class
class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, num_threads, op_device, 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="cpu")

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

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

    def definegraph(self):
        inp = self.inp()
        noise = self.random_uniform()
        out = self.mul(inp, noise)
        return inp, noise, out


def run(device, op_device):
    batch_size = 4
    queue_depth = 2
    num_threads = 1
    base_dir = os.environ['DATASET_DIR']
    dir = base_dir+"/npy_data/fp32/"

    # Create MediaPipe object
    pipe = myMediaPipe(device, queue_depth, batch_size,
                      num_threads, op_device, dir)

    # Build MediaPipe
    pipe.build()

    # Initialize MediaPipe iterator
    pipe.iter_init()

    # Run MediaPipe
    inp, noise, out = pipe.run()

    def as_cpu(tensor):
        if (callable(getattr(tensor, "as_cpu", None))):
            tensor = tensor.as_cpu()
        return tensor

    # Copy data to host from device as numpy array
    inp = as_cpu(inp).as_nparray()
    noise = as_cpu(noise).as_nparray()
    out = as_cpu(out).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__":
    dev_opdev = {'cpu': ['cpu'],
                'mixed': ['hpu'],
                'legacy': ['hpu']}
    for dev in dev_opdev.keys():
        for op_dev in dev_opdev[dev]:
            run(dev, op_dev)

The following is the output of RandomUniform operator:

inp tensor shape: (4, 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.]]]


[[[186. 254.  96.]
  [180.  64. 132.]]

  [[149.  50. 117.]
  [213.   6. 111.]]

  [[ 77.  11. 160.]
  [129. 102. 154.]]]


[[[100.  95. 164.]
  [134. 131. 112.]]

  [[ 77.  38. 127.]
  [123.  87.  71.]]

  [[227. 186. 223.]
  [ 35.  56. 166.]]]


[[[ 62. 144.  89.]
  [142.  15. 199.]]

  [[103. 166.  14.]
  [ 91.  89.  24.]]

  [[ 89. 135.  15.]
  [ 99. 103.  43.]]]]

noise tensor shape: (4, 1)
noise tensor dtype: float32
noise tensor data:
[[0.12539397]
[0.18694901]
[0.13565043]
[0.13125855]]

out tensor shape: (4, 3, 2, 3)
out tensor dtype: float32
out tensor data:
[[[[22.821703  28.464432  14.169518 ]
  [21.943945  16.050428  31.724674 ]]

  [[ 7.2728505 17.555157  17.05358  ]
  [10.783881  10.031518  13.918731 ]]

  [[21.943945  24.577219  22.320127 ]
  [ 2.5078795 20.439217  13.542549 ]]]


[[[34.77252   47.48505   17.947105 ]
  [33.65082   11.964737  24.677269 ]]

  [[27.855404   9.34745   21.873035 ]
  [39.82014    1.1216941 20.75134  ]]

  [[14.395074   2.0564392 29.911842 ]
  [24.116423  19.0688    28.790148 ]]]


[[[13.5650425 12.88679   22.24667  ]
  [18.177156  17.770206  15.192848 ]]

  [[10.445083   5.154716  17.227604 ]
  [16.685003  11.801587   9.63118  ]]

  [[30.792646  25.23098   30.250046 ]
  [ 4.747765   7.596424  22.517971 ]]]


[[[ 8.13803   18.90123   11.682011 ]
  [18.638714   1.9688783 26.120451 ]]

  [[13.51963   21.78892    1.8376197]
  [11.944528  11.682011   3.1502051]]

  [[11.682011  17.719904   1.9688783]
  [12.9945965 13.51963    5.6441174]]]]