Skip to main content
 首页 » 编程设计

python之Tensorflow tf.metrics.mean_iou 返回 0

2025年01月19日29jpfss

我想将函数 tf.metrics.mean_iou 用于 FCN 以进行语义分割。它仅在混淆矩阵在 IoU 之前计算时有效,否则返回 0。

这里是我的例子:

此示例返回正确的值 0.66071427

import tensorflow as tf 
import numpy as np 
 
y_pred0 = np.array([   [ [[0.9,0.1],[0.9,0.1],[0.9,0.1],[0.9,0.1]], [[0.2,0.8],[0.2,0.8],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]] ],   [ [[0.9,0.1],[0.9,0.1],[0.9,0.1],[0.9,0.1]], [[0.2,0.8],[0.2,0.8],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]] ]    ]) 
y_pred1 = tf.constant(y_pred0) 
y_pred2 = tf.argmax(y_pred1, axis=3) 
 
y_label = np.array([[[1,0,1,0],[1,0,1,0],[0,0,1,0],[0,0,1,0]], [[1,0,1,0],[1,0,1,0],[0,0,1,0],[0,0,1,0]]]) 
y_label2 = tf.constant(y_label) 
 
iou, conf_mat = tf.metrics.mean_iou(y_label2, y_pred2, num_classes=2) 
 
sess = tf.Session() 
sess.run(tf.local_variables_initializer()) 
sess.run(tf.global_variables_initializer()) 
 
sess.run(conf_mat) 
res = sess.run(iou) 
 
print(res) 

.

此示例返回 0

import tensorflow as tf 
import numpy as np 
 
def intersection_over_union(prediction, labels): 
    pred = tf.argmax(prediction, axis=3) 
    labl = tf.constant(labels) 
    iou, conf_mat = tf.metrics.mean_iou(labl, pred, num_classes=2) 
    return iou 
 
y_pred0 = np.array([   [ [[0.9,0.1],[0.9,0.1],[0.9,0.1],[0.9,0.1]], [[0.2,0.8],[0.2,0.8],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]] ],   [ [[0.9,0.1],[0.9,0.1],[0.9,0.1],[0.9,0.1]], [[0.2,0.8],[0.2,0.8],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]] ]    ]) 
y_pred1 = tf.constant(y_pred0) 
 
y_label = np.array([[[1,0,1,0],[1,0,1,0],[0,0,1,0],[0,0,1,0]], [[1,0,1,0],[1,0,1,0],[0,0,1,0],[0,0,1,0]]]) 
 
mean__iou = intersection_over_union(y_pred1, y_label) 
 
sess = tf.Session() 
sess.run(tf.local_variables_initializer()) 
sess.run(tf.global_variables_initializer()) 
 
res = sess.run(mean__iou) 
 
print(res) 

如果有一个函数可以计算平均 IoU 而无需初始化其中的所有变量,那就太好了。有什么办法可以修复第二个示例吗?我认为问题在于同时计算 IoU 和混淆矩阵,我没有找到另一种方法,即通过 Session() 分别运行它们。

谢谢

请您参考如下方法:

在从张量中获取 iou 值之前,您需要运行 tf.metrics.mean_iou 返回的更新操作。

固定代码如下:

import tensorflow as tf 
import numpy as np 
 
def intersection_over_union(prediction, labels): 
    pred = tf.argmax(prediction, axis=3) 
    labl = tf.constant(labels) 
    iou, conf_mat = tf.metrics.mean_iou(labl, pred, num_classes=2) 
    return iou, conf_mat 
 
y_pred0 = np.array([   [ [[0.9,0.1],[0.9,0.1],[0.9,0.1],[0.9,0.1]], [[0.2,0.8],[0.2,0.8],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]] ],   [ [[0.9,0.1],[0.9,0.1],[0.9,0.1],[0.9,0.1]], [[0.2,0.8],[0.2,0.8],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]], [[0.9,0.1],[0.9,0.1],[0.2,0.8],[0.9,0.1]] ]    ]) 
y_pred1 = tf.constant(y_pred0) 
 
y_label = np.array([[[1,0,1,0],[1,0,1,0],[0,0,1,0],[0,0,1,0]], [[1,0,1,0],[1,0,1,0],[0,0,1,0],[0,0,1,0]]]) 
 
mean__iou, conf_mat = intersection_over_union(y_pred1, y_label) 
 
sess = tf.Session() 
sess.run(tf.local_variables_initializer()) 
sess.run(tf.global_variables_initializer()) 
 
sess.run([conf_mat]) 
res = sess.run(mean__iou) 
 
print(res) 

返回正确值:0.66071427