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

Add SpaceNet5 #263

Merged
merged 8 commits into from
Nov 29, 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
1 change: 1 addition & 0 deletions docs/api/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ SpaceNet
.. autoclass:: SpaceNet1
.. autoclass:: SpaceNet2
.. autoclass:: SpaceNet4
.. autoclass:: SpaceNet5
.. autoclass:: SpaceNet7

Tropical Cyclone Wind Estimation Competition
Expand Down
Binary file added tests/data/spacenet/sn5_AOI_7_Moscow.tar.gz
Binary file not shown.
Binary file added tests/data/spacenet/sn5_AOI_8_Mumbai.tar.gz
Binary file not shown.
74 changes: 73 additions & 1 deletion tests/datasets/test_spacenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the MIT License.

import glob
import itertools
import os
import shutil
from pathlib import Path
Expand All @@ -14,7 +15,7 @@
from _pytest.fixtures import SubRequest
from _pytest.monkeypatch import MonkeyPatch

from torchgeo.datasets import SpaceNet1, SpaceNet2, SpaceNet4, SpaceNet7
from torchgeo.datasets import SpaceNet1, SpaceNet2, SpaceNet4, SpaceNet5, SpaceNet7

TEST_DATA_DIR = "tests/data/spacenet"

Expand Down Expand Up @@ -223,6 +224,77 @@ def test_plot(self, dataset: SpaceNet4) -> None:
plt.close()


class TestSpaceNet5:
@pytest.fixture(
params=itertools.product(["PAN", "MS", "PS-MS", "PS-RGB"], [False, True])
)
def dataset(
self,
request: SubRequest,
monkeypatch: Generator[MonkeyPatch, None, None],
tmp_path: Path,
) -> SpaceNet5:
radiant_mlhub = pytest.importorskip("radiant_mlhub", minversion="0.2.1")
monkeypatch.setattr( # type: ignore[attr-defined]
radiant_mlhub.Collection, "fetch", fetch_collection
)
test_md5 = {
"sn5_AOI_7_Moscow": "e0d5f41f1b6b0ee7696c15e5ff3141f5",
"sn5_AOI_8_Mumbai": "ab898700ee586a137af492b84a08e662",
}

monkeypatch.setattr( # type: ignore[attr-defined]
SpaceNet5, "collection_md5_dict", test_md5
)
root = str(tmp_path)
transforms = nn.Identity() # type: ignore[attr-defined]
return SpaceNet5(
root,
image=request.param[0],
speed_mask=request.param[1],
collections=["sn5_AOI_7_Moscow", "sn5_AOI_8_Mumbai"],
transforms=transforms,
download=True,
api_key="",
)

def test_getitem(self, dataset: SpaceNet5) -> None:
# Iterate over all elements to maximize coverage
samples = [i for i in dataset] # type: ignore[attr-defined]
x = samples[0]
assert isinstance(x, dict)
assert isinstance(x["image"], torch.Tensor)
assert isinstance(x["mask"], torch.Tensor)
if dataset.image == "PS-RGB":
assert x["image"].shape[0] == 3
elif dataset.image in ["MS", "PS-MS"]:
assert x["image"].shape[0] == 8
else:
assert x["image"].shape[0] == 1

def test_len(self, dataset: SpaceNet5) -> None:
assert len(dataset) == 5

def test_already_downloaded(self, dataset: SpaceNet5) -> None:
SpaceNet5(root=dataset.root, download=True)

def test_not_downloaded(self, tmp_path: Path) -> None:
with pytest.raises(RuntimeError, match="Dataset not found"):
SpaceNet5(str(tmp_path))

def test_collection_checksum(self, dataset: SpaceNet5) -> None:
dataset.collection_md5_dict["sn5_AOI_8_Mumbai"] = "randommd5hash123"
with pytest.raises(RuntimeError, match="Collection sn5_AOI_8_Mumbai corrupted"):
SpaceNet5(root=dataset.root, download=True, checksum=True)

def test_plot(self, dataset: SpaceNet5) -> None:
x = dataset[0].copy()
dataset.plot(x, suptitle="Test")
plt.close()
dataset.plot(x, show_titles=False)
plt.close()


class TestSpaceNet7:
@pytest.fixture(params=["train", "test"])
def dataset(
Expand Down
3 changes: 2 additions & 1 deletion torchgeo/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
from .sen12ms import SEN12MS, SEN12MSDataModule
from .sentinel import Sentinel, Sentinel2
from .so2sat import So2Sat, So2SatDataModule
from .spacenet import SpaceNet, SpaceNet1, SpaceNet2, SpaceNet4, SpaceNet7
from .spacenet import SpaceNet, SpaceNet1, SpaceNet2, SpaceNet4, SpaceNet5, SpaceNet7
from .ucmerced import UCMerced, UCMercedDataModule
from .utils import BoundingBox, collate_dict
from .vaihingen import Vaihingen2D, Vaihingen2DDataModule
Expand Down Expand Up @@ -133,6 +133,7 @@
"SpaceNet1",
"SpaceNet2",
"SpaceNet4",
"SpaceNet5",
"SpaceNet7",
"TropicalCycloneWindEstimation",
"CycloneDataModule",
Expand Down
Loading