Downloads · 30 days
79
1% of all-time downloads
lovetillion/nsfw-image-detection-large
nsfw-image-detection-large is a image classification model from lovetillion. Use it when you need a label for an image. The card lists the license as apache-2.0.
This is a fork of MichalMlodawski/nsfw-image-detection-large which became unavailable.
Downloads · 30 days
79
1% of all-time downloads
All-time downloads
6.6K
Public
Parameters
87.1M
349 MB on disk
Likes
2
Public
Click a slice to open those files.
.safetensors349 MB · 100%
From the Hugging Face model README
This is a fork of MichalMlodawski/nsfw-image-detection-large which became unavailable.
from PIL import Image
import torch
from transformers import AutoProcessor, FocalNetForImageClassification
DEVICE = torch.device("cuda")
model_path = "lovetillion/nsfw-image-detection-large"
# Load the model and feature extractor
feature_extractor = AutoProcessor.from_pretrained(model_path)
model = FocalNetForImageClassification.from_pretrained(model_path).to(DEVICE)
model.eval()
# Mapping from model labels to NSFW categories
label_to_category = {
"LABEL_0": "Safe",
"LABEL_1": "Questionable",
"LABEL_2": "Unsafe"
}
filename = "example.png"
image = Image.open(filename)
inputs = feature_extractor(images=image, return_tensors="pt")
inputs.to(DEVICE)
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
confidence, predicted = torch.max(probabilities, 1)
label = model.config.id2label[predicted.item()]
if label != "SAFE":
print( label, confidence.item() * 100, filename )
else:
print( label, confidence.item() * 100, filename )