habana_frameworks.mediapipe.fn.RandomUniform
habana_frameworks.mediapipe.fn.RandomUniform¶
- Class:
habana_frameworks.mediapipe.fn.RandomUniform(**kwargs)
- Define graph call:
__call__(seed)
- Parameter:
(optional) seed - 1D tensor of size 1. Supported dimensions: minimum = 1, maximum = 1. Supported data types: INT32.
Description:
Generate random numbers from a uniform distribution.
- Supported backend:
HPU, CPU
Keyword Arguments
kwargs |
Description |
---|---|
low |
Lower bound.
|
high |
Upper bound.
|
seed |
Seed to the random number generator.
|
dtype |
Output data type.
|
Note
If seed tensor is provided, then no need to initialize
seed
value in operator arguments.Output tensor supported with minimum rank = 1 and maximum rank = 1.
Example: RandomUniform Operator
The following code snippet shows usage of RandomUniform operator. Random data is generated as noise by RandomUniform 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,
num_workers=4,
cache_bboxes=True,
device=device,
)
self.random_u = fn.RandomUniform(seed=seed,
low=0.1,
high=0.3,
dtype=dt.FLOAT32,
device=device,
)
self.mul = fn.Mult(device=device)
def definegraph(self):
img = self.images()
lbl = self.labels()
img, lbl, coord = self.crop(img, lbl)
random_scale = self.random_u()
img_out = self.mul(img, random_scale)
return img, random_scale, 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, multiplication = pipe.run()
if (device == 'cpu'):
# Copy data as numpy array
input = input.as_nparray()
random_scale = random_scale.as_nparray()
multiplication = multiplication.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()
multiplication = multiplication.as_cpu().as_nparray()
print("\nrandom uniform op tensor shape:", random_scale.shape)
print("\nrandom uniform op tensor dtype:", random_scale.dtype)
print("\nrandom uniform op tensor data:", random_scale)
if __name__ == "__main__":
main()
The following is the output of RandomNormal operator: