【问题标题】:How can I make my VGG model more accurate from the start?如何让我的 VGG 模型从一开始就更准确?
【发布时间】:2021-03-04 15:52:57
【问题描述】:

我正在尝试执行图像分类任务,为此我正在使用 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)

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

【问题讨论】:

  • 请展示model.fit生成的部分数据
  • 对不起,我是 ML 新手,不太明白你的意思,是 epochs 完成后的东西吗?
  • 这是model.fit在训练期间产生的数据。还可以尝试将你的辍学率降低到 0.1 或 0.2。使用 0.5,您在训练期间消除了一半的神经元。

标签: python tensorflow keras deep-learning vgg-net


【解决方案1】:

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

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之后的训练结果。

【讨论】:

    猜你喜欢
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    相关资源
    最近更新 更多