deeptrees.dataloadingο
deeptrees.dataloading.datamoduleο
- class deeptrees.dataloading.datamodule.TreeCrownDelineationDataModule(rasters, masks, outlines, distance_transforms, training_split=0.7, batch_size=16, val_batch_size=2, num_workers=8, augment_train={}, augment_eval={}, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, divide_by=1, dilate_outlines=0, shuffle=True, train_indices=None, val_indices=None, test_indices=None, ground_truth_config={'labels': None}, dim_ordering='CHW')[source]ο
Bases:
LightningDataModuleThis class is responsible for managing the datasets, applying preprocessing steps, and providing DataLoaders for training, validation, testing, and prediction.
- rastersο
List of file paths, or path to folder containing the training raster files (TIF).
- Type:
Union[str, list]
- masksο
List of file paths, or path to folder containing the masks.
- Type:
Union[str, list]
- outlinesο
List of file paths, or path to folder containing the outlines.
- Type:
Union[str, list]
- distance_transformsο
List of file paths, or path to folder containing the distance transforms.
- Type:
Union[str, list]
- training_splitο
Training data split. Defaults to 0.7.
- Type:
float
- batch_sizeο
Training batch size. Defaults to 16.
- Type:
int
- val_batch_sizeο
Validation batch size. Defaults to 2.
- Type:
int
- num_workersο
Number of workers in DataLoader. Defaults to 8.
- Type:
int
- augment_trainο
Dictionary defining torchvision augmentations to be used during training. Defaults to {}.
- Type:
Dict[str, Any]
- augment_evalο
Dictionary defining torchvision augmentations to be used during validation/testing. Defaults to {}.
- Type:
Dict[str, Any]
- ndvi_configο
Dictionary defining the NDVI concatenation settings. Defaults to {βconcatenateβ: False}.
- Type:
Dict[str, Any]
- divide_byο
Scalar used to normalize rasters. Defaults to 1.
- Type:
float
- dilate_outlinesο
If present (>0), dilate outlines by the given number of pixels. Defaults to 0.
- Type:
int
- shuffleο
If True, shuffle data before applying split. Defaults to True.
- Type:
bool
- train_indicesο
List of indices of files to be used for training. Cannot be used with shuffle. Defaults to None.
- Type:
list[int]
- val_indicesο
List of indices of files to be used for validation. Cannot be used with shuffle. Defaults to None.
- Type:
list[int]
- test_indicesο
List of indices of files to be used for testing. Cannot be used with shuffle. Defaults to None.
- Type:
list[int
- ground_truth_configο
Dictionary defining the ground truth preprocessing settings. Defaults to {βlabelsβ: None}.
- Type:
Dict[str, Any]
- __init__(rasters, masks, outlines, distance_transforms, training_split=0.7, batch_size=16, val_batch_size=2, num_workers=8, augment_train={}, augment_eval={}, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, divide_by=1, dilate_outlines=0, shuffle=True, train_indices=None, val_indices=None, test_indices=None, ground_truth_config={'labels': None}, dim_ordering='CHW')[source]ο
TreeCrownDelineationDataModule
Datamodule to hold the different datasets, apply preprocessing, and return DataLoaders.
- Parameters:
rasters (Union[str, list]) β List of file paths, or path to folder containing the training raster files (TIF).
masks (Union[str, list]) β List of file paths, or path to folder containing the masks.
outlines (Union[str, list]) β List of file paths, or path to folder containing the outlines.
distance_transforms (Union[str, list]) β List of file paths, or path to folder containing the distance transforms.
training_split (float, optional) β Training data split. Defaults to 0.7.
batch_size (int, optional) β Training batch size. Defaults to 16.
val_batch_size (int, optional) β Validation batch size. Defaults to 2.
num_workers (int, optional) β Number of workers in DataLoader. Defaults to 8.
augment_train (Dict[str, Any], optional) β Dictionary defining torchvision augmentations to be used during training. Defaults to {}.
augment_eval (Dict[str, Any], optional) β Dictionary defining torchvision augmentations to be used during validation/testing. Defaults to {}.
ndvi_config (_type_, optional) β Dictionary defining the NDVI concatenation settings. Defaults to {βconcatenateβ: False}.
divide_by (float, optional) β Scalar used to normalize rasters. Defaults to 1.
dilate_outlines (int, optional) β If present (>0), dilate outlines be given number of pixels. Defaults to False (=0).
shuffle (bool, optional) β If True, shuffle data before applying split. Defaults to True.
train_indices (list[int], optional) β List of indices of files to be used for training. Cannot be used with shuffle. Defaults to None.
val_indices (list[int], optional) β List of indices of files to be used for validation. Cannot be used with shuffle. Defaults to None.
test_indices (list[int], optional) β List of indices of files to be used for testing. Cannot be used with shuffle. Defaults to None.
ground_truth_config (Dict[str, Any], optional) β Dictionary defining the ground truth preprocessing settings. Defaults to {βlabelsβ: None}.
- predict_dataloader()[source]ο
Return the dataloader for the predict dataset.
- Returns:
Pytorch dataloader for the predict dataset.
- Return type:
DataLoader
- prepare_data()[source]ο
Prepare the ground truth masks, outlines, and distance transforms from ground truth labels.
- Return type:
None
- setup(stage='fit')[source]ο
Setup the dataset.
- Parameters:
stage (str, optional) β Current stage (fit/test). Defaults to fit.
- Raises:
ValueError β If shuffled dataset is passed together with fixed indices.
- test_dataloader()[source]ο
Return the dataloader for the test dataset.
- Returns:
Pytorch dataloader for the test dataset.
- Return type:
DataLoader
deeptrees.dataloading.datasetsο
- class deeptrees.dataloading.datasets.TreeCrownDelineationBaseDataset(raster_files, target_files, augmentation, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, dilate_outlines=0, overwrite_nan_with_zeros=True, in_memory=False, dim_ordering='CHW', dtype='float32', divide_by=1)[source]ο
Bases:
ABCIn-memory remote sensing dataset for image segmentation.
This base dataset class handles the loading and preprocessing of raster and target files for tree crown delineation, and image segmentation task.
Parameters: - raster_files (list[str]): List of file paths to the raster images. - target_files (list[str]): List of file paths to the target images. - augmentation (Dict[str, Any]): Dictionary containing augmentation configurations. - ndvi_config (Dict[str, Any], optional): Configuration for NDVI, default is {βconcatenateβ: False}. - dilate_outlines (int, optional): Number of pixels to dilate the outlines, default is 0. - overwrite_nan_with_zeros (bool, optional): Whether to overwrite NaN values with zeros, default is True. - in_memory (bool, optional): Whether to load the data into memory, default is False. - dim_ordering (str, optional): Dimension ordering, default is βCHWβ. - dtype (str, optional): Data type of the raster images, default is βfloat32β. - divide_by (int, optional): Value to divide the raster data by, default is 1.
- __init__(raster_files, target_files, augmentation, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, dilate_outlines=0, overwrite_nan_with_zeros=True, in_memory=False, dim_ordering='CHW', dtype='float32', divide_by=1)[source]ο
Creates a dataset containing images and targets (masks, outlines, and distance_transforms).
- Parameters:
raster_files (list[str]) β List of file paths to source rasters. File names must be of the form ββ¦/the_name_i.tifβ where i is some index
target_files (list[str]) β mask_files: A tuple containing lists of file paths to different sorts of βmasksβ, e.g. mask, outline, distance transform. The mask and raster file names must have the same index ending.
augmentation (Dict[str, Any]) β Dictionary defining augmentations. Keys correspond to torchvision transforms, values to their kwargs.
ndvi_config (_type_, optional) β Dictionary defining NDVI concatenation. Defaults to {βconcatenateβ: False}.
dilate_outlines (int, optional) β If present, dilate outlines by give amount of pixels. Defaults to 0.
overwrite_nan_with_zeros (bool, optional) β If True, fill missing values in targets with 0. Defaults to True.
in_memory (bool, optional) β If True, load all rasters and targets into memory (works for small datasets, beware of OOM error). Defaults to True.
dim_ordering (str, optional) β Order of dimensions. Defaults to βCHWβ.
dtype (str, optional) β torch Datatype. Defaults to βfloat32β.
divide_by (int, optional) β Scalar to divide the raster pixel values by. Defaults to 1.
- _abc_impl = <_abc._abc_data object>ο
- static concatenate_gci_to_raster(raster, red=0, green=1, nir=3, dim_ordering='CHW', rescale=False)[source]ο
Concatenate GCI to the raster.
- Parameters:
raster (xr.Dataset) β loaded raster tile
red (int, optional) β Index of red channel in raster bands. Defaults to 0.
green (int, optional) β Index of green channel in raster bands. Defaults to 1.
nir (int, optional) β Index of NIR channel in raster bands. Defaults to 3.
rescale (bool, optional) β Rescale GCI to [0, 1]. Defaults to False.
- Returns:
_description_
- Return type:
xr.Dataset
- static concatenate_hue_to_raster(raster, red=0, green=1, blue=2, dim_ordering='CHW', rescale=False)[source]ο
Concatenate hue to the raster.
- Parameters:
raster (xr.Dataset) β loaded raster tile
red (int, optional) β Index of red channel in raster bands. Defaults to 0.
green (int, optional) β Index of green channel in raster bands. Defaults to 1.
blue (int, optional) β Index of blue channel in raster bands. Defaults to 2.
rescale (bool, optional) β Rescale hue to [0, 1]. Defaults to False.
- Returns:
_description_
- Return type:
xr.Dataset
- static concatenate_ndvi_to_raster(raster, red=0, nir=3, dim_ordering='CHW', rescale=False)[source]ο
Concatenate NDVI to the raster.
- Parameters:
raster (xr.Dataset) β loaded raster tile
red (int, optional) β Index of red channel in raster bands. Defaults to 0.
nir (int, optional) β Index of NIR channel in raster bands. Defaults to 3.
rescale (bool, optional) β Rescale NDVI to [0, 1]. Defaults to False.
- Returns:
_description_
- Return type:
xr.Dataset
- class deeptrees.dataloading.datasets.TreeCrownDelineationDataset(raster_files, target_files, augmentation, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, dilate_outlines=0, overwrite_nan_with_zeros=True, in_memory=True, dim_ordering='CHW', dtype='float32', divide_by=1)[source]ο
Bases:
TreeCrownDelineationBaseDataset,IterableDatasetIterable TreeCrownDelineation dataset.
Yields samples from all rasters until, statistically, each pixel has been covered once. If the raster edge length exceeds the sample edge length, this implies that multiple samples can be sampled from one raster.
- __init__(raster_files, target_files, augmentation, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, dilate_outlines=0, overwrite_nan_with_zeros=True, in_memory=True, dim_ordering='CHW', dtype='float32', divide_by=1)[source]ο
Creates a dataset containing images and targets (masks, outlines, and distance_transforms).
- Parameters:
raster_files (list[str]) β List of file paths to source rasters. File names must be of the form ββ¦/the_name_i.tifβ where i is some index
target_files (list[str]) β mask_files: A tuple containing lists of file paths to different sorts of βmasksβ, e.g. mask, outline, distance transform. The mask and raster file names must have the same index ending.
augmentation (Dict[str, Any]) β Dictionary defining augmentations. Keys correspond to torchvision transforms, values to their kwargs.
ndvi_config (_type_, optional) β Dictionary defining NDVI concatenation. Defaults to {βconcatenateβ: False}.
dilate_outlines (int, optional) β If present, dilate outlines by give amount of pixels. Defaults to 0.
overwrite_nan_with_zeros (bool, optional) β If True, fill missing values in targets with 0. Defaults to True.
in_memory (bool, optional) β If True, load all rasters and targets into memory (works for small datasets, beware of OOM error). Defaults to True.
dim_ordering (str, optional) β Order of dimensions. Defaults to βCHWβ.
dtype (str, optional) β torch Datatype. Defaults to βfloat32β.
divide_by (int, optional) β Scalar to divide the raster pixel values by. Defaults to 1.
- _abc_impl = <_abc._abc_data object>ο
- class deeptrees.dataloading.datasets.TreeCrownDelineationInferenceDataset(raster_files, augmentation, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, dilate_outlines=0, overwrite_nan_with_zeros=True, in_memory=False, dim_ordering='CHW', dtype='float32', divide_by=1)[source]ο
Bases:
TreeCrownDelineationBaseDataset,DatasetMap-style (βstandardβ) dataset for the TreeCrownDelineation data.
This dataset is used for inference and returns complete rasters along with selected metadata.
Parameters: - raster_files (list[str]): List of file paths to the raster images. - augmentation (Dict[str, Any]): Dictionary containing augmentation configurations. - ndvi_config (Dict[str, Any], optional): Configuration for NDVI, default is {βconcatenateβ: False}. - dilate_outlines (int, optional): Number of pixels to dilate the outlines, default is 0. - overwrite_nan_with_zeros (bool, optional): Whether to overwrite NaN values with zeros, default is True. - in_memory (bool, optional): Whether to load the data into memory, default is False. - dim_ordering (str, optional): Dimension ordering, default is βCHWβ. - dtype (str, optional): Data type of the raster images, default is βfloat32β. - divide_by (int, optional): Value to divide the raster data by, default is 1.
- __init__(raster_files, augmentation, ndvi_config={'concatenate': False}, gci_config={'concatenate': False}, hue_config={'concatenate': False}, dilate_outlines=0, overwrite_nan_with_zeros=True, in_memory=False, dim_ordering='CHW', dtype='float32', divide_by=1)[source]ο
Creates a dataset containing images and targets (masks, outlines, and distance_transforms).
- Parameters:
raster_files (list[str]) β List of file paths to source rasters. File names must be of the form ββ¦/the_name_i.tifβ where i is some index
target_files (list[str]) β mask_files: A tuple containing lists of file paths to different sorts of βmasksβ, e.g. mask, outline, distance transform. The mask and raster file names must have the same index ending.
augmentation (Dict[str, Any]) β Dictionary defining augmentations. Keys correspond to torchvision transforms, values to their kwargs.
ndvi_config (_type_, optional) β Dictionary defining NDVI concatenation. Defaults to {βconcatenateβ: False}.
dilate_outlines (int, optional) β If present, dilate outlines by give amount of pixels. Defaults to 0.
overwrite_nan_with_zeros (bool, optional) β If True, fill missing values in targets with 0. Defaults to True.
in_memory (bool, optional) β If True, load all rasters and targets into memory (works for small datasets, beware of OOM error). Defaults to True.
dim_ordering (str, optional) β Order of dimensions. Defaults to βCHWβ.
dtype (str, optional) β torch Datatype. Defaults to βfloat32β.
divide_by (int, optional) β Scalar to divide the raster pixel values by. Defaults to 1.
- _abc_impl = <_abc._abc_data object>ο
deeptrees.dataloading.preprocessingο
Classes to be used in preprocessing labels -> ground truth rasters
Based on scripts/rasterize.py and scripts/rasterize_to_distance_transform.py
- class deeptrees.dataloading.preprocessing.DistanceTransformGenerator(rasters, output_path, output_file_prefix, ground_truth_labels, valid_class_ids='all', class_column_name='class', crs='EPSG:25832', nproc=1, area_max=None, area_min=3)[source]ο
Bases:
GroundTruthGeneratorGenerate distance transforms from tiles and ground truth labels.
- __init__(rasters, output_path, output_file_prefix, ground_truth_labels, valid_class_ids='all', class_column_name='class', crs='EPSG:25832', nproc=1, area_max=None, area_min=3)[source]ο
Initialize the MaskOutlinesGenerator instance.
- Parameters:
rasters (Union[str, list]) β (List of) file path(s) to the raster files
output_path (str) β Output directory
output_file_prefix (str) β Output file prefix. Suffix is infered from raster files.
ground_truth_labels (Union[str, gpd.GeoDataFrame]) β Path to ground truth labels or frame with labels
nproc (int, optional) β Number of parallel processes to use. Defaults to 1.
valid_class_ids (Union[str, list]) β Valid class IDs in ground_truth_labels. Defaults to βallβ (use all classes).
class_column_name (str) β Column name of class ID in ground_truth_labels.
crs (str) β Coordinate reference system. Defaults to EPSG:25832.
area_max (int) β Maximum area of polygons to consider. Defaults to None.
area_min (int) β Minimum area of polygons to consider. Defaults to 3.
- _abc_impl = <_abc._abc_data object>ο
- class deeptrees.dataloading.preprocessing.GroundTruthGenerator(rasters, output_path, output_file_prefix, ground_truth_labels, valid_class_ids='all', class_column_name='class', crs='EPSG:25832', nproc=1)[source]ο
Bases:
ABCBase class to generate ground truth (masks, outlines, distance transforms)
Loads a raster and a vector file, then rasterizes the vector file within the extent of the raster with the same resolution. Uses gdal_rasterize under the hood, but provides some more features like specifying which classes to rasterize into which layer of the output. If you want to infer the output file names, the input file name suffixes have to be delimited by an β_β.β,
Based on scripts/rasterize.py in Freudenberg 2022.
- __init__(rasters, output_path, output_file_prefix, ground_truth_labels, valid_class_ids='all', class_column_name='class', crs='EPSG:25832', nproc=1)[source]ο
- Parameters:
rasters (Union[str, list]) β (List of) file path(s) to the raster files
output_path (str) β Output directory
output_file_prefix (str) β Output file prefix. Suffix is infered from raster files.
ground_truth_labels (Union[str, gpd.GeoDataFrame]) β Path to ground truth labels or frame with labels
nproc (int, optional) β Number of parallel processes to use. Defaults to 1.
valid_class_ids (Union[str, list]) β Valid class IDs in ground_truth_labels. Defaults to βallβ (use all classes).
class_column_name (str) β Column name of class ID in ground_truth_labels.
crs (str) β Coordinate reference system. Defaults to EPSG:25832.
- _abc_impl = <_abc._abc_data object>ο
- constrain_geometry_to_tile(input_file)[source]ο
constrain_geometry_to_raster
Filter the ground truth labels that fall within the bounding box of the given raster image.
- Parameters:
input_file (str) β path to input raster file
- Returns:
list of polygons within tile bounding box
- Return type:
list[Polygon]
- class deeptrees.dataloading.preprocessing.MaskOutlinesGenerator(rasters, output_path, output_file_prefix, ground_truth_labels, valid_class_ids='all', class_column_name='class', crs='EPSG:25832', nproc=1, generate_outlines=False)[source]ο
Bases:
GroundTruthGeneratorGenerate masks and outlines from tiles and ground truth labels.
- __init__(rasters, output_path, output_file_prefix, ground_truth_labels, valid_class_ids='all', class_column_name='class', crs='EPSG:25832', nproc=1, generate_outlines=False)[source]ο
Initialize the MaskOutlinesGenerator instance.
- Parameters:
rasters (Union[str, list]) β (List of) file path(s) to the raster files
output_path (str) β Output directory
output_file_prefix (str) β Output file prefix. Suffix is infered from raster files.
ground_truth_labels (Union[str, gpd.GeoDataFrame]) β Path to ground truth labels or frame with labels
nproc (int, optional) β Number of parallel processes to use. Defaults to 1.
valid_class_ids (Union[str, list]) β Valid class IDs in ground_truth_labels. Defaults to βallβ (use all classes).
class_column_name (str) β Column name of class ID in ground_truth_labels.
crs (str) β Coordinate reference system. Defaults to EPSG:25832.
generate_outlines (bool) β If True, generate outlines. If False, generate masks. Defaults to False.
- _abc_impl = <_abc._abc_data object>ο