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 [x_start, y_start, width, height]. 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:

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

# Create media pipeline derived class
class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, img_h, img_w, dir, annfile):
        super(
            myMediaPipe,
            self).__init__(
            device,
            queue_depth,
            batch_size,
            self.__class__.__name__)

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

        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)

    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 main():
    batch_size = 2
    img_width = 300
    img_height = 300
    img_channel = 3
    img_dir = "/path/to/images"
    ann_file = "/path/to/annotation.json"
    queue_depth = 2

    # Create media pipeline object
    pipe = myMediaPipe('hpu', queue_depth, batch_size,
                        img_height, img_width, img_dir, ann_file)

    # Build media pipeline
    pipe.build()

    # Initialize media pipeline iterator
    pipe.iter_init()

    # Run media pipeline
    images, ids, sizes, boxes, labels, lengths, batch = pipe.run()

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

    # 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)

if __name__ == "__main__":
    main()

The following is the output of CocoReader operator:

coco ids dtype: uint32
coco ids:
[391895 522418]
coco sizes dtype: uint32
coco sizes:
[[360 640]
[480 640]]
coco boxes dtype: float32
coco boxes:
[[[0.5612031  0.40602776 0.73690623 0.9992778 ]
  [0.5310625  0.06155556 0.7715     0.8969167 ]
  [0.7369375  0.48005554 0.7930625  0.61366665]
  ...
  [0.         0.         0.         0.        ]
  [0.         0.         0.         0.        ]
  [0.         0.         0.         0.        ]]

[[0.597625   0.         0.998875   0.9881458 ]
  [0.36571875 0.8471042  0.709375   0.936     ]
  [0.         0.6584167  0.63539064 0.9865208 ]
  ...
  [0.         0.         0.         0.        ]
  [0.         0.         0.         0.        ]
  [0.         0.         0.         0.        ]]]
coco labels dtype: uint32
coco labels:
[[ 4  1  1  2  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  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  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  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  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  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  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  0  0  0  0  0  0  0]
[ 1 44 56 72  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  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  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  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  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  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  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  0  0  0  0  0  0  0]]
coco lengths dtype: uint32
coco lengths:
[4 4]
coco batch dtype: uint32
coco batch:
[2]