简体   繁体   English

良好的训练/验证准确度,但测试准确度差

[英]Good training/validation accuracy but poor test accuracy

Ive trained a model to classify 4 types of eye diseases using the VGG16 pretrained model.我训练了一个 model 使用 VGG16 预训练的 model 对 4 种眼部疾病进行分类。 I am fairly new to machine learning so didn't know what to make out of the results.我对机器学习还很陌生,所以不知道如何从结果中得出结论。 After training it for about 6 hours on 90,000 images:在 90,000 张图像上训练了大约 6 个小时后:

  • training accuracy kept increasing as well as the loss (went from roughly 2 to 0.8 ended with an accuracy of 88%)训练准确率和损失不断增加(从大约 2 到 0.8 以 88% 的准确率结束)

  • validation loss kept flucating between 1-2 per epoch (accuracy did improve to 85%) (I accidentally reran the cell so cant see the output)验证损失在每个 epoch 1-2 之间波动(准确度确实提高到 85%)(我不小心重新运行了单元,所以看不到输出)

After looking at the confusion matrix, it seems my test isn't performing well查看混淆矩阵后,我的测试似乎表现不佳

Image_height = 196
Image_width = 300
val_split = 0.2
batches_size = 10
lr = 0.0001
spe = 512
vs = 32
epoch = 10

#Creating batches
#Creating batches
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input,validation_split=val_split) \
    .flow_from_directory(directory=train_folder, target_size=(Image_height,Image_width), classes=['CNV','DME','DRUSEN','NORMAL'], batch_size=batches_size,class_mode="categorical",
                              subset="training")
validation_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input,validation_split=val_split) \
    .flow_from_directory(directory=train_folder, target_size=(Image_height,Image_width), classes=['CNV','DME','DRUSEN','NORMAL'], batch_size=batches_size,class_mode="categorical",
                              subset="validation")
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
                       .flow_from_directory(test_folder, target_size=(Image_height,Image_width), 
                         classes=['CNV','DME','DRUSEN','NORMAL'], batch_size=batches_size,class_mode="categorical")

#Function to create model. We will be using a pretrained model
def create():
  vgg16_model = keras.applications.vgg16.VGG16(input_tensor=Input(shape=(Image_height, Image_width, 3)),input_shape=(Image_height,Image_width,3), include_top = False)
  model = Sequential()
  model.add(vgg16_model)
  for layer in model.layers:
    layer.trainable = False
  model.add(Flatten())
  model.add(Dense(4, activation='softmax'))
  return model

model = create()
model.compile(Adam(lr=lr),loss="categorical_crossentropy",metrics=['accuracy'])

model.fit(train_batches, steps_per_epoch=spe,
                    validation_data=validation_batches,validation_steps=vs, epochs=epoch)

在此处输入图像描述

Any suggestions on what I can improve on so the confusion matrix isn't doing so poorly?关于我可以改进的任何建议,以便混淆矩阵做得不那么差? I also have the model saved if its possible to just retrain it with more layers.如果可能的话,我还保存了 model,只需用更多层重新训练它。

You don't train any layer except the last one.除了最后一层,您不训练任何层。 You need to set the training capability to the last few or add more layers.您需要将训练能力设置为最后几层或添加更多层。

Add添加

tf.keras.applications.VGG16(... weights='imagenet'... )

In your code, the weights are not pretrained on any set.在您的代码中,权重没有在任何集合上进行预训练。

The available options are explained here:此处解释了可用的选项:

https://www.tensorflow.org/api_docs/python/tf/keras/applications/VGG16 https://www.tensorflow.org/api_docs/python/tf/keras/applications/VGG16

A number of issues and recommendations.一些问题和建议。 You are using VGG16 model.您正在使用 VGG16 model。 That model has over 40 million trainable parameters. model 有超过 4000 万个可训练参数。 On a data set of 90,000 images your training time will be very long.在包含 90,000 张图像的数据集上,您的训练时间将非常长。 So I recommend you consider using the MobileNet model.所以我建议你考虑使用 MobileNet model。 It only has 4 million trainable parameters and is essentially just as accurate as VGG16.它只有 400 万个可训练参数,基本上和 VGG16 一样准确。 Documentation is [here.][1] Next irrespective of which model you use you should set the initial weights to the imagenet weights.文档在 [这里][1] 接下来,无论您使用哪个 model,您都应该将初始权重设置为 imagenet 权重。 Your model will start off trained on images.I find I get better results by making all layers in the model trainable.您的 model 将开始对图像进行训练。我发现通过使 model 中的所有层都可训练可以得到更好的结果。 Now you say your model reached an accuracy of 88%.现在你说你的 model 达到了 88% 的准确度。 I do not think that is very good.我不认为这很好。 I believe you need to achieve at least 95%.我相信你至少需要达到 95%。 You can do that by using an adjustable learning rate.您可以通过使用可调整的学习率来做到这一点。 The keras callback ReduceLROnPlateau makes doing that easy. keras 回调 ReduceLROnPlateau 使这变得容易。 Documentation is [here.][2] Set it up to monitor validation loss and reduce the learning rate if it fails to decrease on consecutive epochs.文档在 [here.][2] 设置它以监控验证损失并在连续时期未能降低学习率时降低学习率。 Next you want to save the model that has the lowest validation loss and use that to make predictions.接下来,您要保存具有最低验证损失的 model 并使用它来进行预测。 The Keras callback ModelCheckpoint can be set up to monitor validation loss and save the model with the lowest loss.可以设置 Keras 回调 ModelCheckpoint 来监控验证损失,并保存损失最低的 model。 Documentation is [here.][3].文档在[这里][3]。 Code below shows how to implement the MobileNet model for your problem and define the callbacks.下面的代码显示了如何针对您的问题实施 MobileNet model 并定义回调。 You will also have to make changes to the generator to use Mobilenet preprocessing and set target size to (224,224).您还必须更改生成器以使用 Mobilenet 预处理并将目标大小设置为 (224,224)。 Also I believe you are missing () around the pre-processing function Hope this helps..另外我相信你在预处理 function 周围缺少 () 希望这有帮助..

mobile = tf.keras.applications.mobilenet.MobileNet( include_top=False,
                                                           input_shape=(224, 224,3),
                                                           pooling='max', weights='imagenet',
                                                           alpha=1, depth_multiplier=1,dropout=.5)                                                          
x=mobile.layers[-1].output
x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
predictions=Dense (4, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=predictions)    
for layer in model.layers:
    layer.trainable=True
model.compile(Adamax(lr=lr), loss='categorical_crossentropy', metrics=['accuracy'])
checkpoint=tf.keras.callbacks.ModelCheckpoint(filepath=save_loc, monitor='val_loss', verbose=0, save_best_only=True,
    save_weights_only=False, mode='auto', save_freq='epoch', options=None)
lr_adjust=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=1, verbose=0, mode="auto",
    min_delta=0.00001,  cooldown=0,  min_lr=0) 
callbacks=[checkpoint, lr_adjust]


  [1]: http://httphttps://keras.io/api/applications/mobilenet/s://
  [2]: https://keras.io/api/callbacks/reduce_lr_on_plateau/
  [3]: https://keras.io/api/callbacks/model_checkpoint/

while adding layers to model you have to remove last dense layer of the model, as your model has four classes but vgg16 has 1000 classes so you have to remove last dense layer then add your own dense layers:在向 model 添加层时,您必须删除 model 的最后一个密集层,因为您的 model 有四个类,但 vgg16 有 1000 个密集层然后删除最后一个密集层,所以您有:

def create():
    vgg16_model = keras.applications.vgg16.VGG16(input_tensor=Input(shape=(Image_height, Image_width, 3)),input_shape=(Image_height,Image_width,3), include_top = False)

    model = Sequential()
    for layer in vgg16_model.layers[:-1]:
        model.add(layer)
    model.summary()

    for layer in model.layers:
        layer.trainable = False
    model.add(Flatten())
    model.add(Dense(4, activation='softmax'))

    return model

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

相关问题 训练准确度好但验证准确度差 - Good training accuracy but poor validation accuracy 测试准确性差,同时具有很好的训练和验证准确性 - Poor testing accuracy, while having very good training and validation accuracy 良好的训练准确度和验证准确度,但预测准确度较差 - Good training accuracy and validaiton accuracy but poor prediction accuracy 良好的测试准确性,但混淆矩阵结果较差 - Good test accuracy but poor confusion matrix results 良好的训练准确度和损失与验证的糟糕准确度 - Good accuracy and loss on training vs bad accuracy on validation Keras CNN训练精度不错,但是测试精度很低 - Keras CNN training accuracy is good but test accuracy is very low Keras - 绘制训练、验证和测试集准确性 - Keras - Plot training, validation and test set accuracy 分类 model 产生极低的测试准确度,尽管训练和验证准确度对多类分类有好处 - Classification model produces extremely low test accuracy, although training and validation accuracies are good for multiclass classification 训练精度高 测试精度差 - High train accuracy poor test accuracy 训练和验证分数很高,但测试准确性很差 - Train and validation score is high but very Poor Test Accuracy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM