【问题标题】:Keras VGG16 pretrained model accuracy does not increaseKeras VGG16 预训练模型准确率没有提高
【发布时间】:2019-01-15 10:43:12
【问题描述】:

大家好,我在 Keras 上的 VGG16 上有问题。
我正在尝试提高准确性,但没有成功。

我只有 46 个数据训练、12 个类和 26 个数据验证。

目前,我能得到的最高精度是 0.18。 我尝试将批量大小更改为 2,但结果比我预期的要差。 我认为我不应该将数据训练样本设置为高于我的实际数据。

我应该怎么做才能提高准确性?

这是我的实际代码:

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.layers import Input, Flatten, Dense, Dropout
from keras.models import Model, Sequential
from keras import backend as K
import numpy as np
import matplotlib.pyplot as plt

# dimensions of our images.
from keras.preprocessing.image import ImageDataGenerator

img_width, img_height = 224, 224

train_data_dir = 'database/train'
validation_data_dir = 'database/validation'
nb_train_samples = 46
nb_validation_samples = 26
epochs = 50
batch_size = 4

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)


#Get back the convolutional part of a VGG network trained on ImageNet
vgg_conv = VGG16(weights='imagenet', include_top=True)
vgg_conv.summary()
print('VGG Pretrained Model loaded.')

#Add a layer where input is the output of the  second last layer
x = Dense(12, activation='softmax', name='predictions')(vgg_conv.layers[-2].output)

model = Model(input=vgg_conv.input, output=x)
#In the summary, weights and layers from VGG part will be hidden, but they will be fit during the training
model.summary()


# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 224,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 224)

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

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

# compile model
# model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizers.RMSprop(lr=2e-4), metrics=['accuracy'])
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

# Train the model
history = model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples / batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples / batch_size)

# Save the model
model.save('vgg16_pretrained_5.h5')

# Check Performance
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'b', label='Training acc')
plt.plot(epochs, val_acc, 'r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'b', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()

【问题讨论】:

  • 增加样本量。这是减少 DL 模型的方式
  • 先生培训的最低限度是多少?
  • 我们正在谈论数千甚至更多......

标签: python-3.x keras


【解决方案1】:

由于您有 12 个类并且只有 46 个观察值,因此每个类大致变成 2 个观察值(这只是一个猜测,甚至没有查看数据集)。有了这么少的数据,神经网络模型甚至无法理解数据的模式,最终无法泛化。因此,您至少需要 2k 次以上的观察才能获得更好的结果。

【讨论】:

    猜你喜欢
    • 2020-02-11
    • 2020-11-20
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2018-09-18
    • 2020-12-29
    • 1970-01-01
    相关资源
    最近更新 更多