From 483ecf11c8c3a0d99f85351dc40127e83901cfa4 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 3 Mar 2023 19:35:20 +0000 Subject: [PATCH] add specificity metric --- .../lib/ai/components/MetricSpecificity.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 aimodel/src/lib/ai/components/MetricSpecificity.py diff --git a/aimodel/src/lib/ai/components/MetricSpecificity.py b/aimodel/src/lib/ai/components/MetricSpecificity.py new file mode 100644 index 0000000..d0a9805 --- /dev/null +++ b/aimodel/src/lib/ai/components/MetricSpecificity.py @@ -0,0 +1,46 @@ +import math + +import tensorflow as tf + + +def specificity(y_pred, y_true): + """ + @source https://datascience.stackexchange.com/a/40746/86851 + param: + y_pred - Predicted labels + y_true - True labels + Returns: + Specificity score + """ + neg_y_true = 1 - y_true + neg_y_pred = 1 - y_pred + fp = K.sum(neg_y_true * y_pred) + tn = K.sum(neg_y_true * neg_y_pred) + specificity = tn / (tn + fp + K.epsilon()) + return specificity + + +class MetricSpecificity(tf.keras.metrics.Metric): + """An implementation of the sensitivity. + @source + Args: + smooth (float): The batch size (currently unused). + """ + + def __init__(self, name="specificity", **kwargs): + super(MetricSpecificity, self).__init__(name=name, **kwargs) + + self.param_smooth = smooth + + def call(self, y_true, y_pred): + ground_truth = tf.cast(y_true, dtype=tf.float32) + prediction = tf.cast(y_pred, dtype=tf.float32) + + return specificity(ground_truth, prediction) + + def get_config(self): + config = super(MetricSpecificity, self).get_config() + config.update({ + "smooth": self.param_smooth, + }) + return config