Views
No views yet
nsfw_dataset_v1 dataset. This dataset comprises five distinct classes:drawingshentaineutralpornsexy precision recall f1-score support
0 0.85 0.88 0.87 807
1 0.94 0.88 0.91 839
2 0.90 0.93 0.92 870
3 0.98 0.94 0.96 861
4 0.92 0.97 0.94 823
accuracy 0.92 4200
macro avg 0.92 0.92 0.92 4200
weighted avg 0.92 0.92 0.92 4200
1from transformers import ViTImageProcessor, ViTForImageClassification
2from PIL import Image
3import requests
4import torch
5
6# Load the model and processor
7model_name = "acaciabengo/nsfw_image_detection"
8processor = ViTImageProcessor.from_pretrained(model_name)
9model = ViTForImageClassification.from_pretrained(model_name)
10
11# Mapping IDs to human-readable labels
12labels = {'drawings': 0, 'hentai': 1, 'neutral': 2, 'porn': 3, 'sexy': 4}
13id_to_label = {v: k for k, v in labels.items()}
14
15# Load an image from a URL or local path
16url = "https://example.com/sample_image.jpg"
17image = Image.open(requests.get(url, stream=True).raw)
18
19# Preprocess image and perform inference
20inputs = processor(images=image, return_tensors="pt")
21
22with torch.no_grad():
23 outputs = model(**inputs)
24 logits = outputs.logits
25
26# Calculate probabilities and get the top prediction
27probabilities = logits.softmax(dim=-1).squeeze().tolist()
28predicted_class_idx = logits.argmax(-1).item()
29
30print(f"Predicted label: {id_to_label[predicted_class_idx]}")
31print("\nProbabilities:")
32for i, prob in enumerate(probabilities):
33 print(f"- {id_to_label[i]}: {prob:.2%}")
34