Is it possible, using opencv, to test one single image in a confusion matrix with a model that is already trained? If is possible, can someone give me a code example?
Thank you
What do you mean by testing one single image in a confusion matrix?
After train a cnn with mnist, make a confusion matrix with the classes from mnist and a single image, to test the prediction of the model. Do you understand now?
To create a confusion matrix you can use
def get_preds(model, data_loader):
with torch.no_grad():
preds = torch.tensor([]) # It will hold tuple (predicted_label, real_label)
for batch in loader:
images, labels = batch # labels are already real_labels (0,1,2, ...)
pred = model(images) # pred.shape = [batch_size, num_classes]
pred = torch.argmax(-1) # this will give you index of the predicted class
preds = torch.cat((pred, labels), dim=0)
return preds # shape is [total_images, 2]
# Now you can use a for loop to get the confusion matrix
num_classes = 4
c_mat = torch.zeros(num_classes, num_classes, dtype=torch.int64)
for p in preds:
i,j = p[0], p[1] # Add code here to convert indices to int
c_mat[i,j] += 1
I still don’t understand what you mean by and a single image
.
Can you explain the purpose of using a single image in confusion matrix?
MNIST has 10 classes so confusion matrix will be 10x10. By testing only one image, you get a single 1
in 10x10
of zeros. You cannot calculate recall, precision, f1, etc on such a matrix.
Of course, i will try to explain better. I want to test one image, to check in wich class my model predict that image. I can use a confusion matrix for that, or not?
You do not need confusion matrix for a single image. Confusion matrix is used for large number of inputs to get statistical measurement of model’s behavior. For instance, in training stage, you may have report Accuracy. This optained from confusion matrix, so for single image, it is 0 or 100% accuracy.
For any number of input images, depends on your model, you get a result like [samples, classes]
. For a single image it is like pred=[1, classes]= [[0.1, 0.9, ..., 0.3]]
. By taking argmax over this tensor, you will get the index of predicted class which in our example it would be torch.argmax(pred, dim=-1)
. It will return 1
as the index of maximum. So, there is no need to define confusion matrix for a single image.
Your model predict class 1
and you already have true label.