Applying dictionary transforms to a list of tensors #6936
-
Hi, I am wondering if there is a good way for me to apply dictionary transforms to a list of tensors. I am using a pattern inspired by in this tutorial notebook (see below). ![]() Here is an example of my current code. The motivation behind this is that I want the non-deterministic transforms (e.g. transforms = Compose(
[
RandFlipd(keys=["input", "targets"], prob=0.1),
Spacingd(keys=["input", "targets"], pixdim=[5, 5, 5]),
]
)
# tensors are currently monai metatensors
data = {"input": input_tensor, "targets": target_tensor}
result = transforms(data) The main crux of my problem is that I wish to be able to run this code with this line instead: However I get an ![]() Does anyone have any suggestions on the proper pattern to apply dictionary transforms to a list of tensors? A hacky way would be to flatten out the values but ideally I'd like to avoid this - is there a better way to go about it? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Hi @guillaume-troadec, I think you could do something like:
For more details, you can refer to https://github.com/Project-MONAI/tutorials/blob/main/modules/transforms_metatensor.ipynb. Hope it helps, thanks! |
Beta Was this translation helpful? Give feedback.
-
Hi @KumoLiu , thanks for your help! I did consider this, but I was hoping to perhaps avoid having an arbitrary number of keys (it would be easier for downstream code if everything was under the |
Beta Was this translation helpful? Give feedback.
-
you can also consider
MONAI/monai/transforms/transform.py Lines 101 to 115 in 50e66fa |
Beta Was this translation helpful? Give feedback.
-
Thank you both - I'll give that a go :) |
Beta Was this translation helpful? Give feedback.
you can also consider
monai.transforms.apply_transform
, something like:MONAI/monai/transforms/transform.py
Lines 101 to 115 in 50e66fa