python-3.x - Keras 中的 MNIST 和 VGG16 迁移学习 - 验证精度低

标签 python-3.x tensorflow machine-learning keras vgg-net

我最近开始在一个项目中利用 Keras 的 flow_from_dataframe() 功能,并决定使用 MNIST 数据集对其进行测试。我有一个充满 png 格式的 MNIST 样本的目录,以及一个数据框,其中一列中包含每个样本的绝对目录,另一列中包含标签。

我还使用迁移学习,导入 VGG16 作为基础,并在 10 的 Softmax 层之前添加我自己的 512 节点 ReLU 密集层和 0.5 的 drop-out。(对于数字 0-9)。我使用 rmsprop (lr=1e-4) 作为优化器。

当我启动环境时,它会从 Git 调用最新版本的 keras_preprocessing,它支持绝对目录和大写文件扩展名。

我的问题是我的训练准确度非常高,而验证准确度却非常低。到最后一个周期 (10),我的训练准确度为 0.94,验证准确度为 0.01。

我想知道我的脚本是否存在根本性错误?使用另一个数据集,我什至在第 4 轮之后得到了训练和验证损失值的 NaN。(我检查了相关列,没有任何空值!)

这是我的代码。如果有人能浏览一下它并看看是否有什么东西跳出来,我将非常感激。

import pandas as pd
import numpy as np

import keras
from keras_preprocessing.image import ImageDataGenerator

from keras import applications
from keras import optimizers
from keras.models import Model 
from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras import backend as k 
from keras.callbacks import ModelCheckpoint, CSVLogger

from keras.applications.vgg16 import VGG16, preprocess_input

# INITIALIZE MODEL

img_width, img_height = 32, 32
model = VGG16(weights = 'imagenet', include_top=False, input_shape = (img_width, img_height, 3))

# freeze all layers
for layer in model.layers:
    layer.trainable = False

# Adding custom Layers 
x = model.output
x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(10, activation="softmax")(x)

# creating the final model 
model_final = Model(input = model.input, output = predictions)

# compile the model 
rms = optimizers.RMSprop(lr=1e-4)
#adadelta = optimizers.Adadelta(lr=0.001, rho=0.5, epsilon=None, decay=0.0)

model_final.compile(loss = "categorical_crossentropy", optimizer = rms, metrics=["accuracy"])

# LOAD AND DEFINE SOURCE DATA

train = pd.read_csv('MNIST_train.csv', index_col=0)
val = pd.read_csv('MNIST_test.csv', index_col=0)

nb_train_samples = 60000
nb_validation_samples = 10000
batch_size = 60
epochs = 10

# Initiate the train and test generators
train_datagen = ImageDataGenerator()
test_datagen = ImageDataGenerator()

train_generator = train_datagen.flow_from_dataframe(dataframe=train,
                                                    directory=None,
                                                    x_col='train_samples',
                                                    y_col='train_labels',
                                                    has_ext=True,
                                                    target_size = (img_height,
                                                                   img_width),
                                                    batch_size = batch_size, 
                                                    class_mode = 'categorical',
                                                    color_mode = 'rgb')

validation_generator = test_datagen.flow_from_dataframe(dataframe=val,
                                                        directory=None,
                                                        x_col='test_samples',
                                                        y_col='test_labels',
                                                        has_ext=True,
                                                        target_size = (img_height, 
                                                                       img_width),
                                                        batch_size = batch_size, 
                                                        class_mode = 'categorical',
                                                        color_mode = 'rgb')

# GET CLASS INDICES
print('****************')
for cls, idx in train_generator.class_indices.items():
    print('Class #{} = {}'.format(idx, cls))
print('****************')

# DEFINE CALLBACKS

path = './chk/epoch_{epoch:02d}-valLoss_{val_loss:.2f}-valAcc_{val_acc:.2f}.hdf5'

chk = ModelCheckpoint(path, monitor = 'val_acc', verbose = 1, save_best_only = True, mode = 'max')

logger = CSVLogger('./chk/training_log.csv', separator = ',', append=False)

nPlus = 1
samples_per_epoch = nb_train_samples * nPlus

# Train the model 
model_final.fit_generator(train_generator,
                          steps_per_epoch = int(samples_per_epoch/batch_size),
                          epochs = epochs,
                          validation_data = validation_generator,
                          validation_steps = int(nb_validation_samples/batch_size),
                          callbacks = [chk, logger])

最佳答案

您是否尝试过明确定义图像的类别?像这样:

train_generator=image.ImageDataGenerator().flow_from_dataframe(classes=[0,1,2,3,4,5,6,7,8,9])

在训练和验证生成器中。

我发现有时训练和验证生成器会创建不同的对应字典。

关于python-3.x - Keras 中的 MNIST 和 VGG16 迁移学习 - 验证精度低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53640464/

相关文章:

python - python 文本文件中 'r+' 模式下的意外读/写行为

python-3.x - 如何修改 Python 中的列表?

python urllib : build urls including parameters with and without keyword

python - Keras 预测一个数字,如果在一个范围内则通过

java - 创建未标记实例时出现 weka.core.UnassignedDatasetException

Python3字典基于值的合并

python - 从 Tensorflow 中的自定义图像数据集创建批处理

machine-learning - 神经图灵机损失变为 NaN

tensorflow - 禁用 tensorflow 训练管道中的增强

python - Tensorflow Inception V3 无法加载计算图