kwcoco.examples.simple_kwcoco_torch_dataset

This example demonstrates how to use kwcoco to write a very simple torch dataset. This assumes the dataset will be single-image RGB inputs. This file is intended to talk the reader through what we are doing and why.

This example aims for clairity over being concise. There are APIs exposed by kwcoco (and its sister module ndsampler) that can perform the same tasks more efficiently and with fewer lines of code.

If you run the doctest, it will produce a visualization that shows the images with boxes drawn on it, running it multiple times will let you see the augmentations. This can be done with the following command:

xdoctest -m kwcoco.examples.simple_kwcoco_torch_dataset KWCocoSimpleTorchDataset –show

Or just copy the doctest into IPython and run it.

Module Contents

Classes

KWCocoSimpleTorchDataset

A simple torch dataloader where each image is considered a single item.

Attributes

DatasetBase

kwcoco.examples.simple_kwcoco_torch_dataset.DatasetBase[source]
class kwcoco.examples.simple_kwcoco_torch_dataset.KWCocoSimpleTorchDataset(coco_dset, input_dims=None, antialias=False, rng=None)[source]

Bases: DatasetBase

A simple torch dataloader where each image is considered a single item.

Parameters
  • coco_dset (kwcoco.CocoDataset | str) – something coercable to a kwcoco dataset, this could either be a kwcoco.CocoDataset object, a path to a kwcoco manifest on disk, or a special toydata code. See kwcoco.CocoDataset.coerce() for more details.

  • input_dims (Tuple[int, int]) – These are the (height, width) dimensions that the image will be resized to.

  • antialias (bool, default=False) – If true, we will antialias before downsampling.

  • rng (RandomState | int | None) – an existing random number generator or a random seed to produce deterministic augmentations.

Example

>>> # xdoctest: +REQUIRES(module:torch)
>>> from kwcoco.examples.simple_kwcoco_torch_dataset import *  # NOQA
>>> import kwcoco
>>> coco_dset = kwcoco.CocoDataset.demo('shapes8')
>>> input_dims = (384, 384)
>>> self = torch_dset = KWCocoSimpleTorchDataset(coco_dset, input_dims=input_dims)
>>> index = len(self) // 2
>>> item = self[index]
>>> # xdoctest: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.figure(doclf=True, fnum=1)
>>> kwplot.autompl()
>>> canvas = item['inputs']['rgb'].numpy().transpose(1, 2, 0)
>>> # Construct kwimage objects for batch item visualization
>>> dets = kwimage.Detections(
>>>     boxes=kwimage.Boxes(item['labels']['cxywh'], 'cxywh'),
>>>     class_idxs=item['labels']['class_idxs'],
>>>     classes=self.classes,
>>> ).numpy()
>>> # Overlay annotations on the image
>>> canvas = dets.draw_on(canvas)
>>> kwplot.imshow(canvas)
>>> kwplot.show_if_requested()
__len__(self)[source]

Return the number of items in the Dataset

__getitem__(self, index)[source]

Construct a batch item to be used in training