habana_frameworks.mediapipe.fn.Resize
habana_frameworks.mediapipe.fn.Resize¶
- Class:
habana_frameworks.mediapipe.fn.Resize(**kwargs)
- Define graph call:
__call__(input)
Parameter:
input - Input tensor to operator. Supported dimensions: minimum = 3, maximum = 4. Supported data types: INT8, UINT8, UINT16. 3D tensor or 4D tensor with last dimension size as 1, containing image data. Input image data in
(N*C)xWxHx1
data layout with(N*C)
being the fastest changing dimension.
Description:
Resize an image by the scale factor using either bilinear, cubic, nearest neighbor sampling.
- Supported backend:
HPU
Keyword Arguments
kwargs |
Description |
---|---|
mode |
Interpolation mode to be selected.
|
cubicCoeffA |
Is valid only if
|
scaleDim1 |
Scaling along width axis.
|
scaleDim2 |
Scaling along height axis.
|
scaleDim3 |
Scaling along batch axis.
|
coordTransMode |
Describes how to transform the coordinate in the resized tensor to the coordinate in the original tensor.
|
nearestMode |
Is valid only if
|
excludeOutside |
If set to
|
useScales |
Determine whether scaling will be applied to each dimensions. If
|
size1 |
Width of the output tensor. This is set if
|
size2 |
Height of the output tensor. This is set if
|
size3 |
Batch of the output tensor. This is set if
|
dtype |
Output data type.
|
Note
All Input/Output tensor data type must have same data type.
Input/Output tensor should be in (N*C)xWxH data layout. (N*C) being the fastest changing dimension.
Resize linear mode supports only bilinear interpolation along dimensions 1 (Width) and 2 (Height) only.
Resize neighbor mode supports simultaneous scaling of three non fast changing dimensions.
Resize cubic mode supports only bicubic interpolation along dimensions 1 (Width) and 2 (Height) only.
Example: Resize Operator
The following code snippet shows usage of Resize operator. Reshape and transpose operations are performed before resize operation to arrange tensor layout to (N*C)WH1 as required by resize operation. Post reshape and transpose convert the tensor layout to CWHN.
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 matplotlib.pyplot as plt
import os
g_display_timeout = os.getenv("DISPLAY_TIMEOUT") or 5
# Create MediaPipe derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, batch_size, num_threads,
op_device, dir, img_h, img_w, channels, resize_h, resize_w):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
num_threads,
self.__class__.__name__)
self.input = fn.ReadImageDatasetFromDir(shuffle=False,
dir=dir,
format="jpg",
device="cpu")
# WHCN
self.decode = fn.ImageDecoder(device="hpu",
output_format=it.RGB_P,
resize=[img_w, img_h])
# WHCN -> NCWH
self.pre_transpose = fn.Transpose(permutation=[3, 2, 0, 1],
tensorDim=4,
dtype=dt.UINT8,
device="hpu")
# NCWH -> (N*C)WH1
self.pre_reshape = fn.Reshape(size=[batch_size*channels, img_w, img_h, 1],
tensorDim=4,
layout='',
dtype=dt.UINT8,
device="hpu")
self.resize = fn.Resize(mode=1,
size1=resize_w,
size2=resize_h,
size3=1,
dtype=dt.UINT8,
device=op_device)
# (N*C)WH1 -> NCWH
self.post_reshape = fn.Reshape(size=[batch_size, channels, resize_w, resize_h],
tensorDim=4,
layout='',
dtype=dt.UINT8,
device=op_device)
# NCWH -> CWHN
self.post_transpose = fn.Transpose(permutation=[1, 2, 3, 0],
tensorDim=4,
dtype=dt.UINT8,
device=op_device)
def definegraph(self):
images, labels = self.input()
images = self.decode(images)
images = self.pre_transpose(images)
images = self.pre_reshape(images)
images = self.resize(images)
images = self.post_reshape(images)
images = self.post_transpose(images)
return images, labels
def display_images(images, batch_size, cols):
rows = (batch_size + 1) // cols
plt.figure(figsize=(10, 10))
for i in range(batch_size):
ax = plt.subplot(rows, cols, i + 1)
plt.imshow(images[i])
plt.axis("off")
plt.show(block=False)
plt.pause(g_display_timeout)
plt.close()
def run(device, op_device):
batch_size = 6
queue_depth = 2
num_threads = 1
img_width = 200
img_height = 200
base_dir = os.environ['DATASET_DIR']
dir = base_dir + "/img_data/"
channels = 3
resize_width = 300
resize_height = 300
columns = 3
# Create MediaPipe object
pipe = myMediaPipe(device, queue_depth, batch_size,
num_threads, op_device, dir,
img_height, img_width, channels,
resize_width, resize_height)
# Build MediaPipe
pipe.build()
# Initialize MediaPipe iterator
pipe.iter_init()
# Run MediaPipe
images, labels = 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
images = as_cpu(images).as_nparray()
labels = as_cpu(labels).as_nparray()
del pipe
# Display images
display_images(images, batch_size, columns)
if __name__ == "__main__":
dev_opdev = {'mixed': ['hpu'],
'legacy': ['hpu']}
for dev in dev_opdev.keys():
for op_dev in dev_opdev[dev]:
run(dev, op_dev)
Resized Images 1
- 1
Licensed under a CC BY SA 4.0 license. The images used here are taken from https://data.caltech.edu/records/mzrjq-6wc02.