I loaded 3D CT images as .npy files to 2D UNet with a spatial dimension 512, 512. How can I ensure the information is preserved when resizing to 256, 256 - maybe the choice of interpolation and others when saving as .npy files
@moreshud could you please detail what you mean by
How can I ensure the information is preserved when resizing to 256, 256
I would reframe the statement. Does resizing changes the pixel value for a label with
0, 1 or an image in general? If yes, which of the interpolation can be used to keep the pixel value the same?
Yes, resizing in general changes the pixel values. For resizing targets (e.g. segmentation masks) we usually apply “nearest” interpolation mode. In this mode, resizing is approx equivalent to removing columns and lines => that new pixels are exactly the same to the source. For other modes, new pixels are interpolated => new pixel will have float values between 0 and 1. For resizing images we can apply bilinear or cubic interpolation mode.
The resizing can be done with PyTorch or cv2. How can I perform resampling on slices from 3D CT images?
Yes, both can work. As for torchvision, it would be better to produce a 3D tensor from 2D using unsqueeze before calling
transforms.Resize((new_h, new_w)). For example,
image_slice_2d = torch.rand(32, 32) # shape is (32, 32), dtype = float32 image_slice_3d = image_slice_2d.unsqueeze(dim=0) # shape is (1, 32, 32), dtype = float32 t = transforms.Resize((20, 20), interpolation=2) # interpolation is bilinear here res = t(image_slice_3d) # res.shape = (1, 20, 20) and dtype = float32 res.shape, res.dtype
Does torchvision have the resampling inbuilt function?
Internally it uses either
PIL.Image.transform if input is PIL.Image or
torch.nn.functional.interpolate for tensor inputs.
Please, see here for more info: https://pytorch.org/docs/stable/torchvision/transforms.html
Thanks a lot for your help!