为什么我的IoU在使用tensorflow / keras的训练中不断减少?

rslzwgfq  于 2023-05-29  发布在  其他
关注(0)|答案(2)|浏览(155)

我正在训练一个类似于U网的模型来进行语义分割,但是IoU在一个又一个时期之后不断减少。这是我的IOU和IOU损失函数。我的输入和输出掩码是一个numpy数组dtype=np.bool,所以我把它转换为float32来计算IoU。我不知道有什么问题?我的度量函数或模型。我真的需要有人帮我。

def iou(y_true, y_pred):
    y_true = tf.keras.backend.flatten(y_true)
    y_pred = tf.keras.backend.flatten(y_pred)
    y_true_f = tf.cast(y_true, tf.float32)
    y_pred_f = tf.cast(y_pred, tf.float32)
    intersection = tf.keras.backend.sum(y_true_f * y_pred_f)
    union = tf.keras.backend.sum(y_true_f) + tf.keras.backend.sum(y_pred_f) - intersection
    return (intersection + 1e-7) / (union + 1e-7)

def iou_loss(y_true, y_pred):
    return 1.0 - iou(y_true, y_pred)

# Compile model
metrics = [iou_loss, iou, 'accuracy']
model.compile(optimizer=Adam(learning_rate), loss=iou, metrics=[metrics], run_eagerly=True)

这是我的训练成绩

Epoch 2/100
34/34 [==============================] - 3s 89ms/step - loss: 0.0186 - iou_loss: 0.9814 - iou: 0.0186 - accuracy: 0.9022 - val_loss: 0.0358 - val_iou_loss: 0.9647 - val_iou: 0.0353 - val_accuracy: 0.9460

Epoch 00002: val_loss improved from 0.03619 to 0.03579, saving model to /content/gdrive/MyDrive/model_ccnet_iris.h5
Epoch 3/100
34/34 [==============================] - 3s 89ms/step - loss: 0.0158 - iou_loss: 0.9843 - iou: 0.0157 - accuracy: 0.8972 - val_loss: 0.0352 - val_iou_loss: 0.9652 - val_iou: 0.0348 - val_accuracy: 0.9071

Epoch 00003: val_loss improved from 0.03579 to 0.03525, saving model to /content/gdrive/MyDrive/model_ccnet_iris.h5
Epoch 4/100
34/34 [==============================] - 3s 88ms/step - loss: 0.0132 - iou_loss: 0.9868 - iou: 0.0132 - accuracy: 0.8910 - val_loss: 0.0348 - val_iou_loss: 0.9656 - val_iou: 0.0344 - val_accuracy: 0.8690

Epoch 00004: val_loss improved from 0.03525 to 0.03485, saving model to /content/gdrive/MyDrive/model_ccnet_iris.h5
Epoch 5/100
34/34 [==============================] - 3s 87ms/step - loss: 0.0112 - iou_loss: 0.9888 - iou: 0.0112 - accuracy: 0.8842 - val_loss: 0.0345 - val_iou_loss: 0.9659 - val_iou: 0.0341 - val_accuracy: 0.8411

Epoch 00005: val_loss improved from 0.03485 to 0.03455, saving model to /content/gdrive/MyDrive/model_ccnet_iris.h5
Epoch 6/100
34/34 [==============================] - 3s 85ms/step - loss: 0.0096 - iou_loss: 0.9904 - iou: 0.0096 - accuracy: 0.8740 - val_loss: 0.0343 - val_iou_loss: 0.9662 - val_iou: 0.0338 - val_accuracy: 0.8216
yhqotfr8

yhqotfr81#

优化器的功能是最小化损失函数
你设置IoU作为损失函数,这就是为什么它是递减的。

kx5bkwkv

kx5bkwkv2#

第二个函数iou_loss应用作损失,而不是iou函数。

相关问题