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: |
|
---|
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: |
|
---|
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
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 |
|
__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: |
|
---|
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
plot(sample, suptitle=None)
Plot a sample from the dataset.
Parameters: |
|
---|
Returns: |
|
---|
.. 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
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 347 |
|
__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: |
|
---|
Source code in terratorch/datasets/generic_pixel_wise_dataset.py
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 |
|
plot(sample, suptitle=None)
Plot a sample from the dataset.
Parameters: |
|
---|
Returns: |
|
---|
.. 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 179 |
|
__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: |
|
---|
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 146 |
|
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: |
|
---|
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: |
|
---|
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
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 566 567 568 569 570 571 572 573 574 575 576 577 |
|
__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, pin_memory=False, **kwargs)
Constructor
Parameters: |
|
---|
Source code in terratorch/datamodules/generic_pixel_wise_data_module.py
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 |
|
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 321 322 323 324 325 |
|
__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, pin_memory=False, **kwargs)
Constructor
Parameters: |
|
---|
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 215 216 217 218 |
|
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: |
|
---|
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
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 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 |
|
__init__(data_root, split='train', bands=BAND_SETS['all'], transform=None, no_data_replace=0, no_label_replace=-1, use_metadata=False)
Constructor
Parameters: |
|
---|
Source code in terratorch/datasets/fire_scars.py
plot(sample, suptitle=None)
Plot a sample from the dataset.
Parameters: |
|
---|
Returns: |
|
---|
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 datamodule implementation for Fire Scars