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

Samplers: avoid bounding boxes smaller than patch size #376

Merged
merged 1 commit into from
Feb 1, 2022
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
8 changes: 8 additions & 0 deletions tests/samplers/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ def test_roi(self) -> None:
for query in batch:
assert query in roi

def test_small_area(self) -> None:
ds = CustomGeoDataset()
ds.index.insert(0, (0, 10, 0, 10, 0, 10))
ds.index.insert(1, (20, 21, 20, 21, 20, 21))
sampler = RandomBatchGeoSampler(ds, 2, 2, 10)
for _ in sampler:
continue

@pytest.mark.slow
@pytest.mark.parametrize("num_workers", [0, 1, 2])
def test_dataloader(self, sampler: RandomBatchGeoSampler, num_workers: int) -> None:
Expand Down
16 changes: 16 additions & 0 deletions tests/samplers/test_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ def test_roi(self) -> None:
for query in sampler:
assert query in roi

def test_small_area(self) -> None:
ds = CustomGeoDataset()
ds.index.insert(0, (0, 10, 0, 10, 0, 10))
ds.index.insert(1, (20, 21, 20, 21, 20, 21))
sampler = RandomGeoSampler(ds, 2, 10)
for _ in sampler:
continue

@pytest.mark.slow
@pytest.mark.parametrize("num_workers", [0, 1, 2])
def test_dataloader(self, sampler: RandomGeoSampler, num_workers: int) -> None:
Expand Down Expand Up @@ -145,6 +153,14 @@ def test_roi(self) -> None:
for query in sampler:
assert query in roi

def test_small_area(self) -> None:
ds = CustomGeoDataset()
ds.index.insert(0, (0, 10, 0, 10, 0, 10))
ds.index.insert(1, (20, 21, 20, 21, 20, 21))
sampler = GridGeoSampler(ds, 2, 10)
for _ in sampler:
continue

@pytest.mark.slow
@pytest.mark.parametrize("num_workers", [0, 1, 2])
def test_dataloader(self, sampler: GridGeoSampler, num_workers: int) -> None:
Expand Down
9 changes: 8 additions & 1 deletion torchgeo/samplers/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ def __init__(
self.size = _to_tuple(size)
self.batch_size = batch_size
self.length = length
self.hits = list(self.index.intersection(tuple(self.roi), objects=True))
self.hits = []
for hit in self.index.intersection(tuple(self.roi), objects=True):
bounds = BoundingBox(*hit.bounds)
if (
bounds.maxx - bounds.minx > self.size[1]
and bounds.maxy - bounds.miny > self.size[0]
):
self.hits.append(hit)

def __iter__(self) -> Iterator[List[BoundingBox]]:
"""Return the indices of a dataset.
Expand Down
18 changes: 16 additions & 2 deletions torchgeo/samplers/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ def __init__(
super().__init__(dataset, roi)
self.size = _to_tuple(size)
self.length = length
self.hits = list(self.index.intersection(tuple(self.roi), objects=True))
self.hits = []
for hit in self.index.intersection(tuple(self.roi), objects=True):
bounds = BoundingBox(*hit.bounds)
if (
bounds.maxx - bounds.minx > self.size[1]
and bounds.maxy - bounds.miny > self.size[0]
):
self.hits.append(hit)

def __iter__(self) -> Iterator[BoundingBox]:
"""Return the index of a dataset.
Expand Down Expand Up @@ -161,7 +168,14 @@ def __init__(
super().__init__(dataset, roi)
self.size = _to_tuple(size)
self.stride = _to_tuple(stride)
self.hits = list(self.index.intersection(tuple(self.roi), objects=True))
self.hits = []
for hit in self.index.intersection(tuple(self.roi), objects=True):
bounds = BoundingBox(*hit.bounds)
if (
bounds.maxx - bounds.minx > self.size[1]
and bounds.maxy - bounds.miny > self.size[0]
):
self.hits.append(hit)

self.length: int = 0
for hit in self.hits:
Expand Down