# Understanding ignore\_index and reduce\_labels

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funderstanding-ignore-index-and-reduce-labels%2F64587%3E%5C
**Category:** Beginners\
**Created:** [December 4, 2023, 10:34pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funderstanding-ignore-index-and-reduce-labels%2F64587 "2023-12-04T22:34:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickponline](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nickponline/32/21840_2.png) [@nickponline](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnickponline)%5C
**Post date:** [December 4, 2023, 10:34pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funderstanding-ignore-index-and-reduce-labels%2F64587%2F1 "2023-12-04T22:34:51Z")

</div>

Hi there, I am using Mask2Former for a simple segmentation tasks and am struggling to understand if I should use ignore\_index and reduce\_labels. My dataset is pretty simple each image contains shapes that are either. Circles, triangles or rectangles. Here is an example input image:

 ![011](https://us1.discourse-cdn.com/hellohellohello/original/3X/8/1/817b8f6f62ffc5e7825afebec3decec675f4f165.jpeg)

And the corresponding masks represent circles with 1, triangles with 2 and rectangles with 3. Every other pixel is 0 in the masks.

So should I used ignore\_index and reduce\_labels and if so what should the values be?

---

<div class="post-metadata">

**Author:** ![panigrah](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@panigrah](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpanigrah)%5C
**Post date:** [December 5, 2023, 2:57am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funderstanding-ignore-index-and-reduce-labels%2F64587%2F2 "2023-12-05T02:57:55Z")

</div>

Ignore\_index is the index that is ignored for loss calculations etc. set to 255 - as that’s used in standard loss functions I believe. Some models default to 255 and others don’t set it - not sure exactly why. This will take your background pixels and also unlabelled pixels and set them to the ignore\_index value

Reduce\_labels - set to True. reindexes the label values so that they start from 0 after the background is set to 255

---

<div class="post-metadata">

**Author:** ![nickponline](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nickponline/32/21840_2.png) [@nickponline](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnickponline)%5C
**Post date:** [December 5, 2023, 6:42am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funderstanding-ignore-index-and-reduce-labels%2F64587%2F3 "2023-12-05T06:42:18Z")

</div>

So in my example case I should use ignore\_index=0 and reduce\_labels=True ?

---

<div class="post-metadata">

**Author:** ![panigrah](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@panigrah](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpanigrah)%5C
**Post date:** [December 5, 2023, 3:43pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funderstanding-ignore-index-and-reduce-labels%2F64587%2F4 "2023-12-05T15:43:44Z")

</div>

Set ignore\_index to 255 and reduce\_labels to true

Here is an example, see the note in

> **[Train a MaskFormer Segmentation Model with Hugging Face Transformers -...](https://pyimagesearch.com/2023/03/13/train-a-maskformer-segmentation-model-with-hugging-face-transformers/)**
>
> Train a MaskFormer segmentation model with Hugging Face Transformers.

---

<div class="post-metadata">

**Author:** ![xwen022](https://avatars.discourse-cdn.com/v4/letter/x/bb73d2/32.png) [@xwen022](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fxwen022)%5C
**Post date:** [August 1, 2024, 11:04pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funderstanding-ignore-index-and-reduce-labels%2F64587%2F5 "2024-08-01T23:04:57Z")

</div>

If the background class index is 0, can we just set the ignore\_index to 0 and keep the reduce\_labels to False?
