为什么我的 Keras 模型学会识别背景?

数据挖掘 Python 深度学习 喀拉斯 张量流
2021-09-22 07:50:55

我正在尝试使用预训练模型(也在该数据集上训练)在 Pascal VOC2012 上训练Deeplabv3+ 的 Keras 实现。

我得到了奇怪的结果,准确度迅速收敛到 1.0:

5/5 [==============================] - 182s 36s/step - loss: 26864.4418 - acc: 0.7669 - val_loss: 19385.8555 - val_acc: 0.4818
Epoch 2/3
5/5 [==============================] - 77s 15s/step - loss: 42117.3555 - acc: 0.9815 - val_loss: 69088.5469 - val_acc: 0.9948
Epoch 3/3
5/5 [==============================] - 78s 16s/step - loss: 45300.6992 - acc: 1.0000 - val_loss: 44569.9414 - val_acc: 1.0000

测试模型也可以提供 100% 的准确度。

我决定在训练前后对同一组随机图像进行预测,发现鼓励模型说一切都只是背景(这是 Pascal VOC2012 中的第一类)。

在此处输入图像描述

在此处输入图像描述

我对深度学习很陌生,需要帮助来弄清楚这可能来自哪里。

我认为这可能是我的损失函数,我将其定义为:

def image_categorical_cross_entropy(y_true, y_pred):
    """
    :param y_true: tensor of shape (batch_size, height, width) representing the ground truth.
    :param y_pred: tensor of shape (batch_size, height, width) representing the prediction.
    :return: The mean cross-entropy on softmaxed tensors.
    """
    return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_pred, labels=y_true))

我有点不确定我的张量是否有正确的形状。我正在使用 TF 的数据集 API 来加载.tfrecord文件,我的注释张量是 shape (batch_size, height, width)(batch_size, height, width, 21)需要什么当我尝试将注释图像分成包含 21 个图像(每个类一个)的张量时,模型内部会出现其他错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [12,512,512,21] vs. [12,512,512]
         [[Node: metrics/acc/Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:GPU:0"](metrics/acc/ArgMax, metrics/acc/ArgMax_1)]]
         [[Node: training/Adam/gradients/bilinear_upsampling_2_1/concat_grad/Slice_1/_13277 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:1", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_62151_training/Adam/gradients/bilinear_upsampling_2_1/concat_grad/Slice_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:1"]()]]

谢谢您的帮助!

1个回答

您的模型过度拟合。每个时期只有 5 张图像。该模型正在“记住”每张图像的答案。

为了尽量减少过度拟合的机会,增加图像的数量。每个类别的对象应该有几千个示例图像。