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

GridGeoSampler: don't change stride of last patch #1329

Merged
merged 2 commits into from
May 14, 2023
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
27 changes: 23 additions & 4 deletions tests/samplers/test_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@ def dataset(self) -> CustomGeoDataset:
@pytest.fixture(
scope="function",
params=product(
[(8, 1), (6, 2), (4, 3), (2.5, 3), ((8, 6), (1, 2)), ((6, 4), (2, 3))],
[
(8, 1),
(6, 2),
(4, 3),
(4, 4),
(2, 4),
(2.5, 3),
((8, 6), (1, 2)),
((6, 4), (2, 3)),
],
[Units.PIXELS, Units.CRS],
),
)
Expand All @@ -172,8 +181,18 @@ def sampler(self, dataset: CustomGeoDataset, request: SubRequest) -> GridGeoSamp

def test_iter(self, sampler: GridGeoSampler) -> None:
for query in sampler:
assert sampler.roi.minx <= query.minx <= query.maxx <= sampler.roi.maxx
assert sampler.roi.miny <= query.miny <= query.miny <= sampler.roi.maxy
assert (
sampler.roi.minx
<= query.minx
<= query.maxx
< sampler.roi.maxx + sampler.stride[1]
)
assert (
sampler.roi.miny
<= query.miny
<= query.miny
< sampler.roi.maxy + sampler.stride[0]
)
assert sampler.roi.mint <= query.mint <= query.maxt <= sampler.roi.maxt

assert math.isclose(query.maxx - query.minx, sampler.size[1])
Expand Down Expand Up @@ -222,7 +241,7 @@ def test_float_multiple(self) -> None:
iterator = iter(sampler)
assert len(sampler) == 2
assert next(iterator) == BoundingBox(0, 5, 0, 5, 0, 10)
assert next(iterator) == BoundingBox(1, 6, 0, 5, 0, 10)
assert next(iterator) == BoundingBox(5, 10, 0, 5, 0, 10)

@pytest.mark.slow
@pytest.mark.parametrize("num_workers", [0, 1, 2])
Expand Down
1 change: 1 addition & 0 deletions torchgeo/datasets/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ def _merge_files(
indexes=band_indexes,
out_shape=out_shape,
window=from_bounds(*bounds, src.transform),
boundless=True,
)
else:
dest, _ = rasterio.merge.merge(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge has no boundless parameter, but doesn't need one. I confirmed that it properly samples outside the bounds of the file and uses nodata pixels for padding.

Expand Down
9 changes: 0 additions & 9 deletions torchgeo/samplers/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ class GridGeoSampler(GeoSampler):
The overlap between each chip (``chip_size - stride``) should be approximately equal
to the `receptive field <https://distill.pub/2019/computing-receptive-fields/>`_ of
the CNN.

Note that the stride of the final set of chips in each row/column may be adjusted so
that the entire :term:`tile` is sampled without exceeding the bounds of the dataset.
"""

def __init__(
Expand Down Expand Up @@ -242,17 +239,11 @@ def __iter__(self) -> Iterator[BoundingBox]:
for i in range(rows):
miny = bounds.miny + i * self.stride[0]
maxy = miny + self.size[0]
if maxy > bounds.maxy:
maxy = bounds.maxy
miny = bounds.maxy - self.size[0]

# For each column...
for j in range(cols):
minx = bounds.minx + j * self.stride[1]
maxx = minx + self.size[1]
if maxx > bounds.maxx:
maxx = bounds.maxx
minx = bounds.maxx - self.size[1]

yield BoundingBox(minx, maxx, miny, maxy, mint, maxt)

Expand Down