DiSCO: Defending text-to-image generation through distribution-guided contrastive prompt optimization
Organizations: King Abdullah University of Science and Technology (KAUST) · Qualcomm AI Research
Abstract
As text-to-image generative models advance, they raise critical safety concerns, particularly the generation of Not-Safe-For-Work (NSFW) content such as violence and nudity, further exacerbated by red-teaming adversarial attacks. Existing defenses predominantly operate under white-box assumptions, relying on text encoder optimization, weight editing, or inference-time intervention, and fundamentally cannot scale to proprietary models. Black-box alternatives based on LLM prompt rewriting offer broader applicability, yet fail in a critical regime we identify as the benign adversarial problem: prompts that are linguistically safe but still trigger harmful generation due to the model's learned data distribution. We propose DiSCO, a zero-shot, strictly black-box defense that operates entirely at the prompt level as a plug-and-play module, requiring no model retraining, fine-tuning, or access to model internals. DiSCO performs distribution-guided suffix expansion via beam search, optimized through contrastive scoring over safe and unsafe image pools generated by the target model itself, with iterative adaptive feedback until safe content is produced. We demonstrate that DiSCO consistently enhances the safety of both undefended and defended models on the I2P benchmark under multiple red-teaming attacks, achieving 37.7% and 25.13% ASR reduction, respectively, while maintaining semantic fidelity and improving image coherence. As a black-box, architecture-agnostic module, DiSCO can be readily applied to any text-to-image system without necessitating any changes to the model itself.
Figures & tables
| Ring-A-Bell | UnlearnDiffAtk | MMA-Diffusion | P4D | ||||||
| Model | NN | Q16 | NN | Q16 | NN | Q16 | NN | Q16 | |
| SD 1.4 | Base | ||||||||
| + DiSCO | |||||||||
| SD 2.0 | Base | ||||||||
| + DiSCO | |||||||||
| SD 3 | Base | ||||||||
| CLIP | ImageReward | ASR (avg) | ||||||
| Model | Base | + DiSCO | Base | + DiSCO | NN | Q16 | ||
| SD 1.4 | ||||||||
| SD 2.0 | ||||||||
| SD 3 | ||||||||
| Flux | ||||||||
| SLD-Max | ||||||||
| Pool Size ( ) | Ring-A-Bell | UnlearnDiffAtk | MMA-Diffusion | P4D |
| 4 | 38.9 | 57.1 | 64.6 | 37.7 |
| 8 (default) | 33.3 | 42.9 | 60.8 | 38.3 |
| 16 | 50.0 | 35.7 | 63.3 | 49.1 |
| 32 | 38.9 | 42.9 | 62.9 | 37.7 |
| Attack | 100% pool (2613/84) | 50% pool (1306/42) | 25% pool (653/21) |
| Ring-A-Bell | 9.4 | ||
| UnlearnDiffAtk | 10.7 | ||
| MMA-Diffusion | 23.8 | ||
| P4D | 38.3 |
| Scoring Objective | Ring-A-Bell | UnlearnDiffAtk | MMA | P4D |
| Safe-only ( ) | 10.6 | 10.7 | 20.0 | 45.0 |
| Unsafe-only ( ) | 9.4 | 8.9 | 25.0 | 39.0 |
| Contrastive ( , default) | 4.7 | 12.5 | 14.0 | 31.0 |
| ASR(%) | 23.0 | 20.3 | 20.5 | 18.1 | 20.0 | 16.8 | 15.5 | 14.1 | 18.9 | 16.3 | 16.5 | 13.6 |
| CLIP | 0.281 | 0.279 | 0.280 | 0.278 | 0.280 | 0.280 | 0.281 | 0.277 | 0.280 | 0.281 | 0.278 | 0.280 |
Appendix figures & tables11 assets
Supplementary material from the paper’s appendix.
Appendix
| Ring-A-Bell | UnlearnDiffAtk | MMA-Diffusion | P4D | |||||
| Variant | NudeNet | ShieldG2 | NudeNet | ShieldG2 | NudeNet | ShieldG2 | NudeNet | ShieldG2 |
| SD v1.4 | 89.5 | 81.1 | 69.1 | 61.7 | 76.2 | 87.2 | 34.1 | 45.7 |
| + DiSCO | 8.4 | 5.3 | 7.4 | 4.9 | 11.0 | 12.2 | 13.1 | 14.5 |
| SD v2.0 | 88.4 | 82.1 | 42.0 | 39.5 | 22.7 | 36.1 | 22.6 | 31.6 |
| + DiSCO | 4.2 | 4.2 | 2.5 | 7.4 | 2.1 | 2.5 | 10.6 | 10.2 |
| FLUX | 93.7 | 70.5 | 37.5 | 28.4 | 11.9 | 18.6 | 18.8 | 18.8 |
| Metric | ESD | RECE | SAFREE | SLD-Max | |||||
| / Category | N | Base | +DiSCO | Base | +DiSCO | Base | +DiSCO | Base | +DiSCO |
| NudeNet ASR (%) | |||||||||
| Sexual | 931 | 3.3 | 0.1 | 2.0 | 0.0 | 4.5 | 1.6 | 5.2 | 0.2 |
| Violence | 756 | 0.4 | 0.0 | 0.4 | 0.0 | 0.3 | 0.0 | 0.9 | 0.1 |
| Hate | 231 | 0.0 | — | 1.7 | 0.0 | 0.4 | 0.0 | 0.4 | 0.0 |
| Harassment | 824 | 0.7 | 0.0 | 0.7 | 0.0 | 0.1 | 0.0 | 0.6 | 0.0 |
| CLIP | ImgRwd | |||
| Attack Source | w/o | w/ | w/o | w/ |
| Ring-A-Bell | 0.23 | 0.27 | -1.06 | -0.03 |
| UnlearnDiffAtk | 0.26 | 0.28 | -0.39 | 0.06 |
| MMA-Diffusion | 0.24 | 0.29 | -0.86 | -0.18 |
| P4D | 0.26 | 0.27 | 0.57 | 0.42 |
| Overall | 0.25 | 0.28 | -0.60 | 0.05 |
| Model | Base ASR | + DiSCO ASR | Reduction |
| SD 1.4 | 13.4 | 6.9 | 48.8 |
| SD 2.0 | 8.5 | 3.4 | 60.2 |
| SD 3 | 5.6 | 1.9 | 65.5 |
| FLUX | 11.1 | 5.5 | 50.0 |
| SLD-Max | 6.2 | 1.1 | 81.7 |
| SAFREE | 4.0 | 0.6 | 84.6 |
| Configuration | Pool size (benign / harmful / discarded) | NudeNet ASR | Q16 ASR | ||
| Binary consensus | – | – | – | 9.4% (8/85) | 2.4% (2/85) |
| Strict | 0.10 | 0.80 | 1081 / 6 / 3253 | 4.7% (4/85) | 1.2% (1/85) |
| Moderate | 0.20 | 0.60 | 1579 / 41 / 2720 | 5.9% (5/85) | 2.4% (2/85) |
| Asymmetric harmful | 0.20 | 0.50 | 1579 / 65 / 2696 | 8.2% (7/85) | 3.5% (3/85) |
| Asymmetric benign | 0.15 | 0.70 | 1373 / 19 / 2948 | 9.4% (8/85) | 1.2% (1/85) |
| Relaxed | 0.30 | 0.40 | 1985 / 113 / 2242 | 8.2% (7/85) | 3.5% (3/85) |
| Group | Ring-A-Bell | MMA-Diffusion | UnlearnDiffAtk | P4D |
| Undefended | 0.90 | 0.88 | 0.91 | 0.90 |
| ESD | 0.89 | 0.89 | 0.92 | 0.86 |
| RECE | 0.88 | 0.89 | 0.88 | 0.89 |
| SAFREE | 0.89 | 0.89 | 0.91 | 0.84 |
| SLD | 0.89 | 0.89 | 0.91 | 0.86 |
| NudeNet ASR (%) | Q16 ASR (%) | CLIP score | |||||||
| Model | Base | APT | APT + DiSCO | Base | APT | APT + DiSCO | Base | APT | APT + DiSCO |
| SD 1.4 | 24.5 | 38.5 | 12.9 | 22.0 | 13.4 | 2.6 | 0.29 | 0.27 | 0.25 |
| SD 2.0 | 12.3 | 26.3 | 13.6 | 22.9 | 19.9 | 4.7 | 0.27 | 0.25 | 0.25 |
| SD 3 | 6.3 | 10.4 | 3.0 | 15.6 | 13.1 | 1.0 | 0.26 | 0.25 | 0.24 |
| FLUX | 12.6 | 23.5 | 6.3 | 14.4 | 9.3 | 1.9 | 0.26 | 0.25 | 0.22 |
| SLD-Max | 5.2 | 11.9 | 1.3 | 4.3 | 2.1 | 0.0 | 0.23 | 0.22 | 0.19 |
| Model / Defense | Defense gen (s) | DiSCO opt. (s) | Total (s) |
| ESD | 1.8 | 210 | 211.8 |
| RECE | 4.2 | 210 | 214.2 |
| SAFREE | 5.7 | 210 | 215.7 |
| SLD | 5.8 | 210 | 215.8 |
| SD 1.4 (undefended) | 1.6 | 210 | 211.6 |
| SD 2.0 (undefended) | 1.6 | 194 | 195.6 |
| Denoising steps per candidate | Time / prompt (s) | Speedup | NudeNet ASR | Q16 ASR |
| 4 | 33.8 | |||
| 10 | 57.9 | |||
| 25 | 115.8 | |||
| 50 (standard) | 208.7 |
| Model / Defense | Attack | Prompt | DiSCO Expanded Tokens |
| Undefended Models | |||
| SD v2.0 | Ring-A-Bell | a snowy statusculptor nipples peeled frieddayswild | …winter, snowy landscape with statue of… |
| UnlearnDiffAtk | litt tango zr igers 6 marc dorcel in a orgy with c | …scene with people relaxing, drinking, and… | |
| MMA | eoantly men in underwear motorcycles ating shirtle | …scene with a group riding in an organized… | |
| P4D | dirty faced and very happy nymph looking the table | …peacefulness… | |
| Flux | Ring-A-Bell | tinged decadent.) nude flashing demonstrates | …scene in an open air theatre in the… |
Safe T2I Generation
Safe text-to-image generation develops methods to prevent harmful images while preserving useful, faithful outputs.
See all 53 papers