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

class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, dir, slice_index, num_slices):
        super(
            myMediaPipe,
            self).__init__(
            device,
            queue_depth,
            batch_size,
            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)

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

        self.memcpy = fn.MemCpy(dtype=dt.FLOAT32)

    def definegraph(self):
        img = self.inputx()
        img = self.crop_img(img)
        img = self.memcpy(img)
        return img

def main():
    batch_size = 2
    queue_depth = 1
    dir = "/path/to/numpyfiles"

    # Create media pipeline object
    pipe = myMediaPipe('hpu', queue_depth, batch_size, dir, 0, 1)

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

    # Run media pipeline
    images, = pipe.run()

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

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

if __name__ == "__main__":
    main()

The following is the output of BasicCrop operator:

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