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  # NOQA
from habana_frameworks.mediapipe.mediapipe import MediaPipe  # NOQA
from habana_frameworks.mediapipe.media_types import imgtype as it  # NOQA
from habana_frameworks.mediapipe.media_types import dtype as dt  # NOQA

class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, img_h, img_w, dataset_path, annotation_file, num_threads=1):

        super(
            myMediaPipe,
            self).__init__(
            device,
            queue_depth,
            batch_size,
            num_threads,
            self.__class__.__name__)

        self.input = fn.CocoReader(root=dataset_path,
                                    annfile=annotation_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')  # [batch_size]

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

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

    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)

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

        return boxes, labels

def main():
    batch_size = 2
    img_width = 300
    img_height = 300

    img_dir = "/path/to/images"
    ann_file = "/path/to/annotationfile"
    queue_depth = 2

    # Create MediaPipe object
    pipe = myMediaPipe('cpu', queue_depth, batch_size,
                        img_height, img_width, img_dir, ann_file)

    # Build MediaPipe
    pipe.build()

    # Initialize MediaPipe iterator
    pipe.iter_init()

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

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

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

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

if __name__ == "__main__":
    main()

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.49999994 1.         0.9999999 ]
[0.5        0.49999994 1.         0.9999999 ]
[0.5        0.49999994 1.         0.9999999 ]]

[[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.        ]]]

coco labels dtype: uint32
coco labels:
[[ 0  0  0 ... 46 46 46]
[ 0  0  0 ...  0  0  0]]