Last active
November 25, 2022 01:05
-
-
Save sjtalkar/b10e0ff3139b65bfcbd9e4738a94a4b3 to your computer and use it in GitHub Desktop.
Convolution layer output size calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Source Aurelion Geron https://colab.research.google.com/github/ageron/handson-ml3/blob/main/14_deep_computer_vision_with_cnns.ipynb#scrollTo=jisXP9jfpKz2 | |
import numpy as np | |
def conv_output_size(input_size, kernel_size, strides=1, padding="valid"): | |
if padding=="valid": | |
z = input_size - kernel_size + strides | |
output_size = z // strides | |
num_ignored = z % strides | |
return output_size, num_ignored | |
else: | |
output_size = (input_size - 1) // strides + 1 | |
num_padded = (output_size - 1) * strides + kernel_size - input_size | |
return output_size, num_padded | |
conv_output_size(np.array([70, 120]), kernel_size=7, strides=2, padding="same") | |
https://www.baeldung.com/cs/convolutional-layer-size | |
Now let’s move on to the main goal of this tutorial which is to present the formula for computing the output size of a convolutional layer. We have the following input: | |
An image of dimensions W_{in} \times H_{in}. | |
A filter of dimensions K \times K. | |
Stride S and padding P. | |
The output activation map will have the following dimensions: | |
{W_{out} = {W_{in} - K + 2P}/{S} + 1} | |
{H_{out} = {H_{in} - K + 2P} / {S} + 1} | |
If the output dimensions are not integers, it means that we haven’t set the stride S correctly. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment