habana_frameworks.mediapipe.fn.Neg
habana_frameworks.mediapipe.fn.Neg¶
- Class:
habana_frameworks.mediapipe.fn.Neg(**kwargs)
- Define graph call:
__call__(input)
- Parameter:
input - Input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: INT32, FLOAT16, BFLOAT16, FLOAT32.
Description:
Resultant output tensor has the sign flipped for each element in input
, computed as: y = -x.
- Supported backend:
HPU
Keyword Arguments
kwargs |
Description |
---|---|
dtype |
Ouptput data type.
|
Note
All input/output tensors must be of the same data type and they must have the same dimensionality.
This operator is agnostic to the data layout.
Example: Neg Operator
The following code snippet shows usage of Neg operator:
from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import imgtype as it
from habana_frameworks.mediapipe.media_types import dtype as dt
import os
import numpy as np
# Create MediaPipe 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.input = fn.ReadNumpyDatasetFromDir(num_outputs=1,
shuffle=False,
dir=dir,
pattern="inp_x_*.npy",
dense=True,
dtype=dt.FLOAT32,
device="cpu")
self.neg = fn.Neg(dtype=dt.FLOAT32,
device=op_device)
def definegraph(self):
input = self.input()
output = self.neg(input)
return output, input
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/fp32/"
# 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
inp = as_cpu(inp).as_nparray()
out = as_cpu(out).as_nparray()
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)
return inp, out
def compare_ref(inp, out):
ref = np.negative(inp)
if np.array_equal(ref, out) == False:
raise ValueError(f"Mismatch w.r.t ref for device")
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 Neg operator:
inp tensor shape: (2, 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.]]]
[[[186. 254. 96.]
[180. 64. 132.]]
[[149. 50. 117.]
[213. 6. 111.]]
[[ 77. 11. 160.]
[129. 102. 154.]]]]
out tensor shape: (2, 3, 2, 3)
out tensor dtype: float32
out tensor data:
[[[[-182. -227. -113.]
[-175. -128. -253.]]
[[ -58. -140. -136.]
[ -86. -80. -111.]]
[[-175. -196. -178.]
[ -20. -163. -108.]]]
[[[-186. -254. -96.]
[-180. -64. -132.]]
[[-149. -50. -117.]
[-213. -6. -111.]]
[[ -77. -11. -160.]
[-129. -102. -154.]]]]