About MaxPool and MaxUnpool

username: KwuJohn
cooked: <p>Hi all,</p>
<p>I have got a problem about the pooling function, the code were shown below:</p>
<pre><code>input = Variable(torch.rand(1,1,64,64))
pool1 = nn.MaxPool2d(2, stride=2, padding=1, return_indices=True)
pool2 = nn.MaxPool2d(2, stride=2, return_indices=True)
unpool1= nn.MaxUnpool2d(2, stride=2)
unpool2= nn.MaxUnpool2d(2, stride=2, padding=1)

output1, indices1 = pool1(input)
output2, indices2 = pool2(output1)

output3 = unpool1(output2,indices2)
output4 = unpool2(output3, indices1)
</code></pre>
<p>An error I had got:<br>
RuntimeError: found an invalid max index 1054 (output volumes are of size 32x32) at /data/users/soumith/builder/wheel/pytorch-src/torch/lib/THNN/generic/SpatialMaxUnpooling.c:42</p>
<p>How to solve this problem, I want to used the pooling function to get the ouput4 which with the same dimension as input.</p>
<p>Thanks a lot!</p>

username: fmassa
cooked: <p>As <a href="http://pytorch.org/docs/nn.html#torch.nn.MaxUnpool1d">explained in the docs for <code>MaxUnpool</code></a>, the when doing MaxPooling, there might be some pixels that get rounded up due to integer division on the input size. For example, if your image has size 5, and your stride is 2, the output size can be either 2 or 3, and you can’t retrieve the original size of the image.<br>
That’s why there is an optional argument to MaxUnpool that allows you to specify the desired output size. In your case, it should be something like</p>
<pre><code class="lang-python">output3 = unpool1(output2, indices2, output_size=output1.size())
output4 = unpool2(output3, indices1, output_size=input.size())
</code></pre>

username: Yilin_Liu
cooked: <p>Hello, even when I’ve specified the size I want, I still got this error…QAQ</p>

username: coya
cooked: <p>Works well for me.Thanks a lot</p>

username: kshitijpatil09
cooked: <p>I was playing around with max_pooling unpooling and the results I found are somewhat weird. Please let me know whether this is the desired behavior of unpooling or am I doing something wrong.</p>
<ul>
<li>These are the results of input size <strong>320</strong><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/8/388ed69b01819460df665f1cb13fc5f6db3bc6b5.png" alt="maxpool_unpool" data-base62-sha1="84kLCbg2B4S3EdKRTnMbICXFu0l" width="516" height="178"></li>
<li>These are the results of input size <strong>128</strong><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4b937af5a293c7b7ab7575cd026bbacfaa693a58.png" alt="maxpool_unpool_128" data-base62-sha1="aMzMH2S4VGUyLSX2GI3zc2Q4kaY" width="516" height="178"></li>
</ul>

username: DezzardHD
cooked: <aside class="quote no-group" data-username="kshitijpatil09" data-post="5" data-topic="1349">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kshitijpatil09/48/19552_2.png" class="avatar"> kshitijpatil09:</div>
<blockquote>
<p>Please let me know whether this is the desired behavior of unpooling or am I doing something wrong.</p>
</blockquote>
</aside>
<p>Hey <a class="mention" href="/u/kshitijpatil09">@kshitijpatil09</a>,<br>
I think the pictures you’ve uploaded are showing the desired behaviour.</p>
<p>Assuming the kernel size for the pooling operations is 2x2 with stride=(2,2)</p>
<p>| Original: 320x320 pixels<br>
| MaxPool: 160x160 pixels<br>
| MaxUnpool: 320x320 pixels</p>
<p>I guess the output of the MaxPool layer isn’t that confusing if you know how maxpooling works.</p>
<p>The output of the MaxUnpool layer is probably the cause of confusion. Basically you take the pixels from the output of the MaxPool layer and placing them to certain positions.<br>
You get these black gaps cause MaxUnpool has to fill up the spaces, cause go from 160x160 pixels to 320x320 pixels.</p>
<p>The positions of the pixels from the Maxpool layer’s output are being placed according to the indices that are being outputted by the MaxPool layer.</p>
<p>So it’s quite likely that you won’t get a chessboad like picture back. (That’s what I’ve thought at the beginning.)</p>
<p>Best regards<br>
DezzardHD</p>