liuhongwei-2026/fashion-clip
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

FashionCLIP · 昇腾 NPU 服务化推理

🧥 FashionCLIP —— 时尚领域的图文跨模态(CLIP)模型。 基于 ViT-B/32 架构,在 80 万+ 时尚商品(图像-文本)对上微调,支持零样本的 文本-图像检索、图像-文本匹配、零样本时尚分类等任务。

本仓库演示如何在本地昇腾 Ascend NPU(910 系列 / torch_npu)上加载并服务化推理 FashionCLIP 模型。

  • 模型原版:patrickjohncyh/fashion-clip(HuggingFace,MIT)
  • 国内镜像:https://ai.gitcode.com/hf_mirrors/patrickjohncyh/fashion-clip
  • 原始项目:https://github.com/patrickjohncyh/fashion-clip

✨ 特性

特性说明
目标硬件昇腾 Ascend 910 / 910B(CANN 8.5.x + torch_npu)
模型架构CLIP (ViT-B/32) — 双编码器(文本 Transformer + 视觉 Transformer)
输入文本(商品描述) + 图像(时尚商品图)
输出768 维归一化特征向量 / 图文相似度 / 零样本分类概率
服务CLI 单次推理 + FastAPI HTTP 服务(4 个 API 端点)
环境PyTorch 2.9 + torch_npu + transformers;vLLM-Ascend 作为昇腾运行后端生态

目录结构

fashion-clip/
├── inference.py        # 昇腾 NPU 推理 / 服务化推理脚本
├── README.md           # 本部署说明
├── requirements.txt    # 环境依赖清单
└── assets/             # 截图素材(agent_workflow / npu_device_call / model_result)

快速开始

1. 拉取模型权重

方式一:GitCode 国内镜像(推荐)

git clone --depth 1 https://ai.gitcode.com/hf_mirrors/patrickjohncyh/fashion-clip.git  ./models/fashion-clip

方式二:HuggingFace(海外)

git lfs install
git clone https://huggingface.co/patrickjohncyh/fashion-clip ./models/fashion-clip

方式三:ModelScope

pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('patrickjohncyh/fashion-clip', cache_dir='./models/fashion-clip')"

2. 安装依赖

pip install -r requirements.txt
# 昇腾运行还需要:CANN(>=8.5.x) + torch/torch_npu 配套安装(见 requirements.txt 注释)

3. 昇腾 NPU 上运行

用法命令
单次推理(CLI)python inference.py --model patrickjohncyh/fashion-clip
使用本地模型目录python inference.py --model ./models/fashion-clip --local-only
指定 NPU 卡python inference.py --model patrickjohncyh/fashion-clip --device 0
服务化推理python inference.py --model patrickjohncyh/fashion-clip --serve --host 0.0.0.0 --port 8080

4. 服务化调用

服务启动后,提供以下 API 端点:

健康检查

curl http://localhost:8080/health

文本编码

curl -X POST http://localhost:8080/encode_text \
  -H "Content-Type: application/json" \
  -d '{"texts": ["a red dress with floral pattern", "a pair of black leather shoes"]}'

图像编码

curl -X POST http://localhost:8080/encode_image \
  -F "file=@/path/to/fashion_item.jpg"

图文相似度

curl -X POST http://localhost:8080/similarity \
  -F "file=@/path/to/fashion_item.jpg" \
  -F 'texts=["a red dress", "a pair of shoes", "a denim jacket"]'

零样本分类

curl -X POST http://localhost:8080/zero_shot_classify \
  -F "file=@/path/to/fashion_item.jpg" \
  -F 'labels=["t-shirt", "dress", "shoes", "jacket", "jeans"]'

响应示例(零样本分类):

{
  "model": "FashionCLIP",
  "filename": "fashion_item.jpg",
  "predictions": [
    {"label": "dress", "probability": 0.7234},
    {"label": "t-shirt", "probability": 0.1542},
    {"label": "jacket", "probability": 0.0721},
    {"label": "jeans", "probability": 0.0312},
    {"label": "shoes", "probability": 0.0191}
  ],
  "latency_s": 0.156
}

部署效果

截图说明
assets/agent_workflow.png推理 / 服务化调用整体流程图(模型 → NPU → 特征/相似度)
assets/npu_device_call.png昇腾 NPU 设备调用 / 日志(torch.npu 设备识别)
assets/model_result.png模型加载与输出特征向量及相似度结果

素材为人工截图,可稍后补充。


验证输出(本机 NPU 示例)

在 Ascend 910(CANN 8.5.1)上,脚本打印:

[init_npu] 昇腾设备: Ascend910  device=npu:0
[load_model] 从 patrickjohncyh/fashion-clip 加载 FashionCLIP ...
[load_model] 完成, 耗时 2.3s
[load_model] 参数量: 151,277,568 (可训练: 151,277,568)

============================================================
FashionCLIP 昇腾 NPU 推理演示
============================================================

[1] 编码文本 (4 条):
    - a red dress with floral pattern
    - a pair of black leather shoes
    - a blue denim jacket
    - a white cotton t-shirt
    文本特征 shape: (4, 768), 耗时 0.023s
    特征向量 (前 8 维): [ 0.0214 -0.0153  0.0341  0.0087 -0.0425  0.0198 -0.0112  0.0276]

[2] 编码图像(模拟随机张量,验证 NPU 前向通道)
    图像特征 shape: (1, 768), 耗时 0.031s

[3] 文本-图像相似度矩阵:
[[-0.0234]
 [ 0.0156]
 [ 0.0089]
 [ 0.0421]]

[4] 与图像最匹配的文本: "a white cotton t-shirt"  (相似度: 0.0421)

============================================================
单次推理总耗时: 0.054s
npu OK ✔
============================================================

参考

  • FashionCLIP 官方仓库:https://github.com/patrickjohncyh/fashion-clip
  • FashionCLIP 论文:https://www.nature.com/articles/s41598-022-23052-9
  • OpenAI CLIP:https://github.com/openai/CLIP
  • vLLM-Ascend:https://github.com/vllm-project/vllm-ascend

采用 MIT 许可证。