-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add eurocrops data module. It is based on NAIPChesapeakeDataModule which splits bounding box of dataset into 1/2 train, 1/4 val, and 1/4 test. This may not be the best way to train an actual model. * misc fixes * various fixes per discussion * update eurocrops test data * fix * fix version added placement * fix failing test by forcing integrity test when checksum is requested * Clarify SIZE setting in eurocrops test data * fix currently remaining issues with eurocrops data module * fix style * more style fix * Add documentation --------- Co-authored-by: Adam J. Stewart <[email protected]>
- Loading branch information
1 parent
abceea0
commit 83353b0
Showing
15 changed files
with
177 additions
and
8 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: "unet" | ||
backbone: "resnet18" | ||
in_channels: 13 | ||
num_classes: 3 | ||
num_filters: 1 | ||
data: | ||
class_path: Sentinel2EuroCropsDataModule | ||
init_args: | ||
batch_size: 2 | ||
patch_size: 16 | ||
dict_kwargs: | ||
sentinel2_paths: "tests/data/sentinel2" | ||
eurocrops_paths: "tests/data/eurocrops" |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
ISO-8859-1 |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
PROJCS["WGS_1984_UTM_Zone_16N",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-87.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]] |
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
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. | ||
|
||
"""EuroCrops datamodule.""" | ||
|
||
from typing import Any | ||
|
||
import kornia.augmentation as K | ||
import torch | ||
from kornia.constants import DataKey, Resample | ||
from matplotlib.figure import Figure | ||
|
||
from ..datasets import EuroCrops, 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 Sentinel2EuroCropsDataModule(GeoDataModule): | ||
"""LightningDataModule implementation for the EuroCrops and Sentinel2 datasets. | ||
Uses the train/val/test splits from the dataset. | ||
.. versionadded:: 0.6 | ||
""" | ||
|
||
def __init__( | ||
self, | ||
batch_size: int = 64, | ||
patch_size: int | tuple[int, int] = 256, | ||
length: int | None = None, | ||
num_workers: int = 0, | ||
**kwargs: Any, | ||
) -> None: | ||
"""Initialize a new Sentinel2EuroCropsDataModule 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.EuroCrops` (prefix keys with ``eurocrops_``) | ||
and :class:`~torchgeo.datasets.Sentinel2` | ||
(prefix keys with ``sentinel2_``). | ||
""" | ||
eurocrops_signature = "eurocrops_" | ||
sentinel2_signature = "sentinel2_" | ||
self.eurocrops_kwargs = {} | ||
self.sentinel2_kwargs = {} | ||
for key, val in kwargs.items(): | ||
if key.startswith(eurocrops_signature): | ||
self.eurocrops_kwargs[key[len(eurocrops_signature) :]] = val | ||
elif key.startswith(sentinel2_signature): | ||
self.sentinel2_kwargs[key[len(sentinel2_signature) :]] = val | ||
|
||
super().__init__( | ||
EuroCrops, | ||
batch_size, | ||
patch_size, | ||
length, | ||
num_workers, | ||
**self.eurocrops_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.eurocrops = EuroCrops(**self.eurocrops_kwargs) | ||
self.dataset = self.sentinel2 & self.eurocrops | ||
|
||
generator = torch.Generator().manual_seed(0) | ||
(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 EuroCrops 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.eurocrops.plot(*args, **kwargs) |
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