🚀 分割模型库(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 许可证。