z
zkx_/mattmdjaga_segformer_b2_clothes-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

mattmdjaga/segformer_b2_clothes on Ascend NPU

1. 简介

本文档记录 mattmdjaga/segformer_b2_clothes SegFormer-B2 服装语义分割模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。

该模型基于 SegFormer-B2(层级化 Transformer + All-MLP Decoder,24.7M 参数),在服装解析数据集上微调。支持 18 类服装/人体区域分割:Background、Hat、Hair、Sunglasses、Upper-clothes、Skirt、Pants、Dress、Belt、Left-shoe、Right-shoe、Face、Left-leg、Right-leg、Left-arm、Right-arm、Bag、Scarf。适用于电商商品图解析、虚拟试衣、时尚分析等场景。

SegFormer-B2 是 B0 的增强版——更大的 hidden dimension 和更多 transformer 层,分割精度更高但推理速度稍慢。B2 vs B0:参数量 24.7M vs 3.7M,精度显著提升,适合对服装细节要求高的场景。

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/mattmdjaga/segformer_b2_clothes

2. 验证环境

组件版本
torch2.8.0
torch_npu2.8.0.post4
transformers5.8.1
CANN8.5.1
  • NPU:8 × Ascend 910B3
  • 精度对比基准:CPU(x86, PyTorch 2.8.0)

3. 部署使用流程

3.1 环境准备

conda create -n mattmdjaga_segformer_b2_clothes python=3.11 -y
conda activate mattmdjaga_segformer_b2_clothes

pip install torch==2.8.0 torch_npu==2.8.0.post4 \
    -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers torchvision pillow numpy \
    -i https://pypi.tuna.tsinghua.edu.cn/simple

3.2 推理脚本使用

python inference.py --image person.jpg --device npu
python inference.py --image_dir ./photos/ --device npu

编程接口:

from inference import SegformerSegmentor
seg = SegformerSegmentor(
    model_path="./mattmdjaga_segformer_b2_clothes", device="npu"
)
pred = seg.predict(["person.jpg"])
# pred[0].shape → (128, 128)  像素级 18 类分割

4. Smoke 验证

python inference.py --image person.jpg --device npu

预期输出:各服装区域的像素占比(Background, Upper-clothes, Pants 等),无运行时错误。

5. 性能参考

测试条件:6 张合成 512×512 图像(固定种子),batch_size=4,NPU 预热 1 轮。

指标数值
CPU 吞吐量0.5 img/s
NPU 吞吐量23.4 img/s
CPU/NPU 加速比51.6 ×

SegFormer-B2 比 B0 大(24.7M vs 3.7M),NPU 推理速度稍慢但加速比仍然极高(51.6×)。服装分割的 512×512 输入分辨率导致 CPU 几乎无法实时处理。

6. 精度评测

6.1 评测方法

分别在 CPU 和 NPU 上推理 6 张合成 512×512 图像,比较逐像素 18 维分类 logits 的展平余弦相似度。

6.2 评测结果

指标数值
平均余弦相似度1.000000
精度误差率0.0000%

结论:精度误差率 0.0000%,NPU 与 CPU 输出完全一致,评测通过。

7. 迁移适配说明

7.1 模型结构

  • Backbone:SegFormer-B2 Hierarchical Transformer Encoder(4 stage,Mix-FFN,比 B0 更大的 hidden dim)
  • Decoder:All-MLP Decoder,融合 4 级多尺度特征图
  • Head:18 通道逐像素 logits,上采样到 1/4 分辨率
  • 参数量:24.7M(B2 增强版,B0 为 3.7M,B5 为 84.7M)
  • 输入:512×512 RGB 图像

7.2 适配要点

  1. AutoModelForSemanticSegmentation.from_pretrained() 加载
  2. model.to("npu:0") 迁移,SegFormer 全部算子(Conv2d, LayerNorm, GELU, Mix-FFN)NPU 原生支持
  3. AutoImageProcessor CPU 端预处理(512×512 resize + ImageNet 标准化)
  4. 与 face-parsing 的 SegFormer-B0 完全相同适配代码(仅模型尺寸不同)

7.3 关键代码

import torch, torch_npu
from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation

model = AutoModelForSemanticSegmentation.from_pretrained(
    "segformer_b2_clothes"
).to("npu:0")
processor = AutoImageProcessor.from_pretrained("segformer_b2_clothes")

from PIL import Image
image = Image.open("person.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}

with torch.no_grad():
    outputs = model(**inputs)
    seg_map = outputs.logits.argmax(dim=1)  # (1, 128, 128), 18 classes

8. 注意事项

  1. SegFormer-B2 vs B0:B2 参数更多(24.7M vs 3.7M),服装边缘和细小物件(眼镜、鞋、腰带)的分割更精细,但推理速度约慢 40%。
  2. 18 类服装标签:覆盖完整的人体+服装区域,支持左右对称分类(Left-shoe/Right-shoe, Left-leg/Right-leg 等)。电商场景可精确定位上装/裤/裙/鞋等。
  3. 512×512 固定输入:模型在 512×512 训练,输入会自动 resize。非方形图片建议先中心裁剪。
  4. 首次 NPU 推理:SegFormer-B2 24.7M 参数,算子编译约 5-8 秒。
  5. 服装解析应用:输出可用于背景替换、虚拟试衣、商品属性提取(颜色/款式/类型)等下游任务。