About MaxPool and MaxUnpool

Hi all,

I have got a problem about the pooling function, the code were shown below:

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)

An error I had got:
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

How to solve this problem, I want to used the pooling function to get the ouput4 which with the same dimension as input.

Thanks a lot!


As explained in the docs for MaxUnpool, 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.
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

output3 = unpool1(output2, indices2, output_size=output1.size())
output4 = unpool2(output3, indices1, output_size=input.size())

Hello, even when I’ve specified the size I want, I still got this error…QAQ

1 Like

Works well for me.Thanks a lot

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.

  • These are the results of input size 320
  • These are the results of input size 128

Hey @kshitijpatil09,
I think the pictures you’ve uploaded are showing the desired behaviour.

Assuming the kernel size for the pooling operations is 2x2 with stride=(2,2)

| Original: 320x320 pixels
| MaxPool: 160x160 pixels
| MaxUnpool: 320x320 pixels

I guess the output of the MaxPool layer isn’t that confusing if you know how maxpooling works.

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.
You get these black gaps cause MaxUnpool has to fill up the spaces, cause go from 160x160 pixels to 320x320 pixels.

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.

So it’s quite likely that you won’t get a chessboad like picture back. (That’s what I’ve thought at the beginning.)

Best regards