Home > Back-end >  CNN Model with constant accuracy in Python
CNN Model with constant accuracy in Python

Time:05-29

I am a beginner in Python and I have the following problem:

I need to create a CNN for music genre recognition with the dataset GTZAN, I followed tutorials online and came up with the following code:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, Input

X = X/255.0

#First Layer
Model = Sequential()
Model.add(Conv2D(64, (3, 3), input_shape = X.shape[1:]))
Model.add(Activation("relu"))
Model.add(MaxPooling2D(pool_size=(2, 2)))
Model.add(Dropout (0.2))

#Second Layer
Model.add(Conv2D(64, (3, 3)))
Model.add(Activation("relu"))
Model.add(MaxPooling2D(pool_size=(2, 2)))
Model.add(Dropout (0.2))

#Third Layer
Model.add(Flatten())
Model.add(Dense(64))
Model.add(Activation("relu"))
Model.add(Dropout (0.2))

#Output Layer
Model.add(Dense(1))
Model.add(Activation("softmax"))

#Compiling the Model
Model.compile(loss="categorical_crossentropy", #Suitable for datasets of 2 or more labels
              optimizer="adam",
              metrics=['accuracy']) #Calculates how often predictions equal labels.

Model.fit(X, y, batch_size=25, epochs=6, validation_split=0.1)

However when I run the model I have a constant accuracy over all epochs (0.1023 with the model above) and I cannot figure out why. I imagine that the problem must be from my code as otherwise the accuracy would at least differ slightly between each epochs.

Thank you in advance for your help!

CodePudding user response:

It doesn't make sense to use a softmax activation function on a dense layer of 1 unit, as it always returns an output of 1.0

You want the output of your model to be a probability of classes, so set the number of units in the final dense layer as the number of classes you have.

CodePudding user response:

Constant accuracy is probably a sign of constant output. Look up the downsides of accuracy as a measure. If 99% of your samples belong to the same class (e.g. “pop”), a network that always outputs “genre is pop” will get an accuracy of >99%.

  • Related