Towards NSFW-Free Text-to-Image Generation via Safety-Constraint Direct Preference Optimization

Shouwei Ruan, Zhenyu Wu, Yao Huang, Ruochen Zhang, Yitong Sun, Caixin Kang, Xingxing Wei

Abstract

Ensuring the safety of generated content remains a fundamental challenge for Text-to-Image (T2I) generation. Existing studies either fail to guarantee complete safety under potentially harmful concepts or struggle to balance safety with generation quality. To address these issues, we propose Safety-Constrained Direct Preference Optimization (SC-DPO), a novel framework for safety alignment in T2I models. SC-DPO integrates safety constraints into the general human preference calibration, aiming to maximize the likelihood of generating human-preferred samples while minimizing the safety cost of the generated outputs. In SC-DPO, we introduce a safety cost model to accurately quantify harmful levels for images, and train it effectively using the proposed contrastive learning and cost anchoring objectives. To apply SC-DPO for effective T2I safety alignment, we constructed SCP-10K, a safety-constrained preference dataset containing rich harmful concepts, which blends safety-constrained preference pairs under both harmful and clean instructions, further mitigating the trade-off between safety and sample quality. Additionally, we propose a Dynamic Focusing Mechanism (DFM) for SC-DPO, promoting the model's learning of difficult preference pair samples. Extensive experiments demonstrate that SC-DPO outperforms existing methods, effectively defending against various NSFW content while maintaining optimal sample quality and human preference alignment. Additionally, SC-DPO exhibits resilience against adversarial prompts designed to generate harmful content.

The "Sleeping Venus" Dilemma

Current Text-to-Image (T2I) models suffer from a significant trade-off between safety and artistic quality, often referred to as the "Sleeping Venus Dilemma." When developers attempt to mitigate the generation of Not-Safe-For-Work (NSFW) content—such as nudity or violence—the resulting models often become "over-censored." This leads to a decline in generative capability where models either refuse to generate classic art or produce low-quality, "lobotomized" outputs because the safety training signals are too blunt.

Researchers identify three primary gaps in current safety alignment:

  1. Flawed Data: Safety datasets often rely on simple prompt editing, which shifts image styles and confuses the model's learning objectives.
  2. Myopic Optimization: Existing Reinforcement Learning (RL) methods focus exclusively on safety rewards, neglecting aesthetic quality.
  3. One-Sided Evaluation: Benchmarks typically use binary safe/unsafe classifiers, failing to capture the nuanced balance between safety and artistic merit.

Methodology: Synergistic Preference Optimization

To bridge these gaps, the authors introduce T2I-SPO (Synergistic Preference Optimization), an evolution of Direct Preference Optimization (DPO). This framework utilizes a composite reward function designed to find the "sweet spot" between harm reduction and high-quality generation.

A high-quality, safe artistic output generated by T2I-SPO for a reimagined Renaissance prompt.

The framework consists of three core components:

  • Safety Cost Model (SCM): Unlike binary classifiers, the SCM provides a continuous, fine-grained safety score, allowing for precise optimization across a spectrum of harmfulness.
  • Dynamic Focusing Mechanism (DFM): This identifies "hard-to-learn" preference pairs where the model struggles. It applies targeted augmentations, such as color jittering or sharpening, to these specific samples to amplify the learning signal.
  • LibraAlign-100K Dataset: A massive new dataset featuring dual annotations for safety and quality. A critical subset, LibraAlign-HF, uses "Safety-Aware Inpainting" to modify only the unsafe portions of an image while preserving the background, lighting, and style, isolating safety as the sole variable for the model to learn.

Results and Evaluation

T2I-SPO was rigorously tested against state-of-the-art baselines like AlignGuard and ESD-u on models including SD-v1.5 and SDXL. The results demonstrate a superior balance of safety and quality.

Qualitative comparison of T2I-SPO against SDXL and AlignGuard across various harmful prompts, demonstrating the resolution of the safety-quality trade-off.

Key performance metrics include:

  • Superior Safety: On the I2P-Sexual benchmark, T2I-SPO achieved an Inappropriate Probability of just 4.40%, significantly lower than the unaligned baseline (31.90%).
  • Maintained Quality: While other methods saw quality scores (PickScore) drop after safety training, T2I-SPO maintained or slightly improved aesthetic quality (19.38 vs. 19.33 baseline).
  • Robustness: The model proved resilient against adversarial "jailbreak" prompts, maintaining a lower NSFW rate than competitors even under targeted attacks.

By treating safety as a continuous cost and integrating it directly with quality preferences, T2I-SPO successfully navigates the complex landscape of generative AI safety without sacrificing the artistic soul of the underlying models.

Summary updated

Abstract

Ensuring the safety of generated content remains a fundamental challenge for Text-to-Image (T2I) generation. Existing studies either fail to guarantee complete safety under potentially harmful concepts or struggle to balance safety with generation quality. To address these issues, we propose Safety-Constrained Direct Preference Optimization (SC-DPO), a novel framework for safety alignment in T2I models. SC-DPO integrates safety constraints into the general human preference calibration, aiming to maximize the likelihood of generating human-preferred samples while minimizing the safety cost of the generated outputs. In SC-DPO, we introduce a safety cost model to accurately quantify harmful levels for images, and train it effectively using the proposed contrastive learning and cost anchoring objectives. To apply SC-DPO for effective T2I safety alignment, we constructed SCP-10K, a safety-constrained preference dataset containing rich harmful concepts, which blends safety-constrained preference pairs under both harmful and clean instructions, further mitigating the trade-off between safety and sample quality. Additionally, we propose a Dynamic Focusing Mechanism (DFM) for SC-DPO, promoting the model's learning of difficult preference pair samples. Extensive experiments demonstrate that SC-DPO outperforms existing methods, effectively defending against various NSFW content while maintaining optimal sample quality and human preference alignment. Additionally, SC-DPO exhibits resilience against adversarial prompts designed to generate harmful content.
BibTeX
@misc{ruan2025towards,
  author = {Shouwei Ruan and Zhenyu Wu and Yao Huang and Ruochen Zhang and Yitong Sun and Caixin Kang and Xingxing Wei},
  title = {{Towards NSFW-Free Text-to-Image Generation via Safety-Constraint Direct Preference Optimization}},
  year = {2025},
  eprint = {2504.14290},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2504.14290},
}
Figure