Get Convolutional Layers output in Keras


(M. Rake Linggar Anggoro) #1

Dear forum,

I have built a CNN to classify images with a fairly accurate results using the following architecture using Keras.

classifier = Sequential()


classifier.add(Convolution2D(32, (3,3), input_shape = (64, 64, 3), activation='relu'))


classifier.add(MaxPool2D(pool_size = (2,2)))

classifier.add(Convolution2D(32, (3,3), activation='relu'))
classifier.add(MaxPool2D(pool_size = (2,2)))

classifier.add(Convolution2D(32, (3,3), activation='relu'))
classifier.add(MaxPool2D(pool_size = (2,2)))

classifier.add(Convolution2D(32, (3,3), activation='relu'))
classifier.add(MaxPool2D(pool_size = (2,2)))

classifier.add(Flatten())

classifier.add(Dense(units=128, activation='relu'))
classifier.add(Dropout(rate = 0.25))
classifier.add(Dense(units=128, activation='relu'))
classifier.add(Dropout(rate = 0.25))


classifier.add(Dense(units=1, activation='sigmoid'))
classifier.compile(optimizer = 'sgd', loss = 'binary_crossentropy', metrics=['accuracy'])

What I want to do is to run my images through the model, but only the convolutional steps. I am interested in the output of the Flattening process (i.e. get the features from the convolutional steps). What I am trying to achieve by doing this (end goal) is to run a classifier and clustering algorithm using the features from the convolutional layers.

Can someone help me in how I can get the output from the Flattening process in Keras?

Thanks in advance


(Matthijs) #2

The Keras FAQ shows how to do this: https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer