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 - 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.
|
seed |
Seed to the random number generator.
|
shape |
Shape of output data.
|
dims |
Size of shape list.
|
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 os
# Create MediaPipe derived class
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__)
self.inp = fn.ReadNumpyDatasetFromDir(num_outputs=1,
shuffle=False,
dir=dir,
pattern="inp_x_*.npy",
dense=True,
dtype=dt.FLOAT32,
device="cpu")
std_data = np.ones([batch_size, 1], dtype=dt.FLOAT32)
std_data = std_data*0.5
self.std_dev = fn.MediaConst(data=std_data,
shape=[1, batch_size],
dtype=dt.FLOAT32,
device="cpu",
batch_broadcast=False)
self.random_normal = fn.RandomNormal(mean=0.0,
seed=123456,
dtype=dt.FLOAT32,
device=op_device)
self.add = fn.Add(dtype=dt.FLOAT32,
device=op_device)
self.opdevice = op_device
if op_device == "hpu":
self.reshape = fn.Reshape(size=[batch_size], tensorDim=1,
dtype=dt.FLOAT32, device="hpu")
def definegraph(self):
inp = self.inp()
std_dev = self.std_dev()
if (self.opdevice == "hpu"):
std_dev = self.reshape(std_dev)
rn = self.random_normal(std_dev, inp)
out = self.add(inp, rn)
return inp, rn, out
def run(device, op_device):
batch_size = 4
queue_depth = 2
num_threads = 1
base_dir = os.environ['DATASET_DIR']
dir = base_dir+"/npy_data/fp32/"
# Create MediaPipe object
pipe = myMediaPipe(device, queue_depth, batch_size,
num_threads, op_device, dir)
# Build MediaPipe
pipe.build()
# Initialize MediaPipe iterator
pipe.iter_init()
# Run MediaPipe
inp, rn, out = pipe.run()
def as_cpu(tensor):
if (callable(getattr(tensor, "as_cpu", None))):
tensor = tensor.as_cpu()
return tensor
# Copy data to host from device as numpy array
inp = as_cpu(inp).as_nparray()
rn = as_cpu(rn).as_nparray()
out = as_cpu(out).as_nparray()
print("\ninp tensor shape:", inp.shape)
print("inp tensor dtype:", inp.dtype)
print("inp tensor data:\n", inp)
print("\nrandom normal tensor shape:", rn.shape)
print("random normal tensor dtype:", rn.dtype)
print("random normal tensor data:\n", rn)
print("\nout tensor shape:", out.shape)
print("out tensor dtype:", out.dtype)
print("out tensor data:\n", out)
pipe.del_iter()
if __name__ == "__main__":
dev_opdev = {'cpu': ['cpu'],
'mixed': ['hpu'],
'legacy': ['hpu']}
for dev in dev_opdev.keys():
for op_dev in dev_opdev[dev]:
run(dev, op_dev)
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.]]]]
random normal tensor shape: (4, 3, 2, 3)
random normal tensor dtype: float32
random normal tensor data:
[[[[ 0.02159161 -0.54007787 0.44177347]
[-0.51411563 0.09915066 0.14125563]]
[[ 0.31580365 -0.09017808 0.57830215]
[-0.73553914 -1.0010557 -0.3039657 ]]
[[-0.22232975 0.2661755 -0.4181997 ]
[ 0.04317303 -0.69378126 -0.7690765 ]]]
[[[-0.97233665 -0.4293204 0.7473869 ]
[-0.01149983 -0.40881628 0.23829877]]
[[-0.2552664 -0.43703434 0.71196645]
[-0.4754631 1.0973488 -0.51811713]]
[[ 0.2730863 -0.14729519 -0.9087676 ]
[ 0.35266215 -0.06126683 0.16784267]]]
[[[ 0.07735129 -0.6541513 0.8785467 ]
[-0.01555892 0.05471294 -0.7566798 ]]
[[ 0.10694299 0.3908108 0.59780324]
[ 0.20665352 0.19941776 0.47214127]]
[[ 0.06353162 0.6067512 -0.2244307 ]
[-0.10990486 -0.14129086 -0.24902795]]]
[[[ 0.11625629 -0.58473796 -0.08162089]
[ 0.71919847 -0.71591103 0.3242904 ]]
[[-0.01606756 0.25313002 -0.3778306 ]
[ 1.0024205 -0.28371572 -0.11374747]]
[[ 0.5523298 0.47360474 0.66391027]
[-0.68784523 -0.22951959 0.35998482]]]]
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 ]]]]