I was wondering whether has anyone done bilinear interpolation resizing with PyTorch Tensor under CUDA?
I tried this using torch.nn.functional.F.interpolate(rgb_image,(size,size)) and it works to resize the RGB image tensor shape (batch,channel,size,size). However, the default mode is ‘nearest’ and when I change to ‘bilinear’, it comes with an error about the tensor type which I fixed by changing to torch.FloatTensor but then the output shown is grainy and pixelated with noise. My command:
Your output might be clipped to [0, 1], if you are trying to visualize floating point numbers or [0, 255] if you are using uint8.
I cannot reproduce the issue using the latest stable releases and by making sure I’m casting to the expected type:
You are right! Thank you for your help, appreciate it!
I just needed to cast the bilinear output as .byte()! I did not realised this as mode='nearest' is able to take in tensor as byte type and output given is already in byte. Meanwhile, mode='bilinear needs FloatTensor and outputs in float. Thus, to show the image, we have to put it back to byte type.
I am actually amazed that pytorch has implemented resizing for GPU. Now I can skip using cv2.resize() and remain the tensor on the GPU for resizing instead!
Any idea on whether are there major differences between cv2.resize() -> bilinear to F.interpolate(....,mode='bilinear')? Your comments are appreciated.
I haven’t checked the latest updates in torchvision, but know that there was at least work in progress to avoid small numerical differences between different cv libraries.
You could run a quick test with the latest torchvision release (0.10.0) or the nightly and see how large these differences would be.
Actually, I realised that it matters more that the torchvision.transform.resize() is same as torch.nn.functional.interpolate() for my use case as the model is trained and tested under torchvision transformation for the DataLoader.
Just to complete this thread for anyone interested, I found that both functions is the same: