Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Semantic segmentation models with 500+ pretrained convolutional and transformer-based backbones.

License

NotificationsYou must be signed in to change notification settings

qubvel-org/segmentation_models.pytorch

Repository files navigation

logo
Python library with Neural Networks for Image Semantic
Segmentation based onPyTorch.

GitHub Workflow Status (branch)CodecovRead the Docs
PyPIPyTorch - VersionPython - Version
Generic badgePyPI - Downloads

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, includingtimm support
  • Popular metrics and losses for training routines (Dice, Jaccard, Tversky, ...)
  • ONNX export and torch script/trace/compile friendly

Community-Driven Project, Supported By

withoutBG API LogowithoutBG API
https://withoutbg.com

High-quality background removal API

VisitRead The Docs Project Page or read the following README to know more about Segmentation Models Pytorch (SMP for short) library

📋 Table of content

  1. Quick start
  2. Examples
  3. Models and encoders
  4. Models API
    1. Input channels
    2. Auxiliary classification output
    3. Depth
  5. Installation
  6. Competitions won with the library
  7. Contributing
  8. Citing
  9. License

⏳ Quick start

1. Create your first Segmentation model with SMP

The segmentation model is just a PyTorchtorch.nn.Module, which can be created as easy as:

importsegmentation_models_pytorchassmpmodel=smp.Unet(encoder_name="resnet34",# choose encoder, e.g. mobilenet_v2 or efficientnet-b7encoder_weights="imagenet",# use `imagenet` pre-trained weights for encoder initializationin_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))
  • seetable with available model architectures
  • seetable 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 isnot necessary in case you train the whole model, not only the decoder.

fromsegmentation_models_pytorch.encodersimportget_preprocessing_fnpreprocess_input=get_preprocessing_fn('resnet18',pretrained='imagenet')

Congratulations! You are done! Now you can train your model with your favorite framework!

💡 Examples

NameLinkColab
Train pets binary segmentation on OxfordPetsNotebookOpen In Colab
Train cars binary segmentation on CamVidNotebookOpen In Colab
Train multiclass segmentation on CamVidNotebookOpen In Colab
Train clothes binary segmentation by @ternausRepo
Load and inference pretrained SegformerNotebookOpen In Colab
Load and inference pretrained DPTNotebookOpen In Colab
Load and inference pretrained UPerNetNotebookOpen In Colab
Save and load models locally / to HuggingFace HubNotebookOpen In Colab
Export trained model to ONNXNotebookOpen In Colab

📦 Models and encoders

Architectures

ArchitecturePaperDocumentationCheckpoints
Unetpaperdocs
Unet++paperdocs
MAnetpaperdocs
Linknetpaperdocs
FPNpaperdocs
PSPNetpaperdocs
PANpaperdocs
DeepLabV3paperdocs
DeepLabV3+paperdocs
UPerNetpaperdocscheckpoints
Segformerpaperdocscheckpoints
DPTpaperdocscheckpoints

Encoders

The library provides a wide range ofpretrained encoders (also known as backbones) for segmentation models. Instead of using features from the final layer of a classification model, we extractintermediate features and feed them into the decoder for segmentation tasks.

All encoders come withpretrained weights, which help achievefaster 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 balanceefficiency, 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 likenew_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 supportaux_params parameters, which is default set toNone.Ifaux_params = None then classification auxiliary output is not created, elsemodel produce not onlymask, but alsolabel output with shapeNC.Classification head consists of GlobalPooling->Dropout(optional)->Linear->Activation(optional) layers, which can beconfigured byaux_params as follows:

aux_params=dict(pooling='avg',# one of 'avg', 'max'dropout=0.5,# dropout ratio, default is Noneactivation='sigmoid',# activation function, default is Noneclasses=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 makeyour model lighter if specify smallerdepth.

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.

🤝 Contributing

  1. Install SMP in dev mode
make install_dev# Create .venv, install SMP in dev mode
  1. Run tests and code checks
maketest# Run tests suite with pytestmake fixup# Ruff for formatting and lint checks
  1. 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 underMIT License, while some files are subject to other licenses. Please refer toLICENSES and license statements in each file for careful check, especially for commercial use.

Sponsor this project

    Packages

    No packages published

    Contributors69


    [8]ページ先頭

    ©2009-2025 Movatter.jp