habana_frameworks.mediapipe.fn.RandomNormal
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
Keyword Arguments
kwargs |
Description |
---|---|
mean |
Mean of the distribution.
|
seed |
Seed to the random number generator.
|
dtype |
Output data type.
|
Note
Output tensor supported minimum rank = 1 and maximum rank = 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 dtype as dt
import numpy as np
import glob
# Create media pipeline derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, patch_size, num_channels, batch_size, num_threads, img_list, lbl_list, seed):
super(myMediaPipe, self).__init__(
device,
queue_depth,
batch_size,
num_threads,
self.__class__.__name__)
self.images = fn.ReadNumpyDatasetFromDir(device=device,
num_outputs=1,
shuffle=False,
shuffle_across_dataset=False,
file_list=img_list,
dtype=[dt.FLOAT32],
dense=False,
seed=seed,
num_slices=1,
slice_index=0,
drop_remainder=True,
pad_remainder=False
)
self.labels = fn.ReadNumpyDatasetFromDir(device=device,
num_outputs=1,
shuffle=False,
shuffle_across_dataset=False,
file_list=lbl_list,
dtype=[dt.UINT8],
dense=False,
seed=seed,
num_slices=1,
slice_index=0,
drop_remainder=True,
pad_remainder=False
)
self.crop = fn.RandomBiasedCrop(patch_size=patch_size,
num_channels=num_channels,
seed=seed,
cache_bboxes=True,
device=device,
)
self.random_n = fn.RandomNormal(mean=0.0, seed=seed, dtype=dt.FLOAT32,
device=device)
self.add = fn.Add(dtype=dt.FLOAT32, device=device)
self.const_std_dev = fn.Constant(
constant=0.5, dtype=dt.FLOAT32, device=device)
def definegraph(self):
img = self.images()
lbl = self.labels()
img, lbl, coord = self.crop(img, lbl)
std_dev = self.const_std_dev()
random_noise = self.random_n(std_dev, img)
img_out = self.add(img, random_noise)
return img, random_noise, img_out
def main():
batch_size = 1
patch_size = [5, 5, 5]
queue_depth = 2
num_channels = 1
num_threads = 1
dir = "/path/to/numpy/files/"
pattern0 = "case_*_x.npy"
pattern1 = "case_*_y.npy"
image_list = np.array(sorted(glob.glob(dir + "/{}".format(pattern0))))
label_list = np.array(sorted(glob.glob(dir + "/{}".format(pattern1))))
device = 'hpu'
seed = 1234
# Create media pipeline object
pipe = myMediaPipe(device, queue_depth, patch_size,
num_channels, batch_size, num_threads, image_list, label_list, seed)
# Build media pipeline
pipe.build()
# Initialize media pipeline iterator
pipe.iter_init()
# Run media pipeline
input, random_scale, output = pipe.run()
if (device == 'cpu'):
# Copy data as numpy array
input = input.as_nparray()
random_scale = random_scale.as_nparray()
output = output.as_nparray()
else:
# Copy data to host from device as numpy array
input = input.as_cpu().as_nparray()
random_scale = random_scale.as_cpu().as_nparray()
output = output.as_cpu().as_nparray()
print("\nrandom normal op tensor shape:", random_scale.shape)
print("\nrandom normal op tensor dtype:", random_scale.dtype)
print("\nrandom normal op tensor data:", random_scale)
if __name__ == "__main__":
main()
The following is the output of RandomNormal operator:
random normal op tensor shape: (1, 1, 5, 5, 5)
random normal op tensor dtype: float32
random normal op tensor data: [[[[[ 0.1331722 0.12591946 0.610281 -0.31014785 -0.42623502]
[ 0.05631239 -0.43166965 0.4741554 0.64048505 0.65695477]
[ 0.14974329 -0.16913298 -0.03168099 0.3000634 -0.16352914]
[ 0.76263255 -0.15865378 0.41656327 -0.7217346 0.01347879]
[-0.3003522 -0.02604843 0.3562546 -0.42896518 -0.11353325]]
...
...
...
[[-0.42904752 -0.3695639 0.04222788 -0.10206258 0.6356444 ]
[-0.21170752 0.2851184 0.9673678 -0.50200915 0.2999472 ]
[ 1.3289806 0.3093115 -0.48297912 0.6753965 0.06648529]
[ 0.6900133 -1.0004398 0.8484754 0.7599834 0.39499506]
[ 0.21956512 0.5360224 -0.19906901 0.42577302 1.5408812 ]]]]]