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

RandomGeoSampler: fix performance regression #1968

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
13 changes: 6 additions & 7 deletions torchgeo/samplers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,16 @@ def get_random_bounding_box(
"""
t_size = _to_tuple(size)

width = (bounds.maxx - bounds.minx - t_size[1]) // res
height = (bounds.maxy - bounds.miny - t_size[0]) // res
# May be negative if bounding box is smaller than patch size
width = (bounds.maxx - bounds.minx - t_size[1]) / res
height = (bounds.maxy - bounds.miny - t_size[0]) / res
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the width/height in pixel units. It no longer needs to be an integer, float is fine too. We cast to integer elsewhere.


minx = bounds.minx
miny = bounds.miny

# random.randrange crashes for inputs <= 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a dead comment, we no longer use random.randrange and the input can no longer be negative

if width > 0:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No longer need to guard for negative numbers, it doesn't matter if the sample starts outside the bounds of the image for bounding boxes smaller than the patch size.

minx += torch.rand(1).item() * width * res
if height > 0:
miny += torch.rand(1).item() * height * res
# Use an integer multiple of res to avoid resampling
minx += int(torch.rand(1).item() * width) * res
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the only real important line

miny += int(torch.rand(1).item() * height) * res

maxx = minx + t_size[1]
maxy = miny + t_size[0]
Expand Down
Loading