I have the following model (for example)
input_img = Input(shape=(224,224,1)) # size of the input image
x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)
I have several layers of such in my autoencoder model. I am particularly interested in the filters of the first layer. There are 64 filters each of size 3x3.
To get the filters, I tried using the following code:
x.layers[0].get_weights()[0]
but I am getting the error as follows:
AttributeError Traceback (most recent call last)
<ipython-input-166-96506292d6d7> in <module>()
4 x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)
5
----> 6 x.layers[0].get_weights()[0]
AttributeError: 'Tensor' object has no attribute 'layers'
I am not using the sequential model. My model will be formed using the following command after several such layers.
model = Model()
I am new to CNN and I don't even know if the get_weights function can help me get filters value. How do I get value of filters?
At the moment your code is calling the layers
function on a layer definition itself.
The model first needs to be compiled and then you can use the layers
function on the model to retrieve the weights of the specific layer.
In your case:
weights = model.layers[1].get_weights()
will give you the set of weights of the 1st convolutional layer
Which you can use after compiling the model:
model = Model(inputs=input_img, output=b)
Where b
refers to the last layer in your model.