habana_frameworks.mediapipe.fn.GaussianBlur
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.
|
minSigma |
minimum value of sigma used in gaussian kernel.
|
shape |
input shape
|
dtype |
Output data type.
|
Note
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
g_batch_size = 5
g_channels = 4
g_width = 160
g_height = 192
g_depth = 64
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):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
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="/path/to/input", pattern="*.npy", dtype=dt.FLOAT32)
self.gaussian_kernel_sigma = fn.MediaFunc(func=gaussian_kernel_sigma, shape=([
batch_size]), dtype=dt.FLOAT32, priv_params=priv_params_sigma)
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])
def definegraph(self):
images = self.input() # WHDCN
sigmas = self.gaussian_kernel_sigma()
images = self.gaussian_blur(images, sigmas)
return images
def main():
batch_size = g_batch_size
queue_depth = 1
pipe = myMediaPipe('hpu', queue_depth, batch_size)
pipe.build()
pipe.iter_init()
imgs_hpu = pipe.run()
imgs_hpu = imgs_hpu[0].as_cpu().as_nparray()
print('output shape: ',imgs_hpu.shape)
print(imgs_hpu)
if __name__ == "__main__":
main()
The following is the output of GaussianBlur:
output shape: (5, 4, 64, 192, 160) [[[[[0.49063736 0.5004772 0.51628053 ... 0.47441262 0.4220217 0.4019521 ] [0.50427324 0.5085541 0.5186473 ... 0.46635267 0.41408664 0.39637843] [0.5293856 0.52443945 0.52511 ... 0.4548794 0.4145255 0.40598875] ... ... [0.5074467 0.5268973 0.43669522 ... 0.36094642 0.4172104 0.4300987 ] [0.41855085 0.43413556 0.370736 ... 0.3571423 0.43190902 0.48064244] [0.4887966 0.5049416 0.40667495 ... 0.5317901 0.53293675 0.55931807]]]]]