habana_frameworks.mediapipe.fn.RandomBiasedCrop

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

Define graph call:
  • __call__(input1, input2)

Parameter:
  • input1 - First input tensor to operator. Supported dimensions: minimum = 5, maximum = 5. Supported data types: FLOAT32.

  • input2 - Second input tensor to operator. Supported dimensions: minimum = 5, maximum = 5. Supported data types: UINT8.

Description:

This operator takes input volumetric images and its corresponding labeled segmentation data as its input. Probability of over_sampling decides whether to use random crop based on the foreground object or random crop on entire inputs. Random crop types:

  • Based on foreground object: Any non zero value in labeled data is considered as foreground label. It randomly selects a label from the labeled data, extracts connected blobs of pixels with the selected label and calculates the bounding boxes. Then one bounding box is selected randomly out of the top 2 bounding boxes by volume. Random crop is calculated by including the selected bounding box.

  • Based on entire input: It simply performs the random crop on the entire inputs.

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

over_sampling

Probability of selection of random crop types: Either to choose Based on the foreground object or Based on entire input.

  • Type: float

  • Default: 0.33

  • Optional: yes

num_channels

Number of channels in input1.

  • Type: int

  • Default: 1

  • Optional: yes

seed

Seed for randomization.

  • Type: int

  • Default: 0

  • Optional: yes

num_workers

Number of worker threads used.

  • Type: int

  • Default: 1

  • Optional: yes

cache_bboxes

Used for random crop based on foreground objects. Caches the bounding boxes to avoid the extensive computation of finding object blobs of connected pixels and bounding boxes in already processed input. If there is small dataset and its items size is large, caching can be used to save the bounding boxes for repetitive use. The label data inputs are compared based on 128-bit hash, which is less time consuming than to calculate the object boxes again.

  • Type: bool

  • Default: False

  • Optional: yes

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Optional: no

  • Supported data types:

    • FLOAT32 (cropped images)

    • UINT8 (cropped labels)

    • INT32 (crop window)

Note

Produces three outputs: Cropped image, cropped labeled data and coordinates of the output crop window.

Example: Use of RandomBiasedCrop

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


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

        seed = 0

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

        self.inputy = fn.ReadNumpyDatasetFromDir(num_outputs=1,
                                                  shuffle=False,
                                                  dir=dir,
                                                  pattern="*_y.npy",
                                                  dtype=dt.UINT8,
                                                  dense=False,
                                                  seed=seed,
                                                  num_readers=1,
                                                  shuffle_across_dataset=False,
                                                  slice_index=0,
                                                  num_slices=1,
                                                  device="cpu")

        self.crop = fn.RandomBiasedCrop(patch_size=patch_size,
                                        num_channels=num_channels,
                                        seed=seed,
                                        num_workers=4,
                                        cache_bboxes=True,
                                        device=op_device)

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

        self.memcpy1 = fn.MemCpy(dtype=dt.UINT8,
                                device="hpu")

    def definegraph(self):
        img0 = self.inputx()
        img1 = self.inputy()
        img0, img1, coord = self.crop(img0, img1)
        img0 = self.memcpy0(img0)
        img1 = self.memcpy1(img1)
        return img0, img1, coord


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

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

    # Build MediaPipe
    pipe.build()

    # Initialize MediaPipe iterator
    pipe.iter_init()

    # Run MediaPipe
    images, labels, coord = 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
    # Cropped images
    images = as_cpu(images).as_nparray()

    # Cropped labels
    labels = as_cpu(labels).as_nparray()

    # Crop window
    coord = as_cpu(coord).as_nparray()

    del pipe

    # Print crop coordinates
    for i in range(batch_size):
        print("Index {}, Random Biased Crop window coordinates [X1:X2, Y1:Y2, Z1:Z2]: {}".format(
            i, coord[i]))


if __name__ == "__main__":
    dev_opdev = {'mixed': ['cpu'],
                'legacy': ['cpu']}

    for dev in dev_opdev.keys():
        for op_dev in dev_opdev[dev]:
            run(dev, op_dev)

The following is the crop window coordinates of RandomBiasedCrop operator:

Index 0, Random Biased Crop window coordinates [X1:X2, Y1:Y2, Z1:Z2]: [ 36 164 188 316 222 350]
Index 1, Random Biased Crop window coordinates [X1:X2, Y1:Y2, Z1:Z2]: [ 59 187 152 280 197 325]