🚀 分割模型庫(Segmentation-Models-PyTorch)之UPerNet模型
本項目基於segmentation-models-pytorch
庫,提供了UPerNet模型用於圖像分割任務。UPerNet模型能夠高效準確地對圖像進行語義分割,為相關領域的研究和應用提供了有力支持。
🚀 快速開始
加載預訓練模型
點擊下面的按鈕在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/upernet-convnext-tiny"
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 = mask.argmax(1).cpu().numpy()
💻 使用示例
基礎用法
上述加載預訓練模型並進行推理的代碼即為基礎用法示例,可直接運行對圖像進行分割。
高級用法
在實際應用中,你可能需要根據具體需求調整模型的初始化參數,以下是模型初始化參數的示例:
model_init_params = {
"encoder_name": "tu-swin_tiny_patch4_window7_224",
"encoder_depth": 5,
"encoder_weights": None,
"decoder_channels": 512,
"decoder_use_norm": "batchnorm",
"in_channels": 3,
"classes": 150,
"activation": None,
"upsampling": 4,
"aux_params": None,
"img_size": 512
}
📚 詳細文檔
數據集
本模型使用的數據集為 ADE20K。
更多信息
- 庫地址:https://github.com/qubvel/segmentation_models.pytorch
- 文檔地址:https://smp.readthedocs.io/en/latest/
本模型已使用 PytorchModelHubMixin 推送到模型中心。
📄 許可證
本項目採用 MIT 許可證。