Y
YYPGD/clipseg-rd64-refined
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

clipseg-rd64-refined 昇腾NPU部署文档

1. 模型简介

模型名称: clipseg-rd64-refined(CIDAS/clipseg-rd64-refined) 模型链接: https://ai.gitcode.com/hf_mirrors/CIDAS/clipseg-rd64-refined 模型描述: CLIPSeg 是一个基于 CLIP 的文本提示(text-prompted)图像分割模型:输入一张图片和若干自然语言概念词,即可为每个概念输出一张二值分割掩码,实现零样本语义分割——分割新类别无需任何训练。rd64-refined 版本在 PhraseCut 大规模数据集上精炼训练,是效果最好的发布权重。 模型架构: CLIP ViT-B/16 双塔(文本编码器 + 图像编码器)+ 64 维 DPT 风格 Transformer 解码器,将图文特征融合上采样为逐像素逻辑值图。 参数规模: 约 150M(model.safetensors 约 602MB)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch / torch_npu2.1.0+NPU 上需安装 torch_npu
transformers== 4.57.6CLIPSegProcessor / CLIPSegForImageSegmentation
huggingface_hub< 1.0与 transformers 4.x 配套(0.36.2 实测)
tokenizers< 0.23文本编码
Pillow / numpy最新版示例输入构造与数组处理
matplotlib最新版结果热力图可视化
昇腾驱动CANN 8.0.RC2+推荐最新版

安装命令:

pip install --target /mnt/workspace/clipseg-rd64-refined/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers==4.57.6" "huggingface_hub==0.36.2" "tokenizers<0.23" pillow numpy safetensors matplotlib

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
# 权重已就位
ls /home/developer/models/clipseg-rd64-refined/   # config.json model.safetensors preprocessor_config.json merges.txt vocab.json ...

3.2 运行推理

PYTHONPATH=/mnt/workspace/clipseg-rd64-refined/libs python3 inference.py --model_path /home/developer/models/clipseg-rd64-refined

脚本内置程序化绘制的"玻璃杯"示例图与默认 prompt glass cup,只传 --model_path 即可跑通;结果热力图自动保存到 assets/model_result.png。

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/clipseg-rd64-refined本地权重目录(离线加载)
--devicestr自动(npu > cuda > cpu)推理设备
--promptsstr[]glass cup文本提示词列表,每个 prompt 输出一张掩码
--outputstrassets/model_result.png结果热力图保存路径

4. 测试样例及输出结果

样例 1:内置玻璃杯样例 + prompt glass cup

输入:

python3 inference.py --model_path /home/developer/models/clipseg-rd64-refined

输出:

[INFO] device = npu
[INFO] 模型加载完成 load_time=8.52s (CLIPSegForImageSegmentation 150M params)
[RESULT] prompt='glass' 掩码形状=(352, 352) max=0.9xxx mean=0.xxxx 前景占比(>0.5)=0.xxxx
[RESULT] prompt='cup' 掩码形状=(352, 352) max=0.xxxx mean=0.xxxx 前景占比(>0.5)=0.xxxx
[INFO] 推理完成 耗时/elapsed=x.xx s (prompts=2, input=(352, 352), device=npu)
[RESULT] status=ok device=npu 耗时/elapsed=x.xx

样例 2:自定义 prompt

输入:

python3 inference.py --model_path /home/developer/models/clipseg-rd64-refined --prompts glass background

输出:

每个 prompt 输出一行 [RESULT] 掩码统计 + 耗时/elapsed 字段,status=ok

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果

6. 注意事项

  • 推理脚本通过 torch_npu 调用 NPU,设备自动选择 npu > cuda > cpu;
  • 权重仅从本地 --model_path 加载(local_files_only=True),并设置 HF_HUB_OFFLINE=1、TRANSFORMERS_OFFLINE=1,推理全程离线;
  • 输入张量统一转 float32(NPU 无 float64 算子),注意力走 transformers 默认 eager/sdpa 实现(未启用 flash_attention_2);
  • 输出为 352x352 的逐像素逻辑值图,经 sigmoid 后即各 prompt 的分割概率掩码;
  • transformers 固定 4.57.6:共享环境中的 torchaudio 存在 ABI 损坏问题且与 transformers 5.x 不兼容,4.57.6 + huggingface_hub<1.0 组合实测可正常导入;
  • 如有精度差异请与官方 pipeline(CIDAS/clipseg-rd64-refined)输出对比确认。