Closed Kavchch closed 3 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
Thank you so much..Its great help.
Is it possible to plot these learnt filters from 1st convolutional layer versus frequency?
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
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 .
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)
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.
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.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()
Thank you so much.
@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!
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)