habana_frameworks.mediapipe.fn.RandomBiasedCrop
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.
|
over_sampling |
Probability of selection of random crop types: Either to choose Based on the foreground object or Based on entire input.
|
num_channels |
Number of channels in input1.
|
seed |
Seed for randomization.
|
num_workers |
Number of worker threads used.
|
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.
|
dtype |
Output data type.
|
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 numpy as np
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, patch_size, num_channels, batch_size, dir, slice_index, num_slices):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
self.__class__.__name__)
seed = 0
self.inputxy = fn.ReadNumpyDatasetFromDir(num_outputs=2,
shuffle=False,
dir=dir,
pattern=["*_x.npy",
"*_y.npy"],
dtype=[dt.FLOAT32, dt.UINT8],
dense=False,
seed=seed,
num_readers=4,
shuffle_across_dataset=False,
slice_index=slice_index,
num_slices=num_slices)
self.crop = fn.RandomBiasedCrop(patch_size=patch_size,
num_channels=num_channels,
seed=seed,
num_workers=4,
cache_bboxes=True)
self.memcpy0 = fn.MemCpy(dtype=dt.FLOAT32)
self.memcpy1 = fn.MemCpy(dtype=dt.UINT8)
def definegraph(self):
img0, img1 = self.inputxy()
img0, img1, coord = self.crop(img0, img1)
img0 = self.memcpy0(img0)
img1 = self.memcpy1(img1)
return img0, img1, coord
def main():
batch_size = 2
patch_size = [128, 128, 128]
queue_depth = 1
num_channels = 1
dir = "/path/to/images"
# Create media pipeline object
pipe = myMediaPipe('hpu', queue_depth, patch_size,
num_channels, batch_size, dir, 0, 1)
# Build media pipeline
pipe.build()
# Initialize media pipeline iterator
pipe.iter_init()
# Run media pipeline
images, labels, coord = pipe.run()
# Copy data to host from device as numpy array
# Cropped images
img = images.as_cpu().as_nparray()
# Cropped labels
lbl = labels.as_cpu().as_nparray()
# Crop window
coord = coord.as_cpu().as_nparray()
# 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__":
main()
The following is the crop window coordinates of RandomBiasedCrop operator:
Index 0, Random Biased Crop window coordinates [X1:X2, Y1:Y2, Z1:Z2]: [ 44 172 222 350 159 287]
Index 1, Random Biased Crop window coordinates [X1:X2, Y1:Y2, Z1:Z2]: [ 43 171 197 325 0 128]