Module Contents



Generates positive and negative samples on the fly.

class ndsampler.toydata.DynamicToySampler(n_positives=100000.0, seed=None, gsize=(416, 416), categories=None)

Bases: ndsampler.abstract_sampler.AbstractSampler

Generates positive and negative samples on the fly.


Its probably more robust to generate a static fixed-size dataset with ‘demodata_toy_dset’ or kwcoco.CocoDataset.demo. However, if you need a sampler that dynamically generates toydata, this is for you.

>>> from ndsampler.toydata import *
>>> self = DynamicToySampler()
>>> window_dims = (96, 96)

img, anns = self.load_positive(window_dims=window_dims) kwplot.autompl() kwplot.imshow(img[‘imdata’])

img, anns = self.load_negative(window_dims=window_dims) kwplot.autompl() kwplot.imshow(img[‘imdata’])


xdoctest -m ndsampler.toydata DynamicToySampler –show


>>> # Test that this sampler works with the dataset
>>> from ndsampler.toydata import *
>>> self = DynamicToySampler(1e3)
>>> imgs = [self.load_positive()['im'] for _ in range(9)]
>>> # xdoctest: +REQUIRES(--show)
>>> stacked = kwimage.stack_images_grid(imgs, overlap=-10)
>>> import kwplot
>>> kwplot.autompl()
>>> kwplot.imshow(stacked)
>>> kwplot.show_if_requested()
load_item(self, index, pad=None, window_dims=None)

Loads from positives and then negatives.

property class_ids(self)
property n_positives(self)
property n_annots(self)
property n_images(self)
lookup_class_name(self, class_id)
lookup_class_id(self, class_name)
_lookup_kpnames(self, class_id)
property n_categories(self)
preselect(self, n_pos=None, n_neg=None, neg_to_pos_ratio=None, window_dims=None, rng=None, verbose=0)

Setup a pool of training examples before the epoch begins

load_image(self, image_id=None, rng=None)
load_image_with_annots(self, image_id=None, rng=None)

Returns a random image and its annotations

abstract load_sample(self, tr, pad=None, window_dims=None)
_load_toy_sample(self, window_dims, pad, rng, centerobj, n_annots)
load_positive(self, index=None, pad=None, window_dims=None, rng=None)

Note: window_dims is height / width


>>> from ndsampler.toydata import *
>>> self = DynamicToySampler(1e2)
>>> sample = self.load_positive()
>>> annots = sample['annots']
>>> assert len(annots['aids']) > 0
>>> assert len(annots['rel_cxywh']) == len(annots['aids'])
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> # Draw box in relative sample context
>>> kwplot.imshow(sample['im'], pnum=(1, 1, 1), fnum=1)
>>> annots['rel_boxes'].translate([-.5, -.5]).draw()
>>> annots['rel_ssegs'].draw(color='red', alpha=.6)
>>> annots['rel_kpts'].draw(color='green', alpha=.8, radius=4)
load_negative(self, index=None, pad=None, window_dims=None, rng=None)