Wil*_*one 6 validation binary sigmoid tf.keras keras-metrics
我有一个用于语义分割的网络,模型的最后一层应用 sigmoid 激活,因此所有预测都在 0-1 之间缩放。有一个验证指标 tf.keras.metrics.MeanIoU(num_classes),它将分类预测(0 或 1)与验证(0 或 1)进行比较。因此,如果我进行预测并应用此指标,它会自动将连续预测映射到阈值 = 0.5 的二进制吗?是否有可能手动定义阈值?
小智 8
否,tf.keras.metrics.MeanIoU
不会自动将连续预测映射到阈值 = 0.5 的二进制。
It will convert the continuous predictions to its binary, by taking the binary digit before decimal point as predictions like 0.99
as 0
, 0.50
as 0
, 0.01
as 0
, 1.99
as 1
, 1.01
as 1
etc when num_classes=2
. So basically if your predicted values are between 0
to 1
and num_classes=2
, then everything is considered 0
unless the prediction is 1
.
Below are the experiments to justify the behavior in tensorflow version 2.2.0
:
All binary result :
import tensorflow as tf
m = tf.keras.metrics.MeanIoU(num_classes=2)
_ = m.update_state([0, 0, 1, 1], [0, 0, 1, 1])
m.result().numpy()
Run Code Online (Sandbox Code Playgroud)
Output -
1.0
Run Code Online (Sandbox Code Playgroud)
将一个预测更改为连续 0.99 -此处将其0.99
视为0
。
import tensorflow as tf
m = tf.keras.metrics.MeanIoU(num_classes=2)
_ = m.update_state([0, 0, 1, 1], [0, 0, 1, 0.99])
m.result().numpy()
Run Code Online (Sandbox Code Playgroud)
输出 -
0.5833334
Run Code Online (Sandbox Code Playgroud)
将一个预测更改为连续 0.01 -此处将其0.01
视为0
。
import tensorflow as tf
m = tf.keras.metrics.MeanIoU(num_classes=2)
_ = m.update_state([0, 0, 1, 1], [0, 0.01, 1, 1])
m.result().numpy()
Run Code Online (Sandbox Code Playgroud)
输出 -
1.0
Run Code Online (Sandbox Code Playgroud)
将一个预测更改为连续 1.99 -此处将其1.99
视为1
。
%tensorflow_version 2.x
import tensorflow as tf
m = tf.keras.metrics.MeanIoU(num_classes=2)
_ = m.update_state([0, 0, 1, 1], [0, 0, 1, 1.99])
m.result().numpy()
Run Code Online (Sandbox Code Playgroud)
输出 -
1.0
Run Code Online (Sandbox Code Playgroud)
因此理想的方法是定义一个函数,在评估 之前将连续值转换为二进制值MeanIoU
。
希望这能回答您的问题。快乐学习。