Here is my code snippet to produce confusion matrix: I am wondering how can I change the color of boxes in confusion matrix for those boxes which are not located in diagonal same as heatmap using sklearn.
nb_classes = 15
confusion_matrix = torch.zeros(nb_classes, nb_classes)
with torch.no_grad():
for i, (inputs, target, classes, im_path) in enumerate(dataLoaders['test']):
inputs = inputs.to(device)
target = target.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
for t, p in zip(target.view(-1), preds.view(-1)):
confusion_matrix[t.long(), p.long()] += 1
num_classes = 15
class_names = ['A2CH', 'A3CH', 'A4CH_LV', 'A4CH_RV', 'A5CH', 'Apical_MV_LA_IAS',
'OTHER', 'PLAX_TV', 'PLAX_full', 'PLAX_valves', 'PSAX_AV', 'PSAX_LV',
'Subcostal_IVC', 'Subcostal_heart', 'Suprasternal']
plt.figure()
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)
tick_marks = numpy.arange(num_classes)
classNames = class_names
thresh = confusion_matrix.max() / 2.
for i in range(confusion_matrix.shape[0]):
for j in range(confusion_matrix.shape[1]):
plt.text(j, i, format(confusion_matrix[i, j]),
ha="center", va="center",
color="white" if confusion_matrix[i, j] == 0 or confusion_matrix[i, j] > thresh else "black")
plt.tight_layout()
plt.colorbar()
return plt
plt.show()