How to resize a PyTorch tensor?

15,428

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 effect when directly merging features of different scales: inaccurate interpolation may result with misalignments.

Share:
15,428

Related videos on Youtube

Gerwe1s_Ji
Author by

Gerwe1s_Ji

Updated on June 19, 2022

Comments

  • Gerwe1s_Ji
    Gerwe1s_Ji almost 2 years

    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?

  • jodag
    jodag over 4 years
    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