vit-Large-nsfw-detector (ONNX)
This is an ONNX version of
theusamaaslam/vit-Large-nsfw-detector. It was automatically converted and uploaded using
this Hugging Face Space.
Usage with Transformers.js
Nayatel Vision Transformer NSFW Detector (ViT-Large)
This model is a fine-tuned Vision Transformer (ViT-Large, Patch16-224) developed by the Nayatel AI & DevOps Team to classify images into NSFW or Safe categories.
It is designed for high-recall NSFW detection, making it suitable for content moderation, filtering pipelines, and safety-critical applications.
📊 Evaluation Metrics
Evaluation was performed on a held-out validation set derived from a self-curated dataset. The model demonstrates strong generalization and robustness across diverse NSFW and safe image categories.
| Metric | Value |
|---|
| Accuracy | 95.38% |
| Precision | 90.53% |
| Recall | 93.28% |
| F1 Score | 91.88% |
| ROC AUC | ≈ 0.98 |
🧠 Model Description
- Architecture: Vision Transformer ViT-Large (Patch16, 224×224)
- Parameters: ~304 million
- Base Model:
google/vit-large-patch16-224
- Pretrained on: ImageNet-21k
- Fine-tuned by: Nayatel AI & DevOps Team
- Classes:
nsfw, safe
- Label Mapping:
- Output: Softmax probabilities over two classes
ViT splits an image into fixed-size patches and processes them using multi-head self-attention across 24 transformer layers, enabling strong global context understanding.
📁 Training Details
- Epochs: 5
- Batch Size: 32
- Learning Rate: 3e-5
- Optimizer: AdamW
- Loss Function: Weighted Cross-Entropy (to handle class imbalance)
- Evaluation Strategy: Per epoch
- Precision: FP16 (mixed precision)
- Frameworks: PyTorch, Hugging Face Transformers, Datasets
- Hardware: NVIDIA L4 (24 GB VRAM)
🧪 Dataset
The model was trained on a self-curated, large-scale dataset compiled from multiple sources and internal filtering pipelines.
Dataset Split
| Split | Number of Images |
|---|
| Train | 96,085 |
| Validation | 19,283 |
| Test | 12,884 |
Dataset Characteristics
- Binary labels: safe / nsfw
- Content: Includes explicit, borderline, artistic, and real-world NSFW content.
- Variety: Large variety of safe images (people, objects, environments, graphics).
- Diversity: Diverse ethnicities, lighting conditions, poses, and content styles.
- Quality: Curated to reduce noise and label ambiguity.
✅ Intended Uses
- Content moderation for web and mobile applications.
- Upload filtering and pre-screening pipelines.
- Dataset sanitization for AI/ML workflows.
- ISP-level and enterprise safety enforcement.
- Parental control and compliance systems.
⚠️ Limitations
- Borderline cases (e.g., swimwear, medical imagery, artistic nudity) may be ambiguous.
- Cultural definitions of NSFW content may vary.
- Binary classification only (no fine-grained categories).
- Recommended to use threshold tuning and optional human review in sensitive applications.
🚀 How to Use
Using the Hugging Face Pipeline
1from transformers import pipeline
2from PIL import Image
3
4predict = pipeline(
5 "image-classification",
6 model="theusamaaslam/vit-Large-nsfw-detector",
7 device=0 # GPU
8)
9
10img = Image.open("<path_to_image_file>").convert("RGB")
11result = predict(img)
12print(result)
Usage for a distant image:
1
2from transformers import AutoImageProcessor, AutoModelForImageClassification
3from PIL import Image
4import requests
5import torch
6
7# URL of the image to test
8url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
9
10# Load image from URL
11image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
12
13# Load Processor and Model from your Hub repo
14repo_id = "theusamaaslam/vit-Large-nsfw-detector"
15processor = AutoImageProcessor.from_pretrained(repo_id)
16model = AutoModelForImageClassification.from_pretrained(repo_id)
17
18# Process image and inference
19inputs = processor(images=image, return_tensors="pt")
20
21with torch.no_grad():
22 outputs = model(**inputs)
23
24# Get probabilities and predicted label
25logits = outputs.logits
26probs = logits.softmax(dim=1)
27predicted_class_idx = logits.argmax(-1).item()
28predicted_label = model.config.id2label[predicted_class_idx]
29
30print(f"Predicted class: {predicted_label}")
31print(f"Probabilities: {probs}")
📬 Contact
Author:
@theusamaaslam
Email: [
usamaaslam081@gmail.com]
Organization: Nayatel (Pvt Ltd) AI & DevOps Team