Label Checking in CIFAR-10

Hi! I trained a ResNet model and I want to write an “if” condition for the times that my model predicted correctly and the image was a dog.
this is my data loader:

test_loader = torch.utils.data.DataLoader(dataset=ds,
                                          batch_size=100,
                                          shuffle=False)

And here is the part where I want to put the condition line in:

    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        if(torch.equal(predicted, labels)):
               if (?)

how do I write the condition given the fact that both “labels” and “predicted” are tensors?

Your code does not match what you described. Your current if condition is far stronger than what you say you want. To see this, try replacing

if (?)

with

print("Found it!")

and see if the behaviour is as you expect it to be.

I want the activation of the layer when it has not only predicted correctly but also when that pic was indeed a dog!

Could it be that you overlooked, that your batchsize is 100 not 1?
Could you give me a bit more information:

Predicted what correctly? One out of the 100 images in your batch? All of the 100 images in your batch? All dog images out of the 100 images in your batch?

By the way, this will only be True if all 100 images in your batch are correct.

Again, what image? One dog image of the 100 images? All dog images?

1 Like

Thank you for your answer.
No my batchsize is 1.
I have made a dataset with 1000 pics of dogs and 1000 pics of non-dogs. now when I only write

if(torch.equal(predicted, labels)):

it tells me that my model has predicted 1827 pics correctly. but I would like to know how many of them were actually dog pics and which ones.

I have changed the batchsize to 1 . what I originally posted was incoorect

Ok. If you batchsize is 1 than you just have to check for your label:

for images, labels in test_loader:
    images = images.to(device)
    labels = labels.to(device)
    outputs = model(images)
    _, predicted = torch.max(outputs.data, 1)
    if(torch.equal(predicted, labels)):
        if labels.item() == 5:
            num_dog_label += 1
            if predicted.item() == 5:
                num_correct_dog += 1

I took 5 because that is the number that represents dog in the cifar10 dataset.
cifar10 classes are ordered like this:

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

But since you said you only have 2 classes it might be either 0 or 1. Try printing your labels tensor and check for yourself.

Just in case you do want to use higher batchsize. Try something like:

for i, label in enumerate(labels):
    if label == 5:
        num_dog_label += 1
        if predicted[i] == 5:
            num_correct_dog += 1

accuracy = 100*num_correct_dog/num_dog_label

print(f"number of dog labels: {num_dog_label}")
print(f"number of correctly detected dog labels: {num_correct_dog}")
print(f"dog classification accuracy: {accuracy:.2f}%")
1 Like

It works! Thank you so much :pray:

1 Like