# Segmentation Models PyTorch

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

- Category: Multimodal Generation / Vision & Understanding
- Language: Python
- Licence: MIT
- Repository: https://github.com/qubvel-org/segmentation_models.pytorch
- Website: https://smp.readthedocs.io/
- Canonical page: https://ai-tldr.dev/tools/segmentation-models-pytorch/

## In short

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.

1. Install from PyPI

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

```bash
pip install segmentation-models-pytorch
```

2. 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.

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

3. Create your first model

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

```python
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)
)
```

4. 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.

```python
from segmentation_models_pytorch.encoders import get_preprocessing_fn

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

5. 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.

```text
https://smp.readthedocs.io/
```

## 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
