Data
We rely on TorchGeo for the implementation of datasets and data modules.
Check out the TorchGeo tutorials on datasets for more in depth information.
In general, it is reccomended you create a TorchGeo dataset specifically for your dataset. This gives you complete control and flexibility on how data is loaded, what transforms are done over it, and even how it is plotted if you log with tools like TensorBoard.
TorchGeo provides GeoDataset
and NonGeoDataset
.
- If your data is already nicely tiled and ready for consumption by a neural network, you can inherit from
NonGeoDataset
. This is essentially a wrapper of a regular torch dataset. - If your data consists of large GeoTiffs you would like to sample from during training, you can leverage the powerful
GeoDataset
from torch. This will automatically align your input data and labels and enable a variety of geo-aware samplers.
Using Datasets already implemented in TorchGeo
Using existing TorchGeo DataModules is very easy! Just plug them in!
For instance, to use the EuroSATDataModule
, in your config file, set the data as:
data:
class_path: torchgeo.datamodules.EuroSATDataModule
init_args:
batch_size: 32
num_workers: 8
dict_kwargs:
root: /dccstor/geofm-pre/EuroSat
download: True
bands:
- B02
- B03
- B04
- B08A
- B09
- B10
You can also do this outside of config files! Simply instantiate the data module as normal and plug it in.
Warning
To define transforms
to be passed to DataModules from TorchGeo from config files, you must use the following format:
data:
class_path: terratorch.datamodules.TorchNonGeoDataModule
init_args:
cls: torchgeo.datamodules.EuroSATDataModule
transforms:
- class_path: albumentations.augmentations.geometric.resize.Resize
init_args:
height: 224
width: 224
- class_path: ToTensorV2
TorchNonGeoDataModule
and the class to be used is passed through cls
(there is also a TorchGeoDataModule
for geo modules).
This has to be done as the transforms
argument is passed through **kwargs
in TorchGeo, making it difficult to instantiate with LightningCLI.
See more details below.
terratorch.datamodules.torchgeo_data_module
Ugly proxy objects so parsing config file works with transforms.
These are necessary since, for LightningCLI to instantiate arguments as objects from the config, they must have type annotations
In TorchGeo, transforms
is passed in **kwargs, so it has no type annotations!
To get around that, we create these wrappers that have transforms type annotated.
They create the transforms and forward all method and attribute calls to the
original TorchGeo datamodule.
Additionally, TorchGeo datasets pass the data to the transforms callable as a dict, and as a tensor.
Albumentations expects this data not as a dict but as different key-value arguments, and as numpy. We handle that conversion here.
TorchGeoDataModule
Bases: GeoDataModule
Proxy object for using Geo data modules defined by TorchGeo.
Allows for transforms to be defined and passed using config files. The only reason this class exists is so that we can annotate the transforms argument with a type. This is required for lightningcli and config files. As such, all getattr and setattr will be redirected to the underlying class.
Source code in terratorch/datamodules/torchgeo_data_module.py
__init__(cls, batch_size=None, num_workers=0, transforms=None, **kwargs)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
type[GeoDataModule]
|
TorchGeo DataModule class to be instantiated |
required |
batch_size
|
int | None
|
batch_size. Defaults to None. |
None
|
num_workers
|
int
|
num_workers. Defaults to 0. |
0
|
transforms
|
None | list[BasicTransform]
|
List of Albumentations Transforms. Should enc with ToTensorV2. Defaults to None. |
None
|
**kwargs
|
Any
|
Arguments passed to instantiate |
{}
|
Source code in terratorch/datamodules/torchgeo_data_module.py
TorchNonGeoDataModule
Bases: NonGeoDataModule
Proxy object for using NonGeo data modules defined by TorchGeo.
Allows for transforms to be defined and passed using config files. The only reason this class exists is so that we can annotate the transforms argument with a type. This is required for lightningcli and config files. As such, all getattr and setattr will be redirected to the underlying class.
Source code in terratorch/datamodules/torchgeo_data_module.py
__init__(cls, batch_size=None, num_workers=0, transforms=None, **kwargs)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
type[NonGeoDataModule]
|
TorchGeo DataModule class to be instantiated |
required |
batch_size
|
int | None
|
batch_size. Defaults to None. |
None
|
num_workers
|
int
|
num_workers. Defaults to 0. |
0
|
transforms
|
None | list[BasicTransform]
|
List of Albumentations Transforms. Should enc with ToTensorV2. Defaults to None. |
None
|
**kwargs
|
Any
|
Arguments passed to instantiate |
{}
|
Source code in terratorch/datamodules/torchgeo_data_module.py
Generic datasets and data modules
For the NonGeoDataset
case, we also provide "generic" datasets and datamodules. These can be used when you would like to load data from given directories, in a style similar to the MMLab libraries.
Generic Datasets
terratorch.datasets.generic_pixel_wise_dataset
Module containing generic dataset classes
GenericNonGeoPixelwiseRegressionDataset
Bases: GenericPixelWiseDataset
GenericNonGeoPixelwiseRegressionDataset
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
__init__(data_root, label_data_root=None, image_grep='*', label_grep='*', split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, rgb_indices=None, dataset_bands=None, output_bands=None, constant_scale=1, transform=None, no_data_replace=None, no_label_replace=None, expand_temporal_dimension=False, reduce_zero_label=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_root
|
Path
|
Path to data root directory |
required |
label_data_root
|
Path
|
Path to data root directory with labels. If not specified, will use the same as for images. |
None
|
image_grep
|
str
|
Regular expression appended to data_root to find input images. Defaults to "*". |
'*'
|
label_grep
|
str
|
Regular expression appended to data_root to find ground truth masks. Defaults to "*". |
'*'
|
split
|
Path
|
Path to file containing files to be used for this split. The file should be a new-line separated prefixes contained in the desired files. Files will be seached using glob with the form Path(data_root).glob(prefix + [image or label grep]) |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". Defaults to True. |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
rgb_indices
|
list[str]
|
Indices of RGB channels. Defaults to [0, 1, 2]. |
None
|
dataset_bands
|
list[HLSBands | int] | None
|
Bands present in the dataset. |
None
|
output_bands
|
list[HLSBands | int] | None
|
Bands that should be output by the dataset. |
None
|
constant_scale
|
float
|
Factor to multiply image values by. Defaults to 1. |
1
|
transform
|
Compose | None
|
Albumentations transform to be applied. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float | None
|
Replace nan values in input images with this value. If none, does no replacement. Defaults to None. |
None
|
no_label_replace
|
int | None
|
Replace nan values in label with this value. If none, does no replacement. Defaults to None. |
None
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
reduce_zero_label
|
bool
|
Subtract 1 from all labels. Useful when labels start from 1 instead of the expected 0. Defaults to False. |
False
|
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
plot(sample, suptitle=None)
Plot a sample from the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample
|
dict[str, Tensor]
|
a sample returned by :meth: |
required |
suptitle
|
str | None
|
optional string to use as a suptitle |
None
|
Returns:
Type | Description |
---|---|
Figure
|
a matplotlib Figure with the rendered sample |
.. versionadded:: 0.2
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
GenericNonGeoSegmentationDataset
Bases: GenericPixelWiseDataset
GenericNonGeoSegmentationDataset
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
__init__(data_root, num_classes, label_data_root=None, image_grep='*', label_grep='*', split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, rgb_indices=None, dataset_bands=None, output_bands=None, class_names=None, constant_scale=1, transform=None, no_data_replace=None, no_label_replace=None, expand_temporal_dimension=False, reduce_zero_label=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_root
|
Path
|
Path to data root directory |
required |
num_classes
|
int
|
Number of classes in the dataset |
required |
label_data_root
|
Path
|
Path to data root directory with labels. If not specified, will use the same as for images. |
None
|
image_grep
|
str
|
Regular expression appended to data_root to find input images. Defaults to "*". |
'*'
|
label_grep
|
str
|
Regular expression appended to data_root to find ground truth masks. Defaults to "*". |
'*'
|
split
|
Path
|
Path to file containing files to be used for this split. The file should be a new-line separated prefixes contained in the desired files. Files will be seached using glob with the form Path(data_root).glob(prefix + [image or label grep]) |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". Defaults to True |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
rgb_indices
|
list[str]
|
Indices of RGB channels. Defaults to [0, 1, 2]. |
None
|
dataset_bands
|
list[HLSBands | int] | None
|
Bands present in the dataset. |
None
|
output_bands
|
list[HLSBands | int] | None
|
Bands that should be output by the dataset. |
None
|
class_names
|
list[str]
|
Class names. Defaults to None. |
None
|
constant_scale
|
float
|
Factor to multiply image values by. Defaults to 1. |
1
|
transform
|
Compose | None
|
Albumentations transform to be applied. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float | None
|
Replace nan values in input images with this value. If none, does no replacement. Defaults to None. |
None
|
no_label_replace
|
int | None
|
Replace nan values in label with this value. If none, does no replacement. Defaults to None. |
None
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
reduce_zero_label
|
bool
|
Subtract 1 from all labels. Useful when labels start from 1 instead of the expected 0. Defaults to False. |
False
|
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
plot(sample, suptitle=None)
Plot a sample from the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample
|
dict[str, Tensor]
|
a sample returned by :meth: |
required |
suptitle
|
str | None
|
optional string to use as a suptitle |
None
|
Returns:
Type | Description |
---|---|
Figure
|
a matplotlib Figure with the rendered sample |
.. versionadded:: 0.2
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
GenericPixelWiseDataset
Bases: NonGeoDataset
, ABC
This is a generic dataset class to be used for instantiating datasets from arguments. Ideally, one would create a dataset class specific to a dataset.
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
__init__(data_root, label_data_root=None, image_grep='*', label_grep='*', split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, rgb_indices=None, dataset_bands=None, output_bands=None, constant_scale=1, transform=None, no_data_replace=None, no_label_replace=None, expand_temporal_dimension=False, reduce_zero_label=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_root
|
Path
|
Path to data root directory |
required |
label_data_root
|
Path
|
Path to data root directory with labels. If not specified, will use the same as for images. |
None
|
image_grep
|
str
|
Regular expression appended to data_root to find input images. Defaults to "*". |
'*'
|
label_grep
|
str
|
Regular expression appended to data_root to find ground truth masks. Defaults to "*". |
'*'
|
split
|
Path
|
Path to file containing files to be used for this split. The file should be a new-line separated prefixes contained in the desired files. Files will be seached using glob with the form Path(data_root).glob(prefix + [image or label grep]) |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". Defaults to True. |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
rgb_indices
|
list[str]
|
Indices of RGB channels. Defaults to [0, 1, 2]. |
None
|
dataset_bands
|
list[HLSBands | int | tuple[int, int] | str] | None
|
Bands present in the dataset. This parameter names input channels (bands) using HLSBands, ints, int ranges, or strings, so that they can then be refered to by output_bands. Defaults to None. |
None
|
output_bands
|
list[HLSBands | int | tuple[int, int] | str] | None
|
Bands that should be output by the dataset as named by dataset_bands. |
None
|
constant_scale
|
float
|
Factor to multiply image values by. Defaults to 1. |
1
|
transform
|
Compose | None
|
Albumentations transform to be applied. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float | None
|
Replace nan values in input images with this value. If none, does no replacement. Defaults to None. |
None
|
no_label_replace
|
int | None
|
Replace nan values in label with this value. If none, does no replacement. Defaults to -1. |
None
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
reduce_zero_label
|
bool
|
Subtract 1 from all labels. Useful when labels start from 1 instead of the expected 0. Defaults to False. |
False
|
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
terratorch.datasets.generic_scalar_label_dataset
Module containing generic dataset classes
GenericNonGeoClassificationDataset
Bases: GenericScalarLabelDataset
GenericNonGeoClassificationDataset
Source code in terratorch/datasets/generic_scalar_label_dataset.py
__init__(data_root, num_classes, split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, rgb_indices=None, dataset_bands=None, output_bands=None, class_names=None, constant_scale=1, transform=None, no_data_replace=0, expand_temporal_dimension=False)
A generic Non-Geo dataset for classification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_root
|
Path
|
Path to data root directory |
required |
num_classes
|
int
|
Number of classes in the dataset |
required |
split
|
Path
|
Path to file containing files to be used for this split. The file should be a new-line separated prefixes contained in the desired files. Files will be seached using glob with the form Path(data_root).glob(prefix + [image or label grep]) |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". Defaults to True. |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
rgb_indices
|
list[str]
|
Indices of RGB channels. Defaults to [0, 1, 2]. |
None
|
dataset_bands
|
list[HLSBands | int] | None
|
Bands present in the dataset. |
None
|
output_bands
|
list[HLSBands | int] | None
|
Bands that should be output by the dataset. |
None
|
class_names
|
list[str]
|
Class names. Defaults to None. |
None
|
constant_scale
|
float
|
Factor to multiply image values by. Defaults to 1. |
1
|
transform
|
Compose | None
|
Albumentations transform to be applied. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float
|
Replace nan values in input images with this value. Defaults to 0. |
0
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
Source code in terratorch/datasets/generic_scalar_label_dataset.py
GenericScalarLabelDataset
Bases: NonGeoDataset
, ImageFolder
, ABC
This is a generic dataset class to be used for instantiating datasets from arguments. Ideally, one would create a dataset class specific to a dataset.
Source code in terratorch/datasets/generic_scalar_label_dataset.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
__init__(data_root, split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, rgb_indices=None, dataset_bands=None, output_bands=None, constant_scale=1, transform=None, no_data_replace=0, expand_temporal_dimension=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_root
|
Path
|
Path to data root directory |
required |
split
|
Path
|
Path to file containing files to be used for this split. The file should be a new-line separated prefixes contained in the desired files. Files will be seached using glob with the form Path(data_root).glob(prefix + [image or label grep]) |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". Defaults to True. |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
rgb_indices
|
list[str]
|
Indices of RGB channels. Defaults to [0, 1, 2]. |
None
|
dataset_bands
|
list[HLSBands | int | tuple[int, int] | str] | None
|
Bands present in the dataset. This parameter gives identifiers to input channels (bands) so that they can then be refered to by output_bands. Can use the HLSBands enum, ints, int ranges, or strings. Defaults to None. |
None
|
output_bands
|
list[HLSBands | int | tuple[int, int] | str] | None
|
Bands that should be output by the dataset as named by dataset_bands. |
None
|
constant_scale
|
float
|
Factor to multiply image values by. Defaults to 1. |
1
|
transform
|
Compose | None
|
Albumentations transform to be applied. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float
|
Replace nan values in input images with this value. Defaults to 0. |
0
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
Source code in terratorch/datasets/generic_scalar_label_dataset.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
Generic Data Modules
terratorch.datamodules.generic_pixel_wise_data_module
This module contains generic data modules for instantiation at runtime.
GenericNonGeoPixelwiseRegressionDataModule
Bases: NonGeoDataModule
This is a generic datamodule class for instantiating data modules at runtime. Composes several GenericNonGeoPixelwiseRegressionDataset
Source code in terratorch/datamodules/generic_pixel_wise_data_module.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
__init__(batch_size, num_workers, train_data_root, val_data_root, test_data_root, means, stds, predict_data_root=None, img_grep='*', label_grep='*', train_label_data_root=None, val_label_data_root=None, test_label_data_root=None, train_split=None, val_split=None, test_split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, dataset_bands=None, output_bands=None, predict_dataset_bands=None, predict_output_bands=None, constant_scale=1, rgb_indices=None, train_transform=None, val_transform=None, test_transform=None, expand_temporal_dimension=False, reduce_zero_label=False, no_data_replace=None, no_label_replace=None, drop_last=True, **kwargs)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size
|
int
|
description |
required |
num_workers
|
int
|
description |
required |
train_data_root
|
Path
|
description |
required |
val_data_root
|
Path
|
description |
required |
test_data_root
|
Path
|
description |
required |
predict_data_root
|
Path
|
description |
None
|
img_grep
|
str
|
description |
'*'
|
label_grep
|
str
|
description |
'*'
|
means
|
list[float]
|
description |
required |
stds
|
list[float]
|
description |
required |
train_label_data_root
|
Path | None
|
description. Defaults to None. |
None
|
val_label_data_root
|
Path | None
|
description. Defaults to None. |
None
|
test_label_data_root
|
Path | None
|
description. Defaults to None. |
None
|
train_split
|
Path | None
|
description. Defaults to None. |
None
|
val_split
|
Path | None
|
description. Defaults to None. |
None
|
test_split
|
Path | None
|
description. Defaults to None. |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". Defaults to True. |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
dataset_bands
|
list[HLSBands | int] | None
|
Bands present in the dataset. Defaults to None. |
None
|
output_bands
|
list[HLSBands | int] | None
|
Bands that should be output by the dataset. Naming must match that of dataset_bands. Defaults to None. |
None
|
predict_dataset_bands
|
list[HLSBands | int] | None
|
Overwrites dataset_bands with this value at predict time. Defaults to None, which does not overwrite. |
None
|
predict_output_bands
|
list[HLSBands | int] | None
|
Overwrites output_bands with this value at predict time. Defaults to None, which does not overwrite. |
None
|
constant_scale
|
float
|
description. Defaults to 1. |
1
|
rgb_indices
|
list[int] | None
|
description. Defaults to None. |
None
|
train_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
val_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
test_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float | None
|
Replace nan values in input images with this value. If none, does no replacement. Defaults to None. |
None
|
no_label_replace
|
int | None
|
Replace nan values in label with this value. If none, does no replacement. Defaults to None. |
None
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
reduce_zero_label
|
bool
|
Subtract 1 from all labels. Useful when labels start from 1 instead of the expected 0. Defaults to False. |
False
|
drop_last
|
bool
|
Drop the last batch if it is not complete. Defaults to True. |
True
|
Source code in terratorch/datamodules/generic_pixel_wise_data_module.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
GenericNonGeoSegmentationDataModule
Bases: NonGeoDataModule
This is a generic datamodule class for instantiating data modules at runtime. Composes several GenericNonGeoSegmentationDatasets
Source code in terratorch/datamodules/generic_pixel_wise_data_module.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
|
__init__(batch_size, num_workers, train_data_root, val_data_root, test_data_root, img_grep, label_grep, means, stds, num_classes, predict_data_root=None, train_label_data_root=None, val_label_data_root=None, test_label_data_root=None, train_split=None, val_split=None, test_split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, dataset_bands=None, output_bands=None, predict_dataset_bands=None, predict_output_bands=None, constant_scale=1, rgb_indices=None, train_transform=None, val_transform=None, test_transform=None, expand_temporal_dimension=False, reduce_zero_label=False, no_data_replace=None, no_label_replace=None, drop_last=True, **kwargs)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size
|
int
|
description |
required |
num_workers
|
int
|
description |
required |
train_data_root
|
Path
|
description |
required |
val_data_root
|
Path
|
description |
required |
test_data_root
|
Path
|
description |
required |
predict_data_root
|
Path
|
description |
None
|
img_grep
|
str
|
description |
required |
label_grep
|
str
|
description |
required |
means
|
list[float]
|
description |
required |
stds
|
list[float]
|
description |
required |
num_classes
|
int
|
description |
required |
train_label_data_root
|
Path | None
|
description. Defaults to None. |
None
|
val_label_data_root
|
Path | None
|
description. Defaults to None. |
None
|
test_label_data_root
|
Path | None
|
description. Defaults to None. |
None
|
train_split
|
Path | None
|
description. Defaults to None. |
None
|
val_split
|
Path | None
|
description. Defaults to None. |
None
|
test_split
|
Path | None
|
description. Defaults to None. |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". Defaults to True. |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
dataset_bands
|
list[HLSBands | int] | None
|
Bands present in the dataset. Defaults to None. |
None
|
output_bands
|
list[HLSBands | int] | None
|
Bands that should be output by the dataset. Naming must match that of dataset_bands. Defaults to None. |
None
|
predict_dataset_bands
|
list[HLSBands | int] | None
|
Overwrites dataset_bands with this value at predict time. Defaults to None, which does not overwrite. |
None
|
predict_output_bands
|
list[HLSBands | int] | None
|
Overwrites output_bands with this value at predict time. Defaults to None, which does not overwrite. |
None
|
constant_scale
|
float
|
description. Defaults to 1. |
1
|
rgb_indices
|
list[int] | None
|
description. Defaults to None. |
None
|
train_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
val_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
test_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float | None
|
Replace nan values in input images with this value. If none, does no replacement. Defaults to None. |
None
|
no_label_replace
|
int | None
|
Replace nan values in label with this value. If none, does no replacement. Defaults to None. |
None
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
reduce_zero_label
|
bool
|
Subtract 1 from all labels. Useful when labels start from 1 instead of the expected 0. Defaults to False. |
False
|
drop_last
|
bool
|
Drop the last batch if it is not complete. Defaults to True. |
True
|
Source code in terratorch/datamodules/generic_pixel_wise_data_module.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
terratorch.datamodules.generic_scalar_label_data_module
This module contains generic data modules for instantiation at runtime.
GenericNonGeoClassificationDataModule
Bases: NonGeoDataModule
This is a generic datamodule class for instantiating data modules at runtime. Composes several GenericNonGeoClassificationDatasets
Source code in terratorch/datamodules/generic_scalar_label_data_module.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
__init__(batch_size, num_workers, train_data_root, val_data_root, test_data_root, means, stds, num_classes, predict_data_root=None, train_split=None, val_split=None, test_split=None, ignore_split_file_extensions=True, allow_substring_split_file=True, dataset_bands=None, predict_dataset_bands=None, output_bands=None, constant_scale=1, rgb_indices=None, train_transform=None, val_transform=None, test_transform=None, expand_temporal_dimension=False, no_data_replace=0, drop_last=True, **kwargs)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size
|
int
|
description |
required |
num_workers
|
int
|
description |
required |
train_data_root
|
Path
|
description |
required |
val_data_root
|
Path
|
description |
required |
test_data_root
|
Path
|
description |
required |
means
|
list[float]
|
description |
required |
stds
|
list[float]
|
description |
required |
num_classes
|
int
|
description |
required |
predict_data_root
|
Path
|
description |
None
|
train_split
|
Path | None
|
description. Defaults to None. |
None
|
val_split
|
Path | None
|
description. Defaults to None. |
None
|
test_split
|
Path | None
|
description. Defaults to None. |
None
|
ignore_split_file_extensions
|
bool
|
Whether to disregard extensions when using the split file to determine which files to include in the dataset. E.g. necessary for Eurosat, since the split files specify ".jpg" but files are actually ".jpg". |
True
|
allow_substring_split_file
|
bool
|
Whether the split files contain substrings that must be present in file names to be included (as in mmsegmentation), or exact matches (e.g. eurosat). Defaults to True. |
True
|
dataset_bands
|
list[HLSBands | int] | None
|
description. Defaults to None. |
None
|
predict_dataset_bands
|
list[HLSBands | int] | None
|
description. Defaults to None. |
None
|
output_bands
|
list[HLSBands | int] | None
|
description. Defaults to None. |
None
|
constant_scale
|
float
|
description. Defaults to 1. |
1
|
rgb_indices
|
list[int] | None
|
description. Defaults to None. |
None
|
train_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
val_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
test_transform
|
Compose | None
|
Albumentations transform to be applied to the train dataset. Should end with ToTensorV2(). If used through the generic_data_module, should not include normalization. Not supported for multi-temporal data. Defaults to None, which simply applies ToTensorV2(). |
None
|
no_data_replace
|
float
|
Replace nan values in input images with this value. Defaults to 0. |
0
|
expand_temporal_dimension
|
bool
|
Go from shape (time*channels, h, w) to (channels, time, h, w). Defaults to False. |
False
|
drop_last
|
bool
|
Drop the last batch if it is not complete. Defaults to True. |
True
|
Source code in terratorch/datamodules/generic_scalar_label_data_module.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
Custom datasets and data modules
Below is a documented example of how a custom dataset and data module class can be implemented.
terratorch.datasets.fire_scars
FireScarsHLS
Bases: RasterDataset
RasterDataset implementation for fire scars input images.
Source code in terratorch/datasets/fire_scars.py
FireScarsNonGeo
Bases: NonGeoDataset
NonGeo dataset implementation for fire scars.
Source code in terratorch/datasets/fire_scars.py
plot(sample, suptitle=None)
Plot a sample from the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample
|
dict[str, Tensor]
|
a sample returned by :meth: |
required |
suptitle
|
str | None
|
optional string to use as a suptitle |
None
|
Returns:
Type | Description |
---|---|
Figure
|
a matplotlib Figure with the rendered sample |
Source code in terratorch/datasets/fire_scars.py
FireScarsSegmentationMask
Bases: RasterDataset
RasterDataset implementation for fire scars segmentation mask. Can be easily merged with input images using the & operator.
Source code in terratorch/datasets/fire_scars.py
terratorch.datamodules.fire_scars
FireScarsDataModule
Bases: GeoDataModule
Geo Fire Scars data module implementation that merges input data with ground truth segmentation masks.
Source code in terratorch/datamodules/fire_scars.py
FireScarsNonGeoDataModule
Bases: NonGeoDataModule
NonGeo Fire Scars data module implementation