This repository provides a comprehensive comparison of training the SegFormer model from scratch versus using the Hugging Face library. The goal is to highlight the differences in implementation, training time, performance, and ease of use, along with the pros and cons of each method.
Performance vs. model efficiency on ADE20K dataset.
SegFormer is an efficient and powerful method for semantic segmentation using the Transformer architecture. It has fewer parameters than other models and can run in real-time.
Hugging Face

I fine-tuned models on the Cityscapes dataset. Two different pretrained models from Hugging Face were evaluated. The first model, "nvidia/segformer-b0-finetuned-ade-512-512," pretrained on the ADE20K dataset, performed poorly on Cityscapes. The second model, "nvidia/segformer-b0-finetuned-cityscapes-1024-1024," showed better results but still didn't match the performance of a model trained from scratch. The scratch-trained model, which used an ImageNet-pretrained backbone and was fine-tuned on Cityscapes, yielded the best performance.
Training the SegFormer model from scratch involves:
Data preprocessing: The images and labels are loaded from files. The images are then converted to tensors and normalized using specific mean and standard deviation values: mean=(0.485, 0.56, 0.406) and std=(0.229, 0.224, 0.225)
Training loop: The CrossEntropyLoss function is used to measure the difference between the model's predictions and the actual labels. The Adam optimizer is used as optimizer.
Evaluation metrics: The mean Intersection over Union (meanIoU) is used as the evaluation metric to assess the model's performance.
Detailed steps and code are provided in the FromScratch/ directory.
The steps taken to train the SegFormer model using the Hugging Face library were as follows:
Data handling: The SegformerFeatureExtractor API was used with the pretrained model "nvidia/segformer-b0-finetuned-cityscapes-1024-1024" to prepare the data for Hugging Face pretrained models. Fine-tuning and evaluation: the PyTorch Lightning framework was used to fine-tune the pretrained Hugging Face model, with mean Intersection over Union (meanIoU) as the evaluation metric. The code and instructions can be found in the HuggingFace/ directory.
Implementation
Training Time
Performance
Ease of Use
SegFormer: arXiv Paper
Hugging Face: Hugging Face Documentation
I took this course that helped me understand the SegFormer architecture and coding from scratch better (https://courses.thinkautonomous.ai/view/courses/segformers-exploration/1563205-segformers-workshop/4926978-visualizing-the-attention-maps)
32 commits
Python
100.0%
This repository provides a comprehensive comparison of training the SegFormer model from scratch versus using the Hugging Face library. The goal is to highlight the differences in implementation, training time, performance, and ease of use, along with the pros and cons of each method.
Performance vs. model efficiency on ADE20K dataset.
SegFormer is an efficient and powerful method for semantic segmentation using the Transformer architecture. It has fewer parameters than other models and can run in real-time.
Hugging Face

I fine-tuned models on the Cityscapes dataset. Two different pretrained models from Hugging Face were evaluated. The first model, "nvidia/segformer-b0-finetuned-ade-512-512," pretrained on the ADE20K dataset, performed poorly on Cityscapes. The second model, "nvidia/segformer-b0-finetuned-cityscapes-1024-1024," showed better results but still didn't match the performance of a model trained from scratch. The scratch-trained model, which used an ImageNet-pretrained backbone and was fine-tuned on Cityscapes, yielded the best performance.
Training the SegFormer model from scratch involves:
Data preprocessing: The images and labels are loaded from files. The images are then converted to tensors and normalized using specific mean and standard deviation values: mean=(0.485, 0.56, 0.406) and std=(0.229, 0.224, 0.225)
Training loop: The CrossEntropyLoss function is used to measure the difference between the model's predictions and the actual labels. The Adam optimizer is used as optimizer.
Evaluation metrics: The mean Intersection over Union (meanIoU) is used as the evaluation metric to assess the model's performance.
Detailed steps and code are provided in the FromScratch/ directory.
The steps taken to train the SegFormer model using the Hugging Face library were as follows:
Data handling: The SegformerFeatureExtractor API was used with the pretrained model "nvidia/segformer-b0-finetuned-cityscapes-1024-1024" to prepare the data for Hugging Face pretrained models. Fine-tuning and evaluation: the PyTorch Lightning framework was used to fine-tune the pretrained Hugging Face model, with mean Intersection over Union (meanIoU) as the evaluation metric. The code and instructions can be found in the HuggingFace/ directory.
Implementation
Training Time
Performance
Ease of Use
SegFormer: arXiv Paper
Hugging Face: Hugging Face Documentation
I took this course that helped me understand the SegFormer architecture and coding from scratch better (https://courses.thinkautonomous.ai/view/courses/segformers-exploration/1563205-segformers-workshop/4926978-visualizing-the-attention-maps)
32 commits
Python
100.0%