habana_frameworks.mediapipe.fn.ExtCpuOp
habana_frameworks.mediapipe.fn.ExtCpuOp¶
- Class:
habana_frameworks.mediapipe.fn.ExtCpuOp(**kwargs)
- Define graph call:
__call__(input)
- Parameter:
input - Input tensor to operator. Tensor dimensions and data types depend on the operator used. Supported inputs: minimum = 1, maximum = 10.
Description:
This operator allows user to add user defined CPU operators (which are not part of media operators list) to MediaPipe.
- Supported backend:
CPU
Keyword Arguments
kwargs |
Description |
---|---|
impl |
Implementation of user defined class.
|
seed |
Seed for randomization.
|
priv_params |
User defined params.
|
Example: ExtCpuOp Operator
The following code snippet shows usage of ExtCpuOp operator for adding multiplicatoin operator:
from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import dtype as dt
from habana_frameworks.mediapipe.operators.cpu_nodes.cpu_nodes import media_ext_cpu_op_impl, media_ext_cpu_op_tensor_info
import numpy as np
g_mult_factor = 0.9
batch_size = 3
queue_depth = 2
g_bright_buf = []
g_in_buf = []
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, batch_size, dir):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
self.__class__.__name__)
self.input = fn.ReadNumpyDatasetFromDir(num_outputs=1,
shuffle=False,
dir=dir,
pattern="np_*_f32.npy",
dtype=dt.FLOAT32)
self.mult = fn.MediaExtCpuOp(num_outputs=1,
impl=cpu_mult_node,
dtype=dt.FLOAT32,
priv_params={
'shape': [4, 5, 6, batch_size],
'value': g_mult_factor})
self.memcopy_op = fn.MemCpy(dtype=dt.FLOAT32)
def definegraph(self):
img = self.input()
img = self.mult(img)
img = self.memcopy_op(img)
return img
class cpu_mult_node(media_ext_cpu_op_impl):
def __init__(self, params):
self.params = params['priv_params']
self.factor = self.params['value']
self.shape = self.params['shape']
def __call__(self, input):
input = input * self.factor
return [input]
def set_params(self, params):
self.batch_size = params.batch_size
def gen_output_info(self):
shape = np.array(self.shape, dtype=np.uint32)
return media_ext_cpu_op_tensor_info(dt.FLOAT32,
shape,
"")
def main():
dir = "path/to/numpyfiles"
# Create MediaPipe object
pipe = myMediaPipe('hpu', queue_depth, batch_size, dir)
# Build MediaPipe
pipe.build()
# Initialize MediaPipe iterator
pipe.iter_init()
# Run MediaPipe
for i in range(2):
images = pipe.run()
images = images[0]
images = images.as_cpu().as_nparray()
print('images:\n', images)
# Delete MediaPipe
del pipe
if __name__ == "__main__":
main()
The following is the output of ExtCpuOp operator:
images:
[[[[ 0. 0.9 1.8 2.6999998]
[ 3.6 4.5 5.3999996 6.2999997]
[ 7.2 8.099999 9. 9.9 ]
[ 10.799999 11.7 12.599999 13.5 ]
[ 14.4 15.299999 16.199999 17.1 ]]
...
[[270. 272.69998 275.4 278.1 ]
[280.8 283.5 286.19998 288.9 ]
[291.6 294.3 297. 299.69998 ]
[302.4 305.1 307.8 310.5 ]
[313.19998 315.9 318.6 321.3 ]]]]