How to plot the actual numbers of the confusion matrix along with the color while using tensorboard to view them?

112 Views Asked by At

I am running the code provided on this Github - https://github.com/arthurdouillard/CVPR2021_PLOP/blob/381cb795d70ba8431d864e4b60bb84784bc85ec9/metrics/stream_metrics.py

right now I am able to view the confusion matrix with changes in the colour, but I can't see the actual numbers. What changes do you suggest to get these numbers on the visualization?

1

There are 1 best solutions below

1
On

You could change confusion_matrix_to_fig to:

import itertools

def confusion_matrix_to_fig(self):
    cm = self.confusion_matrix.astype('float') / (self.confusion_matrix.sum(axis=1) +
                                                  0.000001)[:, np.newaxis]
    fig, ax = plt.subplots()
    im = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
    ax.figure.colorbar(im, ax=ax)

    ax.set(title=f'Confusion Matrix', ylabel='True label', xlabel='Predicted label')

    # Adding text to cm
    thresh = cm.max() / 1.5  # Thresh is used to decide color of text (white or black)
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        ax.text(j, i, "{:0.4f}".format(cm[i, j]),
                  horizontalalignment="center",
                  color="white" if cm[i, j] > thresh else "black")

    fig.tight_layout()
    return fig

As you might have noticed, this will plot the normalized values of the confusion matrix.

Check this notebook for more info.