title: Tensorflow - Same Padding Calculation
body: <p>I have the following parameters:</p>
  
  <pre><code>in_height = 28
  in_width = 28
  stride (s) = 2
  padding (p) = 'SAME'
  </code></pre>
  
  <p>The idea of 'SAME' padding is when s = 1 then input map and output map dimensions (height, width) should remain same</p>
  
  <p>So if I should be able to get the padding size using the following:</p>
  
  <pre><code>(28 + 2*p - 5) + 1 = 28
  Solving gives p = 2
  </code></pre>
  
  <p>which means there should be a padding on each side of 2</p>
  
  <p>Using <code>p=2</code> the output map size would be:</p>
  
  <pre><code>(28 + 4 -5)/2 + 1 = 14
  </code></pre>
  
  <p>From Tensorflow documentation, <code>Same Padding</code>:</p>
  
  <pre><code>out_height = ceil(float(in_height) / float(strides[1]))
  out_width  = ceil(float(in_width) / float(strides[2]))
  
  pad_along_height = max((out_height - 1) * strides[1] +
                      filter_height - in_height, 0)
  pad_along_width = max((out_width - 1) * strides[2] +
                     filter_width - in_width, 0)
  pad_top = pad_along_height // 2
  pad_bottom = pad_along_height - pad_top
  pad_left = pad_along_width // 2
  pad_right = pad_along_width - pad_left
  </code></pre>
  
  <p>To follow the above:</p>
  
  <pre><code>out_height = ceil(28.0/2.0) = 14.0
  out_width = ceil(28.0/2.0) = 14.0
  </code></pre>
  
  <p>Hence </p>
  
  <pre><code>pad_along_height = max((14.0 -1)*2 + 5 - 28,0) = 3
  pad_along_width = max((14.0 -1)*2 + 5 - 28,0) = 3
  
  pad_top = 3 // 2 = 1
  pad_bottom = 3//2 - pad_top = 2
  pad_left = pad_along_width // 2 = 1
  pad_right = pad_along_width - pad_left = 2
  </code></pre>
  
  <p>So does it mean that the image should be padded 1 on top and 2 on bottom similarly on the left and right?  </p>