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.
* millionaid * test * separator * remove type ignore * type in test * requested changes * typos and glob pattern * task argument description * add test md5 hash * Remove download logic * Type ignore no longer needed Co-authored-by: Adam J. Stewart <[email protected]>
- Loading branch information
1 parent
8dc01f8
commit 41a20cd
Showing
12 changed files
with
495 additions
and
0 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
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,52 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import hashlib | ||
import os | ||
import shutil | ||
|
||
import numpy as np | ||
from PIL import Image | ||
|
||
SIZE = 32 | ||
|
||
np.random.seed(0) | ||
|
||
PATHS = { | ||
"train": [ | ||
os.path.join( | ||
"train", "agriculture_land", "grassland", "meadow", "P0115918.jpg" | ||
), | ||
os.path.join("train", "water_area", "beach", "P0060208.jpg"), | ||
], | ||
"test": [ | ||
os.path.join("test", "agriculture_land", "grassland", "meadow", "P0115918.jpg"), | ||
os.path.join("test", "water_area", "beach", "P0060208.jpg"), | ||
], | ||
} | ||
|
||
|
||
def create_file(path: str) -> None: | ||
Z = np.random.rand(SIZE, SIZE, 3) * 255 | ||
img = Image.fromarray(Z.astype("uint8")).convert("RGB") | ||
img.save(path) | ||
|
||
|
||
if __name__ == "__main__": | ||
for split, paths in PATHS.items(): | ||
# remove old data | ||
if os.path.isdir(split): | ||
shutil.rmtree(split) | ||
for path in paths: | ||
os.makedirs(os.path.dirname(path), exist_ok=True) | ||
create_file(path) | ||
|
||
# compress data | ||
shutil.make_archive(split, "zip", ".", split) | ||
|
||
# Compute checksums | ||
with open(split + ".zip", "rb") as f: | ||
md5 = hashlib.md5(f.read()).hexdigest() | ||
print(f"{split}: {md5}") |
Binary file not shown.
Binary file added
BIN
+1.21 KB
tests/data/millionaid/test/agriculture_land/grassland/meadow/P0115918.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added
BIN
+1.22 KB
tests/data/millionaid/train/agriculture_land/grassland/meadow/P0115918.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,64 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
import matplotlib.pyplot as plt | ||
import pytest | ||
import torch | ||
import torch.nn as nn | ||
from _pytest.fixtures import SubRequest | ||
|
||
from torchgeo.datasets import MillionAID | ||
|
||
|
||
class TestMillionAID: | ||
@pytest.fixture( | ||
scope="class", params=zip(["train", "test"], ["multi-class", "multi-label"]) | ||
) | ||
def dataset(self, request: SubRequest) -> MillionAID: | ||
root = os.path.join("tests", "data", "millionaid") | ||
split, task = request.param | ||
transforms = nn.Identity() | ||
return MillionAID( | ||
root=root, split=split, task=task, transforms=transforms, checksum=True | ||
) | ||
|
||
def test_getitem(self, dataset: MillionAID) -> None: | ||
x = dataset[0] | ||
assert isinstance(x, dict) | ||
assert isinstance(x["image"], torch.Tensor) | ||
assert isinstance(x["label"], torch.Tensor) | ||
assert x["image"].shape[0] == 3 | ||
assert x["image"].ndim == 3 | ||
|
||
def test_len(self, dataset: MillionAID) -> None: | ||
assert len(dataset) == 2 | ||
|
||
def test_not_found(self, tmp_path: Path) -> None: | ||
with pytest.raises(RuntimeError, match="Dataset not found in"): | ||
MillionAID(str(tmp_path)) | ||
|
||
def test_not_extracted(self, tmp_path: Path) -> None: | ||
url = os.path.join("tests", "data", "millionaid", "train.zip") | ||
shutil.copy(url, tmp_path) | ||
MillionAID(str(tmp_path)) | ||
|
||
def test_corrupted(self, tmp_path: Path) -> None: | ||
with open(os.path.join(tmp_path, "train.zip"), "w") as f: | ||
f.write("bad") | ||
with pytest.raises(RuntimeError, match="Dataset found, but corrupted."): | ||
MillionAID(str(tmp_path), checksum=True) | ||
|
||
def test_plot(self, dataset: MillionAID) -> None: | ||
x = dataset[0].copy() | ||
dataset.plot(x, suptitle="Test") | ||
plt.close() | ||
|
||
def test_plot_prediction(self, dataset: MillionAID) -> None: | ||
x = dataset[0].copy() | ||
x["prediction"] = x["label"].clone() | ||
dataset.plot(x) | ||
plt.close() |
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
Oops, something went wrong.