habana_frameworks.mediapipe.fn.Cast
habana_frameworks.mediapipe.fn.Cast¶
- Class:
habana_frameworks.mediapipe.fn.Cast(**kwargs)
- Define graph call:
__call__(input)
- Parameter:
input - Input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: UINT8, UINT16, UINT32, INT8, INT16, INT32, FLOAT16, BFLOAT16, FLOAT32.
Description:
This operator changes the data type of the input tensor.
- Supported backend:
HPU
Keyword Arguments
kwargs |
Description |
---|---|
round_mode |
Rounding mode selection.
|
dtype |
Output data type.
|
Note
Input and output tensors must have the different data type. Self cast is not allowed.
Cast between all supported data types is allowed.
Example: Cast Operator
The following code snippet shows usage of Cast operator, decoder output is casted to FLOAT32. Print of output image array shows float numbers.
from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import dtype as dt
import os
import numpy as np
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")
self.cast = fn.Cast(dtype=dt.FLOAT32,
device=op_device)
def definegraph(self):
inp = self.inp()
out = self.cast(inp)
return out, inp
def run(device, op_device):
batch_size = 2
queue_depth = 2
num_threads = 1
base_dir = os.environ['DATASET_DIR']
dir = base_dir+"/npy_data/u8/"
# 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
out, inp = pipe.run()
def as_cpu(tensor):
if (callable(getattr(tensor, "as_cpu", None))):
tensor = tensor.as_cpu()
return tensor
out = as_cpu(out).as_nparray()
inp = as_cpu(inp).as_nparray()
del pipe
print("\nout tensor shape:", out.shape)
print("out tensor dtype:", out.dtype)
print("out tensor data:\n", out)
return inp, out
def compare_ref(inp, out):
ref = np.array(inp, dtype=np.float32)
if np.array_equal(ref, out) == False:
raise ValueError(f"Mismatch w.r.t ref")
if __name__ == "__main__":
dev_opdev = {'mixed': ['hpu'],
'legacy': ['hpu']}
for dev in dev_opdev.keys():
for op_dev in dev_opdev[dev]:
inp, out = run(dev, op_dev)
compare_ref(inp, out)
The following is the output of Cast operator:
out tensor shape: (2, 3, 2, 3)
out tensor dtype: float32
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.]]]]