【发布时间】: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 感谢您的建议。我的代码中是否有任何错误?