How to calculate optimal zero padding for convolutional neural networks? So formula for calculating the number of zero padding according to cs231n blog is :  
P = (F-1)/2

where P is number of zero padding ,F is the filter size and the number of stride is 1. But I don't understand what happens if the number of strides is not 1 or if F is an even number. Let's consider the number of strides is 4 and  F is 7 or stride is 1 and F is 4. So according to the formula, in both these cases zero padding is in fraction:
1) P = (7-4)/2 = 3/2
2) P = (4-1)/2 = 3/2

How can the number of zero padding be in fraction ? 
 A: The general formular for the required padding P to achieve SAME padding is as follows:
P = ((S-1)*W-S+F)/2, with F = filter size, S = stride, W = input size

Of course, the padding P cannot be a fraction, hence you should round it up to the next higher int value.
A: There are situations where (input_dim + 2*padding_side - filter) % stride == 0 has no solutions for padding_side.
The formula (filter - 1) // 2 is good enough for the formula where the output shape is (input_dim + 2*padding_side - filter) // stride + 1. The output image will not retain all the information from the padded image but it's ok since we truncate only from the padding.
A: Given an expected output dimension then return the padding
The formula to get the output dimension on the $l$ layer is given below and from there we can extract a general formula for any output value.
\begin{equation}
n_{H}^{[l]} =\lfloor\frac{n_{H}^{[l-1]} + 2p^{[l]} - f^{[l]}}{s^{[l]}}\rfloor + 1
\end{equation}
We can say that:
\begin{equation}
p = \frac{n_{H}^{[l]} - n_{H}^{[l-1]} - s^{[l]} + f^{[l]} }{2}
\end{equation}
Validation of the formula above
If $p = 0, s = 1, f = 7 $ and $n_{H}^{[l-1]} = 63$ the output dimension is $ n_{H}^{[l]} = 57$
\begin{equation}
p = \frac{57 - 63 - 1 + 7 }{2} = 0
\end{equation}
Expecting same convolution
Although, for this case, the formula is more simple. $p = (f^{[l]} − 1)/2$ due to the input dimension and the output is the same.
\begin{equation}
p = \frac{63 - 63 - 1 + 7 }{2} = 3
\end{equation}
When we expect any dimension
Given a $ n_{H}^{[l]} >= n_{H}^{[l-1]}$
\begin{equation}
p = \frac{73 - 63 - 1 + 7 }{2} = 8
\end{equation}
A: The possible values for the padding size, $P$, depends the input size $W$ (following the notation of the blog), the filter size $F$ and the stride $S$. We assume width and height are the same.
What you need to ensure is that the output size, $(W-F+2P)/S+1$, is an integer. When $S = 1$ then you get your first equation $P = (F-1)/2$ as necessary condition. But, in general, you need to consider the three parameters, namely $W$, $F$ and $S$ in order to determine valid values of $P$.
A: To find the padding size for any kernel size :
Horizontal padding
horizontal total_padding = (#rows_in_image * (horizontal_stride -1) - horizontal_stride + horizontal_dilation * (rows_in_kernel - 1) + 1
left pad = horizontal total_paddint // 2 
right pad = total_padding - left pad
similarly, you can find vertical padding.
A: The formula given for calculating the output size (one dimension) of a convolution is $(W - F + 2P) / S + 1$.
You can reason it in this way: when you add padding to the input and subtract the filter size, you get the number of neurons before the last location where the filter is applied. If you divide this by the stride, you get the number of times the filter is applied, before the last location.
For example, with input size 7, filter size 3, and stride 1, you can apply the filter four times before the last location:
[x][x][x][x][ ][ ][ ]

With stride 2, you can apply the filter two times before the last location, where the filter fits.
A couple of things to note about this formula:

*

*$P$ is the amount of zeros added on each side of the input. That's why there's $2P$ in the formula.

*The formula is valid when $F >= S$. For example, with input size $W = 6$, filter size $F = 1$, and stride $S = 2$, you can obviously apply the filter three times, but the formula gives $5 / 2 + 1$. I think generally $(W - max(F, S) + 2P)/S + 1$ is correct.

If you want to keep the output size same as the input size, you can equate $(W - max(F, S) + 2P) / S + 1 = W$.
Solving this, when $S = 1$ gives:
$W - F + 2P + 1 = W$
$2P = F - 1$
In total, $F - 1$ neuros need to be added to the input. If $F - 1$ is an odd number, then you would have to add more padding on one side than the other. Typically the convolution operation supports only adding an equal amount of padding on all sides, so if $(F - 1) / 2$ is not a whole number, you need to add the padding with a separate "pad" operation.
When $S > 1$, you could solve the amount of padding needed to keep the output size same as the input size:
$(W - max(F, S) + 2P) / S + 1 = W$
$W - max(F, S) + 2P = (W - 1)S$
$2P = (W - 1)S - W + max(F, S)$
However, it would be strange to use stride > 1 and add so much padding that the output size isn't reduced.
A more likely scenario is that you want to exactly halve the input size, when using stride 2, and so on. In this case you get:
$(W - max(F, S) + 2P) / S + 1 = W / S$
$W - max(F, S) + 2P + S = W$
$2P = max(F, S) - S$
A: If the Height and Width of the image are different, then you have to calculate the output image separately. The formula remains the same i-e
   ((W - F * 2P)/S) + 1 and ((H - F * 2P)/S) + 1

Dimension difference does not affect the expected output.
