Can I inverse image after using 'tio.SubjectsDataset' and 'torch.utils.data.DataLoader'? #743
-
Hi My test code as follow: # load subj
subject_a = tio.Subject(
img=tio.ScalarImage(nii_path),
mask=tio.LabelMap(mask_path),
)
subjects_list = [subject_a]
# transform
onehot = tio.OneHot()
pad = tio.CropOrPad((96, 112, 96)) # raw is (91, 109, 91)
transforms = [onehot, pad]
transform = tio.Compose(transforms)
# load data by torch
subjects_dataset = tio.SubjectsDataset(subjects_list, transform=transform)
training_loader = DataLoader(subjects_dataset, batch_size=1, num_workers=4)
for subjects_batch in training_loader:
img = subjects_batch['img'][tio.DATA]
mask = subjects_batch['mask'][tio.DATA]
tio_subj = tio.utils.get_subjects_from_batch(subjects_batch)[0]
tio_subj = tio_subj.apply_inverse_transform()
print('raw shape: {}'.format(subject_a.img.shape))
print('new shape: {}'.format(tio_subj.img.shape)) And I get a warning as follow:
I have read the doc about invertibility, but it's using I want to know, is any method to invert images after Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
Hi |
Beta Was this translation helpful? Give feedback.
-
Hi, @Bigsealion. That's a good question. That feature is currently not implemented, but I will commit a patch in a second. After that, your code should work with a slight modification: use import torchio as tio
from torch.utils.data import DataLoader
subject_a = tio.datasets.Colin27()
subjects_list = [subject_a]
# transform
onehot = tio.OneHot()
pad = tio.CropOrPad((96, 112, 96))
transforms = [onehot, pad]
transform = tio.Compose(transforms)
# load data by torch
subjects_dataset = tio.SubjectsDataset(subjects_list, transform=transform)
training_loader = DataLoader(
subjects_dataset,
collate_fn=tio.utils.history_collate,
)
for subjects_batch in training_loader:
tio_subj = tio.utils.get_subjects_from_batch(subjects_batch)[0]
tio_subj = tio_subj.apply_inverse_transform()
print('raw shape: {}'.format(subject_a.t1.shape))
print('new shape: {}'.format(tio_subj.brain.shape)) |
Beta Was this translation helpful? Give feedback.
-
I guess I know why the @fepegar The test code is as follow:
out:
|
Beta Was this translation helpful? Give feedback.
Hi, @Bigsealion. That's a good question.
That feature is currently not implemented, but I will commit a patch in a second. After that, your code should work with a slight modification: use
collate_fn=tio.utils.history_collate
for theDataLoader
.