1
votes

I'm trying to plot two things:

1) Output after applying each filter (Total 32 outputs in case of "Conv2D(32, (3,3)....)

2) Filter which is learned/generated while learning (Total 32 filters as mentioned above)

Below is my code and demo image is: Sample Image

First I've just read an image:

import cv2
import matplotlib.pyplot as plt
%matplotlib inline`
plt.imshow(face)
cv2.waitKey(0)

Ouput of above code

Then I created simple 1 layer CNN with Keras:

from keras.models import Sequential
from keras.layers import Conv2D
import numpy as np

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape = face.shape, padding='valid', activation = 'relu'))

face_batch = np.expand_dims(face, axis=0)
conv_face = model.predict(face_batch)

def visualize_face(face_batch):
    face = np.squeeze(face_batch, axis=0)
    print (face.shape)
    plt.imshow(face[:,:,31])

The above last line vary from 0 to 31 (total 32 filters) for whichever filter I want to visualize. Then final cell would be:

print(np.squeeze(conv_face, axis=0).shape)
visualize_face(conv_face)

Output of above code

So is this a correct way to visualize outputs of filter applied on image?

Now come to my second question:

I'm trying to plot all those 32 filters which are learned by a model while getting those outputs. So can anyone help me with my query?

Thank you in advance :)

1

1 Answers

1
votes

Have you figured out how to visualize yet? First of all, I'm a beginner in this but I'll try to answer your question. My two-cents worth is that you are visualizing the intermediate activation instead of filters. You can refer to this tutorial for more details for both your questions.