[英]Keras CNN model for image classification does not generalize well
I want to implement a model in keras for sentiment classification(anger or non anger) based on spectograms. 我想在keras中实现基于谱图的情绪分类(愤怒或非愤怒)的模型。 I have generated the spectograms using the audio dataset from Friends.
我使用Friends的音频数据集生成了光谱图。 Each spectogram has a length of 8 seconds.
每个谱图的长度为8秒。 In total, I have 9117 train samples, 1006 validation samples and 2402 test samples.
总共有9117次列车样本,1006份验证样本和2402份测试样本。
I use a relatively simple CNN architecture and I tried different combinations of it + optimizer + learning rate + batch size but none of the results seem to generalize well...The loss decreases nicely till a certain point but the validation loss increases by each epoch. 我使用了一个相对简单的CNN架构,我尝试了它的不同组合+优化器+学习率+批量大小但是没有一个结果似乎很好地推广...损失很好地减少直到某一点但是每个时期的验证损失增加。
This is the model I am using: 这是我使用的模型:
model = Sequential()
model.add(Convolution2D(filters=32, kernel_size=3, strides=1,input_shape=input_shape, activation='relu', padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(filters=64, kernel_size=3, strides=1, activation='relu', padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Convolution2D(filters=128, kernel_size=3, strides=1, activation='relu', padding="same"))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(classes, activation='sigmoid')) #output layer
This is how I load the images: 这是我加载图像的方式:
img_rows = 120
img_cols = 160
train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
SPECTOGRAMS_DIRECTORY + TRAIN_SUBDIR,
target_size=(img_cols, img_rows),
batch_size=batch_size,
class_mode='binary')
validation_generator = validation_datagen.flow_from_directory(
SPECTOGRAMS_DIRECTORY + VALIDATION_SUBDIR,
target_size=(img_cols, img_rows),
batch_size=batch_size,
class_mode='binary')
test_generator = test_datagen.flow_from_directory(
SPECTOGRAMS_DIRECTORY + TEST_SUBDIR,
target_size=(img_cols, img_rows),
batch_size=1,
class_mode='binary',
shuffle=False)
input_shape=(img_cols, img_rows, channels)
opt = SGD(lr=0.001)
model.compile(loss='binary_crossentropy',
optimizer=opt,
metrics=['accuracy'])
history = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size,
verbose=2)
##EVALUATE
print("EVALUATE THE MODEL...")
score = model.evaluate_generator(generator=validation_generator,
steps=nb_validation_samples // batch_size)
The spectograms look like this: 视图看起来像这样:
As I said, I tried using different combinations of batch size (16,32,64), SGD with 0.001 learning rate, Adam with 0.0001 learning rate, but for each combination the training loss goes down while the validation loss goes up. 正如我所说,我尝试使用不同的批量大小组合(16,32,64),SGD与0.001学习率,亚当与0.0001学习率,但对于每种组合,训练损失下降而验证损失上升。
Model seems to be over-fitting. 模型似乎过于贴合。 You can try the below approaches to overcome this issue.
您可以尝试以下方法来克服此问题。
If possible try to gather more data or you can use data augmentation techniques to increase the number of samples. 如果可能,尝试收集更多数据,或者您可以使用数据增强技术来增加样本数量。
You can use dropout in Keras to reduce the over-fitting. 您可以在Keras中使用dropout来减少过度拟合。 (Looks like you have already added dropout, you can try tuning the values)
(看起来你已经添加了dropout,你可以尝试调整值)
Thank you 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.