BigEarthNet Utils#

Helpful functions when working with lmdb file which contains BigEarthNet as encoded binary patches. Functions include reading data and extracting specific band combinations and their properties.

class configilm.extra.BENv1_utils.BENv1LMDBReader#
__init__(lmdb_dir, image_size, bands, label_type)#

Initialize a BigEarthNet v1.0 Reader object that reads from a lmdb encoded file.

Parameters:
  • lmdb_dir (Union[str, Path]) – base path that contains a data.mdb and lock.mdb

  • image_size (Sequence[int]) – final size of the image that it is interpolated to

  • bands (Union[Iterable, str, int]) – bands to use for stacking e.g. [“B08”, “B03”, “VV”] or a string of BAND_COMBINATION_PREDEFINTIONS

  • label_type (str) – “new” or “old” depending on if the old labels (43) or new ones (19) should be returned

configilm.extra.BENv1_utils.band_combi_to_mean_std(bands)#

Retrievs the mean and standard deviation for a given BigEarthNet BAND_COMBINATION_PREDEFINTIONS or list of bands.

Parameters:

bands (Union[Iterable, str, int]) – combination of bands as defined in BAND_COMBINATION_PREDEFINTIONS or a list of bandsmb

Returns:

mean and standard deviation for the given combination in same order

configilm.extra.BENv1_utils.ben19_list_to_onehot(lst)#

Converts a list of [BEN19 Labels] to a one hot vector. Elements in the vector are sorted alphabetically. See valid_labels_classification() for details.

configilm.extra.BENv1_utils.read_ben_from_lmdb(env, key)#

Reads a BigEarthNet_S1_S2_Patch from a lmdb environment and decodes it.

Parameters:
  • env – lmdb environment containing encoded BigEarthNet_S1_S2_Patch

  • key – patch name as defined in BigEarthNet as string

Returns:

Decoded BigEarthNet_S1_S2_Patch

Raises:

AssertionError if key not in database

configilm.extra.BENv1_utils.resolve_data_dir(data_dir, allow_mock=False, force_mock=False)#

Helper function that tries to resolve the correct directory.

Parameters:
  • data_dir (Optional[Mapping[str, Path]]) – current path that is suggested

  • allow_mock (bool) – allows mock data path to be returned

  • force_mock (bool) – only mock data path will be returned. Useful for debugging with small data

Returns:

a valid dir to the dataset if data_dir was none, otherwise data_dir

Return type:

Mapping[str, Path]

class configilm.extra.BENv2_utils.BENv2LDMBReader#
S1_keys(update=False)#
Parameters:

update (bool) –

S2_keys(update=False)#
Parameters:

update (bool) –

__init__(image_lmdb_file, metadata_file, metadata_snow_cloud_file=None, bands=None, process_bands_fn=None, process_labels_fn=None)#
Parameters:
  • image_lmdb_file (Union[str, Path]) –

  • metadata_file (Union[str, Path]) –

  • metadata_snow_cloud_file (Optional[Union[str, Path]]) –

  • bands (Optional[Union[Iterable, str, int]]) –

  • process_bands_fn (Optional[Callable[[Dict[str, ndarray], List[str]], Any]]) –

  • process_labels_fn (Optional[Callable[[List[str]], Any]]) –

keys(update=False)#
Parameters:

update (bool) –

open_env()#
configilm.extra.BENv2_utils.STANDARD_BANDS = {'S1': ['VV', 'VH'], 'S2': ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B09', 'B11', 'B12'], 'ALL': ['VV', 'VH', 'B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B09', 'B11', 'B12'], 'RGB': ['B04', 'B03', 'B02'], '10m': ['B02', 'B03', 'B04', 'B08'], '20m': ['B05', 'B06', 'B07', 'B8A', 'B11', 'B12'], '60m': ['B01', 'B09'], '10m_20m': ['B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12'], 'S1_10m_20m': ['VV', 'VH', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12'], 2: ['VV', 'VH'], 3: ['B04', 'B03', 'B02'], 4: ['B02', 'B03', 'B04', 'B08'], 10: ['B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12'], 12: ['VV', 'VH', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12']}#

This file contains band statistics for BigEarthNet v2 (including S1 stats from v1) for various interpolation types. The statistics are calculated based on the images of the official train split.

configilm.extra.BENv2_utils.band_combi_to_mean_std(bands, interpolation='120_nearest')#

Retrievs the mean and standard deviation for a given BigEarthNet BAND_COMBINATION_PREDEFINTIONS or list of bands.

Parameters:
  • bands (Iterable[str]) – a list of bandnames

  • interpolation (str) –

    the interpolation type to use. For available types see means.keys()

    default:

    ”120_nearest”

Returns:

mean and standard deviation for the given combination in same order

configilm.extra.BENv2_utils.ben_19_labels_to_multi_hot(labels, lex_sorted=True)#
Parameters:
  • labels (Iterable[str]) –

  • lex_sorted (bool) –

Return type:

Tensor

configilm.extra.BENv2_utils.ben_19_labels_to_multi_hot_np(labels, lex_sorted=True)#

Convenience function that converts an input iterable of labels into a multi-hot encoded vector. If lex_sorted is True (default) the classes are lexigraphically ordered, as they are in constants.NEW_LABELS. If lex_sorted is False, the original order from the BigEarthNet paper is used, as they are given in constants.NEW_LABELS_ORIGINAL_ORDER.

If an unknown label is given, a KeyError is raised.

Be aware that this approach assumes that all labels are actually used in the dataset! This is not necessarily the case if you are using a subset!

Parameters:
  • labels (Iterable[str]) –

  • lex_sorted (bool) –

Return type:

array

configilm.extra.BENv2_utils.resolve_band_combi(bands)#

Resolves a predefined combination of bands or a list of bands into a list of individual bands and checks if all bands contained are actual S1/S2 band names.

Parameters:

bands (Union[Iterable, str, int]) – a combination of bands as defined in BAND_COMBINATION_PREDEFINTIONS or a list of bands, or a single band, e.g. “B02”, [“B02”, “B03”], 2, “S1”, “S2”

Returns:

a list of bands contained in the predefinition or the single band as list

Return type:

list

configilm.extra.BENv2_utils.resolve_data_dir(data_dir, allow_mock=False, force_mock=False)#

Helper function that tries to resolve the correct directory.

Parameters:
  • data_dir (Optional[Mapping[str, Path]]) – current path that is suggested

  • allow_mock (bool) –

    allows mock data path to be returned

    default:

    False

  • force_mock (bool) –

    only mock data path will be returned. Useful for debugging with small data

    default:

    False

Returns:

a valid dir to the dataset if data_dir was none, otherwise data_dir

Return type:

Mapping[str, Path]

configilm.extra.BENv2_utils.stack_and_interpolate(bands, order=None, img_size=120, upsample_mode='nearest')#

Supports 2D input (as values in the dict) with “nearest”, “bilinear” and “bicubic” interpolation

Parameters:
  • bands (Dict[str, ndarray]) –

  • order (Optional[Iterable[str]]) –

  • img_size (int) –

  • upsample_mode (str) –

Return type:

array