简体   繁体   English

如何提高 CNN model 中的验证准确度

[英]How to increase the validation accuracy in CNN model

I want to build a CNN model to classify down syndrome faces from normal, then classify gender by another model.我想构建一个 CNN model 来对正常的唐氏综合症面孔进行分类,然后通过另一个 model 对性别进行分类。 I've tried to change the number of layers, nodes, epochs, optimizers.我尝试更改层数、节点数、时期数、优化器数。 Also, I tried with colored images and grayscale.另外,我尝试了彩色图像和灰度。 The data set is 799 images including normal and down syndrome.该数据集是 799 张图像,包括正常和唐氏综合症。 This is my code这是我的代码

model.add(Conv2D(filters=16, kernel_size=(5,5), activation="relu",
                 input_shape=X_train[0].shape))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) 
model.add(Dropout(0.2))

model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.3))

model.add(Conv2D(filters=64, kernel_size=(5,5), activation="relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.3))

model.add(Conv2D(filters=64, kernel_size=(5,5), activation="relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.2))

model.add(Flatten())

#Two dense layers and then output layer
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5)) #Using dropouts to make sure that 
                        #the model isn't overfitting

model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))


model.add(Dense(2, activation='softmax'))

I've tried to change the last activation layer from softmax to sigmoid and vise versa with no success.我试图将最后一个激活层从 softmax 更改为 sigmoid,反之亦然,但没有成功。 The size of the input images is 200x200输入图像的大小为 200x200

Model: "sequential_4"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_16 (Conv2D)          (None, 196, 196, 16)      416       
                                                                 
 batch_normalization_24 (Bat  (None, 196, 196, 16)     64        
 chNormalization)                                                
                                                                 
 max_pooling2d_16 (MaxPoolin  (None, 98, 98, 16)       0         
 g2D)                                                            
                                                                 
 dropout_24 (Dropout)        (None, 98, 98, 16)        0         
                                                                 
 conv2d_17 (Conv2D)          (None, 94, 94, 32)        12832     
                                                                 
 batch_normalization_25 (Bat  (None, 94, 94, 32)       128       
 chNormalization)                                                
                                                                 
 max_pooling2d_17 (MaxPoolin  (None, 47, 47, 32)       0         
 g2D)                                                            
                                                                 
 dropout_25 (Dropout)        (None, 47, 47, 32)        0         
                                                                 
 conv2d_18 (Conv2D)          (None, 43, 43, 64)        51264     
                                                                 
 batch_normalization_26 (Bat  (None, 43, 43, 64)       256       
 chNormalization)                                                
                                                                 
 max_pooling2d_18 (MaxPoolin  (None, 21, 21, 64)       0         
 g2D)                                                            
                                                                 
 dropout_26 (Dropout)        (None, 21, 21, 64)        0         
                                                                 
 conv2d_19 (Conv2D)          (None, 17, 17, 64)        102464    
                                                                 
 batch_normalization_27 (Bat  (None, 17, 17, 64)       256       
 chNormalization)                                                
                                                                 
 max_pooling2d_19 (MaxPoolin  (None, 8, 8, 64)         0         
 g2D)                                                            
                                                                 
 dropout_27 (Dropout)        (None, 8, 8, 64)          0         
                                                                 
 flatten_4 (Flatten)         (None, 4096)              0         
                                                                 
 dense_12 (Dense)            (None, 256)               1048832   
                                                                 
 batch_normalization_28 (Bat  (None, 256)              1024      
 chNormalization)                                                
                                                                 
 dropout_28 (Dropout)        (None, 256)               0         
                                                                 
 dense_13 (Dense)            (None, 128)               32896     
                                                                 
 batch_normalization_29 (Bat  (None, 128)              512       
 chNormalization)                                                
                                                                 
 dropout_29 (Dropout)        (None, 128)               0         
                                                                 
 dense_14 (Dense)            (None, 2)                 258       
                                                                 
=================================================================
Total params: 1,251,202
Trainable params: 1,250,082
Non-trainable params: 1,120
_________________________________________________________________

model.compile(optimizer='Adam',  loss='binary_crossentropy', metrics=['accuracy'])
# split train and VALID data
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.15)

I want to increase the accuracy at least to 70 but the highest score I reach is 47%我想将准确率至少提高到 70,但我达到的最高分数是 47%

history = model.fit(X_train, y_train, epochs=50, validation_data=(X_valid, y_valid), batch_size=64)

Epoch 1/50
5/5 [==============================] - 23s 4s/step - loss: 0.9838 - accuracy: 0.5390 - val_loss: 0.6931 - val_accuracy: 0.4800
Epoch 2/50
5/5 [==============================] - 21s 4s/step - loss: 0.8043 - accuracy: 0.6348 - val_loss: 0.7109 - val_accuracy: 0.4800
Epoch 3/50
5/5 [==============================] - 21s 4s/step - loss: 0.6745 - accuracy: 0.6915 - val_loss: 0.7554 - val_accuracy: 0.4800
Epoch 4/50
5/5 [==============================] - 21s 4s/step - loss: 0.6429 - accuracy: 0.7589 - val_loss: 0.8261 - val_accuracy: 0.4800
Epoch 5/50
5/5 [==============================] - 21s 4s/step - loss: 0.5571 - accuracy: 0.8014 - val_loss: 0.9878 - val_accuracy: 0.4800

Is there any way to increase it more?有什么办法可以增加更多吗? and how do I combine two models?以及如何组合两个模型? Any help will be appreciated.任何帮助将不胜感激。 Thank you very much.非常感谢。

Try Image Augmentation .尝试图像增强 I mean;我是说; it's pretty clear the model is overfitting the data很明显 model 过度拟合数据

Maybe even change the train_test_split ratio(Increase It.)甚至可能改变train_test_split比率(增加它。)

I think one of 2 things is going on.我认为发生了两件事之一。 The training data would point to over fitting but given the amount of dropout you have in the model I would not suspect that is the case.训练数据会指向过度拟合,但考虑到 model 中的辍学量,我不会怀疑是这种情况。 I think it may be that the probability distribution of the training data is significantly different from that of the validation data.我认为可能是训练数据的概率分布与验证数据的概率分布明显不同。 That can happen if you have to few training samples.如果您的训练样本很少,就会发生这种情况。 So how many training samples do you have in each of your 2 classes?那么,您的 2 个班级中的每个班级有多少个训练样本? If less than say 120 samples per class use image augmentation to create more training samples.如果每个 class 少于 120 个样本,则使用图像增强来创建更多训练样本。 How did you generate the validation images?您是如何生成验证图像的? If you have seperate validation images it is best to combine the train set with the validatioon set then use sklearn train_test_split to randomly split the combined data into train and validation sets.如果您有单独的验证图像,最好将训练集与验证集结合起来,然后使用 sklearn train_test_split 将组合数据随机拆分为训练集和验证集。 Note: only use augmentation for the training set not the validation set.注意:只对训练集使用扩充,而不是验证集。 I also recommend you use the Keras callback Reduce learning rate on plateau to implement an adjustable learning rate.我还建议您使用 Keras 回调在高原降低学习率以实现可调节的学习率。 Documentation is here.文档在这里。 Code below shows the settings I use for that下面的代码显示了我使用的设置

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

Also recommend using Keras callback Early Stopping, documentation is here.还推荐使用 Keras 回调提前停止,文档在这里。 Code below shows my implementation for that下面的代码显示了我的实现

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

In model.fit include the code在 model.fit 中包含代码

history=model.fit(.....  callbacks[estop, rlronp])

Set the number of epoch to run to a fairly large value.将要运行的 epoch 数设置为相当大的值。

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

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