-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Sentinel2_CDL Datamodule (#1889)
* cdlsentinel2 * update kwargs * style * arg type * add cov * kwargs * update cdl data.py for intersection * style * create 2022 cdl for intersection * test roi method * style * test_cdl year update * intersection * random_grid_cell_assignment * add comments and line * add description * add doc * Update SIZE variable in sentinel2/data.py and test stage in datamodules/cdlsentinel2.py * merge val_aug and test_aug to aug * rename cdlsentinel2 to sentinel2cdl * fix isort * No need to monkeypatch CDL * Smaller backbone == faster tests * Sort docs alphabetically * Smaller Sentinel-2 test files * Smaller CDL files, don't delete directory * Sort imports alphabetically * Fix doc names * extra_args not needed * center crop doesn't do anything * blacken * Revert "extra_args not needed" This reverts commit 859f24e. * Add underscore to filename * Add plot method * import Figure * split 80-10-10 * style --------- Co-authored-by: Adam J. Stewart <[email protected]>
- Loading branch information
1 parent
fe9ee15
commit 5a7b9e5
Showing
70 changed files
with
160 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
model: | ||
class_path: SemanticSegmentationTask | ||
init_args: | ||
loss: "ce" | ||
model: "unet" | ||
backbone: "resnet18" | ||
in_channels: 13 | ||
num_classes: 134 | ||
num_filters: 1 | ||
ignore_index: 0 | ||
data: | ||
class_path: Sentinel2CDLDataModule | ||
init_args: | ||
batch_size: 2 | ||
patch_size: 16 | ||
dict_kwargs: | ||
cdl_paths: "tests/data/cdl" | ||
sentinel2_paths: "tests/data/sentinel2" |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+7.25 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B01.jp2
Binary file not shown.
Binary file modified
BIN
+7.33 KB
(350%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B02.jp2
Binary file not shown.
Binary file modified
BIN
+7.23 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B03.jp2
Binary file not shown.
Binary file modified
BIN
+7.26 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B04.jp2
Binary file not shown.
Binary file modified
BIN
+7.26 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B05.jp2
Binary file not shown.
Binary file modified
BIN
+7.3 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B06.jp2
Binary file not shown.
Binary file modified
BIN
+7.31 KB
(350%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B07.jp2
Binary file not shown.
Binary file modified
BIN
+7.25 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B08.jp2
Binary file not shown.
Binary file modified
BIN
+7.27 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B09.jp2
Binary file not shown.
Binary file modified
BIN
+7.32 KB
(350%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B10.jp2
Binary file not shown.
Binary file modified
BIN
+7.25 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B11.jp2
Binary file not shown.
Binary file modified
BIN
+7.26 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B12.jp2
Binary file not shown.
Binary file modified
BIN
+7.23 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_B8A.jp2
Binary file not shown.
Binary file modified
BIN
+7.24 KB
(340%)
...0.SAFE/GRANULE/L1C_T16TFM_A035544_20220412T163959/IMG_DATA/T16TFM_20220412T162841_TCI.jp2
Binary file not shown.
Binary file modified
BIN
+7.25 KB
(340%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R10m/T26EMU_20220414T110751_AOT_10m.jp2
Binary file not shown.
Binary file modified
BIN
+7.26 KB
(340%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R10m/T26EMU_20220414T110751_B02_10m.jp2
Binary file not shown.
Binary file modified
BIN
+7.24 KB
(340%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R10m/T26EMU_20220414T110751_B03_10m.jp2
Binary file not shown.
Binary file modified
BIN
+7.25 KB
(340%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R10m/T26EMU_20220414T110751_B04_10m.jp2
Binary file not shown.
Binary file modified
BIN
+7.25 KB
(340%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R10m/T26EMU_20220414T110751_B08_10m.jp2
Binary file not shown.
Binary file modified
BIN
+7.31 KB
(350%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R10m/T26EMU_20220414T110751_TCI_10m.jp2
Binary file not shown.
Binary file modified
BIN
+7.32 KB
(350%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R10m/T26EMU_20220414T110751_WVP_10m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_AOT_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B01_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.59 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B02_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B03_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B04_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B05_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B06_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B07_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B11_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B12_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.59 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_B8A_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_SCL_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.58 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_TCI_20m.jp2
Binary file not shown.
Binary file modified
BIN
+1.57 KB
(160%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R20m/T26EMU_20220414T110751_WVP_20m.jp2
Binary file not shown.
Binary file modified
BIN
+70 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_AOT_60m.jp2
Binary file not shown.
Binary file modified
BIN
+67 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B01_60m.jp2
Binary file not shown.
Binary file modified
BIN
+66 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B02_60m.jp2
Binary file not shown.
Binary file modified
BIN
+64 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B03_60m.jp2
Binary file not shown.
Binary file modified
BIN
+67 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B04_60m.jp2
Binary file not shown.
Binary file modified
BIN
+66 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B05_60m.jp2
Binary file not shown.
Binary file modified
BIN
+65 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B06_60m.jp2
Binary file not shown.
Binary file modified
BIN
+67 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B07_60m.jp2
Binary file not shown.
Binary file modified
BIN
+65 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B09_60m.jp2
Binary file not shown.
Binary file modified
BIN
+68 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B11_60m.jp2
Binary file not shown.
Binary file modified
BIN
+66 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B12_60m.jp2
Binary file not shown.
Binary file modified
BIN
+64 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_B8A_60m.jp2
Binary file not shown.
Binary file modified
BIN
+66 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_SCL_60m.jp2
Binary file not shown.
Binary file modified
BIN
+64 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_TCI_60m.jp2
Binary file not shown.
Binary file modified
BIN
+69 Bytes
(100%)
...ANULE/L2A_T26EMU_A035569_20220414T110747/IMG_DATA/R60m/T26EMU_20220414T110751_WVP_60m.jp2
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
from rasterio import Affine | ||
from rasterio.crs import CRS | ||
|
||
SIZE = 36 | ||
SIZE = 128 | ||
|
||
np.random.seed(0) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
"""Sentinel-2 and CDL datamodule.""" | ||
|
||
from typing import Any, Optional, Union | ||
|
||
import kornia.augmentation as K | ||
import torch | ||
from kornia.constants import DataKey, Resample | ||
from matplotlib.figure import Figure | ||
|
||
from ..datasets import CDL, Sentinel2, random_grid_cell_assignment | ||
from ..samplers import GridGeoSampler, RandomBatchGeoSampler | ||
from ..samplers.utils import _to_tuple | ||
from ..transforms import AugmentationSequential | ||
from .geo import GeoDataModule | ||
|
||
|
||
class Sentinel2CDLDataModule(GeoDataModule): | ||
"""LightningDataModule implementation for the Sentinel-2 and CDL datasets. | ||
.. versionadded:: 0.6 | ||
""" | ||
|
||
def __init__( | ||
self, | ||
batch_size: int = 64, | ||
patch_size: Union[int, tuple[int, int]] = 64, | ||
length: Optional[int] = None, | ||
num_workers: int = 0, | ||
**kwargs: Any, | ||
) -> None: | ||
"""Initialize a new Sentinel2CDLDataModule instance. | ||
Args: | ||
batch_size: Size of each mini-batch. | ||
patch_size: Size of each patch, either ``size`` or ``(height, width)``. | ||
length: Length of each training epoch. | ||
num_workers: Number of workers for parallel data loading. | ||
**kwargs: Additional keyword arguments passed to | ||
:class:`~torchgeo.datasets.CDL` (prefix keys with ``cdl_``) and | ||
:class:`~torchgeo.datasets.Sentinel2` | ||
(prefix keys with ``sentinel2_``). | ||
""" | ||
# Define prefix for Cropland Data Layer (CDL) and Sentinel-2 arguments | ||
cdl_signature = "cdl_" | ||
sentinel2_signature = "sentinel2_" | ||
self.cdl_kwargs = {} | ||
self.sentinel2_kwargs = {} | ||
|
||
for key, val in kwargs.items(): | ||
# Check if the current key starts with the CDL prefix | ||
if key.startswith(cdl_signature): | ||
# If so, extract the key-value pair to the CDL dictionary | ||
self.cdl_kwargs[key[len(cdl_signature) :]] = val | ||
# Check if the current key starts with the Sentinel-2 prefix | ||
elif key.startswith(sentinel2_signature): | ||
# If so, extract the key-value pair to the Sentinel-2 dictionary | ||
self.sentinel2_kwargs[key[len(sentinel2_signature) :]] = val | ||
|
||
super().__init__( | ||
CDL, batch_size, patch_size, length, num_workers, **self.cdl_kwargs | ||
) | ||
|
||
self.train_aug = AugmentationSequential( | ||
K.Normalize(mean=self.mean, std=self.std), | ||
K.RandomResizedCrop(_to_tuple(self.patch_size), scale=(0.6, 1.0)), | ||
K.RandomVerticalFlip(p=0.5), | ||
K.RandomHorizontalFlip(p=0.5), | ||
data_keys=["image", "mask"], | ||
extra_args={ | ||
DataKey.MASK: {"resample": Resample.NEAREST, "align_corners": None} | ||
}, | ||
) | ||
|
||
self.aug = AugmentationSequential( | ||
K.Normalize(mean=self.mean, std=self.std), data_keys=["image", "mask"] | ||
) | ||
|
||
def setup(self, stage: str) -> None: | ||
"""Set up datasets and samplers. | ||
Args: | ||
stage: Either 'fit', 'validate', 'test', or 'predict'. | ||
""" | ||
self.sentinel2 = Sentinel2(**self.sentinel2_kwargs) | ||
self.cdl = CDL(**self.cdl_kwargs) | ||
self.dataset = self.sentinel2 & self.cdl | ||
|
||
generator = torch.Generator().manual_seed(0) | ||
|
||
(self.train_dataset, self.val_dataset, self.test_dataset) = ( | ||
random_grid_cell_assignment( | ||
self.dataset, [0.8, 0.10, 0.10], grid_size=8, generator=generator | ||
) | ||
) | ||
if stage in ["fit"]: | ||
self.train_batch_sampler = RandomBatchGeoSampler( | ||
self.train_dataset, self.patch_size, self.batch_size, self.length | ||
) | ||
if stage in ["fit", "validate"]: | ||
self.val_sampler = GridGeoSampler( | ||
self.val_dataset, self.patch_size, self.patch_size | ||
) | ||
if stage in ["test"]: | ||
self.test_sampler = GridGeoSampler( | ||
self.test_dataset, self.patch_size, self.patch_size | ||
) | ||
|
||
def plot(self, *args: Any, **kwargs: Any) -> Figure: | ||
"""Run CDL plot method. | ||
Args: | ||
*args: Arguments passed to plot method. | ||
**kwargs: Keyword arguments passed to plot method. | ||
Returns: | ||
A matplotlib Figure with the image, ground truth, and predictions. | ||
""" | ||
return self.cdl.plot(*args, **kwargs) |