Resample layer in pytorch?

Hello all, I am looking for the resample layer in pytorch that can perform the action likes

Given a 3D input with a spacing of 1x1x1 mm. I want to resample the image to the other resolution for training, as 1.5x1.5x1.5 mm. Any layer in pytorch can do it? I found the upsample and nn.functional.interpolate but I am not sure it allow to change spacing or not

Iā€™m not sure how ITK implements their resampling for medical images, but if you would like to apply it as a preprocessing step, you could probably just use the Python ITK library to do so.

However, if you need to implement this transformation as part of your model, could you post an example of the original and resampled data?

Thanks. I would likes do the second way. It meants build a transformantion model likes the image

It allows the network to extract different size of input likes 25x25x25 or 19x19x19

Based on the diagram it looks like you would like to crop the MRI volume and then resample it.
Are you sure the first step is performed in the model?
If so, I think a simple linear interpolation should yield the results:

slices = 10
channels = 3
height = 24
width = 24
x = torch.randn(channels, slices, height, width)
out = torch.nn.functional.interpolate(x, scale_factor=1.5)

Would that work for you?

Thanks but the problem is that resample using pytorch function considers spacing or not? It looks likes you just use size information. One more, does your resample function has backward?

No, interpolate just works on the raw pixel locations.
I think if you have different pixel spacing in your volume, you might want to resample this volume to an isotropic resolution (e.g. 1mm*1mm*1mm). After I assume your model should be able to deal with the input.

Yes, backward is supported in interpolate.

1 Like