Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding SEN12MS training task #44

Merged
merged 9 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
329 changes: 329 additions & 0 deletions docs/notebooks/SEN12MS Dataset.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rarfile
rasterio>=1.0
rtree>=0.5.0
scikit-learn
segmentation-models-pytorch
setuptools>=42
sphinx
torch>=1.7
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ tests =
train =
pytorch-lightning
scikit-learn
segmentation-models-pytorch
1 change: 1 addition & 0 deletions spack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spack:
- "py-rasterio@0.3:"
- "py-rtree@0.5.0:"
- py-scikit-learn
- py-segmentation-models-pytorch
- "py-setuptools@42:"
- py-sphinx
- "py-torch@1.7:"
Expand Down
Binary file modified tests/data/sen12ms/ROIs1158_spring/lc_1/ROIs1158_spring_lc_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring/lc_1/ROIs1158_spring_lc_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring/s1_1/ROIs1158_spring_s1_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring/s1_1/ROIs1158_spring_s1_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring/s2_1/ROIs1158_spring_s2_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring/s2_1/ROIs1158_spring_s2_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring_lc.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring_s1.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1158_spring_s2.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer/lc_1/ROIs1868_summer_lc_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer/lc_1/ROIs1868_summer_lc_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer/s1_1/ROIs1868_summer_s1_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer/s1_1/ROIs1868_summer_s1_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer/s2_1/ROIs1868_summer_s2_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer/s2_1/ROIs1868_summer_s2_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer_lc.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer_s1.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1868_summer_s2.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall/lc_1/ROIs1970_fall_lc_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall/lc_1/ROIs1970_fall_lc_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall/s1_1/ROIs1970_fall_s1_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall/s1_1/ROIs1970_fall_s1_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall/s2_1/ROIs1970_fall_s2_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall/s2_1/ROIs1970_fall_s2_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall_lc.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall_s1.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs1970_fall_s2.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter/lc_1/ROIs2017_winter_lc_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter/lc_1/ROIs2017_winter_lc_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter/s1_1/ROIs2017_winter_s1_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter/s1_1/ROIs2017_winter_s1_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter/s2_1/ROIs2017_winter_s2_1_p1.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter/s2_1/ROIs2017_winter_s2_1_p2.tif
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter_lc.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter_s1.tar.gz
Binary file not shown.
Binary file modified tests/data/sen12ms/ROIs2017_winter_s2.tar.gz
Binary file not shown.
26 changes: 12 additions & 14 deletions tests/datasets/test_sen12ms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ def dataset(
self, monkeypatch: Generator[MonkeyPatch, None, None], request: SubRequest
) -> SEN12MS:
md5s = [
"3079d1c5038fa101ec2072657f2cb1ab",
"f11487a4b2e641b64ed80a031c4d121d",
"299691b948b37028398d4506d0195c6d",
"76e6847b10ee9323ce022508721e2c6c",
"dfbe57486455c31ae6f4d243186a8da5",
"8d0aae3b12d420cab2feff5035400cbf",
"f524074dcd90b9a770031cbfec50db71",
"5256cf09bd2a0ec44bdff78f28e6653d",
"b85b1641971444c87fedbc7134c437ac",
"af28777ee277e3f9577c10a3c6d952eb",
"44d18ee9efeb83f921b3b7aa6d511bbf",
"00e18016c6af1e55528c535d9b06c35a",
"02d5128ac1fc2bf8762091b4f319762d",
"02d5128ac1fc2bf8762091b4f319762d",
"7f14be13d3f62c09b4dd5b4d55c97fd6",
"48182d44b375360381f36d432956b225",
"96cf1b8405d4149c6fe61ad7100bd65d",
"ba8e7e10fba9eea6900ddc530c86025a",
"7ba7c51f2fb3a2074b7bbd3e24f9d70d",
"280c9be2d1e13e663824dccd85e1e42f",
"a5284baf48534d4bc77acb1b103ff16c",
"c6b176fed0cdd5033cb1835506e40ee4",
"adc672746b79be4c4edc8b1a564e3ff4",
"194fab4a4e067a0452824c4e39f61b77",
"7899c0c36c884ae8c991ab8518b0d177",
"ccfee543d4351bcc5aa68729e8cc795c",
]

monkeypatch.setattr(SEN12MS, "md5s", md5s) # type: ignore[attr-defined]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
from pathlib import Path

import pytest


def test_help() -> None:
args = [sys.executable, "train.py", "--help"]
Expand Down Expand Up @@ -83,3 +85,28 @@ def test_overwrite_experiment_dir(tmp_path: Path) -> None:
b"The experiment directory, .*, already exists, we might overwrite data in it!",
ps.stdout,
)


@pytest.mark.parametrize("task", ["cyclone", "sen12ms"])
def test_tasks(task: str, tmp_path: Path) -> None:
experiment_name = "test"
output_dir = tmp_path / "output"
data_dir = os.path.join("tests", "data")
log_dir = tmp_path / "logs"
args = [
sys.executable,
"train.py",
"--experiment_name",
experiment_name,
"--output_dir",
str(output_dir),
"--data_dir",
data_dir,
"--log_dir",
str(log_dir),
"--fast_dev_run",
"1",
"--task",
task,
]
subprocess.run(args, check=True)
4 changes: 2 additions & 2 deletions torchgeo/datasets/sen12ms.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def __init__(
raise RuntimeError("Dataset not found or corrupted.")

with open(os.path.join(self.root, self.base_folder, split + "_list.txt")) as f:
self.ids = f.readlines()
self.ids = [line.rstrip() for line in f.readlines()]

def __getitem__(self, index: int) -> Dict[str, Tensor]:
"""Return an index within the dataset.
Expand All @@ -147,7 +147,7 @@ def __getitem__(self, index: int) -> Dict[str, Tensor]:
Returns:
data and label at that index
"""
filename = self.ids[index].rstrip()
filename = self.ids[index]

lc = self._load_raster(filename, "lc")
s1 = self._load_raster(filename, "s1")
Expand Down
8 changes: 7 additions & 1 deletion torchgeo/trainers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""TorchGeo trainers."""

from .cyclone import CycloneDataModule, CycloneSimpleRegressionTask
from .sen12ms import SEN12MSDataModule, SEN12MSSegmentationTask

__all__ = ("CycloneDataModule", "CycloneSimpleRegressionTask")
__all__ = (
"CycloneDataModule",
"CycloneSimpleRegressionTask",
"SEN12MSDataModule",
"SEN12MSSegmentationTask",
)

# https://stackoverflow.com/questions/40018681
for module in __all__:
Expand Down
6 changes: 2 additions & 4 deletions torchgeo/trainers/cyclone.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,14 @@ def configure_optimizers(self) -> Dict[str, Any]:
"""Initialize the optimizer and learning rate scheduler."""
optimizer = torch.optim.Adam(
self.model.parameters(),
lr=self.hparams["learning_rate"], # type: ignore[index]
lr=self.hparams["learning_rate"],
)
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": ReduceLROnPlateau(
optimizer,
patience=self.hparams[
"learning_rate_schedule_patience"
], # type: ignore[index]
patience=self.hparams["learning_rate_schedule_patience"],
),
"monitor": "val_loss",
},
Expand Down
Loading