habana_frameworks.mediapipe.fn.ReduceMin
habana_frameworks.mediapipe.fn.ReduceMin¶
- Class:
habana_frameworks.mediapipe.fn.ReduceMin(**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:
Computes the minimum of the input tensor’s elements along the provided dimension. Along with reduced output tensor, the index tensor (Retained-Index) is also produced. The resultant tensor stores the index of minimum element.
- Supported backend:
HPU
Keyword Arguments
kwargs |
Description |
---|---|
reductionDimension |
The dimension in which to perform the reduction operation.
|
dtype |
Output data type.
|
Note
All input/output tensors must be of the same data type.
All tensors should have the same dimensionality.
Size of the reduction dimension in the output tensor must be 1.
The size of the index tensor must be equal to the size of the output tensor.
Example: ReduceMin Operator
The following code snippet shows usage of ReduceMin 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
import os
# Create MediaPipe derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, batch_size, num_threads, op_device, dir, pattern):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
num_threads,
self.__class__.__name__)
self.inputxy = fn.ReadNumpyDatasetFromDir(num_outputs=1,
shuffle=False,
dir=dir,
pattern=pattern,
dtype=dt.FLOAT32)
self.rmin = fn.ReduceMin(reductionDimension=0,
dtype=dt.FLOAT32,
device=op_device)
def definegraph(self):
img = self.inputxy()
rmin, rmin_idx = self.rmin(img)
return rmin, rmin_idx, img
def run(device, op_device):
batch_size = 2
queue_depth = 1
num_threads = 1
base_dir = os.environ['DATASET_DIR']
dir = base_dir+"/npy_data/fp32/"
pattern = "*x*.npy"
# Create MediaPipe object
pipe = myMediaPipe(device, queue_depth, batch_size, num_threads,
op_device, dir, pattern)
# Build MediaPipe
pipe.build()
# Initialize MediaPipe iterator
pipe.iter_init()
# Run MediaPipe
rmin, rmin_idx, img = pipe.run()
def as_cpu(tensor):
if (callable(getattr(tensor, "as_cpu", None))):
tensor = tensor.as_cpu()
return tensor
# Copy data to host from device as numpy array
img = as_cpu(img).as_nparray()
rmin = as_cpu(rmin).as_nparray()
rmin_idx = as_cpu(rmin_idx).as_nparray()
del pipe
# Display shape, data
print('input shape:', img.shape)
print('input data', img)
print('output shape:', rmin.shape)
print('ouput data:\n', rmin)
return img, rmin, rmin_idx
def compare_ref(inp, rmin, rmin_idx):
ref = np.amin(inp, axis=-1, keepdims=True)
ref_idx = np.argmin(inp, axis=-1, keepdims=True)
if np.array_equal(ref, rmin) == False:
raise ValueError(f"Val Mismatch w.r.t ref for device")
if np.array_equal(ref_idx, rmin_idx) == False:
raise ValueError(f"Val 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, rmin, rmin_idx = run(dev, op_dev)
compare_ref(inp, rmin, rmin_idx)
The following is the output of ReduceMin operator:
input shape: (2, 3, 2, 3)
input 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.]]]]
output shape: (2, 3, 2, 1)
ouput data:
[[[[113.]
[128.]]
[[ 58.]
[ 80.]]
[[175.]
[ 20.]]]
[[[ 96.]
[ 64.]]
[[ 50.]
[ 6.]]
[[ 11.]
[102.]]]]