Skip to content

Commit

Permalink
Samplers: avoid bounding boxes smaller than patch size (microsoft#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjstewart authored Feb 1, 2022
1 parent a576867 commit 16d5ce4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
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

0 comments on commit 16d5ce4

Please sign in to comment.