habana_frameworks.mediapipe.fn.RandomFlip
habana_frameworks.mediapipe.fn.RandomFlip¶
- Class:
habana_frameworks.mediapipe.fn.RandomFlip(**kwargs)
- Define graph call:
__call__(input, predicate)
- Parameter:
input - Input tensor to operator. Supported dimensions: minimum = 4, maximum = 4. Supported data types: INT8, UINT8, INT16, FLOAT32.
predicate - Tensor which specifies to flip a image or not, size=[batch_size]. Supported dimensions: minimum = 1, maximum = 1. Supported data types: UINT8.
Description:
This operator flips images in a selected direction (horizontal, vertical and depthwise) with a random predicate.
- Supported backend:
HPU
Keyword Arguments
kwargs |
Description |
---|---|
horizontal |
Set to 1 if image needs to be flipped horizontally.
|
vertical |
Set to 1 if image needs to be flipped vertically.
|
depthwise |
Experimental for 3D images Set to 1 if image needs to be flipped depthwise.
|
dtype |
Output data type.
|
Example: RandomFlip Operator
The following code shows use of RandomFlip operator by providing input images and a predicate tensor:
random_flip_func
is a random number generator class which inherits habana_frameworks.mediapipe.operators.cpu_nodes.cpu_nodes.media_function
.
It can be integrated to mediapipe using fn.MediaFunc
utility.
Output of random_flip_func
is passed to RandomFlip
node for selecting files to flip.
Displaying original images and RandomFlip outputs with title “flipped” or “Not flipped”, based on generated random numbers.
from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import imgtype as it
from habana_frameworks.mediapipe.media_types import dtype as dt
from habana_frameworks.mediapipe.operators.cpu_nodes.cpu_nodes import media_function
import matplotlib.pyplot as plt
import numpy as np
# Create media pipeline derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, dir, queue_depth, batch_size, img_h, img_w):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
self.__class__.__name__)
self.input = fn.ReadImageDatasetFromDir(shuffle=False,
dir=dir,
format="jpg")
# WHCN
self.decode = fn.ImageDecoder(device="hpu",
output_format=it.RGB_P,
resize=[img_w, img_h])
self.predicate = fn.MediaFunc(func=random_flip_func,
shape=[batch_size],
dtype=dt.UINT8,
seed=45827092,
priv_params={'prob':0.5})
self.random_flip = fn.RandomFlip(horizontal=1)
# WHCN -> CWHN
self.transpose = fn.Transpose(permutation=[2, 0, 1, 3],
tensorDim=4,
dtype=dt.UINT8)
def definegraph(self):
images, labels = self.input()
images = self.decode(images)
predicate = self.predicate()
images = self.random_flip(images, predicate)
images = self.transpose(images)
return images, labels, predicate
class random_flip_func(media_function):
"""
Class defining the random flip implementation.
"""
def __init__(self, params):
"""
Constructor method.
:params params: dictionary of params conatining
shape: output shape of this class.
dtype: output dtype of this class.
seed: seed to be used for randomization.
"""
self.np_shape = params['shape'][::-1]
self.np_dtype = params['dtype']
self.seed = params['seed']
self.prob = params['priv_params']['prob']
self.rng = np.random.default_rng(self.seed)
def __call__(self):
"""
Callable class method
:returns : random flip values calculated per image.
"""
a = self.rng.choice([0, 1], p=[1 - self.prob, self.prob], size=self.np_shape)
a = np.array(a, dtype=self.np_dtype)
return a
def main():
batch_size = 6
img_width = 200
img_height = 200
img_dir = "/path/to/images"
queue_depth = 2
# Create media pipeline object
pipe = myMediaPipe('hpu', img_dir, queue_depth, batch_size,
img_height, img_width)
# Build media pipeline
pipe.build()
# Initialize media pipeline iterator
pipe.iter_init()
# Run media pipeline
images, labels, predicate = pipe.run()
# Copy data to host from device as numpy array
images = images.as_cpu().as_nparray()
labels = labels.as_cpu().as_nparray()
predicate = predicate.as_cpu().as_nparray()
# Display images
plt.figure(figsize=(10, 10))
for i in range(batch_size):
ax = plt.subplot(2, 3, i + 1)
plt.imshow(images[i].astype("uint8"))
title = "flipped" if predicate[i] else "not flipped"
plt.title(title)
plt.axis("off")
plt.show()
if __name__ == "__main__":
main()
Images with RandomFlip 1
- 1
Licensed under a CC BY SA 4.0 license. The images used here are taken from https://data.caltech.edu/records/mzrjq-6wc02.