Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cov1d plot Visualization of filters #14251

Closed
Kavchch opened this issue Oct 18, 2020 · 10 comments
Closed

Cov1d plot Visualization of filters #14251

Kavchch opened this issue Oct 18, 2020 · 10 comments
Assignees
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@Kavchch
Copy link

Kavchch commented Oct 18, 2020

I have gone through the code for plotting of weights in GitHub. Issue #5573
But when I tried to implement same thing in my code ,I'm not able to plot it. Please help me in this .I want to extract the info from layer1 what filters has learnt and plot it .Thanks in advance.
Below is my code :
model_cmm.load_weights("results/model1_mfcc_mel_chro_final_cmm.h5")
model_cmm = Sequential()
model_cmm. Add(Conv1D(filters=512,kernel_size=5,padding='same',activation='relu',input_shape=(180,1)))

def plot_filters(layer, x, y):

filters = layer.get_weights()
print(filters)
fig = plt.figure()
for j in range(len(filters)):
ax = fig.add_subplot(y,x,j+1)
ax.matshow(filters[j][0], cmap = matplotlib.cm.binary)
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plt.tight_layout()
plt.show()
return plt
plot_filters(model_cmm.layers[0], 1, 512)

@hexi03
Copy link

hexi03 commented Oct 19, 2020

if I understood you correctly, you need something like this:

model_cmm = Sequential()
model_cmm.add(Conv1D(filters=5,kernel_size=5,padding='same',activation='relu',input_shape=(180,5)))
model_cmm.load_weights("results/model1_mfcc_mel_chro_final_cmm.h5")
def plot_filters(layer, x, y):
filters = layer.get_weights()
fig = plt.figure()
sh=filters[0].shape
for i in range(sh[-1]):
ax = fig.add_subplot(x,y,i+1)
ax.matshow(filters[0][:,:,i].reshape(sh[0],sh[1]), cmap = matplotlib.cm.binary)
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plt.tight_layout()
plt.show()
return plt
plot_filters(model_cmm.layers[0], 1, 5)

I changed the numbers because mpl can't display 512 matrices in Colab and the weight matrices (5,1) look strange
Final result:
изображение
I hope this helps you

@Kavchch
Copy link
Author

Kavchch commented Oct 20, 2020

Thank you so much..Its great help.

@Kavchch Kavchch closed this as completed Oct 20, 2020
@Kavchch Kavchch reopened this Oct 20, 2020
@Kavchch
Copy link
Author

Kavchch commented Oct 22, 2020

Is it possible to plot these learnt filters from 1st convolutional layer versus frequency?

@hexi03
Copy link

hexi03 commented Oct 22, 2020

If I understand your purpose correctly, then the only way I know is to take the weights of the desired layer from the first model and transfer them to the layer of another model, consisting of the data preparation layers and the layer you need (similar to your trained model). (using the get_weights () and set_weights () functions of the layers in model.layers) Then you can use predict () to plot

@Kavchch
Copy link
Author

Kavchch commented Oct 22, 2020

Actually I would like to plot cumulative frequency response of 1st CNN layer from filters, like mentioned in the paper "On Learning to Identify Genders from Raw Speech Signal using CNNs".Thanks for your quick response. It will be great help if you solve this .

@hexi03
Copy link

hexi03 commented Oct 22, 2020

I don't know what layers are present in your model before the convolution layer. Could you please lay out the model generation code up to the convolution layer (including it)

@Kavchch
Copy link
Author

Kavchch commented Oct 22, 2020

Actually I'm passing feature vector of length(40,1) is passed through conv1d of 512 filters and I'm using such 3 1-D Convolution layers and 3 Dense layers. So i have such 5000 samples(40,1) which is my entire dataset. So now I would like to know what CNN has learnt .. So I would like to plot Cumulative frequency response.

@hexi03
Copy link

hexi03 commented Oct 23, 2020

This code displays 40 plots of dependences for each element of the filter vector on frequency, but these plots do not look informative. You supply a vector of frequencies and the corresponding vector of vectors for the neural network, as well as the layer you need from model.layers []. I hope this is what you need

def visualise(frequency,frequency_vectors,layer):
print(frequency_vectors.shape)
lay0 = Input(shape=(40,1))
lay1=Conv1D(512,10,activation="tanh")(lay0)
model=Model(lay0,lay1)
#model.summary()
model.compile()
out=model.predict(frequency_vectors)
print(out.shape)
for j in range(out.shape[1]):#grid
for i in range(512):#filter
plt.plot(frequency,out[:,j,i])
plt.show()
#visualise(list(range(100)),np.random.random(100*40).reshape(100,40,1),[])

@Kavchch
Copy link
Author

Kavchch commented Oct 25, 2020

Thank you so much.

@saikumarchalla saikumarchalla added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label Nov 10, 2020
@sushreebarsa sushreebarsa self-assigned this Jul 9, 2021
@sushreebarsa
Copy link
Collaborator

@Kavchch Moving this issue to closed status as there has been no recent activity.In case you still face the error please create a new issue,we will get you the right help.Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

4 participants