Reputation: 28
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))
Upvotes: 0
Views: 1749
Reputation: 121
You can try the below code,
def F_score(logit, label, threshold=0.5, beta=2):
prob = torch.sigmoid(logit)
prob = prob > threshold
label = label > threshold
TP = (prob & label).sum().float()
TN = ((~prob) & (~label)).sum().float()
FP = (prob & (~label)).sum().float()
FN = ((~prob) & label).sum().float()
accuracy = (TP+TN)/(TP+TN+FP+FN)
precision = torch.mean(TP / (TP + FP + 1e-12))
recall = torch.mean(TP / (TP + FN + 1e-12))
F2 = (1 + beta**2) * precision * recall / (beta**2 * precision + recall + 1e-12)
return accuracy, precision, recall, F2.mean(0)
call the funciton as,
accuracy, precision, recall, F1_score = F_score(output.squeeze(), labels.float())
Reference:- https://www.kaggle.com/c/human-protein-atlas-image-classification/discussion/73246
Upvotes: 2
Reputation: 16450
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
.
Upvotes: 2