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

Datasets: always return sample = dict[str, Tensor] #2567

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

adamjstewart
Copy link
Collaborator

@adamjstewart adamjstewart commented Feb 8, 2025

This PR introduces a new Sample type alias defined as:

Sample = dict[str, Tensor]

All dataset __getitem__ methods return a Sample, and all transforms operate on a Sample.

This PR supersedes #2249 with a simpler approach (can be expanded later).

Pros

  • Compatibility with PyTorch's default_collate function
  • Compatibility with Lightning's transfer_batch_to_device
  • Uniform type hints across all TorchGeo subpackages
  • Avoids most uses of dynamic typing (typing.Any)
  • Allows arbitrary key names (unlike Add Sample and Batch TypedDicts #2249)

Cons

Closes #2249
See #985 for discussion

@adamjstewart adamjstewart added the backwards-incompatible Changes that are not backwards compatible label Feb 8, 2025
@github-actions github-actions bot added datasets Geospatial or benchmark datasets models Models and pretrained weights testing Continuous integration testing trainers PyTorch Lightning trainers transforms Data augmentation transforms datamodules PyTorch Lightning datamodules labels Feb 8, 2025
@adamjstewart adamjstewart added this to the 0.7.0 milestone Feb 8, 2025
@github-actions github-actions bot removed the models Models and pretrained weights label Feb 9, 2025
@adamjstewart
Copy link
Collaborator Author

@ashnair1 do object detection bounding boxes have to be list[Tensor] or can they be Tensor? I wonder if we can use some kind of packed/padded sequence logic.

@ashnair1
Copy link
Collaborator

@ashnair1 do object detection bounding boxes have to be list[Tensor] or can they be Tensor? I wonder if we can use some kind of packed/padded sequence logic.

Object detection bboxes are usually list[Tensor]. This is because each image can have a varying number of objects e.g.

img1 -> [5, 4], 
img2 -> [15, 4], 
img3 -> [1, 4] 

Not sure about packing/padding. I think given a batch, we could theoretically iterate over the box tensors, find the max and pad the rest accordingly. So above example would be

img1 -> [15, 4], # Was 5, padded to 15 
img2 -> [15, 4],  # 15 max
img3 -> [15, 4], # Was 1, padded to 15

But I don't think it's optimal.

@adamjstewart
Copy link
Collaborator Author

In order to create mini-batches, do we need to pad anyway? PyTorch has a PackedSequence class but it's still not a subclass of Tensor. We might be back to #2249 if we need to support dicts with multiple value types. But that doesn't support unknown keys...

@adamjstewart adamjstewart removed this from the 0.7.0 milestone Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards-incompatible Changes that are not backwards compatible datamodules PyTorch Lightning datamodules datasets Geospatial or benchmark datasets testing Continuous integration testing trainers PyTorch Lightning trainers transforms Data augmentation transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants