首页 > 解决方案 > 如何让我的 VGG 模型从一开始就更准确?

问题描述

我正在尝试执行图像分类任务,为此我正在使用 VGG 模型。现在,我正在使用 3 个 epoch,因为我不希望训练花费很多时间,但是从一开始我的模型就给出了非常糟糕的准确性。谁能告诉我如何使这个模型更准确?这就是我在创建模型时的原因:


from tensorflow.keras import layers 
import tensorflow as tf 

base_model = VGG16(input_shape = (224, 224, 3), # Shape of our images
include_top = False, # Leave out the last fully connected layer
weights = 'imagenet')


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


# Flatten the output layer to 1 dimension
x = layers.Flatten()(base_model.output)

# Add a fully connected layer with 512 hidden units and ReLU activation
x = layers.Dense(512, activation='relu')(x)

# Add a dropout rate of 0.5
x = layers.Dropout(0.5)(x)

# Add a final sigmoid layer for classification
x = layers.Dense(1, activation='sigmoid')(x)

model = tf.keras.models.Model(base_model.input, x)

model.compile(optimizer = tf.keras.optimizers.RMSprop(lr=0.0001), loss = 'binary_crossentropy',metrics = ['acc'])

history = model.fit(train_generator, validation_data = validation_generator, steps_per_epoch = 100, epochs = 3)

我真的希望我的模型像现在这样更准确。

标签: pythontensorflowkerasdeep-learningvgg-net

解决方案


我认为您可能希望您的模型类似于

preprocess_input = tf.keras.applications.vgg16.preprocess_input
input=Input((224,224,3))
x=preprocess_input(input)
x=base_model = tf.keras.applications.VGG16( include_top=False, input_shape=(224,224,3),
                                                                pooling='average', weights='imagenet')(x)
preds=Dense(1, activation='sigmoid')(x)
model=Model(inputs=input, outputs=preds)
model.summary()

您对 steps_per_epoch 和验证步骤的值通常设置为样本数//batch_size。在 model.fit 中将这些值保留为 None ,它将在内部确定正确的值。还要设置verbose =1,这样你就可以看到每个epoch之后的训练结果。


推荐阅读