habana_frameworks.mediapipe.fn.RandomNormal

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

Define graph call:
  • __call__(stddev, input)

Parameter:
  • stddev - 1D tensor of size batch size. Supported dimensions: minimum = 1, maximum = 1. Supported data types: FLOAT32.

  • input - 1D tensor of size batch size. Supported dimensions: minimum = 1, maximum = 5. Supported data types: FLOAT32.

Description:

Generate random numbers from a normal distribution. The Normal (or Gaussian) distribution describes the commonly occurring distribution of samples influenced by a large number of tiny, random disturbances, each with its unique distribution.

Supported backend:
  • HPU, CPU

Keyword Arguments

kwargs

Description

mean

Mean of the distribution.

  • Type: float

  • Default: 0.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. Output tensor supported minimum rank = 1 and maximum rank = 4.

  2. At present batching on 4th dimension is supported.

Example: RandomNormal Operator

The following code snippet shows usage of RandomNormal operator. Random data is generated as noise by RandomNormal 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_normal = fn.RandomNormal(mean=0.0, seed=123456,
                                            dtype=dt.FLOAT32,
                                            device=device)

        self.add = fn.Add(dtype=dt.FLOAT32,
                          device=device)

    def definegraph(self):
        inp = self.inp()
        std_dev = self.const()
        out = self.random_normal(std_dev, inp)
        out = self.add(inp, out)
        return inp, std_dev, 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, std_dev, out = pipe.run()

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

    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 RandomNormal 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.]]]]

std_dev tensor shape: (1,)
std_dev tensor dtype: float32
std_dev tensor data:
[0.5]

out tensor shape: (1, 3, 2, 3)
out tensor dtype: float32
out tensor data:
[[[[182.5762   227.42455  111.983475]
  [175.24469  128.19406  253.35397 ]]

  [[ 58.36465  139.38821  136.43294 ]
  [ 86.17063   80.43031  111.197205]]

  [[174.85231  196.8504   177.8005  ]
  [ 20.785583 162.62688  107.902214]]]]