ndsampler.coco_sampler

Example

>>> # Imagine you have some images
>>> import kwimage
>>> image_paths = [
>>>     kwimage.grab_test_image_fpath('astro'),
>>>     kwimage.grab_test_image_fpath('carl'),
>>>     kwimage.grab_test_image_fpath('airport'),
>>> ]  # xdoc: +IGNORE_WANT
['~/.cache/kwimage/demodata/KXhKM72.png',
 '~/.cache/kwimage/demodata/flTHWFD.png',
 '~/.cache/kwimage/demodata/Airport.jpg']
>>> # And you want to randomly load subregions of them in O(1) time
>>> import ndsampler
>>> import kwcoco
>>> # First make a COCO dataset that refers to your images (and possibly annotations)
>>> dataset = {
>>>     'images': [{'id': i, 'file_name': fpath} for i, fpath in enumerate(image_paths)],
>>>     'annotations': [],
>>>     'categories': [],
>>> }
>>> coco_dset = kwcoco.CocoDataset(dataset)
>>> print(coco_dset)
<CocoDataset(tag=None, n_anns=0, n_imgs=3, ...n_cats=0)>
>>> # Now pass the dataset to a sampler and tell it where it can store temporary files
>>> workdir = ub.ensure_app_cache_dir('ndsampler/demo')
>>> sampler = ndsampler.CocoSampler(coco_dset, workdir=workdir)
>>> # Now you can load arbirary samples by specifing a target dictionary
>>> # with an image_id (gid) center location (cx, cy) and width, height.
>>> target = {'gid': 0, 'cx': 200, 'cy': 200, 'width': 100, 'height': 100}
>>> sample = sampler.load_sample(target)
>>> # The sample contains the image data, any visible annotations, a reference
>>> # to the original target, and params of the transform used to sample this
>>> # patch
...
>>> print(sorted(sample.keys()))
['annots', 'classes', 'im', 'kp_classes', 'params', 'tr']
>>> im = sample['im']
>>> print(im.shape)
(100, 100, 3)
>>> # The load sample function is at the core of what ndsampler does
>>> # There are other helper functions like load_positive / load_negative
>>> # which deal with annotations. See those for more details.
>>> # For random negative sampling see coco_regions.

Module Contents

Classes

CocoSampler

Samples patches of positives and negative detection windows from a COCO

Functions

_center_extent_to_slice(center, window_dims)

Transforms a center and window dimensions into a start/stop slice

_ensure_iterablen(scalar, n)

Attributes

profile

ndsampler.coco_sampler.profile
class ndsampler.coco_sampler.CocoSampler(dset, workdir=None, autoinit=True, backend=None, verbose=0)

Bases: ndsampler.abstract_sampler.AbstractSampler, ndsampler.utils.util_misc.HashIdentifiable, ubelt.NiceRepr

Samples patches of positives and negative detection windows from a COCO dataset. Can be used for training FCN or RPN based classifiers / detectors.

Does data loading, padding, etc…

Parameters
  • dset (kwcoco.CocoDataset) – a coco-formatted dataset

  • backend (str | Dict) – either ‘cog’ or ‘npy’, or a dict with {‘type’: str, ‘config’: Dict}. See AbstractFrames for more details. Defaults to None, which does not do anything fancy.

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo('photos')
...
>>> print(sorted(self.class_ids))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> print(self.n_positives)
4

Example

>>> import ndsampler
>>> self = ndsampler.CocoSampler.demo('photos')
>>> p_sample = self.load_positive()
>>> n_sample = self.load_negative()
>>> self = ndsampler.CocoSampler.demo('shapes')
>>> p_sample2 = self.load_positive()
>>> n_sample2 = self.load_negative()
>>> for sample in [p_sample, n_sample, p_sample2, n_sample2]:
>>>     assert 'annots' in sample
>>>     assert 'im' in sample
>>>     assert 'rel_boxes' in sample['annots']
>>>     assert 'rel_ssegs' in sample['annots']
>>>     assert 'rel_kpts' in sample['annots']
>>>     assert 'cids' in sample['annots']
>>>     assert 'aids' in sample['annots']
classmethod demo(cls, key='shapes', workdir=None, backend=None, **kw)

Create a toy coco sampler for testing and demo puposes

SeeAlso:
  • kwcoco.CocoDataset.demo

_init(self)
property classes(self)
property catgraph(self)

DEPRICATED, use self.classes instead

_depends(self)
lookup_class_name(self, class_id)
lookup_class_id(self, class_name)
property n_positives(self)
property n_annots(self)
property n_samples(self)
__len__(self)
property n_images(self)
property n_categories(self)
property class_ids(self)
property image_ids(self)
preselect(self, **kwargs)

Setup a pool of training examples before the epoch begins

new_sample_grid(self, task, window_dims, window_overlap=0)
load_image_with_annots(self, image_id, cache=True)
Parameters
  • image_id (int) – the coco image id

  • cache (bool, default=True) – if True returns the fast subregion-indexable file reference. Otherwise, eagerly loads the entire image.

Returns

img: the coco image dict augmented with imdata anns: the coco annotations in this image

Return type

Tuple[Dict, List[Dict]]

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> rng = None
>>> img, anns = self.load_image_with_annots(1)
>>> dets = kwimage.Detections.from_coco_annots(anns, dset=self.dset)
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> kwplot.imshow(img['imdata'][:])
>>> dets.draw()
>>> kwplot.show_if_requested()
load_annotations(self, image_id)

Loads the annotations within an image

Parameters

image_id (int) – the coco image id

Returns

list of coco annotation dictionaries

Return type

List[Dict]

load_image(self, image_id, cache=True)

Loads the annotations within an image

Parameters
  • image_id (int) – the coco image id

  • cache (bool, default=True) – if True returns the fast subregion-indexable file reference. Otherwise, eagerly loads the entire image.

Returns

either ndarray data or a indexable reference

Return type

ArrayLike

load_item(self, index, pad=None, window_dims=None, with_annots=True)

Loads item from either positive or negative regions pool.

Lower indexes will return positive regions and higher indexes will return negative regions.

The main paradigm of the sampler is that sampler.regions maintains a pool of target regions, you can influence what that pool is at any point by calling sampler.regions.preselect (usually either at the start of learning, or maybe after every epoch, etc..), and you use load_item to load the index-th item from that preselected pool. Depending on how you preselected the pool, the returned item might correspond to a positive or negative region.

Parameters
  • index (int) – index of target region

  • pad (tuple) – (height, width) extra context to add to each size. This helps prevent augmentation from producing boundary effects

  • window_dims (tuple) – (height, width) area around the center of the target region to sample.

  • with_annots (bool | str, default=True) – if True, also extracts information about any annotation that overlaps the region of interest (subject to visibility_thresh). Can also be a List[str] that specifies which specific subinfo should be extracted. Valid strings in this list are: boxes, keypoints, and segmenation.

Returns

sample: dict containing keys

im (ndarray): image data tr (dict): contains the same input items as tr but additionally

specifies rel_cx and rel_cy, which gives the center of the target w.r.t the returned padded sample.

annots (dict): Dict of aids, cids, and rel/abs boxes

Return type

Dict

load_positive(self, index=None, with_annots=True, tr=None, pad=None, rng=None, **kw)

Load an item from the the positive pool of regions.

Parameters
  • index (int) – index of positive target

  • pad (tuple) – (height, width) extra context to add to each size. This helps prevent augmentation from producing boundary effects

  • tr (Dict) – Extra target arguments like window_dims.

  • with_annots (bool | str, default=True) – if True, also extracts information about any annotation that overlaps the region of interest (subject to visibility_thresh). Can also be a List[str] that specifies which specific subinfo should be extracted. Valid strings in this list are: boxes, keypoints, and segmentation.

Returns

sample: dict containing keys

im (ndarray): image data tr (dict): contains the same input items as tr but additionally

specifies rel_cx and rel_cy, which gives the center of the target w.r.t the returned padded sample.

annots (dict): Dict of aids, cids, and rel/abs boxes

Return type

Dict

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> rng = None
>>> sample = self.load_positive(pad=(10, 10), tr=dict(window_dims=(3, 3)))
>>> assert sample['im'].shape[0] == 23
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> kwplot.imshow(sample['im'])
>>> kwplot.show_if_requested()
load_negative(self, index=None, with_annots=True, tr=None, pad=None, rng=None, **kw)

Load an item from the the negative pool of regions.

Parameters
  • index (int) – if specified loads a specific negative from the presampled pool, otherwise the next negative in the pool is returned.

  • with_annots (bool | str, default=True) – if True, also extracts information about any annotation that overlaps the region of interest (subject to visibility_thresh). Can also be a List[str] that specifies which specific subinfo should be extracted. Valid strings in this list are: boxes, keypoints, and segmentation.

  • tr (Dict) – Extra target arguments like window_dims.

  • pad (tuple) – (height, width) extra context to add to each size. This helps prevent augmentation from producing boundary effects

Returns

sample: dict containing keys

im (ndarray): image data tr (dict): contains the same input items as tr but additionally

specifies rel_cx and rel_cy, which gives the center of the target w.r.t the returned padded sample.

annots (dict): Dict of aids, cids, and rel/abs boxes

Return type

Dict

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> rng = None
>>> sample = self.load_negative(rng=rng, pad=(0, 0))
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> box = kwimage.Boxes(tr.reindex(['rel_cx', 'rel_cy', 'width', 'height']).values, 'cxywh')
>>> kwplot.imshow(sample)
>>> kwplot.draw_boxes(box)
>>> kwplot.show_if_requested()

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> rng = None
>>> sample = self.load_negative(rng=rng, pad=(0, 0), tr=dict(window_dims=(64, 64)))
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> box = kwimage.Boxes(tr.reindex(['rel_cx', 'rel_cy', 'width', 'height']).values, 'cxywh')
>>> kwplot.imshow(sample, fnum=1, doclf=True)
>>> kwplot.draw_boxes(box)
>>> kwplot.show_if_requested()
load_sample(self, tr, with_annots=True, visible_thresh=0.0, pad=None, padkw={'mode': 'constant'}, dtype=None, nodata=None)

Loads the volume data associated with the bbox and frame of a target

Parameters
  • tr (dict) – target dictionary indicating an nd source object (e.g. image or video) and the coordinate region to sample from. Unspecified coordinate regions default to the extent of the source object.

    For 2D image source objects, tr must contain or be able to infer the key gid (int), to specify an image id.

    For 3D video source objects, tr must contain the key vidid (int), to specify a video id (NEW in 0.6.1) or gids List[int], as a list of images in a video (NEW in 0.6.2)

    In general, coordinate regions can specified by the key slices, a numpy-like “fancy index” over each of the n dimensions. Usually this is a tuple of slices, e.g. (y1:y2, x1:x2) for images and (t1:t2, y1:y2, x1:x2) for videos.

    You may also specify: space_slice as (y1:y2, x1:x2) for both 2D images and 3D videos and time_slice as t1:t2 for 3D videos.

    Spatial regions can be specified with keys:
    • ‘cx’ and ‘cy’ as the center of the region in pixels.

    • ‘width’ and ‘height’ are in pixels.

    • ‘window_dims’ is a height, width tuple or can be a

    special string key ‘square’, which overrides width and height to both be the maximum of the two.

    Temporal regions are specifiable by slices, time_slice or an explicit list of gids.

    The aid key can be specified to indicate a specific annotation to load. This uses the annotation information to infer ‘gid’, ‘cx’, ‘cy’, ‘width’, and ‘height’ if they are not present. (NEW in 0.5.10)

    The channels key can be specified as a channel code or

    kwcoco.ChannelSpec object. (NEW in 0.6.1)

    as_xarray (bool, default=False):

    if True, return the image data as an xarray object

  • with_annots (bool | str, default=True) – if True, also extracts information about any annotation that overlaps the region of interest (subject to visibility_thresh). Can also be a List[str] that specifies which specific subinfo should be extracted. Valid strings in this list are: boxes, keypoints, and segmentation.

  • visible_thresh (float) – does not return annotations with visibility less than this threshold.

  • pad (tuple) – (height, width) extra context to add to window dims. This helps prevent augmentation from producing boundary effects

  • padkw (dict) – kwargs for numpy.pad

  • dtype (type | None) – Cast the loaded data to this type. If unspecified returns the data as-is.

  • nodata (int | None) – If specified, for integer data with nodata values, this is passed to kwcoco delayed image finalize. The data is converted to float32 and nodata values are replaced with nan. These nan values are handled correctly in subsequent warping operations.

Returns

sample: dict containing keys

im (ndarray | DataArray): image / video data tr (dict): contains the same input items as tr but additionally

specifies rel_cx and rel_cy, which gives the center of the target w.r.t the returned padded sample.

annots (dict): containing items:
frame_dets (List[kwimage.Detections]): a list of detection

objects containing the requested annotation info for each frame.

aids (list): annotation ids DEPRECATED cids (list): category ids DEPRECATED rel_ssegs (ndarray): segmentations relative to the sample DEPRECATED rel_kpts (ndarray): keypoints relative to the sample DEPRECATED

Return type

Dict

CommandLine:

xdoctest -m ndsampler.coco_sampler CocoSampler.load_sample:2 –show

xdoctest -m ndsampler.coco_sampler CocoSampler.load_sample:1 –show xdoctest -m ndsampler.coco_sampler CocoSampler.load_sample:3 –show

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> # The target (tr) lets you specify an arbitrary window
>>> tr = {'gid': 1, 'cx': 5, 'cy': 2, 'width': 6, 'height': 6}
>>> sample = self.load_sample(tr)
...
>>> print('sample.shape = {!r}'.format(sample['im'].shape))
sample.shape = (6, 6, 3)

Example

>>> # Access direct annotation information
>>> import ndsampler
>>> sampler = ndsampler.CocoSampler.demo()
>>> # Sample a region that contains at least one annotation
>>> tr = {'gid': 1, 'cx': 5, 'cy': 2, 'width': 600, 'height': 600}
>>> sample = sampler.load_sample(tr)
>>> annotation_ids = sample['annots']['aids']
>>> aid = annotation_ids[0]
>>> # Method1: Access ann dict directly via the coco index
>>> ann = sampler.dset.anns[aid]
>>> # Method2: Access ann objects via annots method
>>> dets = sampler.dset.annots(annotation_ids).detections
>>> print('dets.data = {}'.format(ub.repr2(dets.data, nl=1)))

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> tr = self.regions.get_positive(0)
>>> pad = (25, 25)
>>> tr['window_dims'] = 'square'
>>> sample = self.load_sample(tr, pad=pad)
>>> print('im.shape = {!r}'.format(sample['im'].shape))
im.shape = (135, 135, 3)
>>> pad = (0, 0)
>>> tr['window_dims'] = None
>>> sample = self.load_sample(tr, pad=pad)
>>> print('im.shape = {!r}'.format(sample['im'].shape))
im.shape = (52, 85, 3)
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> kwplot.imshow(sample['im'])
>>> kwplot.show_if_requested()

Example

>>> # sample an out of bounds target
>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> tr = self.regions.get_positive(0)
>>> tr['window_dims'] = (364, 364)
>>> sample = self.load_sample(tr)
>>> annots = sample['annots']
>>> assert len(annots['aids']) > 0
>>> #assert len(annots['rel_cxywh']) == len(annots['aids'])
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> abs_frame = self.frames.load_image(sample['tr']['gid'])[:]
>>> tf_rel_to_abs = sample['params']['tf_rel_to_abs']
>>> abs_boxes = annots['rel_boxes'].warp(tf_rel_to_abs)
>>> abs_ssegs = annots['rel_ssegs'].warp(tf_rel_to_abs)
>>> abs_kpts = annots['rel_kpts'].warp(tf_rel_to_abs)
>>> # Draw box in original image context
>>> kwplot.imshow(abs_frame, pnum=(1, 2, 1), fnum=1)
>>> abs_boxes.translate([-.5, -.5]).draw()
>>> abs_kpts.draw(color='green', radius=10)
>>> abs_ssegs.draw(color='red', alpha=.5)
>>> # Draw box in relative sample context
>>> kwplot.imshow(sample['im'], pnum=(1, 2, 2), fnum=1)
>>> annots['rel_boxes'].translate([-.5, -.5]).draw()
>>> annots['rel_ssegs'].draw(color='red', alpha=.6)
>>> annots['rel_kpts'].draw(color='green', alpha=.4, radius=10)
>>> kwplot.show_if_requested()

Example

>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo('photos')
>>> tr = self.regions.get_positive(1)
>>> pad = None
>>> tr['window_dims'] = (300, 150)
>>> sample = self.load_sample(tr, pad)
>>> assert sample['im'].shape[0:2] == tr['window_dims']
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> kwplot.imshow(sample['im'], colorspace='rgb')
>>> kwplot.show_if_requested()

Example

>>> # Multispectral video sample example
>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo('vidshapes1-multispectral', num_frames=5)
>>> sample_grid = self.new_sample_grid('video_detection', (3, 128, 128))
>>> tr = sample_grid['positives'][0]
>>> tr['channels'] = 'B1|B8'
>>> tr['as_xarray'] = False
>>> sample = self.load_sample(tr)
>>> print(ub.repr2(sample['tr'], nl=1))
>>> print(sample['im'].shape)
>>> assert sample['im'].shape == (3, 128, 128, 2)
>>> tr['channels'] = '<all>'
>>> sample = self.load_sample(tr)
>>> assert sample['im'].shape == (3, 128, 128, 5)
_infer_target_attributes(self, tr)

Infer unpopulated target attribues

Example

>>> # sample using only an annotation id
>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> tr = {'aid': 1, 'as_xarray': True}
>>> tr_ = self._infer_target_attributes(tr)
>>> print('tr_ = {}'.format(ub.repr2(tr_, nl=1)))
>>> assert tr_['gid'] == 1
>>> assert all(k in tr_ for k in ['cx', 'cy', 'width', 'height'])
>>> self = CocoSampler.demo('vidshapes8-multispectral')
>>> tr = {'aid': 1, 'as_xarray': True}
>>> tr_ = self._infer_target_attributes(tr)
>>> assert tr_['gid'] == 1
>>> assert all(k in tr_ for k in ['cx', 'cy', 'width', 'height'])
>>> tr = {'vidid': 1, 'as_xarray': True}
>>> tr_ = self._infer_target_attributes(tr)
>>> print('tr_ = {}'.format(ub.repr2(tr_, nl=1)))
>>> assert 'gids' in tr_
>>> tr = {'gids': [1, 2], 'as_xarray': True}
>>> tr_ = self._infer_target_attributes(tr)
>>> print('tr_ = {}'.format(ub.repr2(tr_, nl=1)))
_load_slice(self, tr, pad=None, padkw={'mode': 'constant'}, dtype=None, nodata=None)

Example

>>> # sample an out of bounds target
>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> tr = self.regions.get_positive(0)
>>> tr['as_xarray'] = True
>>> sample = self._load_slice(tr)
>>> print('sample = {!r}'.format(ub.map_vals(type, sample)))
>>> # sample an out of bounds target
>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo('vidshapes2')
>>> tr = self._infer_target_attributes({'vidid': 1})
>>> tr['as_xarray'] = True
>>> sample = self._load_slice(tr)
>>> print('sample = {!r}'.format(ub.map_vals(type, sample)))
>>> tr = self._infer_target_attributes({'gids': [1, 2, 3]})
>>> tr['as_xarray'] = True
>>> sample = self._load_slice(tr)
>>> print('sample = {!r}'.format(ub.map_vals(type, sample)))
CommandLine:

xdoctest -m /home/joncrall/code/ndsampler/ndsampler/coco_sampler.py CocoSampler._load_slice –profile

Example

>>> # Multispectral video sample example
>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo('vidshapes1-multispectral', num_frames=5)
>>> sample_grid = self.new_sample_grid('video_detection', (3, 128, 128))
>>> tr = sample_grid['positives'][0]
>>> tr['channels'] = 'B1|B8'
>>> tr['as_xarray'] = False
>>> sample = self.load_sample(tr)
>>> print(ub.repr2(sample['tr'], nl=1))
>>> print(sample['im'].shape)
>>> assert sample['im'].shape == (3, 128, 128, 2)
>>> tr['channels'] = '<all>'
>>> sample = self.load_sample(tr)
>>> assert sample['im'].shape == (3, 128, 128, 5)
_populate_overlap(self, sample, visible_thresh=0.1, with_annots=True)

Add information about annotations overlapping the sample.

with_annots can be a + separated string or list of the the special keys:

‘segmentation’ and ‘keypoints’.

Example

>>> # sample an out of bounds target
>>> from ndsampler.coco_sampler import *
>>> self = CocoSampler.demo()
>>> tr = self.regions.get_item(0)
>>> sample = self._load_slice(tr)
>>> sample = self._populate_overlap(sample)
>>> print('sample = {}'.format(ub.repr2(ub.util_dict.dict_diff(sample, ['im']), nl=-1)))
ndsampler.coco_sampler._center_extent_to_slice(center, window_dims)

Transforms a center and window dimensions into a start/stop slice

Parameters
  • center (Tuple[float]) – center location (cy, cx)

  • window_dims (Tuple[int]) – window size (height, width)

Returns

the slice corresponding to the centered window

Return type

Tuple[slice, …]

Example

>>> center = (2, 5)
>>> window_dims = (6, 6)
>>> slices = _center_extent_to_slice(center, window_dims)
>>> assert slices == (slice(-1, 5), slice(2, 8))
Example:
>>> center = (2, 5)
>>> window_dims = (64, 64)
>>> slices = _center_extent_to_slice(center, window_dims)
>>> assert slices == (slice(-30, 34, None), slice(-27, 37, None))

Example

>>> # Test floating point error case
>>> center = (500.5, 974.9999999999999)
>>> window_dims  = (100, 100)
>>> slices = _center_extent_to_slice(center, window_dims)
>>> assert slices == (slice(450, 550, None), slice(924, 1024, None))
ndsampler.coco_sampler._ensure_iterablen(scalar, n)