habana_frameworks.mediapipe.fn.RandomNormal

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

Define graph call:
  • __call__(Stddev, Seed)

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

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

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

Keyword Arguments

kwargs

Description

mean

Mean of the distribution.

  • Type: float

  • Default: 0.0

  • Optional: yes

stddev

Standard Deviation (Spread or ‘Width’) of the distribution.

  • Type: float

  • Default: 0.0

  • Optional: yes

seed

Seed to the random number generator.

  • Type: int

  • Default: 0.0

  • Optional: yes

dims

Number of dimensions of the output tensor.

  • Type: int

  • Default: 5

  • Optional: yes

shape

Shape of the output tensor.

  • Type: list[int]

  • Default: [0, 0, 0, 0, 0]

  • Optional: no

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: FLOAT32

  • Optional: yes

  • Supported data types:

    • BFLOAT16

    • FLOAT32

Note

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

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

  3. Output tensor supported minimum rank = 1 and maximum rank = 4.

  4. 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 imgtype as it
from habana_frameworks.mediapipe.media_types import dtype as dt
import matplotlib.pyplot as plt
from habana_frameworks.mediapipe.operators.cpu_nodes.cpu_nodes import media_function
import numpy as np

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

        self.input = fn.ReadImageDatasetFromDir(shuffle=False,
                                                dir=dir,
                                                format="jpg")

        # WHCN
        self.decode = fn.ImageDecoder(device="hpu",
                                      output_format=it.RGB_P,
                                      resize=[img_w, img_h])

        # WHCN -> CWHN
        self.transpose = fn.Transpose(permutation=[2, 0, 1, 3],
                                      tensorDim=4,
                                      output_scale=1/255,
                                      output_zerop=0,
                                      dtype=dt.UINT8)

        self.pre_cast = fn.Cast(dtype=dt.FLOAT32)

        self.post_cast = fn.Cast(output_scale=1/255,
                                 output_zerop=0,
                                 dtype=dt.UINT8)

        self.seed = fn.MediaFunc(func=random_seed_func,
                                 shape=[1],
                                 dtype=dt.UINT32,
                                 seed=0)

        self.stddev = fn.MediaFunc(func=random_stddev_func,
                                   shape=[batch_size],
                                   dtype=dt.FLOAT32,
                                   seed=0)

        self.rnd_normal = fn.RandomNormal(mean=0.0,
                                          stddev=0.2,
                                          dtype=dt.FLOAT32,
                                          dims=4,
                                          shape=[img_c, img_w, img_h, batch_size])

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

    def definegraph(self):
        images, labels = self.input()
        images = self.decode(images)
        images = self.transpose(images)
        images = self.pre_cast(images)
        stddev = self.stddev()
        seed = self.seed()
        noise = self.rnd_normal(stddev, seed)
        images = self.add(images, noise)
        images = self.post_cast(images)
        return images, labels, noise

class random_seed_func(media_function):
    """
    Class to randomly generate seed.

    """

    def __init__(self, params):
        """
        Constructor method.

        :params params: random_seed_func specific params.
                        shape: output shape
                        dtype: output data type
                        seed: seed to be used
        """
        self.np_shape = params['shape'][::-1]
        self.np_dtype = params['dtype']
        self.seed = params['seed']
        self.rng = np.random.default_rng(self.seed)

    def __call__(self):
        """
        Callable class method.

        :returns : randomly generated seed value
        """
        a = self.rng.uniform(size=self.np_shape)
        a = np.array(a, dtype=self.np_dtype)
        return a

class random_stddev_func(media_function):
    """
    Class to randomly generate seed.

    """

    def __init__(self, params):
        """
        Constructor method.

        :params params: random_stddev_func specific params.
                        shape: output shape
                        dtype: output data type
                        seed: seed to be used
        """
        self.np_shape = params['shape'][::-1]
        self.np_dtype = params['dtype']
        self.seed = params['seed']
        self.rng = np.random.default_rng(self.seed)

    def __call__(self):
        """
        Callable class method.

        :returns : randomly generated stddev value
        """
        a = self.rng.uniform(low=0.0, high=0.5, size=self.np_shape)
        a = np.array(a, dtype=self.np_dtype)
        return a

def display_images(images, batch_size, cols):
    rows = (batch_size + 1) // cols
    plt.figure(figsize=(10, 10))
    for i in range(batch_size):
        ax = plt.subplot(rows, cols, i + 1)
        plt.imshow(images[i])
        plt.axis("off")
    plt.show()

def main():
    batch_size = 6
    img_width = 200
    img_height = 200
    img_channel = 3
    img_dir = "/path/to/images"
    queue_depth = 2
    columns = 3

    # Create media pipeline object
    pipe = myMediaPipe('hpu', img_dir, queue_depth, batch_size,
                        img_height, img_width, img_channel)

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

    # Run media pipeline
    images, labels, noise = pipe.run()

    # Copy images to CPU as numpy array
    images = images.as_cpu().as_nparray()
    labels = labels.as_cpu().as_nparray()
    noise = noise.as_cpu().as_nparray()

    # Display images, shape, dtype
    display_images(images, batch_size, columns)
    print('random data dtype:', noise.dtype)
    print('random data:\n', noise)

if __name__ == "__main__":
    main()

The following is the output of RandomNormal operator:

random data dtype: float32
random data:
[[[[-4.57208931e-01  4.18369919e-02 -2.86147356e-01]
  [ 2.54383355e-01  2.08734460e-02  3.19651932e-01]
  [-3.38546485e-01 -7.08397090e-01 -3.02480996e-01]

  ...

  [ 8.38548422e-01 -8.07051361e-01 -1.29943490e-01]
  [ 2.84468561e-01  3.36065561e-01  7.25157022e-01]
  [-5.43768466e-01 -5.04202604e-01  2.68687010e-01]]]]