habana_frameworks.mediapipe.fn.CoinFlip
habana_frameworks.mediapipe.fn.CoinFlip¶
- Class:
habana_frameworks.mediapipe.fn.CoinFlip(**kwargs)
- Define graph call:
__call__(input, seed)
- Parameter:
probability - Input probability tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: FLOAT16, BFLOAT16, FLOAT32.
(optional) seed - Seed to the random number generator. This is a scalar value. Supported dimensions: minimum = 1, maximum = 1. Supported data types: UINT32.
Description:
Outputs a tensor with Random numbers from a Bernoulli distribution. Bernoulli distribution is the discrete probability distribution of a random variable which takes the value 1 with probability p and the value 0 with probability q = 1 - p.
- Supported backend:
HPU, CPU
Keyword Arguments
kwargs |
Description |
---|---|
seed |
Seed to the random number generator, only positive integer.
|
dtype |
Output data type.
|
Note
Input type BF16/F16 maps to output type INT16 and Input type FLOAT32 maps to Output type INT32.
Seed can be given either as a static attribute or as a input tensor. If given as input tensor, the static parameter will be ignored.
Example: CoinFlip Operator
The following code snippet shows usage of CoinFlip operator. The predicate tensor generated by CoinFlip operator is fed to RandomFlip operator for flipping the images:
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 media pipeline 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.UINT8,
device="cpu")
data = np.ones(1, dtype=dt.FLOAT32)
data = data*0.5
self.probability = fn.MediaConst(data=data,
shape=[1],
dtype=dt.FLOAT32,
device="cpu")
self.coin_flip = fn.CoinFlip(seed=100,
device=op_device)
self.opdevice = op_device
if op_device == "hpu":
self.reshape = fn.Reshape(size=[batch_size],
tensorDim=1,
layout='',
dtype=dt.UINT8,
device=op_device)
self.random_flip = fn.RandomFlip(horizontal=1,
dtype=dt.UINT8,
device=op_device)
def definegraph(self):
inp = self.inp()
probability = self.probability()
predicate = self.coin_flip(probability)
if (self.opdevice == "hpu"):
predicate = self.reshape(predicate)
out = self.random_flip(inp, predicate)
return inp, predicate, out
def run(device, op_device):
batch_size = 5
queue_depth = 2
num_threads = 1
base_dir = os.environ['DATASET_DIR']
dir = base_dir+"/npy_data/u8"
# Create media pipeline object
pipe = myMediaPipe(device, queue_depth, batch_size,
num_threads, op_device, dir)
# Build media pipeline
pipe.build()
# Initialize media pipeline iterator
pipe.iter_init()
# Run media pipeline
inp, predicate, out = pipe.run()
def as_cpu(tensor):
if (callable(getattr(tensor, "as_cpu", None))):
tensor = tensor.as_cpu()
return tensor
inp = as_cpu(inp).as_nparray()
predicate = as_cpu(predicate).as_nparray()
predicate = predicate.reshape(batch_size,1)
out = as_cpu(out).as_nparray()
del pipe
print("\ninp tensor shape:", inp.shape)
print("inp tensor dtype:", inp.dtype)
print("inp tensor data:\n", inp)
print("\npredicate tensor shape:", predicate.shape)
print("predicate tensor dtype:", predicate.dtype)
print("predicate tensor data:\n", predicate)
print("\nout tensor shape:", out.shape)
print("out tensor dtype:", out.dtype)
print("out tensor data:\n", out)
return inp, predicate, out
def compare_ref(inp, predicate, out):
ref = inp
for i in range(predicate.shape[0]):
if (predicate[i][0] == 1):
ref[i] = np.flip(inp[i], axis=-1)
if np.array_equal(ref, out) == False:
raise ValueError(f"Mismatch w.r.t ref for device {d}")
if __name__ == "__main__":
dev_opdev = {'cpu': ['cpu'],
'mixed': ['hpu'],
'legacy': ['hpu']}
for dev in dev_opdev.keys():
for op_dev in dev_opdev[dev]:
inp, predicate, out = run(dev, op_dev)
compare_ref(inp, predicate, out)
The following is the output for CoinFlip operator:
inp tensor shape: (5, 3, 2, 3)
inp tensor dtype: uint8
inp tensor data:
[[[[149 187 232]
[160 201 202]]
[[ 80 147 153]
[199 174 158]]
[[200 124 139]
[ 3 161 216]]]
[[[106 93 83]
[ 57 253 52]]
[[222 189 26]
[174 60 118]]
[[218 84 43]
[251 75 73]]]
[[[148 87 82]
[216 158 84]]
[[123 206 47]
[169 57 199]]
[[251 167 20]
[ 56 44 12]]]
[[[ 44 10 70]
[227 63 176]]
[[115 169 230]
[185 139 107]]
[[ 64 3 168]
[169 11 143]]]
[[[238 118 201]
[190 243 158]]
[[240 228 237]
[236 126 48]]
[[151 123 165]
[189 187 46]]]]
predicate tensor shape: (5, 1)
predicate tensor dtype: int8
predicate tensor data:
[[0]
[0]
[1]
[1]
[1]]
out tensor shape: (5, 3, 2, 3)
out tensor dtype: uint8
out tensor data:
[[[[149 187 232]
[160 201 202]]
[[ 80 147 153]
[199 174 158]]
[[200 124 139]
[ 3 161 216]]]
[[[106 93 83]
[ 57 253 52]]
[[222 189 26]
[174 60 118]]
[[218 84 43]
[251 75 73]]]
[[[ 82 87 148]
[ 84 158 216]]
[[ 47 206 123]
[199 57 169]]
[[ 20 167 251]
[ 12 44 56]]]
[[[ 70 10 44]
[176 63 227]]
[[230 169 115]
[107 139 185]]
[[168 3 64]
[143 11 169]]]
[[[201 118 238]
[158 243 190]]
[[237 228 240]
[ 48 126 236]]
[[165 123 151]
[ 46 187 189]]]]