在Keras中重新训练VGG16时的准确度非常低

时间:2018-02-23 03:33:01

标签: tensorflow keras

我正在尝试使用转移学习重新训练VGG16以获取新数据集。我已经使用ImageNet权重加载了模型,没有顶部完全连接的图层,从瓶颈层获得了对数据集的预测,并训练了一个带有这些瓶颈预测的小模型。但是,在50个时期之后,验证准确度非常低,为0.002。我无法弄清楚我的代码中的问题所在,这是来自Keras文档的InceptionV3再培训代码的修改版本。我已经能够在相同的数据集上重新训练ResNet50,准确度为0.88。我的代码如下。

from keras.applications import VGG16
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, Flatten, Dropout
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input

img_width, img_height = 224, 224

train_data_dir = 'Dataset/train'
validation_data_dir = 'Dataset/test'

nb_train_samples = 31119
nb_validation_samples = 13362
nb_epoch = 50
nb_classes = 281
batch_size = 16

input_tensor = Input(shape=(224, 224, 3))
base_model = VGG16(weights="imagenet", input_tensor=input_tensor, include_top=False)

x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(nb_classes, activation='sigmoid')(x)

model = Model(input=base_model.input, output=predictions)

for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=    ['accuracy'])

train_datagen = ImageDataGenerator(
        rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=16,
    class_mode='categorical'
)

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=16,
    class_mode='categorical'
)

history = model.fit_generator(
    train_generator,
    nb_epoch=nb_epoch,
    steps_per_epoch=nb_train_samples/batch_size,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples/batch_size)

1 个答案:

答案 0 :(得分:0)

VGG16使用Keras的Sequential模型,ResNet使用功能API。 因此,您应该替换

x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(nb_classes, activation='sigmoid')(x)

model = Model(input=base_model.input, output=predictions)

通过

model = Models.Sequential()
model.add(base_model)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation="softmax"))

使用分类class_mode时也使用softmax而不是sigmoid。