🚀 分割模型庫 - PyTorch版
本項目是一個基於 PyTorch 的圖像分割模型庫,提供了預訓練的 Segformer 模型,可用於圖像分割任務,能幫助開發者快速搭建和訓練自己的圖像分割模型。
🚀 快速開始
加載預訓練模型
點擊下面的按鈕在 Colab 中運行示例:

步驟 1:安裝依賴
pip install -U segmentation_models_pytorch albumentations
步驟 2:運行推理
import torch
import requests
import numpy as np
import albumentations as A
import segmentation_models_pytorch as smp
from PIL import Image
device = "cuda" if torch.cuda.is_available() else "cpu"
checkpoint = "smp-hub/segformer-b2-1024x1024-city-160k"
model = smp.from_pretrained(checkpoint).eval().to(device)
preprocessing = A.Compose.from_pretrained(checkpoint)
url = "https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg"
image = Image.open(requests.get(url, stream=True).raw)
np_image = np.array(image)
normalized_image = preprocessing(image=np_image)["image"]
input_tensor = torch.as_tensor(normalized_image)
input_tensor = input_tensor.permute(2, 0, 1).unsqueeze(0)
input_tensor = input_tensor.to(device)
with torch.no_grad():
output_mask = model(input_tensor)
mask = torch.nn.functional.interpolate(
output_mask, size=(image.height, image.width), mode="bilinear", align_corners=False
)
mask = mask.argmax(1).cpu().numpy()
💻 使用示例
基礎用法
上述加載預訓練模型並進行推理的代碼就是基礎用法示例,通過簡單的幾步操作,即可使用預訓練的 Segformer 模型對圖像進行分割。
📚 詳細文檔
模型初始化參數
model_init_params = {
"encoder_name": "mit_b2",
"encoder_depth": 5,
"encoder_weights": None,
"decoder_segmentation_channels": 768,
"in_channels": 3,
"classes": 19,
"activation": None,
"aux_params": None
}
這些參數可用於自定義模型的初始化。
數據集
數據集名稱:Cityscapes
更多信息
- 庫地址:https://github.com/qubvel/segmentation_models.pytorch
- 文檔地址:https://smp.readthedocs.io/en/latest/
- 許可證:https://github.com/NVlabs/SegFormer/blob/master/LICENSE
本模型已使用 PytorchModelHubMixin 推送到模型中心。
📄 許可證
本項目使用其他許可證,具體請參考:https://github.com/NVlabs/SegFormer/blob/master/LICENSE