title: Intersection over union (IOU) metric for multi-class semantic segmentation task
body: <p>I have a semantic segmentation task to predict 5 channel mask using UNET for example mask shape is (224,244,5).</p>
  <p>I'm using this function for IOU :</p>
  <pre><code>    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`
  </code></pre>
  <p>and the output layer of UNET model :</p>
  <pre><code>  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])
  </code></pre>
  <p>But I'm not sure is the IOU function the correct implementation or not,</p>
  <p>could you clarify this.</p>