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 dtype field to RasterDataset #1149

Merged
merged 12 commits into from
Apr 24, 2023
5 changes: 5 additions & 0 deletions tests/datasets/test_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ def test_no_all_bands(self) -> None:
with pytest.raises(AssertionError, match=msg):
CustomSentinelDataset(root, bands=bands, transforms=transforms, cache=cache)

def test_dtype_warning(self, custom_dtype_ds: RasterDataset) -> None:
custom_dtype_ds.dtype = torch.int32
with pytest.warns(UserWarning, match="Custom dtype is explicitly set*"):
custom_dtype_ds[custom_dtype_ds.bounds]


class TestVectorDataset:
@pytest.fixture(scope="class")
Expand Down
1 change: 1 addition & 0 deletions torchgeo/datasets/chesapeake.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Chesapeake(RasterDataset, abc.ABC):
"""

is_image = False
dtype = torch.long
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
calebrob6 marked this conversation as resolved.
Show resolved Hide resolved

# subclasses use the 13 class cmap by default
cmap = {
Expand Down
16 changes: 15 additions & 1 deletion torchgeo/datasets/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
from collections.abc import Sequence
from typing import Any, Callable, Optional, cast
from warnings import warn

import fiona
import fiona.transform
Expand Down Expand Up @@ -296,6 +297,9 @@ class RasterDataset(GeoDataset):
#: Color map for the dataset, used for plotting
cmap: dict[int, tuple[int, int, int, int]] = {}

#: dtype to force onto the dataset (overrides the dtype of the file via a cast)
dtype: Optional[torch.dtype] = None

def __init__(
self,
root: str = "data",
Expand Down Expand Up @@ -429,10 +433,20 @@ def __getitem__(self, query: BoundingBox) -> dict[str, Any]:
data = self._merge_files(filepaths, query, self.band_indexes)

sample = {"crs": self.crs, "bbox": query}

if self.dtype is not None:
if self.is_image:
warn(
"Custom dtype is explicitly set, but dtype is only valid for mask"
+ " RasterDatasets."
calebrob6 marked this conversation as resolved.
Show resolved Hide resolved
)
else:
data = data.to(self.dtype)

if self.is_image:
sample["image"] = data.float()
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
else:
sample["mask"] = data.long()
sample["mask"] = data

if self.transforms is not None:
sample = self.transforms(sample)
Expand Down