forked from microsoft/torchgeo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add South America Soybean DataModule (microsoft#1959)
* Add South America Soybean DataModule * Add train_aug * Regenerate data --------- Co-authored-by: Adam J. Stewart <[email protected]>
- Loading branch information
1 parent
2849944
commit 5d253c5
Showing
9 changed files
with
147 additions
and
10 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,17 @@ | ||
model: | ||
class_path: SemanticSegmentationTask | ||
init_args: | ||
loss: "ce" | ||
model: "deeplabv3+" | ||
backbone: "resnet18" | ||
in_channels: 13 | ||
num_classes: 2 | ||
num_filters: 1 | ||
data: | ||
class_path: Sentinel2SouthAmericaSoybeanDataModule | ||
init_args: | ||
batch_size: 2 | ||
patch_size: 16 | ||
dict_kwargs: | ||
south_america_soybean_paths: "tests/data/south_america_soybean" | ||
sentinel2_paths: "tests/data/sentinel2" |
Binary file not shown.
Binary file modified
BIN
+2.86 KB
(550%)
tests/data/south_america_soybean/SouthAmericaSoybean/South_America_Soybean_2002.tif
Binary file not shown.
Binary file modified
BIN
+2.85 KB
(550%)
tests/data/south_america_soybean/SouthAmericaSoybean/South_America_Soybean_2021.tif
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
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
123 changes: 123 additions & 0 deletions
123
torchgeo/datamodules/sentinel2_south_america_soybean.py
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,123 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
|
||
"""South America Soybean 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 Sentinel2, SouthAmericaSoybean, random_grid_cell_assignment | ||
from ..samplers import GridGeoSampler, RandomBatchGeoSampler | ||
from ..samplers.utils import _to_tuple | ||
from ..transforms import AugmentationSequential | ||
from .geo import GeoDataModule | ||
|
||
|
||
class Sentinel2SouthAmericaSoybeanDataModule(GeoDataModule): | ||
"""LightningDataModule for SouthAmericaSoybean and Sentinel2 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 Sentinel2SouthAmericaSoybeanDataModule 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.SouthAmericaSoybean` | ||
(prefix keys with ``south_america_soybean_``) and | ||
:class:`~torchgeo.datasets.Sentinel2` | ||
(prefix keys with ``sentinel2_``). | ||
""" | ||
self.south_america_soybean_kwargs = {} | ||
self.sentinel2_kwargs = {} | ||
for key, val in kwargs.items(): | ||
if key.startswith("south_america_soybean_"): | ||
self.south_america_soybean_kwargs[key[22:]] = val | ||
elif key.startswith("sentinel2_"): | ||
self.sentinel2_kwargs[key[10:]] = val | ||
|
||
super().__init__( | ||
SouthAmericaSoybean, | ||
batch_size=batch_size, | ||
patch_size=patch_size, | ||
length=length, | ||
num_workers=num_workers, | ||
**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.south_america_soybean = SouthAmericaSoybean( | ||
**self.south_america_soybean_kwargs | ||
) | ||
self.dataset = self.sentinel2 & self.south_america_soybean | ||
|
||
generator = torch.Generator().manual_seed(1) | ||
(self.train_dataset, self.val_dataset, self.test_dataset) = ( | ||
random_grid_cell_assignment( | ||
self.dataset, [0.8, 0.1, 0.1], 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 SouthAmericaSoybean 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.south_america_soybean.plot(*args, **kwargs) |