5
votes

In convolutional Neural Networks, How to know the output of a specific conv layer? (I am using keras to build a CNN model)

For example if I am using one dimensional conv layer, where number_of_filters=20, kernel_size=10, and input_shape(500,1)

 cnn.add(Conv1D(20,kernel_size=10,strides=1, padding="same",activation="sigmoid",input_shape=(Dimension_of_input,1)))

and if I am using two dimensional conv layer, where number_of_filters=64, kernal_size=(5,100), input_shape= (5,720,1) (height,width,channel)

 Conv2D(64, (5, 100),
       padding="same",
       activation="sigmoid",
       data_format="channels_last",
       input_shape=(5,720,1)

what is the number of output in the above two conv layers? Is there any equation that can be used to know the number of outputs of a conv layer in convolution neural network?

3

3 Answers

4
votes

Yes, there are equations for it, you can find them in the CS231N course website. But as this is a programming site, Keras provides an easy way to get this information programmaticaly, by using the summary function of a Model.

model = Sequential()
fill model with layers
model.summary()

This will print in terminal/console all the layer information, such as input shapes, output shapes, and number of parameters for each layer.

0
votes

Actually, the model.summary() function might not be what you are looking for if you want to do more than just look at the model.

If you want to access layers of your Keras model you can do this by using model.layers which returns all of the layers (assignement stores them as a list). If you then want to look at a specific layer you can simply index the list:

list_of_layers = model.layers
list_of_layers[5] # gives you the 6th layer

What you are still working with are just objects so you probably want to get specific values. You just have to specify attribute you want to look at then:

list_of_layers[-1].output_shape # returns output_shape of last layer

Gives you back the output_shape tuple of the last layer in the model. You can even skip the whole list assignement thing if you already know that you only want to look at the output_shape of a certain layer and just do:

model.layers[-1].output_shape # equivalent to the above method without storing in a list

This might be useful if you want to use these values while building the model to guide the execution in a certain way (adding a pooling layer or doing the padding etc.).

0
votes

when first time i am working with TensorFlow cnn it is very difficult to dealing with dimensions. below is the general scenario for calculating dimensions:

consider

  1. we have a image of dimension (nXn), filter dimension : (fXf), no padding, no strides applies : after convolution dimension are : (n-f+1,n-f+1)

  2. dimension of image = (nXn) and filter dimension = (fXf) and we have padding : p then output dims are = (n+2P-f+1,n+2P-f+1) if we are using Padding = 'SAME" it means output dims = input dims in this case equation looks like : n+2P-f+1=n so from here p = (f-1)/2

if we are using valid padding then it means no padding and p =0

in computer vision f is usually odd if f is even it means we have asymmetric padding.

  1. case when we are using stride = s output dims are ( floor( ((n+2P-f)/s)+1 ),floor( ( (n+2P-f)/s)+1 ) )