█

AI/TLDR

Buy me a coffee
Buy me a coffee

Segmentation Models PyTorch

Image segmentation architectures with hundreds of pretrained encoders, two lines to a model

Vision & UnderstandingOpen source
Language
Python
License
MIT
$pip install segmentation-models-pytorch
IN SHORT

What is Segmentation Models PyTorch?

Segmentation Models PyTorch — SMP for short — is a Python library that builds semantic-segmentation networks out of two interchangeable halves: a decoder architecture and a pretrained encoder. It sits in the Vision & Understanding category of the Multimodal Generation tools and is open-source, written in Python and licensed MIT.

Overview

Segmentation Models PyTorch — SMP for short — is a Python library that builds semantic-segmentation networks out of two interchangeable halves: a decoder architecture and a pretrained encoder. A model is a plain `torch.nn.Module`, created in a single call, so it drops into whatever training loop, Lightning module or framework you already use rather than asking you to adopt a trainer.

The library ships 12 encoder-decoder architectures — Unet, Unet++, MAnet, Linknet, Segformer, DPT and others — and more than 800 pretrained convolution- and transformer-based encoders, including the full timm catalogue. Alongside the models it provides the training pieces that usually get copy-pasted between projects: Dice, Jaccard and Tversky losses, standard segmentation metrics, and a preprocessing helper that matches each encoder's pretraining statistics.

SMP is MIT-licensed and aimed at both research and competition use — the repository keeps a hall-of-fame page of segmentation competitions won with it. Models are ONNX-export friendly and work with torch script, trace and compile, and trained weights can be saved to or loaded from the Hugging Face Hub. It requires PyTorch 1.9+ and Python 3.9+.

What it does

  • 12 encoder-decoder architectures: Unet, Unet++, MAnet, Linknet, Segformer, DPT and more
  • 800+ pretrained encoders, convolutional and transformer-based, including timm backbones
  • Two-line model creation — the result is an ordinary `torch.nn.Module`, framework-agnostic
  • Segmentation losses (Dice, Jaccard, Tversky) and metrics bundled for training routines
  • Preprocessing helper that reproduces each encoder's pretraining normalisation
  • ONNX export plus torch script / trace / compile support, and save/load through the Hugging Face Hub

Getting started

SMP is a library, not a framework: install it, build a model, train it however you already train. All snippets below come from the project README.

Install from PyPI

The published package name uses dashes; the import name uses underscores.

bashbash
pip install segmentation-models-pytorch

Or install the latest from GitHub

Use the git install when you need a fix that has landed on main but is not released yet.

bashbash
pip install git+https://github.com/qubvel/segmentation_models.pytorch

Create your first model

Pick an architecture, an encoder and its pretrained weights; set the input channels and number of classes.

pythonpython
import segmentation_models_pytorch as smp

model = smp.Unet(
    encoder_name="resnet34",        # choose encoder, e.g. mobilenet_v2 or efficientnet-b7
    encoder_weights="imagenet",     # use `imagenet` pre-trained weights for encoder initialization
    in_channels=1,                  # model input channels (1 for gray-scale images, 3 for RGB, etc.)
    classes=3,                      # model output channels (number of classes in your dataset)
)

Match the encoder's preprocessing

Preparing data the way the encoder was pretrained usually converges faster and scores higher. It is not required if you train the whole network rather than only the decoder.

pythonpython
from segmentation_models_pytorch.encoders import get_preprocessing_fn

preprocess_input = get_preprocessing_fn('resnet18', pretrained='imagenet')

Train it with your own loop

From here the model is a standard PyTorch module — train it with your favourite framework. The repository's examples folder has runnable notebooks for binary, multiclass and pretrained-inference workflows.

texttext
https://smp.readthedocs.io/

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

  • Reach for it when you need a segmentation baseline running today rather than a reimplementation of Unet
  • Reach for it to sweep encoders — swap `encoder_name` and keep the rest of the pipeline unchanged
  • Reach for it when you want proven segmentation losses and metrics instead of hand-rolled ones
  • Reach for it when the trained model has to leave Python via ONNX or torch.compile

How Segmentation Models PyTorch compares

Segmentation Models PyTorch alongside other open-source vision & understanding tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
PaddleOCR★ 93.5kA toolkit for detecting and recognizing text in images across many languages, plus document parsing.
OpenCV★ 91.8kThe long-standing open-source computer-vision library for C++, Python and Java; version 5 rewrote the DNN engine and added ONNX-based LLM and VLM inference in-library.
Ultralytics YOLO★ 63.9kA framework for training and running YOLO models for real-time object detection, segmentation, and tracking.
YOLOv5★ 58.3kUltralytics' PyTorch repo for training, running and exporting YOLOv5 object-detection, instance-segmentation and classification models with plain scripts and PyTorch Hub.
Supervision★ 51.8kA Python toolkit for processing, annotating, and visualizing detections and segmentations from many vision models.
MediaPipe★ 38kGoogle's on-device ML framework: ready-to-run vision, text and audio tasks with one cross-platform API for Android, iOS, web, desktop and edge.
MMDetection★ 33.1kAn OpenMMLab toolbox with many object detection and instance segmentation algorithms for research and production.
Segmentation Models PyTorch★ 11.8kImage segmentation architectures with hundreds of pretrained encoders, two lines to a model