[英]Dimensions mismatch error when using tf.metrics.MeanIoU() with SparseCategoricalCrossEntropy loss in Tensorflow 2.2
Refer to # https://github.com/tensorflow/tensorflow/issues/32875参考# https://github.com/tensorflow/tensorflow/issues/32875
The suggested fix was to:建议的修复是:
class UpdatedMeanIoU(tf.keras.metrics.MeanIoU):
@tf.function
def __call__(self, y_true, y_pred, sample_weight=None):
y_pred = tf.argmax(y_pred, axis=-1) # this is the fix
return super().__call__(y_true, y_pred, sample_weight=sample_weight)
It worked for TF2.1, but broke again in TF2.2.它适用于 TF2.1,但在 TF2.2 中再次中断。 Is there a way to pass y_pred = tf.argmax(y_pred, axis=-1)
as y_pred
to this metric other than subclassing?除了子类化之外,有没有办法将y_pred = tf.argmax(y_pred, axis=-1)
作为y_pred
给这个指标?
This fixes the issue:这解决了这个问题:
class UpdatedMeanIoU(tf.keras.metrics.MeanIoU):
def __init__(self,
y_true=None,
y_pred=None,
num_classes=None,
name=None,
dtype=None):
super(UpdatedMeanIoU, self).__init__(num_classes = num_classes,name=name, dtype=dtype)
def update_state(self, y_true, y_pred, sample_weight=None):
y_pred = tf.math.argmax(y_pred, axis=-1)
return super().update_state(y_true, y_pred, sample_weight)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.