Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a PyTorch tensor?

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?

like image 962
Gerwe1s_Ji Avatar asked Nov 03 '19 01:11

Gerwe1s_Ji


1 Answers

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.

like image 84
Shai Avatar answered Sep 28 '22 02:09

Shai