0
0

More than 1 year has passed since last update.

sparse_categorical_crossentropyとMIoUの共存

Posted at

この記事について

 今回はsparse_categorical_crossentropyを使用した際に評価指標としてMIoUを使用する方法を紹介します。

また、以下のサイトを参考にしております。
サイトによるとtf2.2,tf2.3,tf2.4で問題ないようです。

参考:(https://github.com/tensorflow/tensorflow/issues/32875)

sparse_categorical_crossentropyとMIoU

 sparse_categorical_crosentropyは多クラス分類に使用される損失関数ですが、評価指数としてMIoUを使用するとエラーが発生してしまうことがありました。確認すると、入力の型が一致していなければならないそうです。

解決方法

以下のコードを使用することで解決することができます。

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)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0