🚀 nsfw-image-detection-384
nsfw-image-detection-384
是一个轻量级图像分类模型,旨在识别不适宜内容(NSFW)图像。该模型比其他开源模型小约 18 - 20 倍,并且在我们的数据集上达到了 98.56% 的卓越准确率。
🚀 快速开始
安装依赖
pip install timm
代码示例
from urllib.request import urlopen
from PIL import Image
import timm
import torch
img = Image.open(urlopen(
'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
))
model = timm.create_model("hf_hub:Marqo/nsfw-image-detection-384", pretrained=True)
model = model.eval()
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)
with torch.no_grad():
output = model(transforms(img).unsqueeze(0)).softmax(dim=-1).cpu()
class_names = model.pretrained_cfg["label_names"]
print("Probabilities:", output[0])
print("Class:", class_names[output[0].argmax()])
✨ 主要特性
- 轻量级:模型大小约为其他开源模型的 1/18 - 1/20。
- 高准确率:在特定数据集上达到 98.56% 的准确率。
- 灵活输入:使用 384x384 像素图像作为输入,以 16x16 像素块进行处理。
📦 安装指南
使用 timm
库进行安装:
pip install timm
💻 使用示例
基础用法
from urllib.request import urlopen
from PIL import Image
import timm
import torch
img = Image.open(urlopen(
'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
))
model = timm.create_model("hf_hub:Marqo/nsfw-image-detection-384", pretrained=True)
model = model.eval()
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)
with torch.no_grad():
output = model(transforms(img).unsqueeze(0)).softmax(dim=-1).cpu()
class_names = model.pretrained_cfg["label_names"]
print("Probabilities:", output[0])
print("Class:", class_names[output[0].argmax()])
📚 详细文档
评估
此模型在我们的数据集上优于现有的 NSFW 检测器,以下是与 AdamCodd/vit-base-nsfw-detector 和 Falconsai/nsfw_image_detection 的对比评估:

阈值调整与精确率 - 召回率
调整 NSFW 概率的阈值可以在精确率、召回率和准确率之间进行权衡,这在不同应用场景中可能很有用。

训练详情
该模型是基于 timm/vit_tiny_patch16_384.augreg_in21k_ft_in1k 模型进行微调的。
训练参数
batch_size: 256
color_jitter: 0.2
color_jitter_prob: 0.05
cutmix: 0.1
drop: 0.1
drop_path: 0.05
epoch_repeats: 0.0
epochs: 20
gaussian_blur_prob: 0.005
hflip: 0.5
lr: 5.0e-05
mixup: 0.1
mixup_mode: batch
mixup_prob: 1.0
mixup_switch_prob: 0.5
momentum: 0.9
num_classes: 2
opt: adamw
remode: pixel
reprob: 0.5
sched: cosine
smoothing: 0.1
warmup_epochs: 2
warmup_lr: 1.0e-05
warmup_prefix: false
数据集
该模型在包含 220,000 张图像的专有数据集上进行训练。训练集包括 100,000 个 NSFW 示例和 100,000 个适宜内容(SFW)示例,测试集包含 10,000 个 NSFW 示例和 10,000 个 SFW 示例。数据集内容丰富多样,包括真实照片、绘画、规则 34 素材、表情包和 AI 生成图像等。
注意事项
⚠️ 重要提示
与所有模型一样,此模型可能会出现错误。NSFW 内容具有主观性和上下文相关性,该模型旨在帮助识别此类内容,请自行承担使用风险。
💡 使用建议
NSFW 的定义可能有所不同,有时取决于上下文。我们的数据集包含了具有挑战性的示例,但该定义可能并非 100% 适用于每个用例。因此,建议进行实验并尝试不同的阈值,以确定该模型是否适合您的需求。
📄 许可证
本模型使用 Apache 2.0 许可证。
📚 引用
@article{dosovitskiy2020vit,
title={An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and Uszkoreit, Jakob and Houlsby, Neil},
journal={ICLR},
year={2021}
}
@misc{rw2019timm,
author = {Ross Wightman},
title = {PyTorch Image Models},
year = {2019},
publisher = {GitHub},
journal = {GitHub repository},
doi = {10.5281/zenodo.4414861},
howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
}