title: Define a custom accuracy in Keras to ignore samples with a particular label
body: <p>I am writing to you because I have a problem, the same as the question in the link below that has not been answered.</p>
  
  <p><a href="https://stackoverflow.com/questions/47270722/how-to-define-a-custom-accuracy-in-keras-to-ignore-samples-with-a-particular-gol/53093965?noredirect=1#comment93084625_53093965">Previous question</a></p>
  
  <p>I have defined a loss function that excludes all pixel = 0:</p>
  
  <pre><code>def ignore_unknown_xentropy(ytrue, ypred):
      return (1-ytrue[:, :, :, 0]) * K.categorical_crossentropy(ytrue, ypred)
  </code></pre>
  
  <p>I noticed that during the training accuracy is distorted, going to consider the pixels equal to zero that I had previously excluded.</p>
  
  <p>There is a way to define a custom metric to exclude zero pixels on <code>y_true</code> from its calculation?</p>
  
  <p>I'm using this approach, that is to calculate the accuracy for each relevant label and add them, but it does not give me reliable results:</p>
  
  <pre><code>def single_class_accuracy(y_true, y_pred, class_id):
      class_id_true = K.argmax(y_true, axis=-1)
      class_id_preds = K.argmax(y_pred, axis=-1)
      accuracy_mask = K.cast(K.equal(class_id_preds, class_id), 'int32')
      class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask
      class_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1)
      return class_acc
  
  def custom_accuracy(y_true, y_pred):
      return single_class_accuracy(y_true, y_pred, 1) + single_class_accuracy(y_true, y_pred, 2)
  </code></pre>
  
  <p>And my <code>ytrue</code> is a one-hot encoded mask.</p>