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, CPU

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.

  • Type: int

  • Default: 100

  • Optional: yes

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.

  • Type: int

  • Default: 100

  • Optional: yes

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.

  • Type: int

  • Default: 0

  • Optional: yes

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.

  • Type: float

  • Default: 0.0

  • Optional: yes

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.

  • Type: float

  • Default: 0.0

  • Optional: yes

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.

  • Type: float

  • Default: 0.0

  • Optional: yes

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Note: User should manually set dtype same as input data type. Otherwise output tensor’s data type will get changed to default UINT8.

  • Supported data types:

    • UINT8

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 dtype as dt
import os

# Create MediaPipe derived class

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

        self.inp = fn.ReadNumpyDatasetFromDir(num_outputs=1,
                                              shuffle=False,
                                              dir=dir,
                                              pattern="inp_x_*.npy",
                                              dense=True,
                                              dtype=dt.UINT8,
                                              device="cpu")

        self.crop = fn.Crop(crop_h=2,
                            crop_w=2,
                            dtype=dt.UINT8,
                            device=op_device)

    def definegraph(self):
        inp = self.inp()
        out = self.crop(inp)
        return out, inp


def run(device, op_device):
    batch_size = 2
    queue_depth = 2
    num_threads = 1
    base_dir = os.environ['DATASET_DIR']
    dir = base_dir+"/npy_data/u8/"
    device = device

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

    # Build MediaPipe
    pipe.build()

    # Initialize MediaPipe iterator
    pipe.iter_init()

    # Run MediaPipe
    out, inp = pipe.run()

    def as_cpu(tensor):
        if (callable(getattr(tensor, "as_cpu", None))):
            tensor = tensor.as_cpu()
        return tensor
    inp = as_cpu(inp).as_nparray()
    out = as_cpu(out).as_nparray()

    del pipe

    print("\ninp tensor shape:", inp.shape)
    print("inp tensor dtype:", inp.dtype)
    print("inp tensor data:\n", inp)

    print("\nout tensor shape:", out.shape)
    print("out tensor dtype:", out.dtype)
    print("out tensor data:\n", out)
    return inp, out


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

The following is the output of Crop operator:

inp tensor shape: (2, 3, 2, 3)
inp tensor dtype: uint8
inp tensor data:
[[[[149 187 232]
  [160 201 202]]

  [[ 80 147 153]
  [199 174 158]]

  [[200 124 139]
  [  3 161 216]]]


[[[106  93  83]
  [ 57 253  52]]

  [[222 189  26]
  [174  60 118]]

  [[218  84  43]
  [251  75  73]]]]

out tensor shape: (2, 3, 2, 2)
out tensor dtype: uint8
out tensor data:
[[[[149 187]
  [160 201]]

  [[ 80 147]
  [199 174]]

  [[200 124]
  [  3 161]]]


[[[106  93]
  [ 57 253]]

  [[222 189]
  [174  60]]

  [[218  84]
  [251  75]]]]