This paper introduces Wukong, a novel framework for detecting Not Safe For Work (NSFW) content in Text-to-Image (T2I) systems. Named after the legendary Monkey King capable of seeing through demonic disguises, Wukong identifies unsafe content by "peeking" into the early stages of the image generation process.
TL;DR
Wukong is a transformer-based NSFW detector that identifies unsafe content during the early denoising steps of a diffusion model, rather than waiting for the full image to generate. By leveraging intermediate U-Net representations and cross-attention maps, it achieves the accuracy of post-generation image filters with a 5x speedup and remains highly robust against adversarial "jailbreak" prompts that bypass traditional text filters.
What Problem Does This Paper Address?
Ensuring T2I models (like Stable Diffusion) don't generate harmful content is a major challenge. Current "external" safeguards generally fall into two camps, both with significant flaws:
- Text Filters: These scan user prompts for "blacklisted" words. They are fast but easily bypassed by adversarial attacks (e.g., using "red liquid" instead of "blood") and fail to account for the fact that the same prompt can produce safe or unsafe images depending on the random seed.
- Image Filters: These analyze the final generated image. While accurate, they introduce massive latency because the system must perform the entire computationally expensive diffusion process (often 50+ steps) before the filter can even run.
The authors identify a "middle ground": can we detect NSFW content while the image is being born, but before it's finished?
Methods
Wukong operates on two key observations: (1) the semantic layout of an image is established in the very first few denoising steps, and (2) cross-attention layers in the U-Net are responsible for mapping textual concepts to specific image regions.
The framework consists of:
- U-Net-based Encoder: Wukong reuses the pre-trained U-Net of the T2I model itself. It runs the first $T_c$ denoising steps (typically just 10 steps).
- Transformer-based Decoder: This is the core innovation. It reuses the cross-attention parameters ($W_Q$ and $W_K$) from the U-Net.
- NSFW Query Generation: The authors use a CLIP text encoder to generate query vectors for seven NSFW categories (Illegal, Hate, Violence, Sexual, Self-harm, Harassment, Shocking).
- Early Exit Strategy: At a designated early step (e.g., step 10), Wukong extracts features from the latent representation. If the classifier's probability exceeds a threshold, the system halts generation immediately, saving the cost of the remaining 40+ steps.
Datasets
The authors argue that existing datasets are flawed because they label prompts rather than the actual images generated (which vary by seed). They introduce:
- Wukong-Demons: A new large-scale benchmark containing 7,640 prompts and 76,400 images. Crucially, labels are grounded in the generated images themselves using a Vision-Language Model (GPT-4o-mini) and OpenAI’s moderation API for high-quality, category-specific annotations.
- Public Benchmarks: They also evaluate on I2P (real-world NSFW prompts) and CoPro (LLM-generated unsafe prompts) to demonstrate generalizability.
Key Insights and Ideas
- The "Ghost" in the Noise: Even when an image looks like pure white noise to a human (at step 1 or 5), the underlying mathematical representations already contain the "blueprint" of NSFW content.
- Parameter Reuse: By freezing and reusing the U-Net’s own cross-attention weights, Wukong requires very little additional training and "understands" how that specific model interprets textual concepts.
- Seed Sensitivity: Wukong is one of the first frameworks to explicitly handle the "same prompt, different seed" problem, recognizing that a prompt might be safe 90% of the time but generate something unsafe with a specific random initialization.
Key Results
- Superior Accuracy: On the Wukong-Demons dataset, Wukong achieved an ROC AUC of 0.9547, significantly outperforming the best text-based baseline (OpenAI Moderation at 0.7829).
- Efficiency Gains: By halting at step 10, Wukong provides a 5x speedup compared to traditional post-hoc image filters while maintaining comparable detection accuracy.
- Adversarial Robustness: In tests against "jailbreak" methods like SneakyPrompt and Ring-A-Bell, traditional blacklists saw their performance crater (ROC AUC near 0.2), while Wukong remained robust with an ROC AUC above 0.87.
Limitations and Future Work
- Architecture Specificity: The current implementation is tailored for U-Net-based diffusion models (like Stable Diffusion v1.5/v2.1/XL).
- Future Backbones: The authors note that newer models are moving toward Diffusion Transformers (DiT). While the logic of early semantic layout likely holds, Wukong would need to be adapted to these non-U-Net architectures.
- Category Scope: The model is trained on seven specific categories; expanding this to more nuanced or evolving community guidelines would require further data collection.
Summary updated