Implement custom metric in ignite

Are there some examples on how to implement a custom metric in Ignite?

@Luca_Pamparana to implement a custom metric is simple: you need to override ignite.metrics.Metric class and reimplement 3 methods: reset, update and compute:

from ignite.metrics import Metric


class MyMetric(Metric):

    def __init__(self, output_transform=lambda x: x):
        self._var1 = None
        self._var2 = None
        super(MyMetric, self).__init__(output_transform=output_transform)

    def reset(self):
        self._var1 = 0
        self._var2 = 0

    def update(self, output):
        y_pred, y = output
        # ... your custom implementation to update internal state on after a single iteration
        # e.g. self._var2 += y.shape[0]
              
    def compute(self):
        # compute the metric using the internal variables
        # res = self._var1 / self._var2
        res = 0
        return res

For other examples, take a look at the source code :
MSE: https://github.com/pytorch/ignite/blob/master/ignite/metrics/mean_squared_error.py
etc

HTH

3 Likes

Thank you for that reply!