首次使用预训练权重需联网下载对应骨干权重;需自行安装匹配版本的 PyTorch(1.9+)与 Python(3.9+);使用 timm 骨干需额外安装 timm

segmentation_models.pytorch
qubvel-orgMIT活跃维护基于 PyTorch 的语义分割模型库,提供 500+ 预训练卷积与 Transformer 骨干网络,开箱即用,省去自己搭建和训练分割模型的麻烦。
Semantic segmentation models with 500+ pretrained convolutional and transformer-based backbones.
pip install segmentation-models-pytorchREADME 未说明具体显存或 GPU 型号要求,仅标注依赖 PyTorch 1.9+ 与 Python 3.9+
基于 PyTorch 的语义分割模型库,两行代码即可搭建带预训练骨干的分割网络
基于 PyTorch 的语义分割模型库,提供 500+ 预训练卷积与 Transformer 骨干网络,开箱即用,省去自己搭建和训练分割模型的麻烦。
核心能力
- 两行高层 API 创建 Unet 等分割模型
- 内置 12 种编码器-解码器架构如 Unet++、Segformer、DPT
- 提供 800+ 预训练卷积与 Transformer 骨干并支持 timm
- 附带 Dice、Jaccard、Tversky 等常用损失与指标
- 支持 ONNX 导出及 torch script/trace/compile
容易踩的坑
- v0.6.0Segmentation Models - v0.6.0最新正式版2026-10-26 · 2026-10-26 16:43 UTC
⚠️ Breaking changes
- Python 3.10+ is required, and the minimum dependency versions went up:
torch>=1.11,torchvision>=0.12,numpy>=1.21.2,pillow>=8.4(#1281). - Encoder weights are downloaded only from the Hugging Face Hub. The old fallback to the original download URLs is gone, along with
smp.encoders.pretrained_settings. If you can't reach the Hub, setHF_ENDPOINTto a mirror or load weights from a local file (#1398). tu-encoders now warn whenencoder_weightsis a string. UseTrueorNone, and choose the pretrained variant in the name, e.g.tu-convnext_tiny.fb_in22k(#1269).
Highlights
Freeze and unfreeze the encoder
model = smp.Unet("resnet34") model.freeze_encoder() # also stops BatchNorm running-stat updates ... model.unfreeze_encoder()- Add
encoder_freezemethod to SegmentationModel by @ricber in #1220
Losses
class_weightsforDiceLoss,JaccardLoss,TverskyLossandFocalLoss(multiclass and multilabel modes):loss = smp.losses.DiceLoss("multiclass", class_weights=[0.2, 1.0, 3.0])FocalLossaccepts probabilities viafrom_logits=False, with anepsparameter likeDiceLoss.JaccardLosssupportsignore_index.- Losses work on non-contiguous tensors.
Models and encoders
- Transformer-style
tu-encoders work inUnetPlusPlusandLinknet(#1294). - PSPNet exports to ONNX (#1270).
- Fixed an MPS float tensor
ValueError(#1199).
What's Changed
- Fix PyPI package upload by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1128
- Fix notebook rendering by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1130
- Update pypi release by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1131
- Update no_grad usage to inference_mode by @GuillaumeErhard in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1164
- Explicitly set
reductionfor Focal loss by @simonreise in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1165 - Add
ignore_indexto Jaccard loss by @simonreise in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1151 - Replaced
viewwithreplaceto prevent errors on non-contiguous tensors by @simonreise in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1174 - Fix minor typo in dice.py by @snehilsanyal in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1181
- Fix/issue 1198 value error invalid type torch.mps.float tensor by @Farbdrucker in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1199
- Add missing name attribute to deeplabv3 & deeplabv3plus by @sanamorii in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1214
- Add encoder_freeze method to SegmentationModel by @ricber in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1220
- feat: Adding withoutBG by @kocabiyik in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1261
- fix: TorchFix Errors by @isaaccorley in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1262
- docs: Clarify tu- encoder_weights behavior as boolean flag by @GuillaumeErhard in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1269
- CI: test Python 3.12+ by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1282
- Python: bump minimum supported version to 3.10 by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1281
- Codecov: don't comment on PRs by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1283
- setuptools: adopt PEP 639 license metadata by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1280
- Update HALLOFFAME.md with ICIP2025 results by @caglarmert in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1293
- Docs: fix unexpected inden
- Python 3.10+ is required, and the minimum dependency versions went up:
- v0.5.0Segmentation Models - v0.5.02025-04-17 · 2025-04-17 10:13 UTC
查看更新说明
New Models
DPT

The DPT model adapts the Vision Transformer (ViT) architecture for dense prediction tasks like semantic segmentation. It uses a ViT as a powerful backbone, processing image information with a global receptive field at each stage. The key innovation lies in its decoder, which reassembles token representations from various transformer stages into image-like feature maps at different resolutions. These are progressively combined using convolutional PSP and FPN blocks to produce full-resolution, high-detail predictions.
The model in
smpcan be used with a wide variety of transformer-based encodersimport segmentation_models_pytorch as smp # initialize with your own pretrained encoder model = smp.DPT("tu-mobilevitv2_175.cvnets_in1k", classes=2) # load fully-pretrained on ADE20K model = smp.from_pretrained("smp-hub/dpt-large-ade20k") # load the same checkpoint for finetuning model = smp.from_pretrained("smp-hub/dpt-large-ade20k", classes=1, strict=False)The full table of DPT's supported
timmencoders can be found here.- Adding DPT by @vedantdalimkar in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1079
Models export
A lot of work was done to add support for
torch.jit.script,torch.compile(without graph breaks:fullgraph=True) andtorch.exportfeatures in all encoders and models.This provides several advantages:
torch.jit.script: Enables serialization of models into a static graph format, enabling deployment in environments without a Python interpreter and allowing for graph-based optimizations.torch.compile(withfullgraph=True): Leverages Just-In-Time (JIT) compilation (e.g., via Triton or Inductor backends) to generate optimized kernels, reducing Python overhead and enabling significant performance improvements through techniques like operator fusion, especially on GPU hardware.fullgraph=Trueminimizes graph breaks, maximizing the scope of these optimizations.torch.export: Produces a standardized Ahead-Of-Time (AOT) graph representation, simplifying the process of exporting models to various inference backends and edge devices (e.g., through ExecuTorch) while preserving model dynamism where possible.
PRs:
- Fix torch compile, script, export by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1031
- Fix Efficientnet encoder for torchscript by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1037
Core
All encoders from third-party libraries such as
efficientnet-pytorchandpretrainedmodels.pytorchare now vendored by SMP. This means we have copied and refactored the underlying code and moved all checkpoints to the smp-hub. As a result, you will have fewer additional dependencies when installingsmpand get much faster weights downloads.- Move encoders weights to HF-Hub by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1035
- Vendor pretrainedmodels by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1039
- Vendor efficientnet-pytorch by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1036
🚨🚨🚨 Breaking changes
-
UperNet model was significantly changed to reflect the original implementation and to bring pretrained checkpoints into SMP. Unfortunately, UperNet model weights trained with v0.4.0 will be not compatible with SMP v0.5.0.
- Fix UperNet model and add pretrained checkpoints by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/1124
-
While the high-level API for modeling should be backward compatible with v0.4.0, internal modules (such as encoders, decoders
- v0.4.0Segmentation Models - v0.4.02025-01-08 · 2025-01-08 15:28 UTC
查看更新说明
New models
Segformer
contributed by @brianhou0208

Originally, SegFormer is a transformer-based semantic segmentation model known for its simplicity and efficiency. It uses a lightweight hierarchical encoder to capture multi-scale features and a minimal decoder for fast inference.
With
segmentation-models-pytorchyou can utilize the model with a native Mix Vision Transformer encoder as long as with 800+ other encoders supported by the library. Original weights are also supported and can be loaded as follows:import segmentation_models_pytorch as smp model = smp.from_pretrained("smp-hub/segformer-b5-640x640-ade-160k")or with any other encoder:
import segmentation_models_pytorch as smp model = smp.Segformer("resnet34")See more checkpoints on the HF Hub.
UperNet
contributed by @brianhou0208

UPerNet (Unified Perceptual Parsing Network) is a versatile semantic segmentation model designed to handle diverse scene parsing tasks. It combines a Feature Pyramid Network (FPN) with a Pyramid Pooling Module (PPM) to effectively capture multi-scale context.
import segmentation_models_pytorch as smp model = smp.UPerNet("resnet34")New Encoders
Thanks to @brianhou0208 contribution 800+
timmencoders are now supported insegmentation_models.pytorch. New modern encoders likeconvnext,efficientvit,efficientformerv2,hiera,mambaoutand more can be used as easy as:import segmentation_models_pytorch as smp model = smp.create_model("upernet", encoder_name="tu-mambaout_small") # or model = smp.UPerNet("tu-mambaout_small")New examples
Other changes
- Project migrated to
pyproject.tomlby @adamjstewart - Better dependency managing and testing (minimal and latest dependencies, linux/windows/mac platforms) by @adamjstewart
- Better type annotations
- Tests are refactored for faster CI and local testing by @qubvel
All changes
- Updating the tutorial file by @ytzfhqs in https://github.com/qubvel-org/segmentation_models.pytorch/pull/907
- Example on how to save and load model along with Albumentations preprocessing by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/914
- Add open-in-colab badge for all example notebooks by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/915
- Switch to pyproject.toml by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/917
- Remove dep on mock by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/919
- [feat] Adding camvid segmentation multiclass as an example by @TimbusCalin in https://github.com/qubvel-org/segmentation_models.pytorch/pull/922
- Ruff: format Jupyter notebooks too by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/923
- Remove docker files by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/925
- Test minimum and maximum supported dependencies by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/918
- Test on Linux/macOS/Windows for all supported Python versions by @adamjstewart in https://github.com/qubvel-org/segmentation_models.pytorch/pull/930
- Modify Jaccard, Dice and Tve
- Project migrated to
- v0.3.4Segmentation Models - v0.3.42024-08-23 · 2024-08-23 13:15 UTC
查看更新说明
Updates
- 🤗 Hugging Face integration: you can save, load, and share models with HF Hub, see example notebook.
Full log
- To support albumentations >= 1.4.0 some functions need to be renamed by @CallShaul in https://github.com/qubvel-org/segmentation_models.pytorch/pull/870
- Updated false positve and false negative rate functions in functional.py by @vermavinay982 in https://github.com/qubvel-org/segmentation_models.pytorch/pull/855
- Add HF hub mixin by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/876
- use precommit for code linting by @Borda in https://github.com/qubvel-org/segmentation_models.pytorch/pull/829
- Add Ruff for formatting and linting by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/877
- Add docs config by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/878
- Update docs by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/879
- Add
create_modelto docs by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/883 - Update ruff to version 0.5.2 and workflows update by @Smartappli in https://github.com/qubvel-org/segmentation_models.pytorch/pull/892
- Fix hub_mixin.py pop error by @ytzfhqs in https://github.com/qubvel-org/segmentation_models.pytorch/pull/909
- Update HF mixin by @qubvel in https://github.com/qubvel-org/segmentation_models.pytorch/pull/910
New Contributors
- @CallShaul made their first contribution in https://github.com/qubvel-org/segmentation_models.pytorch/pull/870
- @vermavinay982 made their first contribution in https://github.com/qubvel-org/segmentation_models.pytorch/pull/855
- @Borda made their first contribution in https://github.com/qubvel-org/segmentation_models.pytorch/pull/829
- @Smartappli made their first contribution in https://github.com/qubvel-org/segmentation_models.pytorch/pull/892
- @ytzfhqs made their first contribution in https://github.com/qubvel-org/segmentation_models.pytorch/pull/909
Full Changelog: https://github.com/qubvel-org/segmentation_models.pytorch/compare/v0.3.3...v0.3.4
- v0.3.3Segmentation Models - v0.3.32023-05-28 · 2023-05-28 15:49 UTC
查看更新说明
Updates
- Pytorch image models (timm) version upgrade to 0.9.2
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Python library with Neural Networks for Image Semantic
Segmentation based on PyTorch.
The main features of the library are:
- Super simple high-level API (just two lines to create a neural network)
- 12 encoder-decoder model architectures (Unet, Unet++, Segformer, DPT, ...)
- 800+ pretrained convolution- and transform-based encoders, including timm support
- Popular metrics and losses for training routines (Dice, Jaccard, Tversky, ...)
- ONNX export and torch script/trace/compile friendly
🤝 Sponsor: withoutBG
withoutBG is a high-quality background removal tool. They built their open-source image matting and refiner models using smp.Unet and are proudly sponsoring this project.
📚 Project Documentation 📚
Visit Read The Docs Project Page or read the following README to know more about Segmentation Models Pytorch (SMP for short) library
📋 Table of content
- Quick start
- Examples
- Models and encoders
- Models API
- Installation
- Competitions won with the library
- Contributing
- Citing
- License
⏳ Quick start
1. Create your first Segmentation model with SMP
The segmentation model is just a PyTorch torch.nn.Module, which can be created as easy as:
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)
)
- see table with available model architectures
- see table with available encoders and their corresponding weights
2. Configure data preprocessing
All encoders have pretrained weights. Preparing your data the same way as during weights pre-training may give you better results (higher metric score and faster convergence). It is not necessary in case you train the whole model, not only the decoder.
from segmentation_models_pytorch.encoders import get_preprocessing_fn
preprocess_input = get_preprocessing_fn('resnet18', pretrained='imagenet')
Congratulations! You are done! Now you can train your model with your favorite framework!
💡 Examples
| Name | Link | Colab |
|---|---|---|
| Train pets binary segmentation on OxfordPets | Notebook | |
| Train cars binary segmentation on CamVid | Notebook | |
| Train multiclass segmentation on CamVid | Notebook | |
| Train clothes binary segmentation by @ternaus | Repo | |
| Load and inference pretrained Segformer | Notebook | |
| Load and inference pretrained DPT | Notebook | |
| Load and inference pretrained UPerNet | Notebook | |
| Save and load models locally / to HuggingFace Hub | Notebook | |
| Export trained model to ONNX | Notebook |
📦 Models and encoders
Architectures
| Architecture | Paper | Documentation | Checkpoints |
|---|---|---|---|
| Unet | paper | docs | |
| Unet++ | paper | docs | |
| MAnet | paper | docs | |
| Linknet | paper | docs | |
| FPN | paper | docs | |
| PSPNet | paper | docs | |
| PAN | paper | docs | |
| DeepLabV3 | paper | docs | |
| DeepLabV3+ | paper | docs | |
| UPerNet | paper | docs | checkpoints |
| Segformer | paper | docs | checkpoints |
| DPT | paper | docs | checkpoints |
Encoders
The library provides a wide range of pretrained encoders (also known as backbones) for segmentation models. Instead of using features from the final layer of a classification model, we extract intermediate features and feed them into the decoder for segmentation tasks.
All encoders come with pretrained weights, which help achieve faster and more stable convergence when training segmentation models.
Given the extensive selection of supported encoders, you can choose the best one for your specific use case, for example:
- Lightweight encoders for low-latency applications or real-time inference on edge devices (mobilenet/mobileone).
- High-capacity architectures for complex tasks involving a large number of segmented classes, providing superior accuracy (convnext/swin/mit).
By selecting the right encoder, you can balance efficiency, performance, and model complexity to suit your project needs.
All encoders and corresponding pretrained weight are listed in the documentation:
🔁 Models API
Input channels
The input channels parameter allows you to create a model that can process a tensor with an arbitrary number of channels. If you use pretrained weights from ImageNet, the weights of the first convolution will be reused:
- For the 1-channel case, it would be a sum of the weights of the first convolution layer.
- Otherwise, channels would be populated with weights like
new_weight[:, i] = pretrained_weight[:, i % 3], and then scaled withnew_weight * 3 / new_in_channels.
model = smp.FPN('resnet34', in_channels=1)
mask = model(torch.ones([1, 1, 64, 64]))
Auxiliary classification output
All models support aux_params parameters, which is default set to None.
If aux_params = None then classification auxiliary output is not created, else
model produce not only mask, but also label output with shape NC.
Classification head consists of GlobalPooling->Dropout(optional)->Linear->Activation(optional) layers, which can be
configured by aux_params as follows:
aux_params=dict(
pooling='avg', # one of 'avg', 'max'
dropout=0.5, # dropout ratio, default is None
activation='sigmoid', # activation function, default is None
classes=4, # define number of output labels
)
model = smp.Unet('resnet34', classes=4, aux_params=aux_params)
mask, label = model(x)
Depth
Depth parameter specify a number of downsampling operations in encoder, so you can make
your model lighter if specify smaller depth.
model = smp.Unet('resnet34', encoder_depth=4)
🛠 Installation
PyPI version:
$ pip install segmentation-models-pytorch
The latest version from GitHub:
$ pip install git+https://github.com/qubvel/segmentation_models.pytorch
🏆 Competitions won with the library
Segmentation Models package is widely used in image segmentation competitions.
Here you can find competitions, names of the winners and links to their solutions.
🛠 Projects built with SMP
- withoutBG: An open-source background removal tool that uses
smp.Unetfor its image matting and refinement models. Check withoutBG Focus on HuggingFace.
🤝 Contributing
- Install SMP in dev mode
make install_dev # Create .venv, install SMP in dev mode
- Run tests and code checks
make test # Run tests suite with pytest
make fixup # Ruff for formatting and lint checks
- Update a table (in case you added an encoder)
make table # Generates a table with encoders and print to stdout
📝 Citing
@misc{Iakubovskii:2019,
Author = {Pavel Iakubovskii},
Title = {Segmentation Models Pytorch},
Year = {2019},
Publisher = {GitHub},
Journal = {GitHub repository},
Howpublished = {\url{https://github.com/qubvel/segmentation_models.pytorch}}
}
🛡️ License
The project is primarily distributed under MIT License, while some files are subject to other licenses. Please refer to LICENSES and license statements in each file for careful check, especially for commercial use.