Metrics Recall, Specificity (Keras)

from tensorflow import math
from tensorflow import float32 as tf_float32
import tensorflow.keras.backend as K

def recall(y_true, y_predict):
    y_true = math.argmax(y_true, axis=1)
    y_predict = math.argmax(y_predict, axis=1)
    cm = math.confusion_matrix(y_true, y_predict, dtype=tf_float32)
    tn = cm[0,0]
    fp = cm[0,1]
    fn = cm[1,0]
    tp = cm[1,1]
    return tp / (tp + fn + K.epsilon())

def specificity(y_true, y_predict):
    y_true = math.argmax(y_true, axis=1)
    y_predict = math.argmax(y_predict, axis=1)
    cm = math.confusion_matrix(y_true, y_predict, dtype=tf_float32)
    tn = cm[0,0]
    fp = cm[0,1]
    fn = cm[1,0]
    tp = cm[1,1]
    return tn / (tn + fp + K.epsilon())