Label Checking in CIFAR-10

username: Nil_MSh
cooked: <p>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.<br>
this is my data loader:</p>
<pre><code class="lang-auto">test_loader = torch.utils.data.DataLoader(dataset=ds,
                                          batch_size=100,
                                          shuffle=False)
</code></pre>
<p>And here is the part where I want to put the condition line in:</p>
<pre><code class="lang-auto">    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 (?)
</code></pre>
<p>how do I write the condition given the fact that both “labels” and “predicted” are tensors?</p>

username: gphilip
cooked: <p>Your code does not match what you described. Your current <code>if</code> condition is far stronger than what you say you want. To see this, try replacing</p>
<pre><code class="lang-auto">if (?)
</code></pre>
<p>with</p>
<pre><code class="lang-auto">print("Found it!")
</code></pre>
<p>and see if the behaviour is as you expect it to be.</p>

username: Nil_MSh
cooked: <p>I want the activation of the layer when it has not only predicted correctly but also when that pic was indeed a dog!</p>

username: RaLo4
cooked: <p>Could it be that you overlooked, that your batchsize is 100 not 1?<br>
Could you give me a bit more information:</p>
<aside class="quote no-group" data-username="Nil_MSh" data-post="1" data-topic="130294">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/e495f1/48.png" class="avatar"> Nil_MSh:</div>
<blockquote>
<p>an “if” condition for the times that my model predicted correctly</p>
</blockquote>
</aside>
<p>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?</p>
<aside class="quote no-group" data-username="Nil_MSh" data-post="1" data-topic="130294">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/e495f1/48.png" class="avatar"> Nil_MSh:</div>
<blockquote>
<p><code>if(torch.equal(predicted, labels)):</code></p>
</blockquote>
</aside>
<p>By the way, this will only be <code>True</code> if all 100 images in your batch are correct.</p>
<aside class="quote no-group" data-username="Nil_MSh" data-post="1" data-topic="130294">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/e495f1/48.png" class="avatar"> Nil_MSh:</div>
<blockquote>
<p>and the image was a dog</p>
</blockquote>
</aside>
<p>Again, what image? One dog image of the 100 images? All dog images?</p>

username: Nil_MSh
cooked: <p>Thank you for your answer.<br>
No my batchsize is 1.<br>
I have made a dataset with 1000 pics of dogs and 1000 pics of non-dogs. now when I only write</p>
<pre><code class="lang-auto">if(torch.equal(predicted, labels)):
</code></pre>
<p>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.</p>

username: Nil_MSh
cooked: <p>I have changed the batchsize to 1 . what I originally posted was incoorect</p>

username: RaLo4
cooked: <p>Ok. If you batchsize is 1 than you just have to check for your label:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>I took 5 because that is the number that represents dog in the cifar10 dataset.<br>
cifar10 classes are ordered like this:</p>
<pre><code class="lang-auto">classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
</code></pre>
<p>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.</p>
<p>Just in case you do want to use higher batchsize. Try something like:</p>
<pre><code class="lang-auto">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}%")
</code></pre>

username: Nil_MSh
cooked: <p>It works! Thank you so much <img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=10" title=":pray:" class="emoji" alt=":pray:"></p>