class_sampler
data.batch_sampler.class_sampler
BatchSampler
BatchSampler(dataset: Dataset, batch_size: int = 1, repeat: bool = False, shuffle: bool = False, drop_last: bool = False)
Bases: Registerable, ABC
Baseclass for all batch_sampler classes.
Attributes:
-
dataset(Dataset) –dataset to sample from.
-
batch_size(int) –batch size.
-
repeat(bool) –repeat data when data is not enough for batch size.
-
shuffle(bool) –shuffle data.
-
drop_last(bool) –drop last data when data is not enough for batch size.
Note
batch_size,shuffle, anddrop_lastshould be popped from dataloader config, then passed to the batch sampler config.repeat: for SaigeVision2 api, should be set asTrue.- if number of data is enough for batch size, then
repeatis neglected.
- if number of data is enough for batch size, then
drop_last: for SaigeVision2 api, should be set asTrue.- to prevent the last batch from being smaller than the specified batch size.
- if
False, even whenrepeatis set asTrue, the last batch will be smaller than the specified batch size.
- for Training,
shuffleshould be set asTrue.drop_lastandrepeatare optional.
- for Validation,
repeat,shuffle, anddrop_lastshould be set asFalse.
Examples:
>>> cfg_sampler.update(
... {
... "shuffle": cfg_dataloader.pop("shuffle", False),
... "drop_last": cfg_dataloader.pop("drop_last", False),
... "batch_size": cfg_dataloader.pop("batch_size", 1),
... }
... )
... batch_sampler = build_batch_sampler(dataset=dataset, **cfg_sampler)
... dataloader = build_dataloader(
... dataset,
... collate_fn=collate_fn,
... batch_sampler=batch_sampler,
... **cfg_dataloader,
... )
Source code in SaigeToolkit/data/batch_sampler/base_sampler.py
ClassBalancedSampler
Bases: BatchSampler
Sampler implementation for simple class balancing.
Uses class_index value in dataset file dictionaries.
When more than one classes exist in single image, the multiplier of
the rarest class within image will be applied to the data.
Raises:
-
IndexError–Will be raised when class index exceeds MAX_NUM_CLS.
Notes
- TODO: Weighted balancing.
- TODO: Cropper applicability.