title: Custom mean IoU metric to train a U-Net model
body: <p>I'm trying to build a custom mean IoU metric for training a U-Net model to classify four classed [0, 1, 2, 3] dataset but I get an error.</p>
  <p>My code:</p>
  <pre class="lang-py prettyprint-override"><code>#Calculating mean IoU metric
  
  from keras.metrics import MeanIoU
  from keras import backend as K
  n_classes = 4
  
  def mean_IoU_metric(y_true, y_pred):
  
      y_pred_argmax = K.argmax(y_true, axis=3) 
      IOU_keras = MeanIoU(num_classes=n_classes)  
      IOU_keras.update_state(y_true[:,:,:,0], y_pred_argmax)
      IOU_keras = tf.convert_to_tensor(IOU_keras, np.float32)
  
      return IOU_keras
  </code></pre>
  <p>The error:</p>
  <pre><code>TypeError: Expected float32, but got MeanIoU(name=mean_io_u,dtype=float32,num_classes=4) of type 'MeanIoU'.
  </code></pre>
  <p>Would you please help me to solve this issue and thank you very much.</p>