base regression classification hinge probabilistic accuracy fscore iou todo align with tf keras and set the name attribute of metrics with the key name currently it uses default name of class definitions serializes metric function or metric instance args metric a keras metric instance or a metric function returns metric configuration dictionary deserializes a serialized metric classfunction instance args config metric configuration customobjects optional dictionary mapping names strings to custom objects classes and functions to be considered during deserialization returns a keras metric instance or a metric function retrieves a keras metric as a functionmetric class instance the identifier may be the string name of a metric function or class metric metrics getcategoricalcrossentropy typemetric class function metric metrics getcategoricalcrossentropy typemetric class metrics categoricalcrossentropy you can also specify config of the metric to this function by passing dict containing classname and config as an identifier also note that the classname must map to a metric class identifier classname categoricalcrossentropy config fromlogits true metric metrics getidentifier typemetric class metrics categoricalcrossentropy args identifier a metric identifier one of none or string name of a metric functionclass or metric configuration dictionary or a metric function or a metric class instance returns a keras metric as a function metric class instance base regression classification hinge probabilistic accuracy f score iou todo align with tf keras and set the name attribute of metrics with the key name currently it uses default name of class definitions serializes metric function or metric instance args metric a keras metric instance or a metric function returns metric configuration dictionary deserializes a serialized metric class function instance args config metric configuration custom_objects optional dictionary mapping names strings to custom objects classes and functions to be considered during deserialization returns a keras metric instance or a metric function retrieves a keras metric as a function metric class instance the identifier may be the string name of a metric function or class metric metrics get categorical_crossentropy type metric class function metric metrics get categoricalcrossentropy type metric class metrics categoricalcrossentropy you can also specify config of the metric to this function by passing dict containing class_name and config as an identifier also note that the class_name must map to a metric class identifier class_name categoricalcrossentropy config from_logits true metric metrics get identifier type metric class metrics categoricalcrossentropy args identifier a metric identifier one of none or string name of a metric function class or metric configuration dictionary or a metric function or a metric class instance returns a keras metric as a function metric class instance
from keras.api_export import keras_export
from keras.metrics.accuracy_metrics import Accuracy
from keras.metrics.accuracy_metrics import BinaryAccuracy
from keras.metrics.accuracy_metrics import CategoricalAccuracy
from keras.metrics.accuracy_metrics import SparseCategoricalAccuracy
from keras.metrics.accuracy_metrics import SparseTopKCategoricalAccuracy
from keras.metrics.accuracy_metrics import TopKCategoricalAccuracy
from keras.metrics.confusion_metrics import AUC
from keras.metrics.confusion_metrics import FalseNegatives
from keras.metrics.confusion_metrics import FalsePositives
from keras.metrics.confusion_metrics import Precision
from keras.metrics.confusion_metrics import PrecisionAtRecall
from keras.metrics.confusion_metrics import Recall
from keras.metrics.confusion_metrics import RecallAtPrecision
from keras.metrics.confusion_metrics import SensitivityAtSpecificity
from keras.metrics.confusion_metrics import SpecificityAtSensitivity
from keras.metrics.confusion_metrics import TrueNegatives
from keras.metrics.confusion_metrics import TruePositives
from keras.metrics.f_score_metrics import F1Score
from keras.metrics.f_score_metrics import FBetaScore
from keras.metrics.hinge_metrics import CategoricalHinge
from keras.metrics.hinge_metrics import Hinge
from keras.metrics.hinge_metrics import SquaredHinge
from keras.metrics.iou_metrics import BinaryIoU
from keras.metrics.iou_metrics import IoU
from keras.metrics.iou_metrics import MeanIoU
from keras.metrics.iou_metrics import OneHotIoU
from keras.metrics.iou_metrics import OneHotMeanIoU
from keras.metrics.metric import Metric
from keras.metrics.probabilistic_metrics import BinaryCrossentropy
from keras.metrics.probabilistic_metrics import CategoricalCrossentropy
from keras.metrics.probabilistic_metrics import KLDivergence
from keras.metrics.probabilistic_metrics import Poisson
from keras.metrics.probabilistic_metrics import SparseCategoricalCrossentropy
from keras.metrics.reduction_metrics import Mean
from keras.metrics.reduction_metrics import MeanMetricWrapper
from keras.metrics.reduction_metrics import Sum
from keras.metrics.regression_metrics import CosineSimilarity
from keras.metrics.regression_metrics import LogCoshError
from keras.metrics.regression_metrics import MeanAbsoluteError
from keras.metrics.regression_metrics import MeanAbsolutePercentageError
from keras.metrics.regression_metrics import MeanSquaredError
from keras.metrics.regression_metrics import MeanSquaredLogarithmicError
from keras.metrics.regression_metrics import R2Score
from keras.metrics.regression_metrics import RootMeanSquaredError
from keras.saving import serialization_lib
from keras.utils.naming import to_snake_case

ALL_OBJECTS = {
    
    Metric,
    Mean,
    Sum,
    MeanMetricWrapper,
    
    MeanSquaredError,
    RootMeanSquaredError,
    MeanAbsoluteError,
    MeanAbsolutePercentageError,
    MeanSquaredLogarithmicError,
    CosineSimilarity,
    LogCoshError,
    R2Score,
    
    AUC,
    FalseNegatives,
    FalsePositives,
    Precision,
    PrecisionAtRecall,
    Recall,
    RecallAtPrecision,
    SensitivityAtSpecificity,
    SpecificityAtSensitivity,
    TrueNegatives,
    TruePositives,
    
    Hinge,
    SquaredHinge,
    CategoricalHinge,
    
    KLDivergence,
    Poisson,
    BinaryCrossentropy,
    CategoricalCrossentropy,
    SparseCategoricalCrossentropy,
    
    Accuracy,
    BinaryAccuracy,
    CategoricalAccuracy,
    SparseCategoricalAccuracy,
    TopKCategoricalAccuracy,
    SparseTopKCategoricalAccuracy,
    
    F1Score,
    FBetaScore,
    
    IoU,
    BinaryIoU,
    MeanIoU,
    OneHotIoU,
    OneHotMeanIoU,
}
ALL_OBJECTS_DICT = {cls.__name__: cls for cls in ALL_OBJECTS}
ALL_OBJECTS_DICT.update(
    {to_snake_case(cls.__name__): cls for cls in ALL_OBJECTS}
)


ALL_OBJECTS_DICT.update(
    {
        "bce": BinaryCrossentropy,
        "BCE": BinaryCrossentropy,
        "mse": MeanSquaredError,
        "MSE": MeanSquaredError,
        "mae": MeanAbsoluteError,
        "MAE": MeanAbsoluteError,
        "mape": MeanAbsolutePercentageError,
        "MAPE": MeanAbsolutePercentageError,
        "msle": MeanSquaredLogarithmicError,
        "MSLE": MeanSquaredLogarithmicError,
    }
)


@keras_export("keras.metrics.serialize")
def serialize(metric):
    
    return serialization_lib.serialize_keras_object(metric)


@keras_export("keras.metrics.deserialize")
def deserialize(config, custom_objects=None):
    
    return serialization_lib.deserialize_keras_object(
        config,
        module_objects=ALL_OBJECTS_DICT,
        custom_objects=custom_objects,
    )


@keras_export("keras.metrics.get")
def get(identifier):
    
    if identifier is None:
        return None
    if isinstance(identifier, dict):
        obj = deserialize(identifier)
    elif isinstance(identifier, str):
        obj = deserialize(identifier)
    else:
        obj = identifier
    if callable(obj):
        return obj
    else:
        raise ValueError(f"Could not interpret metric identifier: {identifier}")