模型介绍
内容详情
替代品
模型简介
本模型基于EVA架构视觉Transformer,通过10万张合成标注图像训练,支持二元分类或完整概率输出,在NSFW内容检测任务中表现优异。
模型特点
四级风险分类
可识别中性/轻度/中度/重度四级内容风险,提供更精细的等级区分
高性能检测
在重度/中性识别上全面优于对比模型,特别擅长识别轻度/中度内容
AI内容识别
针对AI生成内容优化,是AI生成内容检测的最佳选择
灵活阈值设置
支持自定义风险阈值,可根据需求调整敏感度
模型能力
NSFW图像检测
风险等级分类
AI生成内容识别
批量图像处理
使用案例
内容审核
社交媒体内容过滤
自动检测用户上传图片中的不适宜内容
准确率高达99.54%(重度)/97.02%(中度)/98.31%(轻度)
AI生成内容审核
检测AI生成图像中的不适宜内容
对AI生成的重度内容识别准确率100%
安全防护
家庭网络过滤
为家长控制软件提供内容分级支持
可设置不同年龄段的过滤级别
license: mit base_model:
- timm/eva02_base_patch14_448.mim_in22k_ft_in22k_in1k pipeline_tag: image-classification tags:
- pytorch
- transformers
基于EVA架构的快速NSFW图像分类器
目录
模型描述
本模型是基于EVA架构的视觉Transformer,专为NSFW内容分类微调。通过10万张合成标注图像训练,可检测四个级别(中性、轻度、中度、高度)的视觉内容。
该模型可作为二元分类器(是/否)使用,也可获取完整概率输出。在我们的内部基准测试中,其性能优于Falconsai/nsfw_image_detection和AdamCodd/vit-base-nsfw-detector等优秀公开模型,并能根据使用场景灵活选择NSFW级别。
在线体验 🚀
通过我们的Hugging Face Space可直接在浏览器中体验模型。上传任意图像即可即时获取NSFW分类结果,无需安装任何软件。
模型性能对比
整体性能
类别 | Freepik | Falconsai | Adamcodd |
---|---|---|---|
高度 | 99.54% | 97.92% | 98.62% |
中度 | 97.02% | 78.54% | 91.65% |
轻度 | 98.31% | 31.25% | 89.66% |
中性 | 99.87% | 99.27% | 98.37% |
对比标准说明:
- Falconsai和AdamCodd模型:
- 标注为"轻度"、"中度"或"高度"且模型返回true视为正确
- 标注为"中性"时模型应返回false
- Freepik模型:
- 标注为"轻度"、"中度"或"高度"时,模型至少应返回"轻度"
- 标注为"中性"时模型应返回"中性"
结论:
- 我们的模型在准确率上全面超越AdamCodd和Falconsai,尤其在"高度"和"中性"标签的对比完全公平
- 提供更精细的粒度划分,不仅能检测"高度"和"中性"内容,对"轻度"和"中度"NSFW内容识别同样出色
- Falconsai可能将部分"中度"和"轻度"图像误判为非NSFW
- AdamCodd将"轻度"和"中度"统一归类为NSFW,且对10%的轻度/中度内容误判为SFW
AI生成内容准确率
我们建立了人工标注数据集,特别注意避免性别、种族等偏见。虽然样本量较小,但为训练过程提供了有价值的参考。
AI生成内容
类别 | Freepik模型 | Falconsai模型 | Adamcodd模型 |
---|---|---|---|
高度 | 100.00% | 84.00% | 92.00% |
中度 | 96.15% | 69.23% | 96.00% |
轻度 | 100.00% | 35.71% | 92.86% |
中性 | 100.00% | 100.00% | 66.67% |
结论:
- 避免使用Falconsai处理AI生成内容,其错误率较高
- 我们的模型是检测AI生成NSFW内容的最佳选择
使用方法
pip快速启动
pip install nsfw-image-detector
from PIL import Image
from nsfw_image_detector import NSFWDetector
import torch
# 初始化检测器
detector = NSFWDetector(dtype=torch.bfloat16, device="cuda")
# 加载并分类图像
image = Image.open("your_image")
# 检查是否包含中度及以上NSFW内容
is_nsfw = detector.is_nsfw(image, "medium")
# 获取所有类别的概率分数
probabilities = detector.predict_proba(image)
print(f"是否NSFW: {is_nsfw}")
print(f"概率分布: {probabilities}")
示例输出:
是否NSFW: False
概率分布:
[
{<NSFWLevel.HIGH: 'high'>: 0.00372314453125,
<NSFWLevel.MEDIUM: 'medium'>: 0.1884765625,
<NSFWLevel.LOW: 'low'>: 0.234375,
<NSFWLevel.NEUTRAL: 'neutral'>: 0.765625}
]
Pipeline快速启动
from transformers import pipeline
from PIL import Image
# 创建分类pipeline
classifier = pipeline(
"image-classification",
model="Freepik/nsfw_image_detector",
device=0 # 使用GPU(0)或CPU(-1)
)
# 加载并分类图像
image = Image.open("path/to/your/image.jpg")
predictions = classifier(image)
print(predictions)
示例输出:
[
{'label': 'neutral', 'score': 0.92},
{'label': 'low', 'score': 0.05},
{'label': 'medium', 'score': 0.02},
{'label': 'high', 'score': 0.01}
]
支持批量图像处理:
images = [Image.open(path) for path in ["image1.jpg", "image2.jpg", "image3.jpg"]]
predictions = classifier(images)
注意:生产环境使用前请查阅速度与内存指标章节。
免pip依赖安装
以下示例展示如何自定义NSFW检测阈值(与PyPy代码类似),当NSFW级别达到'中度'时返回True:
from transformers import AutoModelForImageClassification
import torch
from PIL import Image
from typing import List, Dict
import torch.nn.functional as F
from timm.data.transforms_factory import create_transform
from torchvision.transforms import Compose
from timm.data import resolve_data_config
from timm.models import get_pretrained_cfg
device = "cuda" if torch.cuda.is_available() else "cpu"
# 加载模型和处理器
model = AutoModelForImageClassification.from_pretrained("Freepik/nsfw_image_detector", torch_dtype = torch.bfloat16).to(device)
# 加载原始处理器(张量处理更快)
cfg = get_pretrained_cfg("eva02_base_patch14_448.mim_in22k_ft_in22k_in1k")
processor: Compose = create_transform(**resolve_data_config(cfg.__dict__))
def predict_batch_values(model, processor: Compose, img_batch: List[Image.Image] | torch.Tensor) -> List[Dict[str, float]]:
"""
批量处理图像并返回各NSFW类别的预测分数
"""
idx_to_label = {0: 'neutral', 1: 'low', 2: 'medium', 3: 'high'}
# 准备批次数据
inputs = torch.stack([processor(img) for img in img_batch])
output = []
with torch.inference_mode():
logits = model(inputs).logits
batch_probs = F.log_softmax(logits, dim=-1)
batch_probs = torch.exp(batch_probs).cpu()
for i in range(len(batch_probs)):
element_probs = batch_probs[i]
output_img = {}
danger_cum_sum = 0
for j in range(len(element_probs) - 1, -1, -1):
danger_cum_sum += element_probs[j]
if j == 0:
danger_cum_sum = element_probs[j]
output_img[idx_to_label[j]] = danger_cum_sum.item()
output.append(output_img)
return output
def prediction(model, processor, img_batch: List[Image.Image], class_to_predict: str, threshold: float=0.5) -> List[bool]:
"""
预测图像是否达到指定NSFW阈值
"""
if class_to_predict not in ["low", "medium", "high"]:
raise ValueError("class_to_predict必须为: low, medium, high")
if not 0 <= threshold <= 1:
raise ValueError("threshold必须在0到1之间")
output = predict_batch_values(model, processor, img_batch)
return [output[i][class_to_predict] >= threshold for i in range(len(output))]
# 使用示例
image = Image.open("path/to/your/image.jpg")
print(predict_batch_values(model, processor, [image]))
print(prediction(model, processor, [image], "medium")) # 可选: low, medium, high
示例输出:
[
{'label': 'neutral', 'score': 0.92},
{'label': 'low', 'score': 0.08},
{'label': 'medium', 'score': 0.03},
{'label': 'high', 'score': 0.01}
]
[False]
注意:概率总和大于1是因为预测值是所选标签及以上级别的累计值(中性除外)。例如选择'中度'时,结果为'中度'和'高度'的概率和。我们认为这种方法比仅选择最高概率标签更有效。
训练过程
- 使用10万张图像进行训练
- 在3块NVIDIA GeForce RTX 3090上训练3个epoch
- 采用训练集和验证集双划分
- 通过"openai/clip-vit-base-patch32"模型去重,确保训练/验证集间余弦相似度<0.92的图像
- 采用自定义损失函数,极大降低将高级别内容误判为低级别的情况(如"高度"误判为"中性"的概率仅0.46%)
速度与内存指标
批次大小 | 单批平均耗时(ms) | 显存占用(MB) | 优化措施 |
---|---|---|---|
1 | 28 | 540 | BF16+PIL图像 |
4 | 110 | 640 | BF16+PIL图像 |
16 | 412 | 1144 | BF16+PIL图像 |
1 | 10 | 540 | BF16+张量 |
4 | 33 | 640 | BF16+张量 |
16 | 102 | 1144 | BF16+张量 |
说明:
- 模型使用bf16训练,推荐使用bf16推理
- 张量模式:pipeline无法达到张量模式的速度,生产环境应避免使用pipeline
- 测试环境为NVIDIA RTX 3090,服务器环境会有更好表现
- 增大批次尺寸可提升吞吐量,但需根据场景权衡
- 文本生成图像等场景特别推荐使用张量模式,因其输出本就是张量格式
许可协议
本项目采用MIT许可证 - 版权所有 2025 Freepik Company S.L.
引用
如果您在研究或项目中使用本模型,请引用:
@software{freepik2025nsfw,
title={基于EVA架构的快速NSFW图像分类器},
author={Freepik Company S.L.},
year={2025},
publisher={Hugging Face},
url = {https://huggingface.co/Freepik/nsfw_image_detector},
organization = {Freepik Company S.L.}
}
致谢
本模型基于EVA架构(timm/eva02_base_patch14_448.mim_in22k_ft_in22k_in1k),相关论文:
EVA-02:新世纪视觉表征 - https://arxiv.org/abs/2303.11331
@article{EVA02,
title={EVA-02:新世纪视觉表征},
author={方雨欣 and
Nsfw Image Detection
Apache-2.0
基于ViT架构的NSFW图像分类模型,通过监督学习在ImageNet-21k数据集上预训练,并在80,000张图像上微调,用于区分正常和NSFW内容。
图像分类
Transformers

N
Falconsai
82.4M
588
Fairface Age Image Detection
Apache-2.0
基于Vision Transformer架构的图像分类模型,在ImageNet-21k数据集上预训练,适用于多类别图像分类任务
图像分类
Transformers

F
dima806
76.6M
10
Dinov2 Small
Apache-2.0
基于DINOv2方法训练的小尺寸视觉Transformer模型,通过自监督学习提取图像特征
图像分类
Transformers

D
facebook
5.0M
31
Vit Base Patch16 224
Apache-2.0
基于ImageNet-21k预训练和ImageNet微调的视觉变换器模型,用于图像分类任务
图像分类
V
google
4.8M
775
Vit Base Patch16 224 In21k
Apache-2.0
基于ImageNet-21k数据集预训练的视觉Transformer模型,用于图像分类任务。
图像分类
V
google
2.2M
323
Dinov2 Base
Apache-2.0
基于DINOv2方法训练的视觉Transformer模型,通过自监督学习提取图像特征
图像分类
Transformers

D
facebook
1.9M
126
Gender Classification
一个基于PyTorch和HuggingPics构建的图像分类模型,用于识别图像中的性别
图像分类
Transformers

G
rizvandwiki
1.8M
48
Vit Base Nsfw Detector
Apache-2.0
基于Vision Transformer (ViT)架构的图像分类模型,专门用于检测图像是否包含NSFW(不安全)内容。
图像分类
Transformers

V
AdamCodd
1.2M
47
Vit Hybrid Base Bit 384
Apache-2.0
混合视觉变换器(ViT)模型结合了卷积网络和Transformer架构,用于图像分类任务,在ImageNet上表现出色。
图像分类
Transformers

V
google
992.28k
6
Gender Classification 2
这是一个基于PyTorch框架和HuggingPics工具生成的图像分类模型,专门用于性别分类任务。
图像分类
Transformers

G
rizvandwiki
906.98k
32
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers

支持多种语言
L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers

英语
C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统
中文
R
uer
2,694
98
AIbase是一个专注于MCP服务的平台,为AI开发者提供高质量的模型上下文协议服务,助力AI应用开发。
简体中文