g
gcw_coj3XaOd/nsfw_image_detector
模型介绍
文件和版本
Pull Requests
讨论
分析

Freepik/nsfw_image_detector - 昇腾 NPU 推理部署

1. 模型简介

模型名称: Freepik/nsfw_image_detector

模型链接: HuggingFace

模型描述: 基于 EVA-02 Base 架构的 NSFW(不适宜工作场所内容)图像检测模型,用于对图像进行 4 级分类(neutral/low/medium/high)。

模型架构: EVA-02 Base (Patch 14, 448x448) - timm/eva02_base_patch14_448.mim_in22k_ft_in22k_in1k

参数规模: 86,351,620 (EVA-02 Base)

输入规格:

  • RGB 图像,448×448 像素
  • 归一化:mean=[0.4814, 0.4578, 0.4082], std=[0.2686, 0.2613, 0.2758]
  • 数据范围:[0, 1]

输出规格:

  • 4 分类概率分布:neutral, low, medium, high
  • 输出格式:logits → softmax → 概率

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
timm>= 0.9.0PyTorch Image Models 库
Pillow>= 9.0.0图像处理
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu>=2.1.0 timm>=0.9.0 Pillow>=9.0.0 numpy>=1.24.0

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

# 验证 timm
python3 -c "import timm; print(timm.__version__)"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download Freepik/nsfw_image_detector --local-dir ./nsfw_model

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

wget https://ai.gitcode.com/hf_mirrors/Freepik/nsfw_image_detector/resolve/main/pytorch_model.bin -O ./nsfw_model/pytorch_model.bin
wget https://ai.gitcode.com/hf_mirrors/Freepik/nsfw_image_detector/resolve/main/config.json -O ./nsfw_model/config.json

3.3 运行推理

# 单张图片推理
python inference.py --model-path ./nsfw_model --device npu

# 指定图片
python inference.py --model-path ./nsfw_model --device npu --image /path/to/image.jpg

# 批量推理(默认 batch_size=4)
python inference.py --model-path ./nsfw_model --device npu --batch-size 8

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径(包含 config.json)
--devicestrnpu推理设备:npu/cuda/cpu
--imagestrNone输入图片路径(可选)
--batch-sizeint4批量推理的 batch 大小

4. 推理成功日志

4.1 单条推理日志

[模型] Freepik/nsfw_image_detector (EVA-02 Base)
[设备] Ascend910_9362 (npu:0)
[输入] 测试图像 (448x448 RGB)
[输出] neutral (confidence: 0.2502)
[全部分类] neutral: 0.2502, low: 0.2501, medium: 0.2499, high: 0.2499
[耗时] 320.57 ms
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 4次推理统计:
  平均: 13.62 ms/image
  最小: -
  最大: -
  状态: SUCCESS

5. 测试样例及输出结果

样例 1:单张图像推理

运行命令:

python inference.py --model-path /tmp/nsfw_model_full --device npu

输出:

============================================================
NSFW Image Detector - Ascend NPU Inference
============================================================
[INFO] Model path: /tmp/nsfw_model_full
[INFO] Device: npu
[INFO] PyTorch: 2.9.0+cpu
[INFO] timm: 1.0.28
[INFO] NPU devices: 2
[INFO] NPU name: Ascend910_9362

[Step 1] Loading model...
[INFO] Architecture: eva02_base_patch14_448
[INFO] Num classes: 4
[INFO] Labels: ['neutral', 'low', 'medium', 'high']
[INFO] Input size: [3, 448, 448]
[OK] Model loaded on npu

[Step 2] Single image inference...
[RESULT] Predicted: neutral (confidence: 0.2502)
[RESULT] All probabilities: {'neutral': 0.2502, 'low': 0.2501, 'medium': 0.2499, 'high': 0.2499}
[RESULT] Elapsed: 320.57 ms
[STATUS] Single inference SUCCESS

[Step 3] Batch inference (batch_size=4)...
[RESULT] Batch results:
  Image 0: medium (0.2501)
  Image 1: low (0.2501)
  Image 2: medium (0.2502)
  Image 3: medium (0.2502)
[RESULT] Average per image: 13.62 ms
[STATUS] Batch inference SUCCESS

============================================================
All inference tests completed successfully!
============================================================

样例 2:Python API 调用

python3 -c "
import torch, timm
from inference import create_model, preprocess_numpy, inference
import numpy as np

model, config, labels = create_model('/tmp/nsfw_model_full', device='npu')

# 创建测试图像
img = np.random.randint(0, 255, (448, 448, 3), dtype=np.uint8)
tensor = preprocess_numpy(img, config)
result = inference(model, tensor, device='npu', labels=labels)
print(f'Predicted: {result[\"class_name\"]} ({result[\"confidence\"]:.4f})')
"

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 随机生成的测试图像(正式评测需使用已标注的 NSFW 数据集)

评测指标:

指标结果说明
单条推理延迟320.57 msAscend910_9362,448×448 输入
批量推理延迟13.62 ms/张batch=4,Ascend910_9362
模型参数量86,351,620EVA-02 Base
输出类别数4neutral/low/medium/high

评测命令:

python inference.py --model-path ./nsfw_model --device npu --batch-size 4

注意: 当前使用 ImageNet 预训练权重(timm 默认)。如使用 Freepik 微调权重,需先下载 pytorch_model.bin 放置于模型目录,推理脚本会自动加载。


8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡
  • 显存占用: ~3.8 GB HBM (单模型加载)
  • CANN 版本: 8.5.1
  • Tensor Parallel: 未启用(单卡推理)

9. 已知问题

  1. 微调权重下载受限: Freepik/nsfw_image_detector 的微调权重(pytorch_model.bin)从 AtomGit 镜像下载时可能遇到 429 限流。建议多次重试或使用 HuggingFace 直接下载。
  2. 权重替换说明: 当前推理使用 timm ImageNet 预训练权重。如需使用 NSFW 微调权重,需将 pytorch_model.bin 放于模型目录,脚本会自动检测并加载。