Search code examples
deep-learningpytorchconfusion-matrix

I am having trouble calculating the accuracy, recall, precision and f1-score for my model


I have got my confusion matrix working correctly, just having some trouble producing the scores. A little help would go a long way. I am currently getting the error. "Tensor object is not callable".

def get_confused(model_ft):
    nb_classes = 120
    from sklearn.metrics import precision_recall_fscore_support as score
    confusion_matrix = torch.zeros(nb_classes, nb_classes)
    with torch.no_grad():
        for i, (inputs, classes) in enumerate(dataloaders['val']):
            inputs = inputs.to(device)
            classes = classes.to(device)
            outputs = model_ft(inputs)
            _, preds = torch.max(outputs, 1)
            for t, p in zip(classes.view(-1), preds.view(-1)):
                    confusion_matrix[t.long(), p.long()] += 1

            cm = confusion_matrix(classes, preds)
            recall = np.diag(cm) / np.sum(cm, axis = 1)
            precision = np.diag(cm) / np.sum(cm, axis = 0)
    print(confusion_matrix)
    print(confusion_matrix.diag()/confusion_matrix.sum(1))

Solution

  • The problem is with this line.

    cm = confusion_matrix(classes, preds)
    

    confusion_matrix is a tensor and you can't call it like a function. Hence Tensor is not callable. I am also, not sure why you need this line. Instead, I think you would want to write cm= confusion_matrix.cpu().data.numpy() to make it a numpy array I think. From your code, it seems cm is np.array.