我正在尝试使用转移学习来训练图像模型,而我目前正面临一个我不理解的问题。为了验证我的网络,我将相同的数据用于训练和验证,但是,我得到了截然不同的训练和验证准确性。谁能帮助我调试问题?
base_model = applications.MobileNetV2(weights='imagenet', include_top=False,
input_shape=(IMAGE_LENGTH, IMAGE_WIDTH, 3))
CLASSES = 2
print('================='+str(len(base_model.layers))+"==================")
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(CLASSES, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in model.layers[:100]:
layer.trainable = False
for layer in model.layers[100:]:
layer.trainable = True
model.compile(optimizer=optimizers.Adam(lr=CNN_parameters.learning_rate), loss='categorical_crossentropy', metrics=['accuracy'])
model.compile(optimizer=optimizers.SGD(lr=0.005), loss='categorical_crossentropy', metrics=['accuracy'])
train_datagen = ImageDataGenerator(
#preprocessing_function=preprocess_input,
rescale=CNN_parameters.rescale,
#rotation_range=CNN_parameters.rotation_range,
width_shift_range=CNN_parameters.width_shift_range,
height_shift_range=CNN_parameters.height_shift_range,
shear_range=CNN_parameters.shear_range,
zoom_range=CNN_parameters.zoom_range,
#horizontal_flip=CNN_parameters.horizontal_flip,
fill_mode='nearest'
)
train_generator = train_datagen.flow(
x_train, y_train,
batch_size=CNN_parameters.batch_size)
history = model.fit_generator(train_generator,
steps_per_epoch=64,
epochs=CNN_parameters.epochs,
validation_data=train_generator,
validation_steps=64)
但是,当我运行代码时,我得到了
64/64 [==============================] - 35s 554ms/step - loss: 0.4321 - accuracy: 0.8132 - val_loss: 0.8156 - val_accuracy: 0.5632
Epoch 2/50
64/64 [==============================] - 33s 523ms/step - loss: 0.3161 - accuracy: 0.8750 - val_loss: 0.9090 - val_accuracy: 0.5437
Epoch 3/50
64/64 [==============================] - 33s 515ms/step - loss: 0.2945 - accuracy: 0.8817 - val_loss: 0.7585 - val_accuracy: 0.5869
Epoch 4/50
64/64 [==============================] - 34s 531ms/step - loss: 0.2992 - accuracy: 0.8760 - val_loss: 0.9599 - val_accuracy: 0.5305
Epoch 5/50
64/64 [==============================] - 33s 515ms/step - loss: 0.2800 - accuracy: 0.8825 - val_loss: 0.8358 - val_accuracy: 0.5557
Epoch 6/50
64/64 [==============================] - 34s 528ms/step - loss: 0.2720 - accuracy: 0.8923 - val_loss: 0.9077 - val_accuracy: 0.5327
Epoch 7/50
64/64 [==============================] - 33s 515ms/step - loss: 0.2592 - accuracy: 0.8960 - val_loss: 0.7999 - val_accuracy: 0.5601
Epoch 8/50
64/64 [==============================] - 34s 536ms/step - loss: 0.2665 - accuracy: 0.8898 - val_loss: 0.8241 - val_accuracy: 0.5483
Epoch 9/50
64/64 [==============================] - 33s 520ms/step - loss: 0.2527 - accuracy: 0.8991 - val_loss: 0.7893 - val_accuracy: 0.5691
Epoch 10/50
64/64 [==============================] - 34s 533ms/step - loss: 0.2430 - accuracy: 0.9011 - val_loss: 0.8538 - val_accuracy: 0.5403
Epoch 11/50
64/64 [==============================] - 34s 526ms/step - loss: 0.2552 - accuracy: 0.8945 - val_loss: 0.8837 - val_accuracy: 0.5295
Epoch 12/50
64/64 [==============================] - 34s 539ms/step - loss: 0.2422 - accuracy: 0.9067 - val_loss: 0.8678 - val_accuracy: 0.5410
Epoch 13/50
64/64 [==============================] - 33s 520ms/step - loss: 0.2396 - accuracy: 0.9026 - val_loss: 0.7401 - val_accuracy: 0.5991
Epoch 14/50
64/64 [==============================] - 35s 541ms/step - loss: 0.2334 - accuracy: 0.9045 - val_loss: 0.7285 - val_accuracy: 0.5938
Epoch 15/50
64/64 [==============================] - 33s 517ms/step - loss: 0.2420 - accuracy: 0.9014 - val_loss: 0.8004 - val_accuracy: 0.5518
由于我使用相同的数据进行培训和验证,因此我希望看到的培训和验证准确性非常相似。我不明白他们为什么如此不同。我做错什么了吗?谢谢