我正在尝试使用预训练的Inception V3模型对两个不同的类进行图像分类。我有大约1400张图像的数据集,这些图像大致平衡。当我运行程序时,得到的结果在前几个时期就不正确了。训练模型时这正常吗?
epochs = 175
batch_size = 64
#include_top = false to accomodate new classes
base_model = keras.applications.InceptionV3(
weights ='imagenet',
include_top=False,
input_shape = (img_width,img_height,3))
#Classifier Model ontop of Convolutional Model
model_top = keras.models.Sequential()
model_top.add(keras.layers.GlobalAveragePooling2D(input_shape=base_model.output_shape[1:], data_format=None)),
model_top.add(keras.layers.Dense(350,activation='relu'))
model_top.add(keras.layers.Dropout(0.4))
model_top.add(keras.layers.Dense(1,activation = 'sigmoid'))
model = keras.models.Model(inputs = base_model.input, outputs = model_top(base_model.output))
#freeze the convolutional layers of InceptionV3
for layer in model.layers[:30]:
layer.trainable = False
#Compiling model using Adam Optimizer
model.compile(optimizer = keras.optimizers.Adam(
lr=0.000001,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-08),
loss='binary_crossentropy',
metrics=['accuracy'])
使用我当前的参数,当我在一组单独的图像上进行测试时,我的精度仅为89%,测试损失为0.3。是否需要在模型中添加更多图层以提高准确性?
答案 0 :(得分:1)
您的代码有几个问题...
首先,您构建model_top
的方法非常不常规(恕我直言,也很混乱);在这种情况下,documentation examples是您最好的朋友。因此,首先将model_top
部分替换为:
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(350, activation='relu')(x)
x = Dropout(0.4)(x)
predictions = Dense(1, activation='sigmoid')(x)
# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
请注意,我没有更改您选择的参数-您可以在密集层中尝试使用更多单位(文档中的示例使用1024)...
第二,不清楚为什么您选择仅冻结30层的InceptionV3,该层不少于311层:
len(base_model.layers)
# 311
因此,也将这一部分替换为
for layer in base_model.layers:
layer.trainable = False
第三,您的学习率似乎太低; Adam优化器应该可以使用默认参数在开箱即用的情况下很好地工作,因此我也建议您将模型编译为
model.compile(optimizer = keras.optimizers.Adam(),
loss='binary_crossentropy',
metrics=['accuracy'])