我正在训练一个类似U-net的模型来进行语义分割,但IoU在一个又一个时期中不断减少。这是我的IoU和IoU损失函数。我的输入和输出掩码是一个带有dtype=np.bool
的numpy数组,所以我将其强制转换为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
优化器的功能是最小化损失函数
您将IoU
设置为损失函数,这就是它减少的原因。
第二个函数iou_loss
应该用作loss,而不是iou
函数。