【问题标题】:how to fix overfitting or where is my fault in my code如何修复过度拟合或我的代码中的错误在哪里
【发布时间】:2020-02-17 06:41:50
【问题描述】:

我正在使用预训练模型 Vgg16 来解决 100 分类问题。数据集是 tiny-imagenet,每个类有 500 张图像,我从 tiny-imagenet 中随机选择 100 个类作为我的训练(400)和验证(100)数据。所以我将 vgg16 的 input_shape 更改为 32*32 大小。 结果总是看起来像过拟合。训练 acc 很高,但 val_acc 总是停留在近 40%。 我使用了 dropout、正则化 L2、数据增强……,但 val_acc 也卡在了将近 40%。 我该怎么做才能过度拟合或纠正我的代码。 谢谢

img_width, img_height = 32, 32

epochs = 50

learning_rate = 1e-4

steps_per_epoch = 2500

train_path='./training_set_100A/'

valid_path='./testing_set_100A/'

test_path='./testing_set_100A/'

class_num = 100


train_batches = ImageDataGenerator(rescale=1. / 255
                ,rotation_range=20, zoom_range=0.15,
                width_shift_range=0.2, height_shift_range=0.2,     
shear_range=0.15,
                horizontal_flip=True, fill_mode="nearest"
                ).flow_from_directory(
                train_path, target_size=(img_width,img_height),     
batch_size=32, shuffle=True)

valid_batches = ImageDataGenerator(rescale=1. / 255).flow_from_directory(
                valid_path, target_size=(img_width,img_height),     
batch_size=10, shuffle=False)

test_batches = ImageDataGenerator(rescale=1. / 255).flow_from_directory(
test_path, target_size=    
(img_width,img_height),batch_size=10,shuffle=False)


seqmodel = Sequential()

VGG16Model = VGG16(weights='imagenet', include_top=False)

input = Input(shape=(img_width, img_height, 3), name='image_intput')

output_vgg16_conv = VGG16Model(input)

x = Flatten()(output_vgg16_conv)

x = Dense(4096, activation='relu')(x)

x = Dropout(0.5)(x)

x = Dense(4096, activation='relu')(x)

x = Dropout(0.5)(x)

x = Dense(class_num, activation='softmax')(x)

funcmodel = Model([input], [x])

funcmodel.summary()    

funcmodel.compile(optimizer=SGD(lr=learning_rate, momentum=0.9), 
loss='categorical_crossentropy', metrics=['accuracy'])

train_history = funcmodel.fit_generator(train_batches, 
steps_per_epoch=steps_per_epoch, validation_data=valid_batches, 
validation_steps=1000, epochs=epochs, verbose=1)

`

【问题讨论】:

  • 您好,您可以尝试降低模型的复杂度,也许您的密集层的 4096 有点太高了。
  • @AdForte 哦,对不起,我忘记添加变化密集神经元(4096 到 2048、1024、.. 到 128),但是 val_acc 没有太大变化
  • 那么 val_acc 是由于你的训练集的大小,可能有点太小了
  • 但是我觉得那个模型可能太复杂了,你试过更简单的自定义keras模型吗?
  • @AdForte 感谢您的建议。我的代码中是否有任何错误?

标签: python keras


【解决方案1】:

1) 50epoch 太多了。尝试运行更小的 epoch?

2) 检查每个 epoch 的验证准确性?

3) VGG 对于您的小 (32 * 32) 图像数据来说太深了。尝试用更少的参数构建自己的网络。还是试试 Lenet?

【讨论】:

  • 我运行了 50 个 epoch,但我每个 epoch 检查 val_acc 。它总是停留在 40%。所以也许 vgg16 不适合 32*32 尺寸的图像?谢谢
  • @ricky。是的,您的模型过度参数化,这在通常情况下会导致过度拟合。可能是您的数据大小不足以对参数进行无偏估计。使用增强将帮助模型更好地猜测参数
  • 我没有其他这样的数据集。或者我可以使用 ImageDataGenerator 将大小调整为 224*224 进行训练,但我认为这很奇怪。或者你建议有多少密集神经元。除了改变模型,你还有其他选择吗?非常感谢。
  • @ricky 我认为这很可能是模型问题。升级图像不会给您带来任何优势,因为您只是在复制信息。我的建议是尝试使用“Lenet”。
【解决方案2】:

您似乎遵循了其他站点的实施示例,但您训练的样本非常小,无法训练每个 4096 大小的 2 个新 Dense 层。 您必须减小层的大小或添加更多样本 20,000 而不是 500。

【讨论】:

  • 你的意思是我的数据集不适合 vgg16 训练。如果我仍然使用这个数据集,你建议多少密集神经元?或者我在哪里可以改进?我不知道 。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 2020-11-24
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多