模型名称: 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)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| timm | >= 0.9.0 | PyTorch 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# 检查 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__)"方式一: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# 单张图片推理
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| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径(包含 config.json) |
| --device | str | npu | 推理设备:npu/cuda/cpu |
| --image | str | None | 输入图片路径(可选) |
| --batch-size | int | 4 | 批量推理的 batch 大小 |
[模型] 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次推理统计:
平均: 13.62 ms/image
最小: -
最大: -
状态: SUCCESS运行命令:
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!
============================================================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})')
"


测试数据: 随机生成的测试图像(正式评测需使用已标注的 NSFW 数据集)
评测指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 单条推理延迟 | 320.57 ms | Ascend910_9362,448×448 输入 |
| 批量推理延迟 | 13.62 ms/张 | batch=4,Ascend910_9362 |
| 模型参数量 | 86,351,620 | EVA-02 Base |
| 输出类别数 | 4 | neutral/low/medium/high |
评测命令:
python inference.py --model-path ./nsfw_model --device npu --batch-size 4注意: 当前使用 ImageNet 预训练权重(timm 默认)。如使用 Freepik 微调权重,需先下载 pytorch_model.bin 放置于模型目录,推理脚本会自动加载。
pytorch_model.bin 放于模型目录,脚本会自动检测并加载。