0

I have created the following simple CNN in Keras (borrowed from a DeepLizard tutorial).

model = Sequential([
        Conv2D(filters = 10, kernel_size = (3, 3), activation = 'relu', padding = 'same', input_shape = (320, 320, 3)),
        MaxPool2D(pool_size = (2, 2), strides = 2),
        Conv2D(filters = 10, kernel_size = (3, 3), activation = 'relu', padding = 'same'),
        MaxPool2D(pool_size = (2, 2), strides = 2),
        Flatten(),
        Dense(20, activation = 'softmax'),
        ])

model.summary()

model.compile(optimizer = Adam(lr = 0.0001), loss = 'categorical_crossentropy', metrics =['accuracy'])

model.fit(x = train_batches, validation_data = valid_batches, epochs = 10, verbose = 2)


predictions = model.predict(x = test_batches, verbose = 0)

As you can see, I am saving the predictions generated by the model to a dataframe named "predictions". But I am also interested in saving the outputs for each of the MaxPool2D layers, the Conv2D layer, and the flatten layer as well. Is there a way that I can save the outputs of those layers to dataframes/lists as well? Is there a functionality for this in Keras?

Thank you!

Ben
  • 123
  • 6
  • The output of those layers is feature maps. So, you want to save feature maps? Those can be saved as a list or array. – Innat Feb 25 '21 at 23:53
  • Yes, I am looking to save the feature maps for each input I present to the model. Also, shouldn't the flattened layer just be a single array of n length equaling the size of the flattened layer? – Ben Feb 26 '21 at 05:03
  • Does this answer your question? [Keras, How to get the output of each layer?](https://stackoverflow.com/questions/41711190/keras-how-to-get-the-output-of-each-layer) – Alex Metsai Feb 26 '21 at 08:47

2 Answers2

0

You can use model.get_layer() function to extract any layer of your model. Visit the documentation here: https://keras.io/api/models/model/#getlayer-method

0

Thank you for your responses. They led me in the right direction. Here is the solution I ended up utilizing. I recreated the model, but configured the predictions to output the desired layer (in this case, "conv2d", the first convolutional layer). This produces a 4-D array as an output, where the 1st dimension corresponds to the input, the 2nd and 3rd dimensions are the two dimensions of a filter's outputted feature map, and the 4th dimension corresponds to the n-filters being used in that layer (in this case, the 4th dimension would be '10'). My next quest is to find a way to split that 4 dimensional array into separate 3-dimensional arrays, where each new array corresponds to a distinct filter. In this case, I would be looking for 10 3-dimensional arrays, one for each of the filters used in the first convolutional layer.

from keras.models import Model

model = Sequential([
        Conv2D(filters = 10, kernel_size = (3, 3), activation = 'relu', padding = 'same', input_shape = (320, 320, 3)),
        MaxPool2D(pool_size = (2, 2), strides = 2),
        Conv2D(filters = 10, kernel_size = (3, 3), activation = 'relu', padding = 'same'),
        MaxPool2D(pool_size = (2, 2), strides = 2),
        Flatten(),
        Dense(20, activation = 'softmax'),
        ])

layer_name = 'conv2d'
intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(valid_batches)

Ben
  • 123
  • 6