-
Notifications
You must be signed in to change notification settings - Fork 379
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 xView2 Dataset #236
Merged
Merged
Add xView2 Dataset #236
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
32b204e
add xview2 dataset
isaaccorley 8347d48
add to docs'
isaaccorley cc4f586
update docstring
isaaccorley 0ace0f3
update plot method and change to XView2
isaaccorley 87e22ee
style fixes
isaaccorley a96f4a8
mypy fixes
isaaccorley b955dfb
Tuning up xView2 dataset object
calebrob6 9b4fd15
Indexing bug
calebrob6 507f0f1
Adding tests
calebrob6 96b4bc2
Updating doc
calebrob6 6b0111f
Update torchgeo/datasets/utils.py
calebrob6 4c8f90b
Update tests/datasets/test_xview2.py
calebrob6 048a749
Updates
calebrob6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Binary file added
BIN
+125 Bytes
tests/data/xview2/test/images/hurricane-michael_00000105_post_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+125 Bytes
tests/data/xview2/test/images/hurricane-michael_00000105_pre_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+125 Bytes
tests/data/xview2/test/images/hurricane-michael_00000450_post_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+125 Bytes
tests/data/xview2/test/images/hurricane-michael_00000450_pre_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.37 KB
tests/data/xview2/test/targets/hurricane-michael_00000105_post_disaster_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.38 KB
tests/data/xview2/test/targets/hurricane-michael_00000105_pre_disaster_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.38 KB
tests/data/xview2/test/targets/hurricane-michael_00000450_post_disaster_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.37 KB
tests/data/xview2/test/targets/hurricane-michael_00000450_pre_disaster_target.png
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
+125 Bytes
tests/data/xview2/train/images/hurricane-harvey_00000072_post_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+125 Bytes
tests/data/xview2/train/images/hurricane-harvey_00000072_pre_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+125 Bytes
tests/data/xview2/train/images/hurricane-harvey_00000471_post_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+125 Bytes
tests/data/xview2/train/images/hurricane-harvey_00000471_pre_disaster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.38 KB
tests/data/xview2/train/targets/hurricane-harvey_00000072_post_disaster_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.39 KB
tests/data/xview2/train/targets/hurricane-harvey_00000072_pre_disaster_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.39 KB
tests/data/xview2/train/targets/hurricane-harvey_00000471_post_disaster_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.38 KB
tests/data/xview2/train/targets/hurricane-harvey_00000471_pre_disaster_target.png
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.
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,117 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
import shutil | ||
from pathlib import Path | ||
from typing import Generator | ||
|
||
import pytest | ||
import torch | ||
import torch.nn as nn | ||
from _pytest.fixtures import SubRequest | ||
from _pytest.monkeypatch import MonkeyPatch | ||
|
||
from torchgeo.datasets import XView2, XView2DataModule | ||
|
||
|
||
class TestXView2: | ||
@pytest.fixture(params=["train", "test"]) | ||
def dataset( | ||
self, monkeypatch: Generator[MonkeyPatch, None, None], request: SubRequest | ||
) -> XView2: | ||
monkeypatch.setattr( # type: ignore[attr-defined] | ||
XView2, | ||
"metadata", | ||
{ | ||
"train": { | ||
"filename": "train_images_labels_targets.tar.gz", | ||
"md5": "373e61d55c1b294aa76b94dbbd81332b", | ||
"directory": "train", | ||
}, | ||
"test": { | ||
"filename": "test_images_labels_targets.tar.gz", | ||
"md5": "bc6de81c956a3bada38b5b4e246266a1", | ||
"directory": "test", | ||
}, | ||
}, | ||
) | ||
root = os.path.join("tests", "data", "xview2") | ||
split = request.param | ||
transforms = nn.Identity() # type: ignore[attr-defined] | ||
return XView2(root, split, transforms, checksum=True) | ||
|
||
def test_getitem(self, dataset: XView2) -> None: | ||
x = dataset[0] | ||
assert isinstance(x, dict) | ||
assert isinstance(x["image"], torch.Tensor) | ||
assert isinstance(x["mask"], torch.Tensor) | ||
|
||
def test_len(self, dataset: XView2) -> None: | ||
assert len(dataset) == 2 | ||
|
||
def test_extract(self, tmp_path: Path) -> None: | ||
shutil.copyfile( | ||
os.path.join( | ||
"tests", "data", "xview2", "train_images_labels_targets.tar.gz" | ||
), | ||
os.path.join(tmp_path, "train_images_labels_targets.tar.gz"), | ||
) | ||
shutil.copyfile( | ||
os.path.join( | ||
"tests", "data", "xview2", "test_images_labels_targets.tar.gz" | ||
), | ||
os.path.join(tmp_path, "test_images_labels_targets.tar.gz"), | ||
) | ||
XView2(root=str(tmp_path)) | ||
|
||
def test_corrupted(self, tmp_path: Path) -> None: | ||
with open( | ||
os.path.join(tmp_path, "train_images_labels_targets.tar.gz"), "w" | ||
) as f: | ||
f.write("bad") | ||
with open( | ||
os.path.join(tmp_path, "test_images_labels_targets.tar.gz"), "w" | ||
) as f: | ||
f.write("bad") | ||
with pytest.raises(RuntimeError, match="Dataset found, but corrupted."): | ||
XView2(root=str(tmp_path), checksum=True) | ||
|
||
def test_invalid_split(self) -> None: | ||
with pytest.raises(AssertionError): | ||
XView2(split="foo") | ||
|
||
def test_not_downloaded(self, tmp_path: Path) -> None: | ||
with pytest.raises(RuntimeError, match="Dataset not found in `root` directory"): | ||
XView2(str(tmp_path)) | ||
|
||
def test_plot(self, dataset: XView2) -> None: | ||
x = dataset[0].copy() | ||
dataset.plot(x, suptitle="Test") | ||
dataset.plot(x, show_titles=False) | ||
x["prediction"] = x["mask"][0].clone() | ||
dataset.plot(x) | ||
|
||
|
||
class TestXView2DataModule: | ||
@pytest.fixture(scope="class", params=[0.0, 0.5]) | ||
def datamodule(self, request: SubRequest) -> XView2DataModule: | ||
root = os.path.join("tests", "data", "xview2") | ||
batch_size = 1 | ||
num_workers = 0 | ||
val_split_size = request.param | ||
dm = XView2DataModule( | ||
root, batch_size, num_workers, val_split_pct=val_split_size | ||
) | ||
dm.prepare_data() | ||
dm.setup() | ||
return dm | ||
|
||
def test_train_dataloader(self, datamodule: XView2DataModule) -> None: | ||
next(iter(datamodule.train_dataloader())) | ||
|
||
def test_val_dataloader(self, datamodule: XView2DataModule) -> None: | ||
next(iter(datamodule.val_dataloader())) | ||
|
||
def test_test_dataloader(self, datamodule: XView2DataModule) -> None: | ||
next(iter(datamodule.test_dataloader())) |
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
from torch import Tensor | ||
from torch.utils.data import Dataset, Subset, random_split | ||
from torchvision.datasets.utils import check_integrity, download_url | ||
from torchvision.utils import draw_segmentation_masks | ||
|
||
__all__ = ( | ||
"check_integrity", | ||
|
@@ -33,9 +34,13 @@ | |
"rasterio_loader", | ||
"dataset_split", | ||
"sort_sentinel2_bands", | ||
"draw_semantic_segmentation_masks", | ||
) | ||
|
||
|
||
ColorMap = Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
class _rarfile: | ||
class RarFile: | ||
def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
|
@@ -431,3 +436,26 @@ def sort_sentinel2_bands(x: str) -> str: | |
if x == "B8A": | ||
x = "B08A" | ||
return x | ||
|
||
|
||
def draw_semantic_segmentation_masks( | ||
image: Tensor, mask: Tensor, alpha: float = 0.5, colors: Optional[ColorMap] = None | ||
) -> np.ndarray: # type: ignore[type-arg] | ||
"""Overlay a semantic segmentation mask onto an image. | ||
|
||
Args: | ||
image: tensor of shape (3, h, w) | ||
mask: tensor of shape (h, w) with pixel values representing the classes | ||
alpha: alpha blend factor | ||
colors: list of RGB int tuples, or color strings e.g. red, #FF00FF | ||
Returns: | ||
a list of the subset datasets. Either [train, val] or [train, val, test] | ||
""" | ||
classes = torch.unique(mask) # type: ignore[attr-defined] | ||
classes = classes[1:] | ||
class_masks = mask == classes[:, None, None] | ||
img = draw_segmentation_masks( | ||
image=image, masks=class_masks, alpha=alpha, colors=colors | ||
) | ||
img = img.permute((1, 2, 0)).numpy() | ||
return img # type: ignore[no-any-return] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ScopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a 'class' scoped request object, involved factories