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

Add warning if SubjectsLoader is not used in PyTorch >= 2.3 #1215

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

fepegar
Copy link
Owner

@fepegar fepegar commented Sep 25, 2024

import torch
import torchio as tio

colin = tio.datasets.Colin27()
dataset = tio.SubjectsDataset([colin])
loader = torch.utils.data.DataLoader(dataset, batch_size=1)
next(iter(loader))
[/Users/fernando/git/torchio/src/torchio/data/image.py:188): UserWarning: Using TorchIO images without a SubjectsLoader in PyTorch >= 2.3 might have unexpected consequences. Please replace your PyTorch DataLoader with a SubjectsLoader. See https://github.com/fepegar/torchio/issues/1179 for more context about this problem
  warnings.warn(message, stacklevel=1)
Subject(Keys: ('t1', 'head', 'brain'); images: 3)
loader = tio.SubjectsLoader(dataset, batch_size=1)
next(iter(loader))
{
    't1': {
        'path': ['/Users/fernando/.cache/torchio/mni_colin27_1998_nifti/colin27_t1_tal_lin.nii.gz'],
        'stem': ['colin27_t1_tal_lin'],
        'type': ['intensity'],
        'data': tensor([[[[[210684.4219, 225600.1250, 231532.5156,  ...,      0.0000,
                 0.0000,      0.0000],
           [250516.1406, 225600.1250, 219837.2188,  ...,      0.0000,
                 0.0000,      0.0000],
           [241236.2031, 216532.0469, 222167.8125,  ...,      0.0000,
                 0.0000,      0.0000],
           ...,
           [236576.5156, 239396.7031, 224654.7500,  ...,      0.0000,
                 0.0000,      0.0000],
           [229333.7188, 204913.3281, 226833.9844,  ...,      0.0000,
                 0.0000,      0.0000],
           [187158.8750, 165750.9844, 201131.6875,  ...,      0.0000,
                 0.0000,      0.0000]]]]]),
        'affine': array([[[   1.,    0.,    0.,  -90.],
        [   0.,    1.,    0., -126.],
        [   0.,    0.,    1.,  -72.],
        [   0.,    0.,    0.,    1.]]])
...
        [   0.,    1.,    0., -126.],
        [   0.,    0.,    1.,  -72.],
        [   0.,    0.,    0.,    1.]]])
    }
}

Copy link

codecov bot commented Sep 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.84%. Comparing base (0314926) to head (8408171).
Report is 34 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1215      +/-   ##
==========================================
- Coverage   87.43%   84.84%   -2.59%     
==========================================
  Files          92       92              
  Lines        5975     5997      +22     
==========================================
- Hits         5224     5088     -136     
- Misses        751      909     +158     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fepegar fepegar marked this pull request as ready for review September 25, 2024 21:44
@fepegar fepegar changed the title Add warning if SubjectsLoader is not used Add warning if SubjectsLoader is not used in PyTorch >= 2.3 Sep 25, 2024
@romainVala
Copy link
Contributor

Hi
yes it is a good idea to give an explicit warning. Just a suggestion to be even more specific :

UserWarning: Using TorchIO images without a SubjectsLoader in PyTorch >= 2.3 might have unexpected consequences. Batch element will be type torchio.Subject whereas is you replace your PyTorch DataLoader with a torchio SubjectsLoader, you will get batch element of type dictionary. See #1179 for more context about this problem

@fepegar
Copy link
Owner Author

fepegar commented Sep 26, 2024

Thanks, @romainVala. I've updated the warning message, inspired by your suggestion.

@fepegar fepegar merged commit 5df1638 into main Sep 26, 2024
23 of 24 checks passed
@fepegar fepegar deleted the add-subjects-loader-warning branch September 26, 2024 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants