RandomPatchSampler#

../../_images/RandomPatchSampler.gif
class torchgeo.samplers.RandomPatchSampler(dataset, *, size, length=None, roi=None, units=Units.PIXELS, generator=None)[source]#

Bases: SpatialSampler

Sample locations from a region of interest randomly.

This is particularly useful during training when you want to maximize the size of the dataset and return as many random chips as possible. Note that randomly sampled chips may overlap.

Added in version 0.10.

__init__(dataset, *, size, length=None, roi=None, units=Units.PIXELS, generator=None)[source]#

Initialize a new RandomPatchSampler instance.

The size argument can either be:

  • a single float - in which case the same value is used for the height and width dimension

  • a tuple of two floats - in which case, the first float is used for the height dimension, and the second float for the width dimension

Parameters:
  • dataset (GeoDataset) – Dataset to sample from.

  • size (tuple[float, float] | float) – Dimensions of each patch.

  • length (int | None) – Number of random samples to draw per epoch (defaults to approximately the maximal number of non-overlapping chips of size size that could be sampled from the dataset).

  • roi (Polygon | None) – Region of interest to sample from (defaults to the bounds of dataset.index).

  • units (Units) – Defines if size is in pixel or CRS units.

  • generator (int | BitGenerator | Generator | RandomState | SeedSequence | None) – Pseudo-random number generator (PRNG).

__iter__()[source]#

Iterate over generated sample locations for each epoch.

Yields:

[xmin – xmax, ymin:ymax] coordinates to index a dataset.