Ignore_index() in nn.CrossEntropyLoss() for semantic segmentation

username: tpostadj
cooked: <p>I am trying to train a fully convolutional net from scratch for a semantic segmentation task, but the training set I have is sparse, meaning that I have to ignore pixels that do not contain information (label=0) while training.<br>
Otherwise, I have 5 classes I am interested to retrieve.<br>
To achieve that, I just added the argument ignore_index to the cross entropy loss function to drop the 0-labeled pixels.<br>
However, I am confused because I would expect the net to output 5 scores (for the 5 classes of interest), although it also gives me a score for the class 0.</p>
<p>Does anyone have any ideas on why I get 6 class scores, expecting only 5 ?</p>

username: ptrblck
cooked: <p>The <code>ignore_index</code> argument just masks the loss for the current class.<br>
From the docs:</p>
<blockquote>
<p>ignore_index (int, optional) – Specifies a target value that is ignored and does not contribute to the input gradient. When size_average is True, the loss is averaged over non-ignored targets.</p>
</blockquote>
<p>If you would like to ignore it in the prediction, you could just slice the output:</p>
<pre><code class="lang-auto">torch.randn(1, 5)
_, preds = torch.max(output[0, 1:], 0)
</code></pre>

username: tpostadj
cooked: <p>Thank you,<br>
I hesitated slicing the output score vector since I am not sure of what to do if, at prediction, the ignored class gets a very high score and the other ones (non-ignored) are likely equal…</p>

username: TitsHunter
cooked: <p>Thanks man. Helps a lot.</p>

username: sanchit2843
cooked: <p>Hey, I was facing a similar issue but I am not sure why it is working here (there are total 19 classes in cityscape dataset with one ignore label, since with ignore label there will be 20 classes, the model they should create must have 20 classes but it is not the case, they are using only 19 classes. When I try to do similar thing(although with a different pipeline), the assertion error of class comes). Please check this out, thanks in advance.<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/HRNet/HRNet-Semantic-Segmentation/blob/master/lib/datasets/cityscapes.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/HRNet/HRNet-Semantic-Segmentation/blob/master/lib/datasets/cityscapes.py" target="_blank" rel="nofollow noopener">HRNet/HRNet-Semantic-Segmentation/blob/master/lib/datasets/cityscapes.py</a></h4>
<pre><code class="lang-py"># ------------------------------------------------------------------------------
# Copyright (c) Microsoft
# Licensed under the MIT License.
# Written by Ke Sun (sunk@mail.ustc.edu.cn)
# ------------------------------------------------------------------------------

import os

import cv2
import numpy as np
from PIL import Image

import torch
from torch.nn import functional as F

from .base_dataset import BaseDataset

class Cityscapes(BaseDataset):
    def __init__(self, 
                 root, 
</code></pre>

  This file has been truncated. <a href="https://github.com/HRNet/HRNet-Semantic-Segmentation/blob/master/lib/datasets/cityscapes.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/HRNet/HRNet-Semantic-Segmentation/blob/master/lib/core/criterion.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/HRNet/HRNet-Semantic-Segmentation/blob/master/lib/core/criterion.py" target="_blank" rel="nofollow noopener">HRNet/HRNet-Semantic-Segmentation/blob/master/lib/core/criterion.py</a></h4>
<pre><code class="lang-py"># ------------------------------------------------------------------------------
# Copyright (c) Microsoft
# Licensed under the MIT License.
# Written by Ke Sun (sunk@mail.ustc.edu.cn)
# ------------------------------------------------------------------------------

import torch
import torch.nn as nn
from torch.nn import functional as F

class CrossEntropy(nn.Module):
    def __init__(self, ignore_label=-1, weight=None):
        super(CrossEntropy, self).__init__()
        self.ignore_label = ignore_label
        self.criterion = nn.CrossEntropyLoss(weight=weight, 
                                             ignore_index=ignore_label)

    def forward(self, score, target):
        ph, pw = score.size(2), score.size(3)
        h, w = target.size(1), target.size(2)
</code></pre>

  This file has been truncated. <a href="https://github.com/HRNet/HRNet-Semantic-Segmentation/blob/master/lib/core/criterion.py" target="_blank" rel="nofollow noopener">show original</a>

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


username: ptrblck
cooked: <p>Is this a double post from <a href="https://discuss.pytorch.org/t/when-to-use-ignore-index/5935/6">here</a> or another issue we should have a look at? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>