Search code examples
pythonconfusion-matrixunet-neural-network

Confusion matrix for instance segmentation returning no FN


I calculated a confusion matrix according to python script below. I ran it for four different instance segmentation problems but received always 0 FN. I became sceptical but couldn't find the error so far.

Is there an error in my code?

import seaborn as sns

FP = len(np.where(preds_test_t - Y_test == 1)[0])
FN = len(np.where(preds_test_t - Y_test == -1)[0])
TP = len(np.where(preds_test_t + Y_test == 2)[0])
TN = len(np.where(preds_test_t + Y_test == 0)[0])
cmat = [[TP, FN], [FP, TN]]
print(cmat)
x = ["pipe", "no pipe"]
y = ["pipe", "no pipe"]

plt.figure(figsize = (6,6))
sns.heatmap(cmat/np.sum(cmat), cmap="Reds", annot=True, fmt = '.2%', square=1,   linewidth=2.,
            xticklabels= x, yticklabels= y)

plt.xlabel("predictions")
plt.ylabel("target values")
plt.show()
plt.savefig("heatmap.png")

--> Returned matrix: [[718370, 0], [113023, 8753183]]

Confusion matrix


Solution

  • Solved:

    I had a boolean matrix and an integer matrix

    FN = 0
    preds = np.ravel(preds_test_t)*1
    target = np.ravel(Y_test)*1
    print(len(preds))
    for i in range(0, len(preds)):
      if (preds[i] == 0 and target[i] == 1):
        FN += 1