简体   繁体   English

如何提高我的 CNN 模型的准确性

[英]How Can I Increase My CNN Model's Accuracy

I built a cnn model that classifies facial moods as happy, sad, energetic and neutral faces.我建立了一个 cnn model,它将面部情绪分类为快乐、悲伤、精力充沛和中性的面孔。 I used Vgg16 pre-trained model and freezed all layers.我使用 Vgg16 预训练 model 并冻结所有层。 After 50 epoch of training my model's test accuracy is 0.65 validatation loss is about 0.8.经过 50 个 epoch 的训练后,我的模型的测试精度为 0.65,验证损失约为 0.8。

My train data folder has 16000(4x4000), validation data folder has 2000(4x500) and Test data folder has 4000(4x1000) rgb images.我的训练数据文件夹有 16000(4x4000),验证数据文件夹有 2000(4x500),测试数据文件夹有 4000(4x1000) rgb 图像。

1)What is your suggestion to increase the model accuracy? 1)您对提高 model 精度有何建议?

2)I have tried to do some prediction with my model, predicted class is always same. 2)我试图用我的 model 做一些预测,预测 class 总是一样的。 What can cause the problem?什么会导致问题?

What I Have Tried So Far?到目前为止我尝试了什么?

  1. Add dropout layer (0.5)添加丢弃层 (0.5)
  2. Add Dense (256, relu) before last layer在最后一层之前添加 Dense (256, relu)
  3. Shuff the train and validation datas.洗牌火车和验证数据。
  4. Decrease the learning rate to 1e-5将学习率降低到 1e-5

But I could not the increase validation and test accuracy.但我无法提高验证和测试的准确性。

My Codes我的代码

train_src = "/content/drive/MyDrive/Affectnet/train_class/"
val_src = "/content/drive/MyDrive/Affectnet/val_class/"
test_src="/content/drive/MyDrive/Affectnet/test_classs/"

train_datagen = tensorflow.keras.preprocessing.image.ImageDataGenerator(
      rescale=1./255, 
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
    
      )

train_generator = train_datagen.flow_from_directory(
        train_src,
        target_size=(224,224 ),
        batch_size=32,
        class_mode='categorical',
        shuffle=True
        )

validation_datagen = tensorflow.keras.preprocessing.image.ImageDataGenerator(
        rescale=1./255
        )

validation_generator = validation_datagen.flow_from_directory(
        val_src,
        target_size=(224, 224),
        batch_size=32,
        class_mode='categorical',
        shuffle=True
        )
conv_base = tensorflow.keras.applications.VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(224, 224, 3)
                  )
for layer in conv_base.layers:
  layer.trainable = False

model = tensorflow.keras.models.Sequential()

# VGG16 is added as convolutional layer.
model.add(conv_base)

# Layers are converted from matrices to a vector.
model.add(tensorflow.keras.layers.Flatten())

# Our neural layer is added.
model.add(tensorflow.keras.layers.Dropout(0.5))
model.add(tensorflow.keras.layers.Dense(256, activation='relu'))

model.add(tensorflow.keras.layers.Dense(4, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=tensorflow.keras.optimizers.Adam(lr=1e-5),
              metrics=['acc'])
history = model.fit_generator(
      train_generator,
      epochs=50,
      steps_per_epoch=100,
      validation_data=validation_generator,
      validation_steps=5,
      workers=8
      )

Loss and accuracy损失和准确性

Well a few things.好吧,有几件事。 For training set you say you have 16,0000 images.对于训练集,你说你有 16,0000 张图像。 However with a batch size of 32 and steps_per_epoch= 100 then for any given epoch you are only training on 3,200 images.然而,如果批量大小为 32 且 steps_per_epoch= 100,则对于任何给定的时期,您仅在 3,200 张图像上进行训练。 Similarly you have 2000 validation images, but with a batch size of 32 and validation_steps = 5 you are only validating on 5 X 32 = 160 images.同样,您有 2000 张验证图像,但批量大小为 32 且 validation_steps = 5,您仅在 5 X 32 = 160 张图像上进行验证。 Now Vgg is an OK model but I don't use it because it is very large which increases the training time significantly and there are other models out there for transfer learning that are smaller and even more accurate.现在 Vgg 是一个 OK model 但我不使用它,因为它非常大,这会显着增加训练时间,并且还有其他模型用于迁移学习,它们更小但更准确。 I suggest you try using EfficientNetB3.我建议您尝试使用 EfficientNetB3。 Use the code使用代码

conv_base = tensorflow.keras.applications.EfficientNetB3(weights='imagenet',
                  include_top=False,
                  input_shape=(224, 224, 3)
                  pooling='max'
                  )

with pooling='max' you can eliminate the Flatten layer.使用 pooling='max' 您可以消除 Flatten 层。 Also EfficientNet models expect pixels in the range 0 to 255 so remove the rescale=1/255 in your generators.此外,EfficientNet 模型期望像素在 0 到 255 范围内,因此请删除生成器中的 rescale=1/255。 Next thing to do is to use an adjustable learning rate.接下来要做的是使用可调节的学习率。 This can be done using Keras callbacks.这可以使用 Keras 回调来完成。 Documentation for that is here.相关文档在这里。 You want to use the ReduceLROnPlateau callback.您想要使用 ReduceLROnPlateau 回调。 Documentation for that is here.相关文档在这里。 Set it up to monitor validation loss.将其设置为监控验证损失。 My suggested code for that is below我建议的代码如下

rlronp=tf.keras.callbacks.ReduceLROnPlateau(monitor="val_loss",factor=0.5,
                                            patience=1, verbose=1)

I also recommend you use the callback EarlyStopping.我还建议您使用回调 EarlyStopping。 Documentation for that is here.相关文档在这里。 . . My recomended code for that is shown below我推荐的代码如下所示

estop=tf.keras.callbacks.EarlyStopping( monitor="val_loss", patience=4, verbose=1,
                                        restore_best_weights=True)

Now in model.fit include现在在 model.fit 中包含

callbacks=[rlronp, estop]

set your learning rate to.001.将你的学习率设置为 .001。 Set epochs=50.设置纪元 = 50。 The estop callback if tripped will return your model loaded with the weights from the epoch with the lowest validation loss.如果被触发,estop 回调将返回您的 model,其中加载了验证损失最低的时期的权重。 I notice you have the code我注意到你有代码

for layer in conv_base.layers:
  layer.trainable = False

I know the tutorials tell you to do that but I get better results leaving it trainable and I have done this on hundreds of models.我知道教程告诉你这样做,但我得到了更好的结果,让它可以训练,我已经在数百个模型上这样做了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM