habana_frameworks.mediapipe.fn.GaussianBlur

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

Define graph call:
  • __call__(input, sigmas)

Parameter:
  • input - Input tensor to operator. Supported dimension: 5. Supported data types: FLOAT16, BFLOAT16, FLOAT32. Supported Layout WHDCN.

  • sigmas - It should be Numpy array of batch_size, specifying sigma of Gaussian blur for respective input in the batch.

Description:

This operator generates Gaussian Kernel and use grouped 1D convolution along with width, height and depth to apply Gaussian blur.

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

maxSigma

maximum value of sigma used in gaussian kernel.

  • Type: float

  • Default: 1.5

  • Optional: yes

minSigma

minimum value of sigma used in gaussian kernel.

  • Type: int

  • Default: 0.5

  • Optional: yes

shape

input shape

  • Type: int

  • Optional: No

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Supported data types:

    • FLOAT16

    • BFLOAT16

    • FLOAT32

Note

  1. Data type of input/ output tensor and gaussian_kernel must be same.

Example: Gaussian Blur Operator

The following code snippet shows usage of GaussianBlur operator. It use randomly generted inputs of shape (4, 64, 192, 160).

class gaussian_kernel_sigma provides sample implementation to randomly generate gaussian kernel sigma values for every image of the batch. It takes batch_size, min_sigma, max_sigma and seed as priv_params and generates output sigmas as 1D array of batch_size. Output of class gaussian_kernel_sigma is passed to GaussianBlur operator along with input tensor.

import numpy as np
from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.operators.cpu_nodes.cpu_nodes import media_function
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import dtype as dt
import os

g_batch_size = 2
g_channels = 1
g_width = 128
g_height = 128
g_depth = 128
g_seed = 123
g_max_sigma = 1.5
g_min_sigma = 0.5

class gaussian_kernel_sigma(media_function):
    def __init__(self, params):
        self.priv_params = params['priv_params']
        self.batch_size = self.priv_params['batch_size']
        self.min_sigma = self.priv_params['min_sigma']
        self.max_sigma = self.priv_params['max_sigma']
        self.seed = self.priv_params['seed']
        self.rng = np.random.default_rng(self.seed)
        self.s_start = 0
        self.s_end = self.batch_size

    def __call__(self):
        sigmas = self.rng.uniform(
            low=self.min_sigma, high=self.max_sigma, size=(self.batch_size))
        return sigmas


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__)

        priv_params_sigma = {}
        priv_params_sigma['batch_size'] = batch_size
        priv_params_sigma['min_sigma'] = g_min_sigma
        priv_params_sigma['max_sigma'] = g_max_sigma
        priv_params_sigma['seed'] = g_seed

        self.input = fn.ReadNumpyDatasetFromDir(num_outputs=1,
                                                shuffle=False,
                                                dir=dir,
                                                pattern="*_x.npy",
                                                dtype=dt.FLOAT32,
                                                dense=False,
                                                device="cpu")
        self.random_gb_sigma = fn.RandomUniform(seed=123,
                                                low=g_min_sigma,
                                                high=g_max_sigma,
                                                dtype=dt.FLOAT32,
                                                device='cpu')
        self.crop = fn.Crop(crop_h=128,
                            crop_w=128,
                            crop_d=128,
                            dtype=dt.FLOAT32,
                            device="cpu")
        self.gaussian_blur = fn.GaussianBlur(max_sigma=g_max_sigma,
                                            min_sigma=g_min_sigma,
                                            shape=[
                                                g_width, g_height, g_depth, g_channels, g_batch_size],
                                            device=op_device)

    def definegraph(self):
        images = self.input()  # WHDCN
        sigmas = self.random_gb_sigma()
        images = self.crop(images)
        images = self.gaussian_blur(images, sigmas)
        return images


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

    pipe = myMediaPipe(device, queue_depth, batch_size,
                      num_threads, op_device, dir)
    pipe.build()
    pipe.iter_init()

    imgs = pipe.run()

    imgs_hpu = imgs.as_cpu().as_nparray()
    print('output shape: ', imgs_hpu.shape)
    print(imgs_hpu)


if __name__ == "__main__":
    dev_opdev = {'mixed': ['hpu']}

    for dev in dev_opdev.keys():
        for op_dev in dev_opdev[dev]:
            run(dev, op_dev)

The following is the output of GaussianBlur:

output shape:  (2, 1, 128, 128, 128)

[[[[[-2.340702   -2.340702   -2.340702   ... -2.340702   -2.340702   -2.340702  ]
    [-2.340702   -2.340702   -2.340702   ... -2.340702   -2.340702   -2.340702  ]
    [-2.340702   -2.340702   -2.340702   ... -2.340702   -2.340702   -2.340702  ]
 ...
 ...
    [-2.2346025  -1.9432695  -1.2308582  ... -2.3404427  -2.3394356   -2.339077  ]
    [-2.0746992  -1.6348587  -0.9661784  ... -2.3404639  -2.3406258   -2.3406367 ]
    [-1.9166806  -1.4312491  -0.9040763  ... -2.339802   -2.3405108   -2.3406944 ]]]]]