提问人:Yimeng Liu 提问时间:11/13/2023 更新时间:11/13/2023 访问量:15
在 Keras 中使用相同的数据集和模型进行训练和验证之间的准确性不一致
Inconsistent Accuracy Between Training and Validation Using Same Dataset and Model in Keras
问:
我在使用 TensorFlow/Keras 训练深度学习模型时遇到了一个令人困惑的问题。尽管使用相同的数据集进行训练和验证,但我观察到两者在准确性和损失方面存在显着差异。我正在寻求有关可能导致此问题的原因以及如何解决该问题的见解或建议。
问题描述:
我正在使用 ResNet 18 架构执行二进制分类任务。 同一数据集用于训练和验证。 不涉及数据增强、丢弃或批量规范化。 尽管数据相同,但训练精度与验证精度有很大不同。
型
# ResNet 18
def model_resnet_set(input_shape):
input_tensor = Input(shape=input_shape)
# Initial convolution layer
x = Conv2D(64, (7, 7), strides=(2, 2), padding='same')(input_tensor)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
# Residual blocks
x = residual_block(x, 64, 2)
x = residual_block(x, 128, 2, stride=2)
x = residual_block(x, 256, 2, stride=2)
x = residual_block(x, 512, 2, stride=2)
# Global average pooling and classification head
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation="relu", kernel_regularizer=l2(0.001))(x)
x = Dropout(0.5)(x)
x = Dense(1, activation="sigmoid")(x)
model = Model(inputs=input_tensor, outputs=x)
model.compile(loss="binary_crossentropy", optimizer=Adam(lr=0.0001),metrics=["accuracy"])
return model
def residual_block(x, filters, blocks, stride=1):
shortcut = x
for i in range(blocks):
if i == 0:
shortcut = Conv2D(filters, (1, 1), strides=(stride, stride))(shortcut)
shortcut = BatchNormalization()(shortcut)
x = Conv2D(filters, (3, 3), strides=(stride, stride), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
stride = 1 # Only the first convolution in the block uses the specified stride
x = tf.keras.layers.add([x, shortcut])
return x
要加载图像,我只需使用
from tensorflow.keras.preprocessing.image import img_to_array, load_img
将图像追加到列表中。
对于培训,我使用
history = model.fit(train_set,labels,epochs=10,validation_data=(train_set,labels), batch_size = 128)
训练结果显示为
12/12 [==============================] - 12s 246ms/step - loss: 0.4158 - accuracy: 0.7966 - val_loss: 1.0283 - val_accuracy: 0.5024
Epoch 2/10
12/12 [==============================] - 2s 134ms/step - loss: 0.3266 - accuracy: 0.8556 - val_loss: 0.6288 - val_accuracy: 0.7831
Epoch 3/10
12/12 [==============================] - 2s 159ms/step - loss: 0.2529 - accuracy: 0.8983 - val_loss: 1.5992 - val_accuracy: 0.4997
Epoch 4/10
12/12 [==============================] - 2s 129ms/step - loss: 0.2207 - accuracy: 0.9031 - val_loss: 2.2351 - val_accuracy: 0.4997
Epoch 5/10
12/12 [==============================] - 2s 157ms/step - loss: 0.1795 - accuracy: 0.9329 - val_loss: 2.6216 - val_accuracy: 0.4997
导致此问题的可能原因以及可能的解决方案。或者只是一些可能发生问题的见解
答: 暂无答案
评论