I have written a CNN that takes in MFCC spectrograms and is meant to classify the images into five different classes. I trained the model for 30 epochs and after the first epoch, no metrics change. Could it be a problem with imbalanced classification, and if so, how would I bias the model for the dataset, if possible? Below is the data generator code, the model definition, and the outputs. The original model had two additional layers however, I started tweaking things when I was trying to troubleshoot the issue
Data Generator Definition:
path = 'path_to_dataset'
CLASS_NAMES = ['belly_pain', 'burping', 'discomfort', 'hungry', 'tired']
CLASS_NAMES = np.array(CLASS_NAMES)
BATCH_SIZE = 32
IMG_HEIGHT = 150
IMG_WIDTH = 150
# 457 is the number of images total
STEPS_PER_EPOCH = np.ceil(457/BATCH_SIZE)
img_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, validation_split=0.2, horizontal_flip=True, rotation_range=45, width_shift_range=.15, height_shift_range=.15)
train_data_gen = img_generator.flow_from_directory( directory=path, batch_size=BATCH_SIZE, shuffle=True, target_size=(IMG_HEIGHT, IMG_WIDTH), classes = list(CLASS_NAMES), subset='training', class_mode='categorical')
validation_data_gen = img_generator.flow_from_directory( directory=path, batch_size=BATCH_SIZE, shuffle=True, target_size=(IMG_HEIGHT, IMG_WIDTH), classes = list(CLASS_NAMES), subset='validation', class_mode='categorical')
Model Definition:
EPOCHS = 30
model = Sequential([
Conv2D(128, 3, activation='relu',
input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
MaxPooling2D(),
Flatten(),
Dense(512, activation='sigmoid'),
Dense(1)
])
opt = tf.keras.optimizers.Adamax(lr=0.001)
model.compile(optimizer=opt,
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
First 5 Epochs:
Epoch 1/30
368/368 [==============================] - 371s 1s/step - loss: 0.6713 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 2/30
368/368 [==============================] - 235s 640ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 3/30
368/368 [==============================] - 233s 633ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 4/30
368/368 [==============================] - 236s 641ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 5/30
368/368 [==============================] - 234s 636ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Last Five Epochs:
Epoch 25/30
368/368 [==============================] - 231s 628ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 26/30
368/368 [==============================] - 227s 617ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 27/30
368/368 [==============================] - 228s 620ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 28/30
368/368 [==============================] - 234s 636ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 29/30
368/368 [==============================] - 235s 638ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
Epoch 30/30
368/368 [==============================] - 234s 636ms/step - loss: 0.5004 - accuracy: 0.8000 - val_loss: 0.5004 - val_accuracy: 0.8000
You are trying to achieve a classification task with 4 classes but your last layer only contain one neuron.
It should be a dense layer with 4 neurons and a softmax activation:
Dense(4, activation="softmax")
You need to also change the loss function accordingly to classification loss like for example categorical_crossentropy
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.