56 Fine Tune Segformer
This guide shows how you can fine-tune Segformer, a state-of-the-art semantic segmentation model. Our goal is to build a model for a pizza delivery robot, so it can see where to drive and recognize obstacles 🍕🤖. We'll first label a set of sidewalk images on Segments.ai. Then we'll fine-tune a pre-trained SegFormer model by using 🤗 transformers, an open-source library that offers easy-to-use implementations of state-of-the-art models. Along the way, you'll learn how to work with the Hugging Face hub, the largest open-source catalog of models and datasets.
Semantic segmentation is the task of classifying each pixel in an image. You can see it as a more precise way of classifying an image. It has a wide range of use cases in fields such as medical imaging and autonomous driving. As an example, for our pizza delivery robot, it is important to know exactly where the sidewalk is in an image, not just whether there is a sidewalk or not.
Because semantic segmentation is a type of classification, the network architectures that are used for image classification and semantic segmentation are very similar. In 2014, a seminal paper by Long et al. used convolutional neural networks for semantic segmentation. More recently, Transformers have been used for image classification (e.g. ViT), and now they're also being used for semantic segmentation, pushing the state-of-the-art further.
SegFormer is a model for semantic segmentation introduced by Xie et al in 2021. It has a hierarchical Transformer encoder that doesn't use positional encodings (in contrast to ViT) and a simple multi-layer perceptron decoder. SegFormer achieves state-of-the-art performance on multiple common datasets. Let's see how it performs for sidewalk images in our pizza delivery robot.

Let's get started by installing the necessary dependencies and logging in to Hugging Face.
1. Create/choose a dataset
The first step in any ML project is assembling a good dataset. In order to train a semantic segmentation model, we need a dataset with semantic segmentation labels. We can either use an existing dataset from the Hugging Face Hub, such as ADE20k, or create our own dataset.
For our pizza delivery robot, we could use an existing autonomous driving dataset such as CityScapes or BDD100K. However, these datasets were captured by cars driving on the road. Since our delivery robot will be driving on the sidewalk, there will be a mismatch between the images in these datasets and the data our robot will see in the real world.
We don't want our delivery robot to get confused, so we'll create our own semantic segmentation dataset using images captured on sidewalks. In the next steps, we'll show how you can label the images we captured. If you just want to use our finished labeled dataset, you can skip the "Create your own dataset" section and continue from "Use a dataset from the Hub".
Create your own dataset
To create your own semantic segmentation dataset, you'll need two things: 1) images covering the situations your model will encounter in the real world, 2) segmentation labels, i.e. images where each pixel represents a class/category.
We went ahead and captured a thousand images of sidewalks in Belgium. Collecting and labeling such a dataset can take a long time, so you can also start with a smaller dataset, and expand it if the model does not perform well enough.

To obtain segmentation labels, we need to indicate the classes of all the regions/objects in these images. This can be a time-consuming endeavour, but using the right tools can speed up the task significantly. For labeling, we'll use Segments.ai, since it has smart labeling tools for image segmentation, and an easy-to-use Python SDK.
Set up the labeling task on Segments.ai
First, create an account at https://segments.ai/join. Next, you can create a dataset by using the web interface, or via the Python SDK. Here, we'll show how you can create a dataset programmatically.
We'll start by initializing the Segments.ai client using an API key. This key can be found on the account page.
Next, we'll create a new dataset by choosing a name and by listing the different categories we want to label.
The next cell contains the task_attributes with the categories we want to label. The format for task_attributes is defined in the docs.
Now we can add images to the dataset. As an example, we'll add 10 examples images from the sidewalk dataset using segments_client.add_sample().
If you don't have URLs for the images you want to upload, you can use segments_client.upload_asset() first, see this example.
Alternatively, you can also drag and drop your files to the Samples tab of your dataset.
Label the images
Now that the raw data is loaded, go to segments.ai/home and open the newly created dataset. Click "Start labeling" and create segmentation masks. You can use the ML-powered superpixel and autosegment tools to label faster.

Tip: when using the superpixel tool, scroll to change the superpixel size, and click and drag to select segments.
Push the result to the Hugging Face Hub
When you're done labeling, create a new dataset release containing the labeled data. You can either do this on the releases tab on Segments.ai, or programmatically through the SDK as shown below.
Note that creating the release can take a few seconds. You can check the releases tab on Segments.ai to check if your release is still being created.
Now, we'll use the release2dataset function to convert our release to a Hugging Face dataset. This can take a while, depending on the size of your dataset.
If we inspect the features of the new dataset, we can see the image column and the corresponding label. The label consists of two parts: a list of annotations and a segmentation bitmap. The annotation corresponds to the different objects in the image. For each object, the annotation contains an id and a category_id. The segmentation bitmap is an image where each pixel contains the id of the object at that pixel. More information can be found in the relevant docs.
For semantic segmentation, we need a semantic bitmap with a category_id for each pixel. We'll use the get_semantic_bitmap function from the Segments.ai SDK to convert the bitmaps to semantic bitmaps. In order to apply this function to all the rows in our dataset, we'll use dataset.map.
You can also rewrite the convert_segmentation_bitmap function to use batches and pass batched=True to dataset.map. This will speed up the mapping significantly, but you might need to tweak the batch_size to make sure the process doesn't run out of memory.
The SegFormer model we're going to fine-tune later expects certain names for the features. For convenience, we'll already match this format now. Thus, we'll rename the image feature to pixel_values, the label.segmentation_bitmap to label and discard the other features.
We can now push the transformed dataset to the Hugging Face Hub. That way, your team and the Hugging Face community can make use of it. In the next section, we'll see how you can load the dataset from the hub.
Use a dataset from the Hub
If you don't want to create your own dataset, but found a suitable dataset for your use case on the Hugging Face Hub, you can define the identifier here.
For example, you can use the full labeled sidewalk dataset. Note that you can check out the examples directly in your browser.
2. Load and prepare the Hugging Face dataset for training
Now that we've created a new dataset and pushed it to the Hugging Face Hub, we can load the dataset in a single line.
Let's shuffle the dataset and split the dataset in a train and test set.
We'll extract the number of labels and the human-readable ids, so we can configure the segmentation model correctly later on.
Image processor & data augmentation
A SegFormer model expects the input to be of a certain shape. To transform our training data to match the expected shape, we can use SegFormerImageProcessor. We could use the ds.map function to apply the image processor to the whole training dataset in advance, but this can take up a lot of disk space. Instead, we'll use a transform, which will only prepare a batch of data when that data is actually used (on-the-fly). This way, we can start training without waiting for further data preprocessing.
In our transform, we'll also define some data augmentations to make our model more resilient to different lighting conditions. We'll use the ColorJitter function from torchvision to randomly change the brightness, contrast, saturation, and hue of the images in the batch.
3. Fine-tune a SegFormer model
Load the model to fine-tune
The SegFormer authors define 5 models with increasing sizes: B0 to B5. The following chart (taken from the original paper) shows the performance of these different models on the ADE20K dataset, compared to other models.

Here, we'll load the smallest SegFormer model (B0), pre-trained on ImageNet-1k. It's only about 14MB in size!
Using a small model will make sure that our model can run smoothly on our pizza delivery robot.
Set up the Trainer
To fine-tune the model on our data, we'll use Hugging Face's Trainer API. In order to use a Trainer, we need to set up the training configuration, and an evalutation metric.
First, we'll set up the TrainingArguments. This defines all training hyperparameters, such as learning rate and the number of epochs, frequency to save the model and so on. We also specify to push the model to the hub after training (push_to_hub=True) and specify a model name (hub_model_id).
Next, we'll define a function that computes the evaluation metric we want to work with. Because we're doing semantic segmentation, we'll use the mean Intersection over Union (mIoU), directly accessible in the datasets library (see here). IoU represents the overlap of segmentation masks. Mean IoU is the average of the IoU of all semantic classes. Take a look at this blogpost for an overview of evaluation metrics for image segmentation.
Because our model outputs logits with dimensions height/4 and width/4, we have to upscale them before we can compute the mIoU.
Finally, we can instantiate a Trainer object.
Now that our trainer is set up, training is as simple as calling the train function. We don't need to worry about managing our GPU(s), the trainer will take care of that.
When we're done with training, we can push our fine-tuned model and the image processor to the Hugging Face hub.
This will also automatically create a model card with our results. We'll supply some extra information in kwargs to make the model card more complete.
4. Inference
Now comes the exciting part, using our fine-tuned model! In this section, we'll show how you can load your model from the hub and use it for inference.
However, you can also try out your model directly on the Hugging Face Hub, thanks to the cool widgets powered by the hosted inference API. If you pushed your model to the hub in the previous step, you should see an inference widget on your model page. You can add default examples to the widget by defining example image URLs in your model card. See this model card as an example.

Use the model from the hub
We'll first load the model from the hub using SegformerForSemanticSegmentation.from_pretrained().
Next, we'll load an image from our test dataset.
To segment this test image, we first need to prepare the image using the image processor. Then we forward it through the model.
We also need to remember to upscale the output logits to the original image size. In order to get the actual category predictions, we just have to apply an argmax on the logits.
Now it's time to display the result. The next cell defines the colors for each category, so that they match the "category coloring" on Segments.ai.
The next function overlays the output segmentation map on the original image.
We'll display the result next to the ground-truth mask.
What do you think? Would you send our pizza delivery robot on the road with this segmentation information?
The result might not be perfect yet, but we can always expand our dataset to make the model more robust. We can now also go train a larger SegFormer model, and see how it stacks up.
5. Conclusion
That's it! You now know how to create your own image segmentation dataset and how to use it to fine-tune a semantic segmentation model.
We introduced you to some useful tools along the way, such as:
- Segments.ai for labeling your data
- 🤗 datasets for creating and sharing a dataset
- 🤗 transformers for easily fine-tuning a state-of-the-art segmentation model
- 🤗 hub for sharing our dataset and model, and for creating an inference widget for our model
We hope you enjoyed this post and learned something. Feel free to share your own model with us on Twitter (@TobiasCornille, @NielsRogge, and @huggingface).