waylong/Freepik-nsfw_image_detector-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Freepik/nsfw_image_detector on Ascend NPU

1. 模型简介

  • 模型名称: Freepik/nsfw_image_detector
  • 模型类型: 计算机视觉 - 图像分类 (NSFW 检测)
  • 架构: eva02_base_patch14_448 (EVA02 Vision Transformer) via TimmWrapperForImageClassification
  • 任务: 4 分类 NSFW 检测 -> 标签 neutral, low, medium, high
  • 权重: model.safetensors 172.7 MB, config.json 含 pretrained_cfg 输入尺寸 3x448x448, 均值 [0.4814,0.4578,0.4082], 标准差 [0.2686,0.2613,0.2757]
  • 权重 dtype: bfloat16 (config torch_dtype: bfloat16), 推理保持 bfloat16 在 NPU 上
  • 来源: https://huggingface.co/Freepik/nsfw_image_detector (license: mit)
  • 模型描述: 基于 EVA 架构的 Vision Transformer, 使用 100k 合成标注图像微调, 支持二分类或四级概率输出, 在内部基准中优于 Falconsai/AdamCodd 等模型。

2. 验证环境

  • 硬件: Ascend910 (npu-smi 显示 Ascend910, 设备名 Ascend910_9362, 双卡)
  • NPU 显存: 64 GB HBM/卡, 实测 free 54GB / total 61.27GB
  • 系统: npu-smi info 可用, torch.npu.is_available() == True, device_count=2
  • 软件: torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 5.9.0, timm 1.0.x, CANN 8.5.1
  • 检查命令:
npu-smi info
python -c "import torch, torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))"
python scripts/check_npu.py --output env_check.json

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch>=2.0
# torch-npu>=2.5.0
# transformers>=4.48.0
# timm>=1.0.0
# Pillow>=9.0
# numpy
# safetensors
# accelerate

4. NPU 推理

默认命令(自动优先 NPU):

python inference.py

指定验证与性能:

python inference.py --mode validate   # CPU vs NPU 一致性
python inference.py --mode benchmark  # 同步计时 3 warmup + 10 迭代

推理契约:

  • 输入: PIL RGB 448x448, 通过 TimmWrapperImageProcessor 归一化
  • 设备: 自动 npu:0 (若不可用回退 CPU, 但标记非 NPU 成功)
  • 输出: logits [1,4] -> softmax probs -> argmax label, 同时打印耗时
  • 同步: 计时区间前后均调用 torch.npu.synchronize()

5. 真实推理结果

以下为本次 npu:0 上真实执行 python inference.py 的输出 (bfloat16, 4 张确定性测试图):

[INFO] Loading model from: model_local
[INFO] Model: Freepik/nsfw_image_detector | Architecture: eva02_base_patch14_448 | Type: timm_wrapper
[INFO] Processor: TimmWrapperImageProcessor | Image size: 448x448
[INFO] Requested dtype: torch.bfloat16 | Labels: ['neutral', 'low', 'medium', 'high']
[INFO] Model device: npu:0 | dtype: torch.bfloat16

[INPUT] neutral_gray_448 | shape=(1, 3, 448, 448) | dtype=torch.bfloat16
[OUTPUT] logits=[3.5625, -1.625, -1.515625, -1.75]
[OUTPUT] probs=[0.984375, 0.0054931640625, 0.006134033203125, 0.004852294921875]  (neutral, low, medium, high)
[OUTPUT] pred=neutral (idx=0) | latency=291.54ms (首次含编译)

[INPUT] warm_beige_448 | shape=(1, 3, 448, 448)
[OUTPUT] logits=[4.65625, -1.953125, -2.234375, -2.5625]
[OUTPUT] probs=[0.99609375, 0.0013427734375, 0.00101470947265625, 0.000728607177734375]
[OUTPUT] pred=neutral (idx=0) | latency=11.07ms

[INPUT] dark_448
[OUTPUT] logits=[2.515625, -1.015625, -0.93359375, -1.359375]
[OUTPUT] probs=[0.92578125, 0.027099609375, 0.0294189453125, 0.0191650390625]
[OUTPUT] pred=neutral (idx=0) | latency=11.21ms

[INPUT] gradient_448
[OUTPUT] logits=[4.9375, -0.859375, -2.734375, -2.203125]
[OUTPUT] probs=[0.99609375, 0.003021240234375, 0.0004634857177734375, 0.000789642333984375]
[OUTPUT] pred=neutral (idx=0) | latency=11.24ms

[RESULT] Primary image pred: neutral | probs: [0.984375, 0.005493, 0.006134, 0.004852]
[STATUS] PASS
[DONE] NPU inference succeeded

首帧含编译 291ms, 后续稳定 ~11ms (见性能节)。

6. CPU-NPU 一致性验证

执行 python inference.py --mode validate:

  • CPU (bfloat16) logits [3.546875, -1.625, -1.5078125, -1.7578125] probs [0.984375, 0.00558, 0.00628, 0.00488]
  • NPU (bfloat16, npu:0) logits [3.5625, -1.625, -1.515625, -1.75] probs [0.984375, 0.00549, 0.00613, 0.00485]
  • 对比: max_abs_logits=0.015625 mean=0.007812 max_abs_probs=0.000153
  • 预测一致: CPU=neutral (0) NPU=neutral (0) -> PASS (bfloat16 容差 <0.05 通过)
  • 说明: 单样本一致性验证 (smoke consistency), 非完整数据集评测; 容差源于 NPU 与 CPU bfloat16 数值差异。

7. 性能测试

执行 python inference.py --mode benchmark (同步计时, warmup 3, 迭代 10, bfloat16, batch=1, 448x448, npu:0):

[BENCHMARK] Running 10 iterations (synchronized)...
  iter 1: 10.69ms
  iter 2: 10.54ms
  iter 3: 10.64ms
  iter 4: 10.66ms
  iter 5: 10.74ms
  iter 6: 10.69ms
  iter 7: 10.66ms
  iter 8: 10.54ms
  iter 9: 10.01ms
  iter 10: 9.84ms
[RESULT] avg=10.50ms min=9.84ms max=10.74ms p50=10.65ms p90=10.70ms p95=10.72ms
[RESULT] throughput=95.24 images/s
[RESULT] NPU memory: 54.38GB free / 61.27GB total
  • 预热后稳定推理 ~10.5ms/图, 吞吐 ~95 images/s
  • 首次推理因算子编译较慢 (~291ms) 与稳定阶段区分报告
  • 峰值显存占用低 (EVA base ~165MB 权重, 激活 <1GB)

8. 自验证截图

三张证据图由 scripts/render_xterm_evidence.mjs 从真实日志渲染 (raw 终端风格):

  • assets/agent_workflow.png - 适配流水: 下载、侦察、加载、NPU 推理、验证、性能
  • assets/npu_device_call.png - NPU 设备调用: npu-smi, torch.npu 可用性, 设备名, 模型 device/dtype
  • assets/model_result.png - 模型推理结果: 默认 inference.py 输出、概率、延迟与 PASS

9. 已知限制

  • bfloat16 精度: NPU 与 CPU logits 存在 ~0.015 差异, 属正常浮点误差
  • 输入固定: 当前验证使用 448x448 RGB, processor 配置 squash+ bicubic 插值; 其他分辨率需符合 processor 要求
  • 单样本验证: 未进行完整数据集精度评测
  • 首次编译: 首帧延迟含 Ascend 算子编译, 后续稳定
  • 依赖远程权重: 推理时若本地无缓存会通过 huggingface.co 下载 (hf-mirror 可配置)

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910