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

Jaesin Ahn, Heechul Jung · DOI

Abstract

Diffusion models show remarkable image generation performance following text prompts, but risk generating sexual contents. Existing approaches, such as prompt filtering, concept removal, and even sexual contents mitigation methods, struggle to defend against adversarial attacks while maintaining benign image quality. In this paper, we propose a novel approach called Distorting Embedding Space (DES), a text encoder-based defense mechanism that effectively tackles these issues through innovative embedding space control. DES transforms unsafe embeddings, extracted from a text encoder using unsafe prompts, toward carefully calculated safe embedding regions to prevent unsafe contents generation, while reproducing the original safe embeddings. DES also neutralizes the "nudity" embedding, by aligning it with neutral embedding to enhance robustness against adversarial attacks. As a result, extensive experiments on explicit content mitigation and adaptive attack defense show that DES achieves state-of-the-art (SOTA) defense, with attack success rate (ASR) of 9.47% on FLUX.1, a recent popular model, and 0.52% on the widely adopted Stable Diffusion v1.5. These correspond to ASR reductions of 76.5% and 63.9% compared to previous SOTA methods, EraseAnything and AdvUnlearn, respectively. Furthermore, DES maintains benign image quality, achieving Frechet Inception Distance and CLIP score comparable to those of the original FLUX.1 and Stable Diffusion v1.5.

Introduction

Modern text-to-image (T2I) diffusion models like Stable Diffusion and FLUX.1 possess remarkable generative capabilities but are susceptible to producing Not-Safe-For-Work (NSFW) or sexually explicit content. Standard defenses like prompt filtering (blacklists) are easily bypassed by adversarial attacks—gibberish prompts optimized to trigger NSFW outputs. Alternatively, concept removal via U-Net unlearning often degrades benign image quality or remains vulnerable to adaptive white-box attacks.

This paper introduces Distorting Embedding Space (DES), a robust defense mechanism that operates on the text encoder rather than the U-Net. DES effectively blocks both explicit and adversarial NSFW prompts without compromising baseline image quality or adding inference overhead.

Methodology: Distorting Embedding Space

DES fine-tunes the text encoder (e.g., CLIP or T5) to map unsafe prompts into safe embedding regions while neutralizing the "nudity" concept.

Conceptual overview of Distorting Embedding Space (DES) mapping unsafe prompts to safe regions.

The approach utilizes three primary loss functions:

  1. Unsafe Loss: Forces embeddings of unsafe prompts toward "target safe vectors." These targets are generated by identifying a safe prompt dissimilar to the unsafe one and mathematically subtracting the "nudity" direction, ensuring the prompt is pushed away from NSFW semantics.
  2. Safe Loss: Anchors safe embeddings to their original positions. A "loss adjustment" technique preserves prompts with zero correlation to nudity while allowing minor shifts for ambiguous prompts.
  3. Nudity Neutralization Loss: Aligns the "nudity" vector with a neutral vector (empty string ""), rendering the concept semantically meaningless to prevent attackers from extracting and using the vector to bypass filters.

A key insight is that adversarial prompts share the same embedding space as natural language unsafe prompts. By distorting this shared "unsafe" region, DES simultaneously neutralizes both readable and gibberish attacks.

Experimental Evaluation

The authors trained DES on 6,911 safe-unsafe prompt pairs from the CoPro dataset and evaluated it against a suite of attacks including I2P, SneakyPrompt, MMA, and adaptive white-box attacks like UDA. Benign image quality was measured using COCO 30k to calculate Fréchet Inception Distance (FID) and CLIP scores.

Key Results

DES achieves state-of-the-art (SOTA) performance across multiple models:

  • Stable Diffusion v1.5: Reduced Attack Success Rate (ASR) to 0.52% (a 63.9% improvement over AdvUnlearn).
  • FLUX.1: Achieved a 9.47% ASR (a 76.5% reduction compared to EraseAnything).
  • Efficiency: Training takes only 90 seconds on a single GPU with zero additional inference latency.
ASR vs FID comparison across various defense methods.

As shown in the figure above, DES maintains an optimal balance, achieving low ASR while keeping FID scores comparable to the original, unprotected models (e.g., FID of 15.44 on SDv1.5).

Conclusion and Future Work

DES demonstrates that text encoders are highly effective targets for concept unlearning. By manipulating the cleaner embedding space, DES avoids the collateral damage to benign generation typical of U-Net modifications. While highly effective against text-based attacks, future work may address image-based adversarial noise in image-to-image pipelines and explore cultural nuances in defining sexual content through tunable hyperparameters.

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