habana_frameworks.mediapipe.fn.Crop
habana_frameworks.mediapipe.fn.Crop¶
- Class:
habana_frameworks.mediapipe.fn.Crop(**kwargs)
- Define graph call:
__call__(input)
- Parameter:
input - Input tensor to operator. Supported dimensions: minimum = 4, maximum = 5. Supported data types: UINT8, UINT16.
Description:
This operator crops input tensor with specified window dimensions and window position. Crop kernel can crop input along W/H/C dimensions. It can also cast output to FLOAT32 or FLOAT16 data type.
- Supported backend:
HPU
Keyword Arguments
kwargs |
Description |
---|---|
crop_w |
Specify width of crop window in pixels. crop_w should be non zero value and less than or equal to input tensor width.
|
crop_h |
Specify height of crop window in pixels. crop_h should be non zero value and less than or equal to input tensor height.
|
crop_d |
Cropping along depth axis is optional. crop_d should be set to 0 if there are no cropping along depth axis. crop_d specify depth of crop window in pixels, its default set to zero, only for volumetric data crop_d should be non zero value and less than or equal to input tensor depth.
|
crop_pos_x |
Normalized (0.0 - 1.0) position of the cropping window along width. Actual position is calculated as crop_x = crop_pos_x * (w - crop_w), where crop_pos_x is the normalized position, w is the width of the input tensor and crop_w is the width of the cropping window.
|
crop_pos_y |
Normalized (0.0 - 1.0) position of the cropping window along height. Actual position is calculated as crop_y = crop_pos_y * (h - crop_h), where crop_pos_y is the normalized position, h is the height of the input tensor and crop_h is the height of the cropping window.
|
crop_pos_z |
Only for volumetric data, normalized (0.0 - 1.0) position of the cropping window along depth. Actual position is calculated as crop_z = crop_pos_z * (d - crop_d), where crop_pos_z is the normalized position, d is the depth of the input tensor and crop_d is the depth of the cropping window.
|
dtype |
Output data type.
|
Example: Crop Operator
The following code snippet shows usage of Crop operator. The first decoder produces images of width=200, height=200. These are fed to crop operator with crop_w=150, crop_h=150.
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
# Create media pipeline derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, dir, queue_depth, batch_size, img_h, img_w):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
self.__class__.__name__)
self.input = fn.ReadImageDatasetFromDir(shuffle=False,
dir=dir,
format="jpg")
# WHCN
self.decode = fn.ImageDecoder(device="hpu",
output_format=it.RGB_P,
resize=[img_w, img_h])
self.crop = fn.Crop(crop_w=150,
crop_h=150,
dtype=dt.UINT8)
# WHCN -> CWHN
self.transpose = fn.Transpose(permutation=[2, 0, 1, 3],
tensorDim=4,
dtype=dt.UINT8)
def definegraph(self):
images, labels = self.input()
images = self.decode(images)
images = self.crop(images)
images = self.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()
def main():
batch_size = 6
img_width = 200
img_height = 200
img_dir = "/path/to/images"
queue_depth = 2
columns = 3
# Create media pipeline object
pipe = myMediaPipe('hpu', img_dir, queue_depth, batch_size,
img_height, img_width)
# Build media pipeline
pipe.build()
# Initialize media pipeline iterator
pipe.iter_init()
# Run media pipeline
images, labels = pipe.run()
# Copy data to CPU as numpy array
images = images.as_cpu().as_nparray()
labels = labels.as_cpu().as_nparray()
# Display images
display_images(images, batch_size, columns)
if __name__ == "__main__":
main()
Cropped 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.