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
import glob
# Create media pipeline derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, patch_size, num_channels, batch_size, num_threads, img_list, lbl_list, seed):
super(myMediaPipe, self).__init__(
device,
queue_depth,
batch_size,
num_threads,
self.__class__.__name__)
self.images = fn.ReadNumpyDatasetFromDir(device=device,
num_outputs=1,
shuffle=False,
shuffle_across_dataset=False,
file_list=img_list,
dtype=[dt.FLOAT32],
dense=False,
seed=seed,
num_slices=1,
slice_index=0,
drop_remainder=True,
pad_remainder=False
)
self.labels = fn.ReadNumpyDatasetFromDir(device=device,
num_outputs=1,
shuffle=False,
shuffle_across_dataset=False,
file_list=lbl_list,
dtype=[dt.UINT8],
dense=False,
seed=seed,
num_slices=1,
slice_index=0,
drop_remainder=True,
pad_remainder=False
)
self.crop = fn.RandomBiasedCrop(patch_size=patch_size,
num_channels=num_channels,
seed=seed,
num_workers=4,
cache_bboxes=True,
device=device,
)
self.mul = fn.Mult(device=device)
self.const_val = fn.Constant(
constant=0.5, dtype=dt.FLOAT32, device=device)
def definegraph(self):
img = self.images()
lbl = self.labels()
img, lbl, coord = self.crop(img, lbl)
scale = self.const_val()
img_out = self.mul(img, scale)
return img, scale, img_out
def main():
batch_size = 1
patch_size = [5, 5, 5]
queue_depth = 2
num_channels = 1
num_threads = 1
dir = "/path/to/numpy/files/"
pattern0 = "case_*_x.npy"
pattern1 = "case_*_y.npy"
image_list = np.array(sorted(glob.glob(dir + "/{}".format(pattern0))))
label_list = np.array(sorted(glob.glob(dir + "/{}".format(pattern1))))
device = 'hpu'
seed = 1234
# Create media pipeline object
pipe = myMediaPipe(device, queue_depth, patch_size,
num_channels, batch_size, num_threads, image_list, label_list, seed)
# Build media pipeline
pipe.build()
# Initialize media pipeline iterator
pipe.iter_init()
# Run media pipeline
input, constant, multiplication = pipe.run()
if (device == 'cpu'):
# Copy data as numpy array
input = input.as_nparray()
constant = constant.as_nparray()
multiplication = multiplication.as_nparray()
else:
# Copy data to host from device as numpy array
input = input.as_cpu().as_nparray()
constant = constant.as_cpu().as_nparray()
multiplication = multiplication.as_cpu().as_nparray()
print("\nconstant op tensor shape:", constant.shape)
print("\nconstant op tensor dtype:", constant.dtype)
print("\nconstant op tensor data:", constant)
if __name__ == "__main__":
main()
The following is the output of Constant operator:
constant tensor shape: (1,)
constant tensor dtype: float32
constant tensor data: [0.5]