Views
No views yet
1import torch
2from transformers import AutoImageProcessor, SiglipForImageClassification
3from PIL import Image
4import torch.nn.functional as F
5
6model_path = "Ateeqq/nsfw-image-detection"
7processor = AutoImageProcessor.from_pretrained(model_path)
8model = SiglipForImageClassification.from_pretrained(model_path)
9
10image_path = r"/content/download.jpg"
11image = Image.open(image_path).convert("RGB")
12inputs = processor(images=image, return_tensors="pt")
13
14with torch.no_grad():
15 logits = model(**inputs).logits
16probabilities = F.softmax(logits, dim=1)
17
18predicted_class_id = logits.argmax().item()
19predicted_class_label = model.config.id2label[predicted_class_id]
20confidence_scores = probabilities[0].tolist()
21
22print(f"Predicted class ID: {predicted_class_id}")
23print(f"Predicted class label: {predicted_class_label}\n")
24for i, score in enumerate(confidence_scores):
25 label = model.config.id2label[i]
26 print(f"Confidence for '{label}': {score:.6f}")Predicted class ID: 2
Predicted class label: safe_normal
Confidence for 'gore_bloodshed_violent': 0.000002
Confidence for 'nudity_pornography': 0.000005
Confidence for 'safe_normal': 0.999993google/siglip2-base-patch16-224
| ID | Label | Excluded |
|---|---|---|
| 0 | ✅gore_bloodshed_violent | ❌ Fight, Accident, Angry |
| 1 | ✅nudity_pornography | ❌ Normal Romance, Normal Kissing |
| 2 | ✅safe_normal | ❌ |
1label2id = {'gore_bloodshed_violent': 0, 'nudity_pornography': 1, 'safe_normal': 2}
2id2label = {0: 'gore_bloodshed_violent', 1: 'nudity_pornography', 2: 'safe_normal'}| Epoch | Training Loss | Validation Loss | Accuracy |
|---|---|---|---|
| 1 | 0.0765 | 0.1166 | 95.70% |
| 2 | 0.0719 | 0.0477 | 98.34% |
| 3 | 0.0089 | 0.0634 | 98.05% |
| 4 | 0.0109 | 0.0437 | 98.61% |
| 5 ✅ | 0.0001 | 0.0389 | 99.02% |
