# How to resize a tensor or image

In pytorch, I have a tensor data with size (B,C,T1,V,), how could a resize it to (B,C,T2,V,) like image_resize does(eg:`tf.image.resize_bilinear` in`tensoflow`)?where T2 may be either larger or smaller than T1; I find import torch.nn.functional.upsample could only perform unsmaple(T1<T2), is there any function perform `unsample(T1<T2)` and `downsample(T2<T1)` both?

downsampled=F.upsample(img, size=(img.size(2)//2, img.size(3)//2), mode=‘bilinear’)

Thank you for replay! But, In the way , If T2 =3, T1 =5, how to do? I mean `T1 mod T2 !=0`

Your way `new_img=F.upsample(img, size_new=(H,W), mode=‘bilinear’)`, it works for both downsample , upsample, and no matter what size_new could be. Thank you again.

1 Like

Hi~ Thank you for your sharing. Supposing we want to resize a tenor of shape (batch, channel, height, width) and we use F.upsample to do the task, do we just need to specify the last two channel new size F.unsamping(img, size=(H, W))?
Or we have to specify each channel size as F.unsamping(img, size=(batch, channel, H, W)) ?

Hi,

specify each channel size as F.upsampling(img, size=(batch, channel, H, W)) doesn’t make sense to me,
`torch.nn.upsampling(img, size)` size just need a list of 2 ints(H and W) so it auto upsample on each channel, futhermore, `torch.nn.upsampling` is deprecated and use `torch.nn.functional.interpolate` instead.

1 Like

Just a note: the backward of upsampling, and interpolate (upscale) is not deterministic. (see here) (Pytorch 1.0.0, 1.0.1).