【发布时间】:2021-05-04 12:54:29
【问题描述】:
我有一个语义分割任务,使用 UNET 预测 5 通道掩码,例如掩码形状为 (224,244,5)。
我在借条上使用这个功能:
def mean_iou(y_true, y_pred):
y_pred = tf.round(tf.cast(y_pred, tf.int32))
intersect = tf.reduce_sum(tf.cast(y_true, tf.float32) * tf.cast(y_pred, tf.float32), axis=[1])
union = tf.reduce_sum(tf.cast(y_true, tf.float32),axis=[1]) + tf.reduce_sum(tf.cast(y_pred, tf.float32),axis=[1])
smooth = tf.ones(tf.shape(intersect))
return tf.reduce_mean((intersect + smooth) / (union - intersect + smooth))
def iou_loss(y_true, y_pred):
y_true = tf.reshape(y_true, [-1])
y_pred = tf.reshape(y_pred, [-1])
intersection = tf.reduce_sum(tf.cast(y_true, tf.float32) * tf.cast(y_pred, tf.float32))
score = (intersection + 1.) / (tf.reduce_sum(tf.cast(y_true, tf.float32)) +
tf.reduce_sum(tf.cast(y_pred, tf.float32)) - intersection + 1.)
return 1 - score`
以及UNET模型的输出层:
outputs = tf.keras.layers.Conv2D(5, (1, 1), activation='softmax')(c9)
model = tf.keras.Model(inputs=[input_img], outputs=[outputs])
opti = tf.keras.optimizers.Adam(lr=0.003, clipvalue=0.7)
model.compile(optimizer=opti, loss=iou_loss, metrics=['accuracy',mean_iou])
但我不确定 IOU 函数是否正确实现,
你能澄清一下吗?
【问题讨论】:
标签: python tensorflow keras semantic-segmentation