habana_frameworks.mediapipe.fn.BasicCrop

Class:
  • habana_frameworks.mediapipe.fn.BasicCrop(**kwargs)

Define graph call:
  • __call__(input)

Parameter:
  • input - Input tensor to operator, supported dimensions: minimum = 5, maximum = 5, supported data types: UINT8, FLOAT32.

Description:

This operator crops input tensor along width, height and depth. Supports center crop and crop from beginning of width, height and depth.

Supported backend:
  • CPU

Keyword Arguments

kwargs

Description

patch_size

Crop window size for width, height and depth.

  • Type: list[int]

  • Default: [0, 0, 0]

  • Optional: no

num_channels

Number of channels in an image.

  • Type: int

  • Default: 1

  • Optional: yes

center_crop

Flag to enable/disable center crop.

  • Type: bool

  • Default: False

  • Optional: yes

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: None

  • Optional: no

  • Supported data types:

    • UINT8

    • FLOAT32

Example: BasicCrop Operator

The following code snippet shows usage of BasicCrop 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 os


class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, num_threads, op_device, dir, slice_index, num_slices):
        super(
            myMediaPipe,
            self).__init__(
            device,
            queue_depth,
            batch_size,
            num_threads,
            self.__class__.__name__)

        self.inputx = fn.ReadNumpyDatasetFromDir(num_outputs=1,
                                                shuffle=False,
                                                dir=dir,
                                                pattern=["*_x.npy"],
                                                dtype=[dt.FLOAT32],
                                                dense=False,
                                                seed=0,
                                                num_readers=4,
                                                shuffle_across_dataset=False,
                                                slice_index=slice_index,
                                                num_slices=num_slices,
                                                device="cpu")

        self.crop_img = fn.BasicCrop(patch_size=[128, 128, 128],
                                    center_crop=True,
                                    dtype=dt.FLOAT32,
                                    device=op_device)

        self.memcpy = fn.MemCpy(dtype=dt.FLOAT32,
                                device="hpu")

    def definegraph(self):
        inp = self.inputx()
        img = self.crop_img(inp)
        img = self.memcpy(img)
        return 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_4d/"
    slice_index = 0
    num_slices = 1

    # Create MediaPipe object
    pipe = myMediaPipe(device, queue_depth, batch_size, num_threads,
                      op_device, dir, slice_index, num_slices)

    # Build MediaPipe
    pipe.build()

    # Initialize MediaPipe iterator
    pipe.iter_init()

    # Run MediaPipe
    images = pipe.run()

    # Copy data to host from device as numpy array
    images = images.as_cpu().as_nparray()

    del pipe

    # Display shape
    for i in range(batch_size):
        print("Index {}, Output shape {}".format(i, images[i].shape))

    return images


if __name__ == "__main__":
    dev_opdev = {'legacy': ['cpu']}
    for dev in dev_opdev.keys():
        for op_dev in dev_opdev[dev]:
            out = run(dev, op_dev)

The following is the output of BasicCrop operator:

Index 0, Output shape (1, 128, 128, 128)
Index 1, Output shape (1, 128, 128, 128)