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 - Input tensor to operator. Supported dimensions: minimum = 1, maximum = 4. 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. Input tensor is optional. Shape of output tensor will be taken from shape parameter if input tensor is not provided.

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

shape

Shape of output data.

  • Type: list[int]

  • Default: []

  • Optional: yes

dims

Size of shape list.

  • Type: int

  • Default: 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
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.FLOAT32,
                                              device=device)

        std_data = np.ones(batch_size,dtype=dt.FLOAT32)
        std_data = std_data*0.5
        self.std_dev = fn.MediaConst(data=std_data,
                                      shape=[batch_size],
                                      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.std_dev()
        out = self.random_normal(std_dev, inp)
        out = self.add(inp, out)
        return inp, out


def main():
    batch_size = 4
    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, out = pipe.run()

    if (device == 'cpu'):
        # Copy data as numpy array
        inp = inp.as_nparray()
        out = out.as_nparray()
    else:
        # Copy data to host from device as numpy array
        inp = inp.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("\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: (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.]]]]

out tensor shape: (4, 3, 2, 3)
out tensor dtype: float32
out tensor data:
[[[[182.02159   226.45992   113.44177  ]
  [174.48589   128.09915   253.14125  ]]

  [[ 58.315804  139.90982   136.57831  ]
  [ 85.26446    78.99895   110.69604  ]]

  [[174.77766   196.26617   177.5818   ]
  [ 20.043173  162.30621   107.23093  ]]]


[[[185.02766   253.57068    96.74738  ]
  [179.9885     63.591183  132.2383   ]]

  [[148.74474    49.562965  117.71197  ]
  [212.52454     7.0973487 110.48188  ]]

  [[ 77.27309    10.852705  159.09123  ]
  [129.35266   101.938736  154.16785  ]]]


[[[100.077354   94.34585   164.87854  ]
  [133.98444   131.05472   111.24332  ]]

  [[ 77.10694    38.390812  127.5978   ]
  [123.20665    87.19942    71.47214  ]]

  [[227.06354   186.60675   222.77557  ]
  [ 34.890095   55.858707  165.75098  ]]]


[[[ 62.116257  143.41527    88.91838  ]
  [142.7192     14.284089  199.3243   ]]

  [[102.98393   166.25313    13.6221695]
  [ 92.00242    88.716286   23.886253 ]]

  [[ 89.55233   135.4736     15.66391  ]
  [ 98.31216   102.77048    43.359985 ]]]]