🚀 图像分割模型 - UPerNet
UPerNet 是一个基于 PyTorch 的图像分割模型,可用于语义分割任务。它借助 segmentation-models-pytorch
库实现,能帮助开发者快速搭建和训练图像分割模型。
🚀 快速开始
加载预训练模型
你可以点击下面的链接在 Google Colab 中运行示例代码:

安装依赖
pip install -U segmentation_models_pytorch albumentations
运行推理
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-base"
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()
📦 安装指南
运行以下命令安装所需的库:
pip install -U segmentation_models_pytorch albumentations
💻 使用示例
基础用法
上述“加载预训练模型”部分的代码即为基础用法示例,展示了如何加载预训练模型并进行推理。
高级用法
你可以根据需求调整模型的初始化参数,以下是模型初始化参数的示例:
model_init_params = {
"encoder_name": "tu-convnext_base",
"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
}
📚 详细文档
数据集
本模型使用的数据集为 ADE20K。
更多信息
- 库地址:https://github.com/qubvel/segmentation_models.pytorch
- 文档地址:https://smp.readthedocs.io/en/latest/
- 许可证:https://github.com/NVlabs/SegFormer/blob/master/LICENSE
本模型通过 PytorchModelHubMixin 推送到 Hugging Face Hub。
📄 许可证
本项目采用 MIT 许可证。