---
title: "Fine-Tune a Semantic Segmentation Model with a Custom Dataset"
description: "SegFormer, a hierarchical Transformer encoder with a lightweight MLP decoder, can be fine-tuned for semantic segmentation of sidewalk images for a delivery robot using Hugging Face transformers and datasets."
url: "https://swe.quest/articles/fine-tune-a-semantic-segmentation-model-with-a-custom-dataset-99839187c9e4f9fb"
canonical: "https://huggingface.co/blog/fine-tune-segformer"
source: "Hugging Face"
published: "2022-03-17"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["computer-vision","model-training","python","machine-learning"]
---

# Fine-Tune a Semantic Segmentation Model with a Custom Dataset

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-03-17, 9 min read, How-to. Original: [https://huggingface.co/blog/fine-tune-segformer](https://huggingface.co/blog/fine-tune-segformer)

## Summary

SegFormer, a hierarchical Transformer encoder with a lightweight MLP decoder, can be fine-tuned for semantic segmentation of sidewalk images for a delivery robot using Hugging Face transformers and datasets. The walkthrough loads the segments/sidewalk-semantic dataset, applies on-the-fly preprocessing with SegformerImageProcessor and ColorJitter augmentation, and fine-tunes the 14MB B0 variant with the Trainer API (lr 6e-5, batch size 2, 50 epochs). Evaluation uses mean IoU, which requires upsampling the model's quarter-resolution logits to label size. Inference follows the same pattern of bilinear interpolation plus argmax, and the model is pushed to the Hub with an auto-generated model card.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Computer vision](https://swe.quest/tags/computer-vision.md) (AI & ML)
- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)

## Prerequisites

- PyTorch basics
- Python ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- image classification concepts

## Keywords

SegFormer, Hugging Face Transformers, Hugging Face Datasets, Hugging Face Hub, Segments.ai, mean IoU, ColorJitter

## Related posts

- [Universal Image Segmentation with Mask2Former and OneFormer](https://swe.quest/articles/universal-image-segmentation-with-mask2former-and-oneformer-c9679decf8636d8b.md): Hugging Face · 2023-01-19 · Medium · 7 min read · Computer vision, Machine learning, Python
  Instance, semantic and panoptic segmentation have converged on "universal" architectures that treat every task as binary mask classification rather than per-pixel classification, a line running from DETR through MaskFormer to Mask2Former.
- [Timm ❤️ Transformers: Use any timm model with transformers](https://swe.quest/articles/timm-transformers-use-any-timm-model-with-transformers-ae757824718fc026.md): Hugging Face · 2025-01-16 · Medium · 10 min read · Computer vision, Model training, Inference & serving, Python
  TimmWrapper lets any timm vision model load through Hugging Face transformers' Auto classes and pipeline API.
- [Deploy Hugging Face models easily with Amazon SageMaker](https://swe.quest/articles/deploy-hugging-face-models-easily-with-amazon-sagemaker-e09e9e34ef94ffa8.md): Hugging Face · 2021-07-08 · Medium · 9 min read · Inference & serving, Cloud platforms, ML platforms, Python
  Hugging Face Inference Deep Learning Containers and the SageMaker Hugging Face Inference Toolkit let Transformers models be deployed as SageMaker endpoints with a few lines of Python, using transformers pipelines for zero-code pre- and post-processing.
- [Hosting your Models and Datasets on Hugging Face Spaces using Streamlit](https://swe.quest/articles/hosting-your-models-and-datasets-on-hugging-face-spaces-using-streamlit-a8983f0b0bfbdd51.md): Hugging Face · 2021-10-05 · Medium · 3 min read · Python, Machine learning, Deployment
  Streamlit apps can showcase Hugging Face models and datasets and be hosted on Hugging Face Spaces.
- [Open-sourcing Knowledge Distillation Code and Weights of SD-Small and SD-Tiny](https://swe.quest/articles/open-sourcing-knowledge-distillation-code-and-weights-of-sd-small-and-sd-tiny-053ea66ae7ff27cc.md): Hugging Face · 2023-08-01 · Medium · 5 min read · Computer vision, Model training, Inference & serving, Open source
  Segmind releases training code and weights for SD-Small and SD-Tiny, Stable Diffusion variants compressed by block-removal knowledge distillation that cut UNet parameters by 35% and 55% while keeping image fidelity close to the base model.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
