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

Keyword Arguments

kwargs

Description

horizontal

Set to 1 if image needs to be flipped horizontally.

  • Type: int

  • Default: 0

  • Optional: yes

vertical

Set to 1 if image needs to be flipped vertically.

  • Type: int

  • Default: 0

  • Optional: yes

depthwise

Experimental for 3D images Set to 1 if image needs to be flipped depthwise.

  • Type: int

  • Default: 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 change to default UINT8.

  • Supported data types:

    • INT8

    • UINT8

    • INT16

    • FLOAT32

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

# Create media pipeline 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")

        data = np.ones(1, dtype=dt.FLOAT32)
        data = data*0.5
        self.probability = fn.MediaConst(data=data,
                                        shape=[1],
                                        dtype=dt.FLOAT32,
                                        device="cpu")

        self.coin_flip = fn.CoinFlip(seed=100,
                                    device=op_device)

        self.opdevice = op_device
        if op_device == "hpu":
            self.reshape = fn.Reshape(size=[batch_size],
                                    tensorDim=1,
                                    layout='',
                                    dtype=dt.UINT8,
                                    device=op_device)

        self.random_flip = fn.RandomFlip(horizontal=1,
                                        dtype=dt.UINT8,
                                        device=op_device)

    def definegraph(self):
        inp = self.inp()
        probability = self.probability()
        predicate = self.coin_flip(probability)
        if (self.opdevice == "hpu"):
            predicate = self.reshape(predicate)
        out = self.random_flip(inp, predicate)
        return inp, predicate, out


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

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

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

    # Run media pipeline
    inp, predicate, out = 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()
    predicate = as_cpu(predicate).as_nparray()
    predicate = predicate.reshape(batch_size, 1)
    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("\npredicate tensor shape:", predicate.shape)
    print("predicate tensor dtype:", predicate.dtype)
    print("predicate tensor data:\n", predicate)

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

    return inp, predicate, out


def compare_ref(inp, predicate, out):
    ref = inp
    for i in range(predicate.shape[0]):
        if (predicate[i][0] == 1):
            ref[i] = np.flip(inp[i], axis=-1)
    if np.array_equal(ref, out) == False:
        raise ValueError(f"Mismatch w.r.t ref for device")


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, predicate, out = run(dev, op_dev)
            compare_ref(inp, predicate, out)

The following is the output for RandomFlip operator:

inp tensor shape: (5, 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]]]


[[[148  87  82]
  [216 158  84]]

  [[123 206  47]
  [169  57 199]]

  [[251 167  20]
  [ 56  44  12]]]


[[[ 44  10  70]
  [227  63 176]]

  [[115 169 230]
  [185 139 107]]

  [[ 64   3 168]
  [169  11 143]]]


[[[238 118 201]
  [190 243 158]]

  [[240 228 237]
  [236 126  48]]

  [[151 123 165]
  [189 187  46]]]]

predicate tensor shape: (5, 1)
predicate tensor dtype: int8
predicate tensor data:
[[0]
[0]
[1]
[1]
[1]]

out tensor shape: (5, 3, 2, 3)
out tensor dtype: uint8
out 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]]]


[[[ 82  87 148]
  [ 84 158 216]]

  [[ 47 206 123]
  [199  57 169]]

  [[ 20 167 251]
  [ 12  44  56]]]


[[[ 70  10  44]
  [176  63 227]]

  [[230 169 115]
  [107 139 185]]

  [[168   3  64]
  [143  11 169]]]


[[[201 118 238]
  [158 243 190]]

  [[237 228 240]
  [ 48 126 236]]

  [[165 123 151]
  [ 46 187 189]]]]