python - 机器学习模型在测试数据上的表现比验证数据差
问题描述
我对机器学习很陌生。
首先,我想训练一个模型来对猫和狗的图片进行分类。
我遇到的问题是,当我训练我的模型时,它在训练数据和验证数据上给了我(大约)80-85% 的准确率。损失非常低,验证数据和训练数据的损失都在 0.4 - 0.5 左右。因为这些数字非常相似,我怀疑我没有过度拟合的问题,对吧?
但是当我用数据集中的图片(它以前没有见过)测试我的模型时,准确率结果是在 70%-73% 左右。所以显着降低。我找不到任何关于为什么会这样的信息。而且,正如我所说,我怀疑过度拟合不是问题,但由于我是初学者,我不太确定。
我的模型看起来像这样(我在 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 函数在 +1 和 -1 之间重新缩放像素值,因此无需包含 rescale=1/255。在您的测试生成器中,您不会重新调整像素值。所以删除训练和验证生成器中的 rescale=1/255
推荐阅读
- r - 将数据框中的每一行与另一个数据框中的多行进行比较并获得结果
- java - 如何转换地图
> 在 java 中将字符串转换为 Json 对象? - javascript - 页面在vue中加载两次
- xamarin - Xamarin Form Android API 级别支持?
- android - Xamarin - Recycler 视图的每个项目中的 Android Tablayout
- swiftui - 为什么视图在 SwiftUI 中没有完全破坏
- php - 我在使用 Azure 登录时跳过了 Permission Requested Interface 。提供“授予管理员同意”权限。但 MFA 现在禁用
- printing - 在 kentiko 中打印背景图像
- amazon-web-services - AWS : Python SDK, 我需要配置访问密钥和安全访问密钥吗
- swift - 通过视频 ID 获取 Facebook 视频链接