🚀 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}}
}