Resizing? Any simple direct way?

flupadup: <p>Hello, is there a simple way, to resize an image? For example from (256,256) to (244,244)?<br>
I looked at this thread <a href="https://discuss.pytorch.org/t/autogradable-image-resize/580">Autogradable image resize</a> and used the AvgPool2 method, but it seems quiet complicated for me, to resize an image from 256p to 244…<br>
I am sitting on this problem for a quiet long time now…and I don’t find a way to fix it. Some help would be very appreciated!<br>
thanks!</p>

apsvieira: <p>Hello,</p>
<p>You can use Torchvision transforms on your images. Pretty straight forward.</p>
<p><a href="http://pytorch.org/docs/master/torchvision/transforms.html" class="onebox" target="_blank" rel="nofollow noopener">http://pytorch.org/docs/master/torchvision/transforms.html</a></p>

flupadup: <p>Sorry, that is a quiet low level question, but I am quiet new to pytroch, but I tried resizing my image for ours now, also with .Resize.<br>
Could you give me an simple example, how you use that? thanks!</p>

apsvieira: <p>The <a href="http://pytorch.org/tutorials/beginner/data_loading_tutorial.html?highlight=torchvision" rel="nofollow noopener">Data Loading and Processing Tutorial</a> includes a session on image transforms.</p>
<p>I believe it would be best to follow/understand the tutorial than for me to try to summarize it.</p>

flupadup: <p>I think an example, on how to simply apply a rescaling on my image would help me very much with understanding all that</p>

Mahir_Atmis: <p>First of all as <a href="https://discuss.pytorch.org/u/apsvieira">Antonio</a> stated read the transforms documentation it will teach you much more. Below is a simplified version of what is explained in the tutorials.</p>
<p>from skimage import io, transform</p>
<p>image = io.imread(PATH_TO_THE_IMAGE)<br>
img = transform.resize(image, (desired_h, desired_w))</p>

arnowaczynski: <p>I use adaptive average pooling for resizing if I can / want move resizing step to GPU:</p>
<pre><code>import torch
import torch.nn.functional as F
from torch.autograd import Variable

def resize2d(img, size):
    return (F.adaptive_avg_pool2d(Variable(img,volatile=True), size)).data

img = torch.Tensor(3,256,256).uniform_()
print(img.size())
img = resize2d(img, (224,224))
print(img.size())</code></pre>

xin71: <pre><code class="lang-auto">def resize_tensor(input_tensors, h, w):
  final_output = None
  batch_size, channel, height, width = input_tensors.shape
  input_tensors = torch.squeeze(input_tensors, 1)
  
  for img in input_tensors:
    img_PIL = transforms.ToPILImage()(img)
    img_PIL = torchvision.transforms.Resize([h,w])(img_PIL)
    img_PIL = torchvision.transforms.ToTensor()(img_PIL)
    if final_output is None:
      final_output = img_PIL
    else:
      final_output = torch.cat((final_output, img_PIL), 0)
  final_output = torch.unsqueeze(final_output, 1)
  return final_output

</code></pre>
<p>You have to convert to PIL and use transform functions there. Also, transform functions don’t support batch so I wrote a helper function. Hope this helps.</p>

nivniv: <p>I would use this wonderful repo <a href="https://github.com/assafshocher/ResizeRight" rel="noopener nofollow ugc">ResizeRight</a></p>

danielgonzalezcortes: <p>Hi Antonio, the link is not working.</p>

kakrafoon: <p>On the same lines, here’s a modified version directly using the transforms class. I lifted it directly from my training code.</p>
<pre><code class="lang-auto">transform = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor(), 
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
img = Image.open(os.path.join(input_path, file)) #PIL image
img = transform(img) #torch.FloatTensor 
</code></pre>
<p>￼<br>
￼</p>