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

# Create media pipeline derived class
class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, num_threads, 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=device)

        self.const = fn.Constant(constant=0.7,
                                dtype=dt.FLOAT32,
                                device=device)

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

        self.random_flip = fn.RandomFlip(horizontal=1,
                                        device=device)

    def definegraph(self):
        inp = self.inp()
        probability = self.const()
        predicate = self.coin_flip(probability)
        out = self.random_flip(inp, predicate)
        return inp, predicate, out


def main():
    batch_size = 1
    queue_depth = 2
    num_threads = 1
    device = 'cpu'
    dir = '/path/to/numpy/files'

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

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

    # Run media pipeline
    inp, predicate, out = pipe.run()

    if (device == 'cpu'):
        # Copy data as numpy array
        inp = inp.as_nparray()
        predicate = predicate.as_nparray()
        out = out.as_nparray()
    else:
        # Copy data to host from device as numpy array
        inp = inp.as_cpu().as_nparray()
        predicate = predicate.as_cpu().as_nparray()
        out = out.as_cpu().as_nparray()

    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)

    pipe.del_iter()

if __name__ == "__main__":
    main()

The following is the output for RandomFlip operator:

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

predicate tensor shape: (1,)
predicate tensor dtype: uint8
predicate tensor data:
[1]

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

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

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