python - 如何让我的 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)
我真的希望我的模型像现在这样更准确。
解决方案
我认为您可能希望您的模型类似于
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之后的训练结果。
推荐阅读
- android - Recyclerview 没有滚动,但仍然显示我的所有项目
- php - PHP symfony 框架中是否有类似于 rails 控制台的东西?
- razorpay - 使用 razorpay 发票的收据 API 集成
- json - 颤振在 JSON 中存储 FontAwesomeIcon 的最佳方式?
- swift - NSAlert 破坏性按钮
- java - 使用驱动器 api 将文件或数据库备份到驱动器时出错
- php - Wordpress:在帖子模板上显示最新帖子,但在最新帖子上不显示
- javascript - 使用 javascript 添加和删除类时,Css 转换属性不起作用
- html - 如何在视频元素标签内重叠 div?
- r - 使用一年中不同地块的常见月份的年平均值