habana_frameworks.mediapipe.fn.CocoReader

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

Define graph call:
  • __call__()

Parameter:
  • None

Description:

Reader for SSD Coco dataset which produces outputs as images and metadata.

Supported backend:
  • CPU

Keyword Arguments

kwargs

Description

root

Input directory path of Coco dataset.

  • Type: str

  • Default: None

  • Optional: no

annfile

Annotation .json file to be used for given Coco dataset.

  • Type: str

  • Default: None

  • Optional: no

drop_remainder

If True, reader will drop the last partial batch of the epoch. If False, padding mechanism can be controlled by pad_reminder.

  • Type: bool

  • Default: False

  • Optional: yes

pad_remainder

If True, reader will replicate last image of partial batch. If False, partial batch is filled with images randomly selected from dataset.

  • Type: bool

  • Default: False

  • Optional: yes

  • Node: pad_reminder is valid when drop reminder is false.

partial_batch

Used only when drop_remainder is False. If set to True then batch output of reader indicates number of non-padded images in the batch. This can be used to remove padded images from the batch.

  • Type: bool

  • Default: False

  • Optional: yes

num_slices

It indicates number of cards in multi-card training. Before first epoch, input data is divided into num_slices i.e. one slice for every card. During entire training, same slice will be used for that particular card for creating batches in every epoch. Default value is 1, which indicates single card training.

  • Type: int

  • Default: 1

  • Optional: yes

slice_index

In multi-card training, it indicates index of card.

  • Type: int

  • Default: 0

  • Optional: yes (if num_slices=1, Otherwise user must provide it)

  • Note: Default value is zero for single card training. For multi-card it must be between 0 and num_slices - 1.

shuffle

If set to True, the reader shuffles the entire dataset before each epoch. In case of multi-card training, it first creates random slices of files for every card then shuffle.

  • Type: bool

  • Default: True

  • Optional: yes

max_file

Full path of biggest input file. This is used for pre-allocating buffer. If not provided, reader will find it.

  • Type: str

  • Default: None

  • Optional: yes

  • Note: This feature is provided to optimize reading time, especially in case of bigger datasets.

seed

Seed for randomization. If not provided it will be generated internally. It is used for shuffling the dataset as well as for randomly selecting the images to pad the last batch when the drop_reminder is False and pad_reminder is False.

  • Type: int

  • Default: None

  • Optional: yes

Output:

Output Value

Description

images

List of images.

ids

Image id from annotation file.

sizes

Image sizes.

boxes

List of bounding boxes for every image [left, top, right, bottom]. Maximum 200 ground truth boxes per image supported.

labels

List of labels for every bounding box.

lengths

Number of boxes per image.

batch

Number of valid images in a batch. Normally, this is equal to batch size except in case of partial batch, where it is set to number of valid images in that batch.

Example: CocoReader Operator

The following code snippet shows usage of CocoReader operator, it usage fabricated data.:

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
from media_pipe_api import MetadataOps
import os

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

# Create MediaPipe derived class
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=0,
                                  shuffle=False,
                                  drop_remainder=True,
                                  num_slices=1,
                                  slice_index=0,
                                  partial_batch=False,
                                  device=op_device)

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

        self.transpose = fn.Transpose(permutation=[2, 0, 1, 3],
                                      tensorDim=4,
                                      dtype=dt.UINT8,
                                      device="hpu")

    def definegraph(self):
        images, ids, sizes, boxes, labels, lengths, batch = self.input()
        images = self.decode(images)
        images = self.transpose(images)
        return images, ids, sizes, boxes, labels, lengths, batch

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
    img_channel = 3
    num_threads = 1
    base_dir = os.environ['DATASET_DIR']
    base_dir = base_dir+"/coco_data/"
    dir = base_dir + "/imgs/"
    ann_file = base_dir + "/annotation.json"
    queue_depth = 2

    # 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, ids, sizes, boxes, labels, lengths, batch = pipe.run()

    def as_cpu(tensor):
        if (callable(getattr(tensor, "as_cpu", None))):
            tensor = tensor.as_cpu()
        return tensor

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

    del pipe

    # Display images, shape, dtype
    print('coco ids dtype:', ids.dtype)
    print('coco ids:\n', ids)

    print('coco sizes dtype:', sizes.dtype)
    print('coco sizes:\n', sizes)

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

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

    print('coco lengths dtype:', lengths.dtype)
    print('coco lengths:\n', lengths)

    print('coco batch dtype:', batch.dtype)
    print('coco batch:\n', batch)

    display_images(images, batch_size, 3)

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

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

The following is the output of CocoReader operator:

coco ids dtype: int32
coco ids:
[[391895]
[522418]
[184613]
[318219]
[554625]
[574769]]
coco sizes dtype: int32
coco sizes:
[[300 248]
[202 300]
[300 295]
[300 291]
[256 300]
[214 300]]
coco boxes dtype: float32
coco boxes:
[[[0.5        0.         0.9153226  0.3       ]
  [0.         0.         0.         0.        ]
  ...
  [0.         0.         0.         0.        ]]

[[0.13333334 0.0990099  0.46666667 0.7920792 ]
  [0.         0.         0.         0.        ]
  ...
  [0.         0.         0.         0.        ]]
...
...
[[0.16666667 0.16666636 0.8333333  0.83330184]
  [0.         0.         0.         0.        ]
  ...
  [0.         0.         0.         0.        ]]]
coco labels dtype: int32
coco labels:
[[1 0 0 ... 0 0 0]
[2 0 0 ... 0 0 0]
[1 0 0 ... 0 0 0]
[1 0 0 ... 0 0 0]
[2 0 0 ... 0 0 0]
[2 0 0 ... 0 0 0]]
coco lengths dtype: int32
coco lengths:
[[1]
[1]
[1]
[1]
[1]
[1]]
coco batch dtype: int32
coco batch:
[[6]
[6]
[6]
[6]
[6]
[6]]

Output Images from CocoReader Operation 1

Image1 of CocoReader.
Image2 of CocoReader.
Image3 of CocoReader.
Image4 of CocoReader.
Image5 of CocoReader.
Image6 of CocoReader.
1

Licensed under a CC BY SA 4.0 license. The images used here are taken from https://data.caltech.edu/records/mzrjq-6wc02.