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 multiple versions of the So2Sat dataset #1283

Merged
merged 22 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 tests/conf/so2sat_all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ datamodule:
root: "tests/data/so2sat"
batch_size: 1
num_workers: 0
version: "2"
band_set: "all"
18 changes: 18 additions & 0 deletions tests/conf/so2sat_rgb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module:
_target_: torchgeo.trainers.ClassificationTask
loss: "ce"
model: "resnet18"
learning_rate: 1e-3
learning_rate_schedule_patience: 6
weights: null
in_channels: 3
num_classes: 17

datamodule:
_target_: torchgeo.datamodules.So2SatDataModule
root: "tests/data/so2sat"
batch_size: 1
num_workers: 0
version: "3_random"
band_set: "rgb"
validation_pct: 0.5
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions tests/conf/so2sat_s1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ datamodule:
root: "tests/data/so2sat"
batch_size: 1
num_workers: 0
version: "2"
band_set: "s1"
1 change: 1 addition & 0 deletions tests/conf/so2sat_s2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ datamodule:
root: "tests/data/so2sat"
batch_size: 1
num_workers: 0
version: "2"
band_set: "s2"
Binary file added tests/data/so2sat/block/testing.h5
Binary file not shown.
Binary file added tests/data/so2sat/block/training.h5
Binary file not shown.
Binary file added tests/data/so2sat/culture_10/testing.h5
Binary file not shown.
Binary file added tests/data/so2sat/culture_10/training.h5
Binary file not shown.
20 changes: 13 additions & 7 deletions tests/data/so2sat/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import hashlib
import os
import shutil

import h5py
import numpy as np

SIZE = 64 # image width/height
SIZE = 32 # image width/height
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
NUM_CLASSES = 17
NUM_SAMPLES = 1
NUM_SAMPLES = 2
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved

np.random.seed(0)

Expand All @@ -28,16 +29,21 @@
]

# Random images
sen1 = np.random.randint(256, size=(NUM_SAMPLES, SIZE, SIZE, 8), dtype=np.uint8)
sen2 = np.random.randint(256, size=(NUM_SAMPLES, SIZE, SIZE, 10), dtype=np.uint8)
sen1 = np.random.randint(2, size=(NUM_SAMPLES, SIZE, SIZE, 8), dtype=np.uint8)
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
sen2 = np.random.randint(2, size=(NUM_SAMPLES, SIZE, SIZE, 10), dtype=np.uint8)

# Create datasets
with h5py.File(filename, "w") as f:
f.create_dataset("label", data=label)
f.create_dataset("sen1", data=sen1)
f.create_dataset("sen2", data=sen2)
f.create_dataset("label", data=label, compression="gzip", compression_opts=9)
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
f.create_dataset("sen1", data=sen1, compression="gzip", compression_opts=9)
f.create_dataset("sen2", data=sen2, compression="gzip", compression_opts=9)

# Compute checksums
with open(filename, "rb") as f:
md5 = hashlib.md5(f.read()).hexdigest()
print(repr(split.replace("ing", "")) + ":", repr(md5) + ",")

for version in ["random", "block", "culture_10"]:
os.makedirs(version, exist_ok=True)
shutil.copyfile("training.h5", os.path.join(version, "training.h5"))
shutil.copyfile("testing.h5", os.path.join(version, "testing.h5"))
Binary file added tests/data/so2sat/random/testing.h5
Binary file not shown.
Binary file added tests/data/so2sat/random/training.h5
Binary file not shown.
Binary file modified tests/data/so2sat/testing.h5
Binary file not shown.
Binary file modified tests/data/so2sat/training.h5
Binary file not shown.
Binary file modified tests/data/so2sat/validation.h5
Binary file not shown.
16 changes: 9 additions & 7 deletions tests/datasets/test_so2sat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
class TestSo2Sat:
@pytest.fixture(params=["train", "validation", "test"])
def dataset(self, monkeypatch: MonkeyPatch, request: SubRequest) -> So2Sat:
md5s = {
"train": "82e0f2d51766b89cb905dbaf8275eb5b",
"validation": "bf292ae4737c1698b1a3c6f5e742e0e1",
"test": "9a3bbe181b038d4e51f122c4be3c569e",
md5s_by_version = {
"2": {
"train": "56e6fa0edb25b065124a3113372f76e5",
"validation": "940c95a737bd2fcdcc46c9a52b31424d",
"test": "e97a6746aadc731a1854097f32ab1755",
}
}

monkeypatch.setattr(So2Sat, "md5s", md5s)
monkeypatch.setattr(So2Sat, "md5s_by_version", md5s_by_version)
root = os.path.join("tests", "data", "so2sat")
split = request.param
transforms = nn.Identity()
Expand All @@ -51,13 +53,13 @@ def test_getitem(self, dataset: So2Sat) -> None:
assert isinstance(x["label"], torch.Tensor)

def test_len(self, dataset: So2Sat) -> None:
assert len(dataset) == 1
assert len(dataset) == 2

def test_out_of_bounds(self, dataset: So2Sat) -> None:
# h5py at version 2.10.0 raises a ValueError instead of an IndexError so we
# check for both here
with pytest.raises((IndexError, ValueError)):
dataset[1]
dataset[2]

def test_invalid_split(self) -> None:
with pytest.raises(AssertionError):
Expand Down
1 change: 1 addition & 0 deletions tests/trainers/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class TestClassificationTask:
"so2sat_all",
"so2sat_s1",
"so2sat_s2",
"so2sat_rgb",
"ucmerced",
],
)
Expand Down
Loading