How to down-sample a tensor using interpolation?

falmasri: <p>I want to downsample the last feature map by 2 or 4 using interpolation. the function nn.Upsample can’t take fraction in the factor.</p>

ptrblck: <p>You could use <code>grid_sample</code> for bilinear interpolation.<br>
I created a small example for this use case:</p>
<pre><code class="lang-auto"># Create fake image
image = torch.zeros(1, 3, 24, 24)
image[0, :, 6:18, 6:18] = 1.

# Create grid
out_size = 12
x = torch.linspace(-1, 1, out_size).view(-1, 1).repeat(1, out_size)
y = torch.linspace(-1, 1, out_size).repeat(out_size, 1)
grid = torch.cat((x.unsqueeze(2), y.unsqueeze(2)), 2)
grid.unsqueeze_(0)

image_small = F.grid_sample(image, grid)

import matplotlib.pyplot as plt
plt.imshow(image[0].permute(1, 2, 0).numpy())
plt.imshow(image_small[0].permute(1, 2, 0).numpy())
</code></pre>
<p>Also, you could try <a href="http://pytorch.org/docs/stable/nn.html#avgpool2d">AvgPool2d</a> if that fits your use case.</p>

falmasri: <p>I’m using AvgPool2d but I’ll test your example thanks</p>

falmasri: <p>Why the grid has 2 in its fourth dimension? what is related for ?</p>
<p>let’s say I have (B,1,x,y), does this procedure make the interpolation on the last two dimension only ?  assuming that each last two dimensions are an image of channel/depth 1 ?</p>
<p>What about downsampling of non rectangular size ? if x and y are not equal</p>

falmasri: <p>I did it this way</p>
<pre><code class="lang-auto">image = torch.zeros(2, 1, 24, 28)
image[0, :, 6:18, 6:18] = 1.

# Create grid
xout_size = 12
yout_size = 14
x = torch.linspace(-1, 1, yout_size).repeat(xout_size, 1)
y = torch.linspace(-1, 1, xout_size).view(-1, 1).repeat(1, yout_size)
grid = torch.cat((x.unsqueeze(2), y.unsqueeze(2)), 2)
grid = grid.unsqueeze_(0).repeat(2,1,1,1)
image_small = F.grid_sample(image, grid)

import matplotlib.pyplot as plt
plt.subplot(121)
plt.imshow(image[0,0].numpy())
plt.subplot(122)
plt.imshow(image_small[0,0].numpy())
</code></pre>
<p>But i got this error when I run the model</p>
<blockquote>
<p>return torch._C._VariableFunctions.cudnn_grid_sampler(input, grid)<br>
RuntimeError: get_device is not implemented for type torch.FloatTensor</p>
</blockquote>

ptrblck: <p>The fourth dimension stores the x, y input pixel locations which are used to compute output.</p>
<p>Yes, as the <a href="https://pytorch.org/docs/master/nn.html#torch.nn.functional.grid_sample">doc</a> states, <code>grid_sample</code> computes the output using input pixel locations from the grid, i.e. using <code>x</code> and <code>y</code> in your case.</p>
<p>Downsampling of a non rectangular size should also work.<br>
Your code runs on my machine using PyTorch <code>0.4.0</code>.<br>
Which version are you using?</p>

falmasri: <p>I have 0.4.0a0+847fad7</p>

ptrblck: <p>The error seems to be pointing to CuDNN? Are you using the code on GPU?<br>
I’ve also tried it on my GPU and it runs without a problem.</p>

falmasri: <p>Yes I’m running on GPU and cuda release 9.1, V9.1.85</p>

ptrblck: <p>Could you try to disable CuDNN for a moment and run it again?</p>
<pre><code>torch.backends.cudnn.enabled = False</code></pre>

falmasri: <p>CudaSpatialGridSamplerBilinear_updateOutput received an invalid combination of arguments - got (int, !Variable!, !Variable!, !Variable!, int), but expected (int state, torch.cuda.FloatTensor input, torch.cuda.FloatTensor grid, torch.cuda.FloatTensor output, int padding_mode)</p>

ptrblck: <p>Could you post the code you are running?<br>
Also, it seems you have an alpha version of <code>0.4.0</code>.<br>
Could you update to the current stable version using the <a href="https://pytorch.org/">instructions</a> from the website?</p>

falmasri: <pre><code class="lang-auto">xout_size =x4.shape[2]
yout_size = x4.shape[3]
xc = torch.linspace(-1, 1, yout_size).repeat(xout_size, 1)
yc = torch.linspace(-1, 1, xout_size).view(-1, 1).repeat(1, yout_size)
grid = torch.cat((xc.unsqueeze(2), yc.unsqueeze(2)), 2)
grid = grid.unsqueeze_(0).repeat(self.MiniBatch, 1, 1, 1)

image_small = F.grid_sample(x, grid)

</code></pre>
<p>x4 is an out of shape (M,1,x,y) from a conv2d</p>
<p>I have the version cloned form Github.</p>

ptrblck: <p>It seems you don’t push <code>grid</code> to the GPU.<br>
You probably cloned a few weeks ago, so I would suggest to update to the current stable <code>0.4.0</code> release.</p>

falmasri: <p>I’m pulling the last github right now. could you give me the best way to update the current version after pulling please</p>

ptrblck: <p>Usually you don’t need to pull from master unless you need some features or bug fixes, which are not yet available in the stable binary release or if you would like to implement some new features or bug fixes yourself.<br>
Just visit the homepage and have a look at the install instructions.</p>
<p>Using conda you can simply install PyTorch using</p>
<pre><code>conda install pytorch torchvision -c pytorch</code></pre>

falmasri: <p>I don’t have  conda. I’ll install it using python setup.py install and give the output</p>

falmasri: <p>I got again the last version which is the same version as I had 0.4.0a0+847fad7</p>

ptrblck: <p>Probably the old version is still installed.<br>
You might have to run</p>
<pre><code>pip uninstall torch
</code></pre>
<p>a few times after building from source, until no installation is found.</p>

falmasri: <p>pip -H uninstall torch<br>
then<br>
sudo python3 setup.py install<br>
I have again<br>
0.4.0a0+847fad7</p>
<p>it seems like the verison I’m pulling from github is only this one</p>