Technical Overview
What is this model? vit-base-nsfw-detector is a fine‑tuned Vision Transformer (ViT) that classifies images into two mutually exclusive categories: SFW (Safe For Work) and NSFW (Not Safe For Work). It was built on top of the google/vit-base-patch16-384 checkpoint and subsequently trained on a curated set of roughly 25 000 real‑world images spanning photographs, drawings, and 3‑D renders.
Key features & capabilities
- Binary classification with a high accuracy of 96.54 % on the held‑out evaluation set.
- Supports both local and remote images via the
transformersPython pipeline andTransformers.jsfor browser‑side inference. - Exported in multiple formats –
safetensors,onnxand ready‑to‑use withtransformers.js– making deployment on edge devices or cloud services straightforward. - Optimized for a 384 × 384 input resolution, preserving fine‑grained skin‑tone and texture details crucial for NSFW detection.
Architecture highlights
- Base architecture: Vision Transformer‑Base (ViT‑B/16) with 12 transformer encoder layers, 12 attention heads, and a hidden size of 768.
- Patch embedding: 16 × 16 pixel patches, resulting in 384 × 384 → 24 × 24 token grid (plus CLS token).
- Classification head: A single linear layer mapping the CLS token to two logits (SFW / NSFW).
- Pre‑training lineage: ImageNet‑21k → ImageNet‑1k (384 × 384) → domain‑specific fine‑tuning on the 25 k custom dataset.
Intended use cases
- Content moderation pipelines for social media platforms, forums, and image‑hosting services.
- Pre‑filtering of user‑generated content before it reaches downstream generative models (e.g., Stable Diffusion).
- Integration into browser‑based moderation tools via Transformers.js, enabling client‑side privacy‑preserving checks.
- Batch processing of large image archives to flag or segregate NSFW material.
Benchmark Performance
The most relevant benchmarks for a binary image‑classification model are Accuracy, Area Under the ROC Curve (AUC), Loss (cross‑entropy), and, when dealing with imbalanced data, Precision/Recall/F1. The README provides the following results on the held‑out evaluation set:
- Accuracy: 0.9654 (96.54 %)
- AUC: 0.9948
- Loss: 0.0937
These numbers indicate that the model is both highly discriminative (AUC ≈ 0.995) and well‑calibrated (low loss). For generative images (e.g., AI‑generated art) the performance drops noticeably (Accuracy ≈ 86 %, AUC ≈ 0.938), which the author explicitly notes. This drop is expected because the fine‑tuning set contains no synthetic images, and the model’s decision boundary is tuned to natural‑photographic cues.
When compared to other open‑source NSFW detectors such as OpenAI CLIP‑ViT or ResNet‑50 based classifiers, the vit-base-nsfw-detector offers a competitive edge in AUC while maintaining a relatively small footprint (≈ 86 M parameters). Its 384 × 384 resolution also yields better sensitivity to subtle skin exposure than 224 × 224 models.
Hardware Requirements
VRAM for inference – The model’s checkpoint size (including the classification head) is roughly 340 MB in safetensors format. During inference a single 384 × 384 image occupies about 1 GB of GPU memory when using FP16 precision and up to 2 GB with FP32. A GPU with at least 4 GB VRAM is therefore sufficient for single‑image inference; for batch processing of 8‑16 images simultaneously, 8 GB VRAM is recommended.
Recommended GPU specifications
- Desktop: NVIDIA RTX 3060 (12 GB) or higher – provides ample headroom for FP16 and batch inference.
- Edge / Mobile: NVIDIA Jetson Orin or Apple M‑series (M1/M2) – both support FP16 and can run the model within 200‑300 ms per image.
- Cloud: AWS g4dn.xlarge (GPU 4 GB) or Azure NC6 (GPU 6 GB) – suitable for low‑latency API services.
CPU requirements – While CPU inference is possible, it is considerably slower (≈ 1–2 seconds per 384 × 384 image on a modern 8‑core CPU). For production services, a GPU is strongly advised. If only CPU is available, enable torch.compile or ONNX Runtime with use_gpu=False to improve throughput.
Storage needs – The model repository (including config, tokenizer, and safety files) occupies roughly 500 MB. Adding the ONNX export adds another ~200 MB. Ensure at least 1 GB of free disk space for the model and temporary inference buffers.
Performance characteristics – On an RTX 3060 (FP16), a single image is classified in ~45 ms (including preprocessing). Batch size 8 reduces per‑image latency to ~30 ms due to parallelism. The Transformers.js version runs in a browser at ~120 ms per image on a mid‑range laptop GPU.
Use Cases
Primary intended applications – The model is designed for content‑moderation pipelines that need a fast, binary decision on whether an image contains potentially adult or explicit material. It is especially useful for:
- Social‑media platforms that must filter user uploads before public posting.
- Image‑hosting services that provide “safe‑search” toggles for end‑users.
- Pre‑processing step for generative AI (e.g., Stable Diffusion) to prevent NSFW outputs from being generated.
Real‑world examples
- A forum that automatically tags or removes NSFW images during upload, reducing manual moderation workload by > 80 %.
- An e‑learning platform that scans instructor‑uploaded slide decks to ensure no accidental adult content appears.
- A mobile photo‑gallery app that offers a “hide NSFW” toggle powered by on‑device inference via Transformers.js.
Industries & domains – Media & entertainment, online marketplaces, cloud storage providers, education technology, and any SaaS that handles user‑generated visual content.
Integration possibilities – The model can be wrapped as a REST API using FastAPI or Flask, deployed on Azure Functions (as indicated by the deploy:azure tag), or embedded directly in a web page with Transformers.js. Its ONNX export enables low‑latency inference on edge devices such as Raspberry Pi 4 with a Coral USB accelerator.
Training Details
Training methodology – The author fine‑tuned the base ViT‑B/16 checkpoint on a custom dataset of ~25 000 images. The training pipeline follows the standard transformers Trainer workflow:
- Loss function: Cross‑entropy.
- Optimizer: AdamW with a learning rate of 2e‑5.
- Scheduler: Linear warm‑up for the first 10 % of steps, then cosine decay.
- Batch size: 32 images per GPU (FP16 mixed precision).
- Number of epochs: 5–7 (early stopping based on validation loss).
Datasets used – The fine‑tuning set consists of a balanced mix of:
- Real photographs (e.g., COCO, Flickr).
- Illustrations and 3‑D renders (to improve generalisation across styles).
- Manually curated NSFW examples focusing on cleavage, nudity, and explicit poses.
Training compute – Fine‑tuning a ViT‑B/16 model on 25 k images typically requires:
- One NVIDIA RTX 3090 (24 GB VRAM) for ~2 hours of wall‑clock time.
- Approximately 30 TFLOPs‑hours of compute (≈ 0.03 PFLOP‑days).
Fine‑tuning capabilities – Users can further adapt the model to specific domains (e.g., generative art) by:
- Loading the checkpoint with
AutoModelForImageClassification. - Replacing the classification head with a custom linear layer (e.g., 3‑class or multi‑label).
- Continuing training on a domain‑specific dataset (e.g., AI‑generated images).
Licensing Information
The model card lists the Apache‑2.0 license for the underlying ViT‑B/16 checkpoint, but the model‑specific license is marked as “unknown”. In practice, this means the author has not explicitly overridden the base license, so the Apache‑2.0 terms are assumed to apply to the fine‑tuned weights and code.
What Apache‑2.0 permits – The license is permissive:
- Free use for commercial and non‑commercial purposes.
- Modification, redistribution, and inclusion in proprietary products.
- Patent grant for any patents held by contributors that are necessary to use the software.
Potential restrictions – Because the license field is “unknown”, downstream users should:
- Check the repository’s files page for any additional
LICENSEdocuments. - Contact the author (AdamCodd) if you plan to embed the model in a commercial SaaS offering that requires explicit warranty or liability coverage.
Attribution requirements – Apache‑2.0 requires that you retain the original copyright notice and a copy of the license in any distribution. A typical attribution line would be:
Model “vit-base-nsfw-detector” by AdamCodd – based on Google’s ViT‑B/16 (Apache‑2.0).