pysegcnn.core.dataset.ImageDataset

class pysegcnn.core.dataset.ImageDataset(root_dir, use_bands=[], tile_size=None, pad=False, gt_pattern='(.*)gt\\.tif', sort=False, seed=0, transforms=[])[source]

Bases: torch.utils.data.dataset.Dataset

Base class for multispectral image data.

Inheriting from torch.utils.data.Dataset to be compliant to the PyTorch standard. This enables the use of the handy torch.utils.data.DataLoader class during model training.

Attributes
root_dirstr

The root directory, path to the dataset.

use_bandslist [str]

List of the spectral bands to use during model training.

tile_sizeint or None

The size of the tiles.

padbool

Whether to center pad the input image.

gt_patternstr

A regural expression to match the ground truth naming convention.

sortbool

Whether to chronologically sort the samples.

seedint

The random seed.

transformslist

List of pysegcnn.core.transforms.Augment instances.

sizetuple [int]

The size of an image of the dataset.

sensorenum.Enum

An enumeration of the bands of sensor the dataset is derived from, see e.g. pysegcnn.core.constants.Landsat8.

bandsdict [int, str]

The spectral bands of sensor. The keys are the number and the values are the name of the spectral bands.

labelsdict [int, dict]

The label dictionary. The keys are the values of the class labels in the ground truth. Each nested dict has keys:

'color'

A named color (str).

'label'

The name of the class label (str).

tilesint

Number of tiles with size (tile_size, tile_size) within an image.

paddingtuple [int]

The amount of padding, (bottom, left, top, right).

heightint

The height of a padded image.

widthint

The width of a padded image.

topleftdict [int, tuple]

The topleft corners of the tiles. The keys of are the tile ids (int) and the values are the topleft corners (y, x) of the tiles.

cvalint

When padding, cval is the value of the “no data” label in the ground truth. Otherwise, cval=0.

gtlist [str or pathlib.Path]

List of the ground truth images.

keyslist

List of required keys for each dictionary in scenes.

sceneslist [dict]

List of dictionaries representing the samples of the dataset.

__init__(root_dir, use_bands=[], tile_size=None, pad=False, gt_pattern='(.*)gt\\.tif', sort=False, seed=0, transforms=[])[source]

Initialize.

Parameters
root_dirstr

The root directory, path to the dataset.

use_bandslist [str], optional

A list of the spectral bands to use. The default is [].

tile_sizeint or None, optional

The size of the tiles. If not None, each scene is divided into square tiles of shape (tile_size, tile_size). The default is None.

padbool, optional

Whether to center pad the input image. Set pad=True, if the images are not evenly divisible by the tile_size. The image data is padded with a constant padding value of zero. For each image, the corresponding ground truth image is padded with a “no data” label. The default is False.

gt_patternstr, optional

A regural expression to match the ground truth naming convention. All directories and subdirectories in root_dir are searched for files matching gt_pattern. The default is (.*)gt\.tif.

sortbool, optional

Whether to chronologically sort the samples. Useful for time series data. The default is False.

seedint, optional

The random seed. Used to split the dataset into training, validation and test set. Useful for reproducibility. The default is 0.

transformslist, optional

List of pysegcnn.core.transforms.Augment instances. Each item in transforms generates a distinct transformed version of the dataset. The total dataset is composed of the original untransformed dataset together with each transformed version of it. If transforms=[], only the original dataset is used. The default is [].

Methods

__init__(root_dir[, use_bands, tile_size, …])

Initialize.

build_samples(scene)

Stack the bands of a sample in a single array.

compose_scenes()

Build the list of samples of the dataset.

get_labels()

Return an enumeration of the class labels of the dataset.

get_sensor()

Return an enumeration of the bands of the sensor of the dataset.

get_size()

Return the size of the images in the dataset.

parse_scene_id(scene_id)

Parse the scene identifier.

preprocess(data, gt)

Preprocess a sample before feeding it to a model.

read_scene(idx)

Read the data of the sample with index idx.

to_tensor(x, dtype)

Convert x to torch.Tensor.