Getting zero accuracy

username: chetan06
cooked: <p>I am working on dog breed identification problem. While training I am getting zero accuracy though my loss is decreasing after each epoch. I can’t get where I am wrong so please help. I have used a dataset of images which is being loaded in the following class using root directory. The label passed to the following class is  a pandas dataframe where the first column denotes id, i.e., the name of the file using which we can load the image. The second one is the breed_name( total 120 breeds ) and third is the labels or targets from 0 to 119.</p>
<pre><code class="lang-auto">class DogsDataset(Dataset):
    def __init__(self, labels, root_dir, transform=None):
        self.labels = labels
        self.root_dir = root_dir
        self.transform = transform
    
    def __len__(self):
        return self.labels.shape[0]
    
    def __getitem__(self, idx):
        img_name = '{}.jpg'.format(self.labels.iloc[idx, 0])
        fullname = self.root_dir+ img_name
        image = Image.open(fullname)
        label = torch.tensor(labels.iloc[idx,2])
        if self.transform:
            image = self.transform(image)
        return [image, label]

</code></pre>
<p>Used SGD as optimizer and cross entrpy loss as loss function</p>
<pre><code class="lang-auto"># Creating a model
use_gpu = torch.cuda.is_available()
model = models.resnet50(pretrained=True)
if use_gpu:     
    model = model.cuda()
for param in model.parameters():
    param.requires_grad = False
if use_gpu:
    model.fc = (nn.Linear(2048, num_classes)).cuda()
else:
    model.fc = (nn.Linear(2048, num_classes))


def train(model, criterion, validaton_data, train_data,  optimizer, epochs = 30):
    use_gpu = torch.cuda.is_available()
    train_loss_history = []
    valid_loss_history = []
    train_loader = {
        'train' : train_data,
        'validation' : validation_data
    }
    best_acc = 0
    best_model = None
    for epoch in tqdm(range(epochs), desc = "Loading", position = 0):
        for dataset in ['train', 'validation']:

            running_loss = 0
            running_corrects = 0
            for x, y in tqdm(train_loader[dataset]):
                optimizer.zero_grad()
                if use_gpu:
                    x, y = Variable(x.cuda()), Variable(y.cuda())
                else:
                    x, y = Variable(x), Variable(y)
                yhat = model(x)
                _, preds = yhat.max(1)
                loss = criterion(yhat, y)
                loss.backward()
                optimizer.step()
                #loss_history.append(loss.item())
                running_loss += loss.item()*x.size(0)
                running_corrects += (torch.sum(preds == y))
                                
            if dataset == 'train':
                train_loss = running_loss / 8151
                train_acc = float(running_corrects / 8151)
                print
                print("train_loss: ", train_loss)
                print("train_accuracy: ", train_acc)
                train_loss_history.append(train_loss)
            else:
                valid_loss = running_loss / 2071
                valid_acc = float(running_corrects / 2071)
                print("validation_ loss: " , valid_loss)
                print("validation_accuracy: " , valid_acc)
                valid_loss_history.append(valid_loss)
        if valid_acc &gt; best_acc:
            best_acc = valid_acc
            best_model = model

    print()
    print("best accuracy : ", best_acc)
        
    return best_model
</code></pre>
<p>The transforms that I have used</p>
<pre><code class="lang-auto">normalize = transforms.Normalize(
   mean=[0.485, 0.456, 0.406],
   std=[0.229, 0.224, 0.225]
)

train_transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(200),
    transforms.ColorJitter(brightness=[0.5, 1], contrast=[0.5, 1], saturation=[0.5, 1]),  
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ToTensor(),
    normalize
])
</code></pre>

username: iamgroot42
cooked: <p>What version of Python are you on? The code seems okay (at a quick glance). The only possible reason for it to be doing what it is doing is a potential error in division, for eg.</p>
<aside class="quote no-group" data-username="chetan06" data-post="1" data-topic="80193">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chetan06/48/45417_2.png" class="avatar"> chetan06:</div>
<blockquote>
<p>float(running_corrects / 8151)</p>
</blockquote>
</aside>
<p>Can you try printing the actual value of <code>running_corrects</code> and replace the above with <code>running_corrects/8151.0</code>, while you’re at it?</p>

username: chetan06
cooked: <p>I thought the same first but on seeing the running_loss expression it seems that it works quite well as I am getting the loss after each epoch. Thinking the same I had added float in the starting.</p>

username: chetan06
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> and others please help</p>

username: iamgroot42
cooked: <p>So are you saying <code>running_corrects</code> is exactly 0 when you print it? Even with a model that makes random predictions, this seems highly unlikely.</p>

username: chetan06
cooked: <p>If you want to see I can share my whole code here. Should I?</p>

username: ATUL_YADAV1
cooked: <p>I am also getting this same error accuracy is 0. Did you find any solution ?</p>

username: chetan06
cooked: <p>For the same problem?</p>

username: chetan06
cooked: <p>I applied your idea, increased the learning rate and also the number of epochs and I am getting some accuracy value, though not enough but yeah better than zero <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:">.</p>

username: ATUL_YADAV1
cooked: <p>no, for detection but i am getting accuracy 0 and i tried to print out the correct prediction and its greater than 0.</p>

username: chetan06
cooked: <p>Then you might be committing the same mistake as I did before that is taking integer division which would always be returning integer.</p>

username: iamgroot42
cooked: <p>If it’s still low, I’d suggest toying around with hyperparameters and architectures. As a sanity check, you might want to:</p>
<ol>
<li>Make sure you’ve defined data loaders properly (image-label mapping) by inspecting a few examples.</li>
<li>Start with a pre-trained feature extractor and see how a model on top of those features performs.</li>
</ol>

username: ATUL_YADAV1
cooked: <p>I have also applied your solution and got acc &gt; 0 but still it is very low and i am using resnet34 for my detection task. And i have tried few samples to check if my data is correct and there is no problem in my data</p>
<p>and here is the link for my post.</p>
<aside class="quote quote-modified" data-post="1" data-topic="80177">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/atul_yadav1/48/23902_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/error-is-increasing-in-single-object-detection/80177">Error is increasing in Single Object detection</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    Hi, I am trying to detect the largest item(Single object detection) and I have used resnet34. 
and I am using L1 loss for bounding box and Cross-Entropy for classification but my loss in training is quite low around 20-30 but invalidation the loss is above 200. And Accuracy is always 0 
Any suggestions on how can I solve this problem. 
`Custom head for resnet34 -



custom_fc = nn.Sequential(
Flatten(),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512*7*7, 256),
nn.ReLU(),
nn.BatchNorm1d(256),
nn.Dropo…
  </blockquote>
</aside>

<p>Any suggestions why its happening</p>

username: chetan06
cooked: <p>Yeah I saw the same problem with my model when I trained it today. Actually I think we should look more into augmentation and better hyperparameter search for better result.</p>

username: ATUL_YADAV1
cooked: <p>I have implemented classifier and bounding box detection separately and they both performed decently but when i combine both these models results are not good.</p>

username: chetan06
cooked: <p>I don’t have any working experience with object detection algorithms and just know their concepts. But I have a source that you could refer to.<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars3.githubusercontent.com/u/10481431?s=400&amp;v=4" class="thumbnail onebox-avatar" width="400" height="400">

<h3><a href="https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection" target="_blank" rel="nofollow noopener">sgrvinod/a-PyTorch-Tutorial-to-Object-Detection</a></h3>

<p>SSD: Single Shot MultiBox Detector | a PyTorch Tutorial to Object Detection - sgrvinod/a-PyTorch-Tutorial-to-Object-Detection</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

username: ATUL_YADAV1
cooked: <p>Thanks, I will look into it.</p>

username: Kenneth
cooked: <p>I had a similar problem.<br>
I had to convert the 1d tensors into int or float to compute the accuracy.</p>