Guide · Open source
How to use segmentation_models.pytorch: a beginner's guide
Updated · written with AI from the project's own documentation and checked against our catalogue data
Segmentation Models PyTorch (SMP) is a Python library for semantic segmentation, which means labeling every pixel in an image by category. This guide is for Python developers who already work with PyTorch or want to start. By the end you will know how to clone the repository, create a model in a few lines, set up preprocessing for pretrained encoders, and find the examples and ONNX export notebook.
Quick start
- 1Check prerequisites. Read the Python and PyTorch minimums on the README badges and confirm your environment meets them.
- 2Clone the repository. Run the git clone command from the README to get the source and examples.
- 3Install the library. Follow the install section of the full README or the documentation, since the excerpt does not show the install command.
- 4Create a model. Call smp.Unet with an encoder name, encoder_weights set to imagenet, and the correct in_channels and classes for your data.
- 5Prepare preprocessing. Use get_preprocessing_fn with your encoder and pretrained weights so your input matches what the encoder was trained on.
- 6Train on your labeled data. Train the model with your own training code and dataset, using the example notebooks as a reference.
- 7Export if needed. Use the ONNX export notebook in the examples folder to convert your trained model.
What SMP is and what it is not
SMP is a code library, not a ready-made app. The README documents it as a set of building blocks you call from your own Python code. It is built on PyTorch, so you need PyTorch in your project to use it.
The library offers a high-level API, a range of encoder-decoder architectures such as Unet, Unet++, Segformer and DPT, and pretrained encoders. It also includes popular training metrics and losses such as Dice, Jaccard and Tversky.
- Good fit: Python developers building segmentation models in PyTorch, and researchers comparing architectures and encoders.
- Not a fit: people who want a finished app with no coding, or projects that do not use PyTorch.
- You still need labeled data for your own task. The README covers training with your own dataset and includes examples.
Install segmentation_models.pytorch: clone the repository
The install instructions in the source start with cloning the GitHub repository. The README badges list the minimum Python and PyTorch versions, so check those before you start.
The README excerpt shows a PyPI badge for a package named segmentation-models-pytorch, but the excerpt does not show a pip install command. Check the full README or the documentation at smp.readthedocs.io for the installation steps that match your setup.
- Check the Python and PyTorch minimums shown on the README badges.
- Clone the repository, then follow the install section of the full README or the documentation.
git clone https://github.com/qubvel-org/segmentation_models.pytorchCreate your first segmentation model
The README says a segmentation model is a PyTorch torch.nn.Module and can be created in about two lines. The import name is segmentation_models_pytorch, shortened to smp. The example below uses a Unet with a resnet34 encoder that starts from ImageNet weights.
Set in_channels to match your images: 1 for grayscale and 3 for RGB. Set classes to the number of classes in your dataset. The README links to a table of architectures and a table of encoders with their weights.
- encoder_name chooses the encoder, for example mobilenet_v2 or efficientnet-b7.
- encoder_weights='imagenet' starts the encoder from ImageNet pretrained weights.
- in_channels is the number of input channels.
- classes is the number of output channels, which is the number of classes in your data.
import segmentation_models_pytorch as smp
model = smp.Unet(
encoder_name="resnet34",
encoder_weights="imagenet",
in_channels=1,
classes=3,
)Prepare your data with the matching preprocessing
Every encoder has pretrained weights. The README says that preparing your data the same way as during the weights' pre-training may give better results, with higher metric scores and faster convergence. It is not necessary if you train the whole model rather than only the decoder.
Use get_preprocessing_fn from segmentation_models_pytorch.encoders to get the matching function for an encoder and its pretrained weights.
from segmentation_models_pytorch.encoders import get_preprocessing_fn
preprocess_input = get_preprocessing_fn('resnet18', pretrained='imagenet')Train the model and run the examples
The README says you can train the model with your favorite framework once it is created. The source does not describe a built-in training loop, so you write or reuse your own training code.
The repository includes example notebooks. They cover training binary segmentation on OxfordPets and CamVid, multiclass segmentation on CamVid, loading pretrained Segformer, DPT and UPerNet models, saving and loading models locally or to the HuggingFace Hub, and exporting to ONNX. Open them from the examples folder or from the Colab links in the README.
- Binary segmentation on OxfordPets (examples/binary_segmentation_intro.ipynb).
- Multiclass segmentation on CamVid (examples/camvid_segmentation_multiclass.ipynb).
- Save and load models, including HuggingFace Hub (examples/save_load_model_and_share_with_hf_hub.ipynb).
- Export to ONNX (examples/convert_to_onnx.ipynb).
Export a trained model to ONNX
The README lists ONNX export as a feature, and the examples include a notebook called convert_to_onnx.ipynb. The source also says the models are torch script, trace and compile friendly.
Use the notebook as your starting point, since the README excerpt does not show the export code itself.
What people use it for
- Training a binary segmentation model to separate pets from background, as shown in the OxfordPets example.
- Training a multiclass segmentation model on the CamVid dataset.
- Loading a pretrained Segformer, DPT or UPerNet model for inference.
- Saving a trained model locally or sharing it through the HuggingFace Hub.
- Exporting a trained model to ONNX.
Frequently asked questions
Is segmentation_models.pytorch free to use?
The README lists the license as MIT. The source does not describe any paid tier, so you can read the LICENSE file in the repository to confirm the terms.
What do I need to install segmentation_models.pytorch?
The README badges list minimum Python and PyTorch versions. The source shows a git clone command, and the full README or docs cover the rest of the install steps.
Does segmentation_models.pytorch need PyTorch?
Yes. The README describes the library as built on PyTorch and as a set of PyTorch neural networks, so projects without PyTorch are not a fit.
What is the difference between SMP and MMSegmentation?
The source lists MMSegmentation as a comparable project but does not compare the two. SMP is documented as a high-level API for building models in a few lines of code, with pretrained encoders.
Can I use segmentation_models.pytorch with timm encoders?
Yes. The README says the library supports pretrained encoders, including timm support, and links to the timm project.
Can I export an SMP model to ONNX?
Yes. The README lists ONNX export as a feature, and the examples include a notebook called convert_to_onnx.ipynb.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Use SMP if you write Python, work in PyTorch, and have labeled images for your own segmentation task. It is a good fit for researchers comparing architectures and for teams that want pretrained encoders. If you want a ready-made app with no coding, or you do not use PyTorch, pick a different tool.
Install options & downloads