Alternative to MaxPool2D & MaxUnpool2d

username: chaslie
cooked: <p>Hi,</p>
<p>I would like to keep an encoder and decoder as seperate classes, bur i would also like to use maxpool &amp; maxunpool.</p>
<p>When the network trains and I try and explore the latent domain by placing random vectors into the trained decoder section I get an error (no indices for the maxunpool). Removing these layers means that the network does not perform as well.</p>
<p>I have tried to replace with avgpool2d but this doesn’t improve the situation, so the question is there an alternative to Maxpool/unpool or is there a way to use these without passing the indices=true switch?</p>
<p>Can i save the indices from the training model, so that i can reload them in the trained model?</p>
<p>so many questions…</p>
<p>Thanks,</p>
<p>Chaslie</p>

username: Nikronic
cooked: <p>Hi,</p>
<p>About alternatives for pooling, you can do it using stride in Conv layers or using different poolings but I do not think that is good idea to skip a method just because of syntax error! <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>About the <code>MaxPool</code> and <code>MaxUnpool</code>, something I should mention is that you have to provide the indices to <code>MaxUnpool</code>. When you are using <code>MaxPool</code> in your encoder, it returns a tuple (values, indices) and you have to save those and pass them to your decoder.</p>
<p>I think this post explains the situation very well.</p><aside class="quote quote-modified" data-post="1" data-topic="1349">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/b487fb/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/about-maxpool-and-maxunpool/1349">About MaxPool and MaxUnpool</a> 
  </div>
  <blockquote>
    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: 
Run…
  </blockquote>
</aside>

<p>bests</p>

username: chaslie
cooked: <p>I think you missed the point.</p>
<p>I would like to use the decoder part of a CVAE, however because it uses maxunpool2d i need to pass the indices into the decoder, however i would like to know:</p>
<ol>
<li>is there another way of using maxunpool2d which doesn’t require using the indices?</li>
<li>if not, is there a way i can generate the indices tensors externally (I don’t think so)?</li>
<li>if not then what is the best way of mimicing maxpool2d/unpool2d? I have tried conv2d, avg2d and adaptive2d to no avail.</li>
</ol>

username: Nikronic
cooked: <p>About point 1, there is no way as the definition of maxunpool needs the indices.<br>
About second point, you can get the indices. You need to define a class on top of the pretrained model and change the line in the forward function to return indices too. It won’t hurt the weights as pooling layers do not have any parameters. (you already have encoder so it is possible.)<br>
Concerning point 3, <a href="https://pytorch.org/docs/master/generated/torch.nn.ConvTranspose2d.html" rel="nofollow noopener">ConvTranspose2d</a> may be able to help you but still it needs training and may not perform well.</p>
<p>As you have access to encoder and maxpooling layers, the easiest way still is to use indices.</p>

username: chaslie
cooked: <p>in summary:</p>
<p>You cannot use the maxpool2d &amp; unpool2d in a VAE or CVAE if you want to explore the latent space ‘z’ in the decoder module independetly of the encoder, becayuse there is no way of generating the indices tensors independently for each input into the decoder module.</p>
<p>This is because the indices tensors are different for each input into the encoder module.</p>
<p>I have to find an alternative architecture.</p>

username: Nikronic
cooked: <p>Can you please share the resources that lead you to the idea of using pooling/unpooling in autoencoders?<br>
I have read few renown papers in this area and they use conv2d and transposedConv2d for the exact same reason we are discussing. As you mentioned we may need to treat decoder independently from encoder so the only reasonable choice is TransposedConv as it has been used in Unet too.</p>

username: chaslie
cooked: <p>Its my own devise. without pooling and unpooling the results are not as good, guess i need to work on the architecture a bit more…</p>

username: Nikronic
cooked: <p>Yes, there may be some other problems with your defined network. Because in almost all of the VAE papers, Conv2d and TransposedConv2d has been used for changing channel size and dimension at the same time and it will work fine if training procedure goes well.<br>
The reason is that maxpooling is a simple operation and a Conv2d matrix can learn weights to even put more attention on some particular features. I think UNet is the big example that works very well in different tasks.</p>
<p>Good luck</p>