keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.94k stars 19.46k forks source link

Cov1d plot Visualization of filters #14251

Closed Kavchch closed 3 years ago

Kavchch commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Thank you so much..Its great help.

Kavchch commented 4 years ago

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

hexi03 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Thank you so much.

sushreebarsa commented 3 years ago

@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!