繁体   English   中英

机器学习 model 在测试数据上的表现比验证数据差

[英]Machine Learning model performs worse on test data than validation data

我对机器学习很陌生。

首先,我想训练一个 model 来对猫和狗的图片进行分类。

我遇到的问题是,当我训练我的 model 时,它在训练数据和验证数据上给了我(大约)80-85% 的准确度。 损失非常低,验证数据和训练数据的损失都在 0.4 - 0.5 左右。 因为这些数字非常相似,我怀疑我没有过度拟合的问题,对吧?

但是,当我用数据集中的图片(以前从未见过)测试我的 model 时,准确率大约是 70%-73%。 所以显着降低。 我找不到任何关于为什么会这样的信息。 而且,正如我所说,我怀疑过度拟合不是问题,但由于我是初学者,我不太确定。

我的 model 看起来像这样(我在 python 中使用 tensorflow):

model = Sequential([
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same', input_shape=(224,224,3), kernel_initializer="he_uniform"),
    MaxPool2D(pool_size=(2, 2)),
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same', kernel_initializer="he_uniform"),
    MaxPool2D(pool_size=(2, 2)),
    Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding = 'same', kernel_initializer="he_uniform", kernel_regularizer=l2(.001)),
    MaxPool2D(pool_size=(2, 2)),
    Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding = 'same', kernel_initializer="he_uniform", kernel_regularizer=l2(.001)),
    MaxPool2D(pool_size=(2, 2)),
    Flatten(),
    Dense(units = 128, activation='relu'),
    Dropout(.5),
    Dense(units=2, activation='softmax')
])

Trainable params: 3,471,810
Non-trainable params: 0

优化器,损失:

model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

这是我使用的数据集: https://www.kaggle.com/tongpython/cat-and-dog我使用 3000 张图像进行训练(1500 只狗和 1500 只猫),1000 张用于验证,1000 张用于测试。 没有重复(因此验证集中没有图像,它们也在训练集中等等)。

我像这样预处理图像(并且我还使用数据增强):

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255, horizontal_flip=True, vertical_flip=True, width_shift_range=.2, height_shift_range=.2) \
    .flow_from_directory(directory=training_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=64)

valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255, horizontal_flip=True, vertical_flip=True, width_shift_range=.2, height_shift_range=.2) \
    .flow_from_directory(directory=validation_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=64)

test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=test_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=64, shuffle=False)

编辑:解决了这个问题。 我的错误是我没有以完全相同的方式预处理训练、验证和测试数据,因为我误解了一个参数。 感谢所有帮助过我的人。

我相信您的问题是由于您拥有的验证数据和训练数据

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255, .....

vgg16.preprocess_input function 在 +1 和 -1 之间重新缩放像素值,因此无需包含 rescale=1/255。 在您的测试生成器中,您不会重新调整像素值。 所以删除训练和验证生成器中的 rescale=1/255

暂无
暂无

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

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