segmentation_models.pytorch README

The project's README from github.com/qubvel-org/segmentation_models.pytorch. As one markdown file: README.md.

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

GitHub Workflow Status (branch) Codecov Read the Docs
PyPI PyTorch - Version Python - Version
Generic badge PyPI - Downloads

The main features of the library are:

🤝 Sponsor: withoutBG

Sponsored by 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

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

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 Open In Colab
Train cars binary segmentation on CamVid Notebook Open In Colab
Train multiclass segmentation on CamVid Notebook Open In Colab
Train clothes binary segmentation by @ternaus Repo
Load and inference pretrained Segformer Notebook Open In Colab
Load and inference pretrained DPT Notebook Open In Colab
Load and inference pretrained UPerNet Notebook Open In Colab
Save and load models locally / to HuggingFace Hub Notebook Open In Colab
Export trained model to ONNX Notebook Open In Colab

📦 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:

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:

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

🤝 Contributing

  1. Install SMP in dev mode
make install_dev  # Create .venv, install SMP in dev mode
  1. Run tests and code checks
make test          # Run tests suite with pytest
make 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 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.

segmentation_models.pytorch (qubvel-org/segmentation_models.pytorch)

Searching Database…

这是什么?一个基于 PyTorch 的 Python 库,用于构建图像语义分割模型,内置500多个预训练的卷积和基于 Transformer 的骨干网络。
这是框架,还是我直接导入使用的库?它是一个库:你在自己的 PyTorch 训练代码中导入它,就能得到一个模型对象(torch.nn.Module),而不是独立的应用程序或训练框架。
我该怎么用它创建分割模型?先安装它,然后选择一种架构(例如 Unet、DeepLabV3+、Segformer)和一个编码器/骨干网络,两行代码就能创建模型,再把它接入你自己的 PyTorch 训练循环。
使用它之前,需要先配置好 PyTorch 吗?需要,它构建在 PyTorch 之上,其500多个编码器中的很多都可以通过 timm 库使用。
它包含训练代码,还是只提供模型构建模块?它只提供模型架构、编码器、损失函数和评估指标等构建模块;你仍然需要用自己选择的框架编写训练循环。
整个仓库都是 MIT 许可证吗?大部分是 MIT 许可证,但有些文件采用其他许可证,所以请仔细核对,尤其是用于商业场景时。
支持哪些模型架构?共12种架构,包括 Unet、Unet++、MAnet、Linknet、FPN、PSPNet、PAN、DeepLabV3、DeepLabV3+、UPerNet、Segformer 和 DPT。
我能修改输入通道数吗?比如用于灰度图像或多光谱图像。可以,in_channels 参数允许模型接受任意数量的通道,预训练的 ImageNet 权重会自动适配(单通道输入时权重求和,其他通道数时则平铺并缩放)。
模型能否在输出分割掩码的同时输出分类标签?可以,设置 aux_params 会创建一个辅助分类头(GlobalPooling -> Dropout -> Linear -> Activation),除掩码外还会输出一个标签。
我能把训练好的模型导出为 ONNX 吗?可以,该库在设计上便于导出 ONNX,并兼容 torch script/trace/compile,还提供了一个导出 ONNX 的示例笔记本。
Creator
qubvel-org
Size
16.3 MB
Stars
11.8K
Age
7y
Commits · 90 Days