0

This is part of a code on construction of a CNN in a book. I don't understand why 'filters =64' here. As far as I know this is the number of feature maps. How do I determine this number when I make my own CNN?

# network parameters 
# image is processed as is (square grayscale)
input_shape = (image_size, image_size, 1)
batch_size = 128
kernel_size = 3
pool_size = 2
filters = 64
dropout = 0.2

model = Sequential()
model.add(Conv2D(filters = filters,
                 kernel_size = kernel_size,
                 activation = 'relu',
                 input_shape = input_shape))
model.add(MaxPooling2D(pool_size))
model.add(Conv2D(filters = filters,
                 kernel_size = kernel_size,
                 activation = 'relu'))
model.add(MaxPooling2D(pool_size))
model.add(Conv2D(filters = filters,
                 kernel_size = kernel_size,
                 activation = 'relu'))
model.add(Flatten())
# dropout added as regularizer
model.add(Dropout(dropout))
# output layer is 10-dim one-hot vector
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.summary()
plot_model(model, to_file='cnn-mnist.png', show_shapes=True)
AndyLiuin
  • 11
  • 4
  • A filter is like a drop-in image which is smaller than the input image. You slide-in/stride it on the input image to build feature maps. Have you look at this question and answers? https://stackoverflow.com/questions/48243360/how-to-determine-the-filter-parameter-in-the-keras-conv2d-function – Laksitha Ranasingha Jun 20 '20 at 22:16
  • @LaksithaRanasingha Thanks a lot! I didn't find this answer, will have a look – AndyLiuin Jun 20 '20 at 22:24

1 Answers1

0

Filters are the number of features you want to detect in the image. Also known as the feature detectors in the image. It is a hyper-parameter and entirely up to you. There are a couple of architectures of CNN. It will be better if you look for existing solutions on the problem you are trying to solve using your CNN. Then try to tune filter value and check if accuracy increases or not.

You can choose filters based on the complexity of the task. The number of filters tends to increase with each layer. As first layer will extract simple features, and the next layer will extract more complex features. Have a look at the link below for further reference. Hope I helped :)

https://stackoverflow.com/a/48243420/9024042