Mitigating Sexual Content Generation via Embedding Distortion in Text-conditioned Diffusion Models

Jaesin Ahn, Heechul Jung

This paper introduces Distorting Embedding Space (DES), a novel defense mechanism for text-to-image diffusion models. By strategically warping the text encoder's embedding space, DES prevents the generation of sexual content—even when triggered by sophisticated adversarial attacks—while maintaining the high visual quality of benign images.

TL;DR

DES is a text-encoder-based defense that redirects "unsafe" prompt embeddings into safe regions and neutralizes the "nudity" concept vector. It achieves state-of-the-art protection, reducing attack success rates by over 60-70% compared to previous methods on models like Stable Diffusion v1.5 and FLUX.1, all with zero inference overhead and just 90 seconds of training.

What Problem Does This Paper Address?

While diffusion models like Stable Diffusion and FLUX.1 are incredibly powerful, they are prone to misuse for generating NSFW content. Existing defenses generally fall into three categories: simple keyword filtering, U-Net-based concept removal, and sexual content mitigation.

However, these approaches have significant gaps:

  1. Fragility: Adversarial attacks (like "SneakyPrompt") can bypass keyword filters by using seemingly benign words that map to unsafe regions in the embedding space.
  2. Quality Degradation: Modifying the U-Net (the "image renderer") often leads to "catastrophic forgetting," where the model loses its ability to generate high-quality benign images or introduces visible artifacts like blurring.
  3. Complexity: Many methods require extensive retraining or introduce significant computational overhead during image generation.

DES aims to be a "plug-and-play" defense that is robust against adversarial prompts without sacrificing the model's creative capabilities.

Methods

The core philosophy of DES is to fix the problem at the source: the text encoder. Instead of trying to teach the U-Net not to draw certain things, DES teaches the text encoder to interpret unsafe prompts as safe ones.

The framework operates in two phases:

  1. Target Vector Generation: For a set of unsafe prompts, the system identifies "safe" target embeddings. It does this by finding safe vectors that are mathematically most dissimilar to the unsafe ones and then further pushing them away from the "nudity" concept direction.
  2. Training Phase: The text encoder is fine-tuned using three synergistic loss functions:
  3. Unsafe Loss ($L_u$): Forces unsafe prompt embeddings to align with the pre-calculated safe target vectors.
  4. Safe Loss ($L_s$): Ensures that safe prompts retain their original embeddings to preserve image quality. Crucially, it uses an adaptive loss adjustment (see Figure 4 below) that relaxes constraints on prompts that are semantically close to unsafe concepts, allowing the space to distort effectively without breaking the model.
  5. Nudity Neutralization Loss ($L_n$): Specifically targets the "nudity" concept vector itself, aligning it with a neutral vector (like an empty string), effectively making the concept semantically "invisible" to the model.
Figure 2: Overview of DES framework. During target vector generation phase, DES searches safe-unsafe vector pairs and creates target vectors by subtracting “nudity” direction from minimum similarity safe vectors. In training phase, DES aligns unsafe vectors with target vectors and maintains safe vectors by aligning both their current and nudity-integrated states with the originals. It also aligns the “nudity” vector with a neutral vector, removing its semantics. Here, v and ˜v denote vectors from the original and training text encoders, respectively.

Datasets

  • Training: The authors used 6,911 safe-unsafe prompt pairs from the sexual category of the CoPro dataset. For generalization tests, they also utilized prompts from violence and illegal categories.
  • Evaluation:
    • I2P Dataset: Used to test against explicit, "real-world" unsafe prompts.
  • Adversarial Benchmarks: Evaluated against SneakyPrompt, MMA, Ring-A-Bell, and P4D to test robustness against "jailbreaking."
    • COCO 30k: Used to evaluate benign image quality via FID and CLIP scores.

Key Insights and Ideas

  • The Power of the Encoder: The authors argue that because concepts are spread across U-Net layers but localized in the text encoder, modifying the encoder is a more surgical and effective way to remove concepts.
  • Flexible Distortion: A major "aha" moment is the loss adjustment mechanism. By adaptively scaling the loss based on how much a safe prompt correlates with the "nudity" vector, DES avoids the rigid constraints that typically cause image quality to tank in other defense methods.
Figure 4: Mechanism of loss adjustment. Vi- sualization of how the loss is adaptively scaled based on the correlation between si and n. It assigns a larger loss to vectors dissimilar to n and a smaller loss to those similar to n.
  • Neutralization vs. Removal: Instead of just trying to "delete" a concept, DES neutralizes it by mapping it to a "null" state. This makes it much harder for adversarial optimization attacks to "find" the concept again.

Key Results

DES demonstrates significant improvements over previous state-of-the-art (SOTA) methods like AdvUnlearn and EraseAnything:

  • Superior Defense: On Stable Diffusion v1.5, DES achieved an Attack Success Rate (ASR) of just 0.52%, a 63.9% reduction compared to AdvUnlearn. On the newer FLUX.1 model, it achieved an ASR of 9.47%, a 76.5% reduction over EraseAnything.
  • Quality Preservation: Unlike many competitors that see FID scores (lower is better) jump to 19 or 21, DES maintains an FID of 15.44 on SDv1.5, nearly identical to the original model's performance.
  • Efficiency: The training process is remarkably fast, requiring only 90 seconds on a standard GPU setup. Because it only modifies the text encoder, there is zero inference overhead—the model generates images just as fast as the undefended version.

Limitations and Future Work

  • Text-Centricity: DES primarily addresses text-based attacks. The authors acknowledge that image-based attacks (like those in image-to-image tasks) would require complementary defense methods focused on image components.
  • Closed-Source Models: While effective for open-source models where the text encoder can be swapped, the insights might not directly apply to closed-source APIs (like DALL-E 3) unless implemented by the provider.
  • Semantic Distortion: While benign quality is high, there is a slight "semantic distortion" for prompts very close to the nudity concept (e.g., "a shirtless man"). Future work could involve finer-grained control to allow mild content while blocking explicit content.

Summary updated

BibTeX
@inproceedings{ahn2025mitigating,
  author = {Jaesin Ahn and Heechul Jung},
  title = {{Mitigating Sexual Content Generation via Embedding Distortion in Text-conditioned Diffusion Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=0LUu5rd0GQ},
}
Figure