ndsampler.toydata module

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

Bases: AbstractSampler

Generates positive and negative samples on the fly.

Note

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.

CommandLine

xdoctest -m ndsampler.toydata DynamicToySampler --show

Example

>>> # 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()
_images/fig_ndsampler_toydata_DynamicToySampler_002.jpeg
load_item(index, pad=None, window_dims=None)[source]

Loads from positives and then negatives.

property class_ids
property n_positives
property n_annots
property n_images
image_ids()[source]
lookup_class_name(class_id)[source]
lookup_class_id(class_name)[source]
property n_categories
preselect(n_pos=None, n_neg=None, neg_to_pos_ratio=None, window_dims=None, rng=None, verbose=0)[source]
load_image(image_id=None, rng=None)[source]
load_image_with_annots(image_id=None, rng=None)[source]

Returns a random image and its annotations

load_sample(tr, pad=None, window_dims=None)[source]
load_positive(index=None, pad=None, window_dims=None, rng=None)[source]

Note: window_dims is height / width

Example

>>> 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)
_images/fig_ndsampler_toydata_DynamicToySampler_load_positive_002.jpeg
load_negative(index=None, pad=None, window_dims=None, rng=None)[source]