habana_frameworks.mediapipe.fn.Constant
habana_frameworks.mediapipe.fn.Constant¶
- Class:
habana_frameworks.mediapipe.fn.Constant(**kwargs)
- Define graph call:
__call__()
- Parameter:
None
Description:
Constant operator generates scalar constant value tensor.
- Supported backend:
HPU, CPU
Keyword Arguments
kwargs |
Description |
---|---|
constant |
Constant value.
|
dtype |
Output data type.
|
Example: Constant Operator
The following code snippet shows usage of Constant operator:
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
# Create media pipeline derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, batch_size, num_threads, 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=device)
self.const = fn.Constant(constant=0.5,
dtype=dt.FLOAT32,
device=device)
self.mul = fn.Mult(device=device)
def definegraph(self):
inp = self.inp()
const = self.const()
out = self.mul(inp, const)
return out, inp, const
def main():
batch_size = 1
queue_depth = 2
num_threads = 1
device = 'hpu'
dir = '/path/to/numpy/files'
# Create media pipeline object
pipe = myMediaPipe(device, queue_depth, batch_size, num_threads, dir)
# Build media pipeline
pipe.build()
# Initialize media pipeline iterator
pipe.iter_init()
# Run media pipeline
out, inp, const = pipe.run()
if (device == 'cpu'):
# Copy data as numpy array
out = out.as_nparray()
inp = inp.as_nparray()
const = const.as_nparray()
else:
# Copy data to host from device as numpy array
out = out.as_cpu().as_nparray()
inp = inp.as_cpu().as_nparray()
const = const.as_cpu().as_nparray()
print("\nconst tensor shape:", const.shape)
print("const tensor dtype:", const.dtype)
print("const tensor data:\n", const)
print("\ninp tensor shape:", inp.shape)
print("inp tensor dtype:", inp.dtype)
print("inp tensor data:\n", inp)
print("\nout tensor shape:", out.shape)
print("out tensor dtype:", out.dtype)
print("out tensor data:\n", out)
pipe.del_iter()
if __name__ == "__main__":
main()
The following is the output of Constant operator:
const tensor shape: (1,)
const tensor dtype: float32
const tensor data:
[0.5]
inp tensor shape: (1, 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.]]]]
out tensor shape: (1, 3, 2, 3)
out tensor dtype: float32
out tensor data:
[[[[ 91. 113.5 56.5]
[ 87.5 64. 126.5]]
[[ 29. 70. 68. ]
[ 43. 40. 55.5]]
[[ 87.5 98. 89. ]
[ 10. 81.5 54. ]]]]