Keras beginner: CNN applied to spectrograms. Help building the network.

1,206 views
Skip to first unread message

Fran Carranza

unread,
Jan 17, 2018, 1:41:38 AM1/17/18
to Keras-users
Hi, I'm very new to this topics. I'm trying to build a CNN in order to feed with spectrograms. A spectrogram is a matrix where the x axis is time and y axis, frequency.
For example my data is 128 mel components (frequency) and 130 timesteps.

To summarize the instructions for the network is:
The network consisted of two convolutional layers with 128 filters span-
ning 6 timesteps and the entire frequency range, yielding one-dimensional
convolutions. A max-pooling layer with a pooling window of 4 timesteps
followed the first convolutional layer, and one with a pooling window of 5
timesteps followed the second convolutional layer. A fully connected hidden
layer with 400 units was stacked on top of this, and finally the output layer
had a number of units equaling the number of latent factors to predict.

That is expanded in this reddit's thread.



I'm currently testing with a tiny portion of the dataset just to test if the network is properly settled.

My data:
  • 80 Spectrograms with 128 mel frequencies and 130 time frames
I reshaped the data to match de 2d Convolution requirements. So my input_shape is (128, 130, 1)

K.set_image_data_format('channels_first')
input_shape
= (128, 130, 1)
n_filters
= 128
num_classes
= 50


And the network is built like this:
model = Sequential()
model
.add(Conv2D(n_filters, 4, padding='valid', strides=1,
                 input_shape
=input_shape))
model
.add(Activation('relu'))
model
.add(Conv2D(n_filters, 5))
model
.add(Activation('relu'))
model
.add(MaxPooling2D(pool_size=(4,1)))
model
.add(Dropout(0.25))


model
.add(Flatten())
model
.add(Dense(400))
model
.add(Activation('relu'))
model
.add(Dropout(0.5))
model
.add(Dense(num_classes))
model
.add(Activation('softmax'))

Please tell me if the network parameters I've filled match with the above description.


Also, when I excecute the last piece of code I get this error:
ValueError: ('The specified size contains a dimension with value <= 0', (-23400, 400))
I also read this answer https://groups.google.com/forum/#!topic/keras-users/vWG8cjCip8Q, but I can't get it to work.


Thanks for the support!

Fran Carranza

unread,
Jan 17, 2018, 10:50:27 AM1/17/18
to Keras-users
model.add(Conv2D(n_filters, 4, padding='valid', strides=1,
                 input_shape
=input_shape))

I've found that the second number (kernel_size) must be 2 dimensional. If I put (4,1), now it compiles!

But I think that something is still wrong. The model scores almost 0 of accuracy.
Message has been deleted

baptist...@gmail.com

unread,
Jan 23, 2018, 4:54:34 AM1/23/18
to Keras-users
Keras has a Conv1D layer that is more appropriate to your particular task; you're doing 2D convolutions on a (130,1) vector. Also, make sure that your backend is using the "channel_first" data format. Theano does, Tensorflow, as far as I know, does not. Also, your strides for a 2D layer should also be a tuple. Finally, the number of filters in each conv layer does not need to be 128. It can be anything you want, it just changes the complexity of the network. Good luck!
Reply all
Reply to author
Forward
0 new messages