14

I have a PyTorch tensor of size (5, 1, 44, 44) (batch, channel, height, width), and I want to 'resize' it to (5, 1, 224, 224)

How can I do that? What functions should I use?

iacob
  • 20,084
  • 6
  • 92
  • 119
Gerwe1s_Ji
  • 159
  • 1
  • 1
  • 5

2 Answers2

21

It seems like you are looking for interpolate (a function in nn.functional):

import torch.nn.functional as nnf

x = torch.rand(5, 1, 44, 44)
out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False)

If you really care about the accuracy of the interpolation, you should have a look at ResizeRight: a pytorch/numpy package that accurately deals with all sorts of "edge cases" when resizing images. This can have an effect when directly merging features of different scales: inaccurate interpolation may result in misalignments.

Innat
  • 16,113
  • 6
  • 53
  • 101
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 3
    just a word of warning about bicubic interpolation is that the range of the result may be wider than the range of the input. If this is important than you can use bilinear instead – jodag Nov 03 '19 at 12:39
3

The TorchVision transforms.functional.resize() function is what you're looking for:

import torchvision.transforms.functional as F

t = torch.randn([5, 1, 44, 44])
t_resized = F.resize(t, 224)

If you wish to use another interpolation mode than bilinear, you can specify this with the interpolation argument.

iacob
  • 20,084
  • 6
  • 92
  • 119