'Same' convolution in pytorch

username: Ahmed_m
cooked: <p>Hi,</p>
<p>Is there a simple way to do ‘same’ convolution in pytorch such that the output spacial dimensions equals the input counterpart ?</p>
<p>Best</p>

username: tom
cooked: <p>Use the padding parameter. The formula for the output size is given in <a href="https://pytorch.org/docs/master/nn.html#torch.nn.Conv2d" rel="nofollow noopener">the shape section at the bottom of the torch.nn.Conv2d documentation</a>. So e.g. <code>k//2</code> for odd kernel sizes k with default stride and dilation.</p>
<p>Best regards</p>
<p>Thomas</p>

username: pinocchio
cooked: <p>There are formulas for this. You can see a worked out example here:</p>
<aside class="onebox stackexchange" data-onebox-src="https://stats.stackexchange.com/questions/419902/does-padding-ensure-that-the-sizes-of-input-and-output-of-a-cnn-match">
  <header class="source">

      <a href="https://stats.stackexchange.com/questions/419902/does-padding-ensure-that-the-sizes-of-input-and-output-of-a-cnn-match" target="_blank" rel="noopener nofollow ugc">stats.stackexchange.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stats.stackexchange.com/users/28986/charlie-parker" target="_blank" rel="noopener nofollow ugc">
    <img alt="Charlie Parker" src="https://i.stack.imgur.com/2B6rV.png?s=256&amp;g=1" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stats.stackexchange.com/questions/419902/does-padding-ensure-that-the-sizes-of-input-and-output-of-a-cnn-match" target="_blank" rel="noopener nofollow ugc">Does padding ensure that the sizes of input and output of a CNN match?</a>
</h4>

<div class="tags">
  <strong>machine-learning, neural-networks, conv-neural-network</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stats.stackexchange.com/users/28986/charlie-parker" target="_blank" rel="noopener nofollow ugc">
    Charlie Parker
  </a>
  on <a href="https://stats.stackexchange.com/questions/419902/does-padding-ensure-that-the-sizes-of-input-and-output-of-a-cnn-match" target="_blank" rel="noopener nofollow ugc">11:31PM - 30 Jul 19 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>also here is a reference for arithmetic of convolutions:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://arxiv.org/abs/1603.07285">
  <header class="source">
      <img src="https://static.arxiv.org/static/browse/0.3.4/images/icons/favicon-32x32.png" class="site-icon" width="" height="">

      <a href="https://arxiv.org/abs/1603.07285" target="_blank" rel="noopener nofollow ugc">arXiv.org</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/402;"><img src="https://static.arxiv.org/static/browse/0.3.4/images/arxiv-logo-fb.png" class="thumbnail" width="" height=""></div>

<h3><a href="https://arxiv.org/abs/1603.07285" target="_blank" rel="noopener nofollow ugc">A guide to convolution arithmetic for deep learning</a></h3>

  <p>We introduce a guide to help deep learning practitioners understand and manipulate convolutional neural network architectures. The guide clarifies the relationship between various properties (input shape, kernel shape, zero padding, strides and...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<hr>
<p>Though I agree that this so staandard that Pytorch should just have a layer or an option that calculates all this once for the user. It seems silly and repetitive that we have to think about this every time we want to use same convolution. Would be nice if the code was already ready and provided for us, specially for standard things like this.</p>
<hr>
<aside class="quote quote-modified" data-post="1" data-topic="52103">
  <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/pinocchio/48/22093_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-do-i-make-sure-the-output-of-a-cnn-never-decrease-in-size-using-padding/52103">How do I make sure the output of a CNN never decrease in size using padding?</a> 
  </div>
  <blockquote>
    I want to create one code such that no matter what task or input size the image is, it will always produce a ConvNet such that the padding is set such that the size of the features is always equal to the size of the original image (up until the end/final layer). 
How does one do that in pytorch? 

I realized this might be useful: 



Cross posted with code:
  </blockquote>
</aside>


username: rwightman
cooked: <p>I’ve ported weights from several Google AI Tensorflow models (EfficientNet, MixNet, MnasNet, etc). These weights require this type of padding so I created a factory of sorts that allows selecting between a PyTorch symmetric padding that comes close to ‘SAME’ and actually matching the TF ‘SAME’ with asymmetric padding when needed for compatibility.</p>
<p>Note that the Tensorflow compatible padding has a runtime penalty since I implemented it in a way that is dynamic based on input size (as opposed to requiring the input size passed in at model creation time). This pentaly is not worth paying if you don’t need compatibility with TF weights.</p>
<p>Current version of this lives here with the mixed kernel convolutions from MixNet: <a href="https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/conv2d_helpers.py" rel="nofollow noopener">https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/conv2d_helpers.py</a></p>
<p>My impl does work well with several TF models so I assume it’s reasonably close to correct. May be some issues/scenarios I haven’t tested well wrt dilations, etc. Any feedback welcome. There are lots of discussion on this out there in various forums or issue trackers. Mistakes in many of those too <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Kinyugo
cooked: <p>Hello. Your link no longer works.</p>