How to resize a PyTorch tensor?

一笑奈何 提交于 2021-02-18 23:00:20

问题


Now I have a torch.Tensor of size (5, 1, 44, 44) in Pytorch.

  • 5 = batch size
  • 1 = channel
  • 44= image height
  • 44= image width

and I want to 'resize' it to shape (5, 1, 224, 224)

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


回答1:


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.



来源:https://stackoverflow.com/questions/58676688/how-to-resize-a-pytorch-tensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!