habana_frameworks.mediapipe.fn.SSDEncode

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

Define graph call:
  • __call__(boxes, labels, lengths)

Parameter:

  • boxes - Input tensor of bounding boxes (each bbox should be in [left, top, right, bottom] format). size=[batch, 200, 4] Supported dimensions: minimum = 3, maximum = 3. Supported data types: FLOAT32.

  • labels - Input tensor of image labels for each bounding box. size=[batch, 200]. Supported dimensions: minimum = 2, maximum = 2. Supported data types: UINT32.

  • lengths - Input tensor of number of bounding boxes per image. size=[batch]. Supported dimensions: minimum = 1, maximum = 1. Supported data types: UINT32.

Description:

SSDEncode operator encodes input bounding boxes and labels using predefined 8732 SSD anchor boxes.

Supported backend:
  • CPU

Output:

Output Value

Description

boxes

List of encoded (wrt 8732 anchors) bounding boxes for every image [x, y, w, h].

labels

List of labels for every encoded bounding box.

Example: SSDEncode Operator

The following code snippet shows usage of SSDEncode operator:

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
import matplotlib.pyplot as plt
import os

g_display_timeout = os.getenv("DISPLAY_TIMEOUT") or 5

class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, num_threads,
                op_device, dir, ann_file, img_h, img_w):
        super(
            myMediaPipe,
            self).__init__(
            device,
            queue_depth,
            batch_size,
            num_threads,
            self.__class__.__name__)

        self.input = fn.CocoReader(root=dir,
                                  annfile=ann_file,
                                  seed=1234,
                                  shuffle=False,
                                  drop_remainder=True,
                                  num_slices=1,
                                  slice_index=0,
                                  partial_batch=False,
                                  device='cpu')

        self.reshape_ids = fn.Reshape(size=[batch_size],
                                      tensorDim=1,
                                      layout='',
                                      dtype=dt.UINT32, device='hpu')

        self.ssd_crop_win_gen = fn.SSDCropWindowGen(num_iterations=1,
                                                    seed=1234,
                                                    device='cpu')

        self.ssd_encode = fn.SSDEncode(device='cpu')

        self.decode = fn.ImageDecoder(device="hpu",
                                      output_format=it.RGB_P,
                                      resize=[img_w, img_h])

        # WHCN -> CWHN
        self.transpose = fn.Transpose(permutation=[2, 0, 1, 3],
                                      tensorDim=4,
                                      dtype=dt.UINT8)

    def definegraph(self):
        # Train pipe
        jpegs, ids, sizes, boxes, labels, lengths, batch = self.input()

        # ssd crop window generation
        sizes, boxes, labels, lengths, windows = self.ssd_crop_win_gen(
            sizes, boxes, labels, lengths)

        images = self.decode(jpegs, windows)

        # ssd encode
        boxes, labels = self.ssd_encode(boxes, labels, lengths)

        images = self.transpose(images)

        return images, boxes, labels


def display_images(images, batch_size, cols):
    rows = (batch_size + 1) // cols
    plt.figure(figsize=(10, 10))
    for i in range(batch_size):
        ax = plt.subplot(rows, cols, i + 1)
        plt.imshow(images[i])
        plt.axis("off")
    plt.show(block=False)
    plt.pause(g_display_timeout)
    plt.close()

def run(device, op_device):
    batch_size = 6
    img_width = 300
    img_height = 300
    num_threads = 1
    queue_depth = 2

    base_dir = os.environ['DATASET_DIR']
    base_dir = base_dir+"/coco_data/"
    dir = base_dir + "/imgs/"
    ann_file = base_dir + "/annotation.json"

    # Create MediaPipe object
    pipe = myMediaPipe(device, queue_depth, batch_size, num_threads,
                      op_device, dir, ann_file, img_height, img_width)

    # Build MediaPipe
    pipe.build()

    # Initialize MediaPipe iterator
    pipe.iter_init()

    # Run MediaPipe
    images, boxes, labels = pipe.run()

    # Copy data to host from device as numpy array
    images = images.as_cpu().as_nparray()
    boxes = boxes.as_nparray()
    labels = labels.as_nparray()

    del pipe

    print('coco boxes dtype:', boxes.dtype)
    print('coco boxes:', boxes)

    print('coco labels dtype:', labels.dtype)
    print('coco labels:', labels)

    display_images(images, batch_size, 3)


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

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

SSB BBox Encode Output Images 1

Image1 of slice
Image2 of slice
Image3 of slice
Image4 of slice
Image5 of slice
Image6 of slice
1

The following is the output of SSDMetadata operator:

coco boxes dtype: float32
coco boxes: [[[0.01333333 0.01333333 0.07       0.07      ]
  [0.04       0.01333333 0.07       0.07      ]
  [0.06666667 0.01333333 0.07       0.07      ]
  ...
  [0.5        0.5        0.9557719  0.9557719 ]
  [0.5        0.5        1.         0.6151829 ]
  [0.5        0.5        0.6151829  1.        ]]

[[0.01333333 0.01333333 0.07       0.07      ]
  [0.04       0.01333333 0.07       0.07      ]
  [0.06666667 0.01333333 0.07       0.07      ]
  ...
  [0.5        0.5        0.9557719  0.9557719 ]
  [0.5        0.5        1.         0.6151829 ]
  [0.5        0.5        0.6151829  1.        ]]

[[0.01333333 0.01333333 0.07       0.07      ]
  [0.04       0.01333333 0.07       0.07      ]
  [0.06666667 0.01333333 0.07       0.07      ]
  ...
  [0.5        0.5        0.9557719  0.9557719 ]
  [0.5        0.5        1.         0.6151829 ]
  [0.5        0.5        0.6151829  1.        ]]

[[0.01333333 0.01333333 0.07       0.07      ]
  [0.04       0.01333333 0.07       0.07      ]
  [0.06666667 0.01333333 0.07       0.07      ]
  ...
  [0.5        0.5        0.9557719  0.9557719 ]
  [0.5        0.5        1.         0.6151829 ]
  [0.5        0.5        0.6151829  1.        ]]

[[0.01333333 0.01333333 0.07       0.07      ]
  [0.04       0.01333333 0.07       0.07      ]
  [0.06666667 0.01333333 0.07       0.07      ]
  ...
  [0.49218747 0.45380607 0.98437494 0.90761214]
  [0.49218747 0.45380607 0.98437494 0.90761214]
  [0.49218747 0.45380607 0.98437494 0.90761214]]

[[0.01333333 0.01333333 0.07       0.07      ]
  [0.04       0.01333333 0.07       0.07      ]
  [0.06666667 0.01333333 0.07       0.07      ]
  ...
  [0.5441176  0.49999997 0.9117647  0.99999994]
  [0.5441176  0.49999997 0.9117647  0.99999994]
  [0.5441176  0.49999997 0.9117647  0.99999994]]]
coco labels dtype: int32
coco labels: [[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 2 2 2]
[0 0 0 ... 2 2 2]]