# Binary semantic segmentation using SegFormer

**URL:** <https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745>\
**Category:** 🤗Transformers\
**Created:** [November 8, 2022, 4:14pm UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745 "2022-11-08T16:14:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivor22](https://avatars.discourse-cdn.com/v4/letter/i/f19dbf/32.png) [@Ivor22](https://discuss.huggingface.co/u/Ivor22)\
**Post date:** [November 8, 2022, 4:14pm UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745/1 "2022-11-08T16:14:46Z")

</div>

Hello, I’m trying to perform binary segmentation, using SegFormer on a custom dataset, where the mask (PNG grayscale image) consists of value **1** for object of interest and **0** for everything else. I’ve used [this](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/SegFormer/Fine_tune_SegFormer_on_custom_dataset.ipynb) tutorial and therefore I applied `reduce_labels=True` which leaves me with label **0** for object and **255** for background. However, when I pass labels to model I’m getting this error:

```auto
ValueError: The number of labels should be greater than one

```

How should I fix it, please? Should I consider region outside of my object not as a background, but rather as another label?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [November 8, 2022, 8:53pm UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745/2 "2022-11-08T20:53:46Z")

</div>

Hi,

The current implementation of SegFormer actually only supports multi-class classification per pixel, using the regular CrossEntropyLoss.

For binary classification per pixel, one should actually use the [BCEWithLogitsLoss](https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html) (which applies a sigmoid activation on the logits + computes cross-entropy loss).

I’ll add support for this! Thanks for bringing this up.

---

<div class="post-metadata">

**Author:** ![Ivor22](https://avatars.discourse-cdn.com/v4/letter/i/f19dbf/32.png) [@Ivor22](https://discuss.huggingface.co/u/Ivor22)\
**Post date:** [November 8, 2022, 10:24pm UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745/3 "2022-11-08T22:24:49Z")

</div>

Hello,

Thank you for your answer. I’m still wondering if this couldn’t be overcome by pretending there are two classes (object and not object), therefore two labels and no background. Or something like a virtual background on **255** which simply won’t be present in training data.

Thanks.

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [November 9, 2022, 8:37am UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745/4 "2022-11-09T08:37:40Z")

</div>

Yes that’s totally possible, in that case you can just use the regular cross-entropy loss. Segmentation maps should in that case contain values 0 and 1 (which the model needs to learn for each pixel), and you can use 255 for pixels you want to ignore (i.e. the model shouldn’t learn). So you can set config.num\_labels to 2 in that case.

---

<div class="post-metadata">

**Author:** ![Ivor22](https://avatars.discourse-cdn.com/v4/letter/i/f19dbf/32.png) [@Ivor22](https://discuss.huggingface.co/u/Ivor22)\
**Post date:** [November 16, 2022, 12:29pm UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745/5 "2022-11-16T12:29:37Z")

</div>

Hello,

Thank you very much. Can you please reply when you add support for BCEWithLogitsLoss?

Thanks

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [November 16, 2022, 3:21pm UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745/6 "2022-11-16T15:21:37Z")

</div>

Hi,

@Ivor22, I’ve opened a PR here: [[SegFormer] Add support for segmentation masks with one label by NielsRogge · Pull Request #20279 · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/pull/20279).

If it works fine for you, I’ll add it to all other models.

---

<div class="post-metadata">

**Author:** ![ollibolli](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ollibolli/32/16121_2.png) [@ollibolli](https://discuss.huggingface.co/u/ollibolli)\
**Post date:** [July 26, 2023, 9:54am UTC](https://discuss.huggingface.co/t/binary-semantic-segmentation-using-segformer/25745/7 "2023-07-26T09:54:44Z")

</div>

As this seems to work now, should I use `num_labels = 1` in this case?  
I think the documentation still only refers and explains the `num_labels >1` case?

And apologize, from the github pull request, is this now automatically using dice loss?
