Reputation: 63
I have the cnn model code.
classifier = Sequential()
classifier.add(Convolution2D(32,3,3, input_shape =
(256,256,3),activation = "relu"))
classifier.add(MaxPooling2D(pool_size = (2,2)))
So now i need to find what values the 32 filters were initialized with ? Any code that helps in printing the values of the filters
Upvotes: 0
Views: 197
Reputation: 314
Get the corresponding layer from the model
layer = classifier.layers[0] # 0th layer is the convolution in your architecture
There will be two variables for each convolution layer (Filter kernels and Bias). Get the corresponding one
filters = layer.weights[0] # kernel is the 0th index
Now filters contain the values you are looking for and it is a tensor. To get the values of the tensor, use get_value() function of Keras backend
import keras.backend as K
print(K.get_value(wt))
This will print an array of shape (3, 3, 3, 32) which translates to 32 filters of kernel size 3x3 for 3 channels.
Upvotes: 1
Reputation: 745
Here is the default keras Conv2d initialization : kernel_initializer='glorot_uniform'
(or init='glorot_uniform'
for older version of keras).
You can look at what this initializer does here : Keras initializers
Finally, here is one way to access the weights of your first layer :
classifier = Sequential()
classifier.add(Convolution2D(32,3,3, input_shape =
(256,256,3),activation = "relu"))
classifier.add(MaxPooling2D(pool_size = (2,2)))
first_layer = classifier.layers[0]
print(first_layer.get_weights()) # You may need to process this output tensor to get a readable output and not just a raw tensor
Upvotes: 2