Everything K-culture — comebacks to K-beauty, straight to your inboxGet it in your inbox

METAL MEDIA

DiSCO: Defending text-to-image generation through distribution-guided contrastive prompt optimization

arXiv:2608.170672026-08-16

A text-to-image prompt can look perfectly safe and still generate unsafe images, so this method rewrites the prompt itself, without touching the model, to steer it away

Text-to-image models can produce unsafe images even from prompts that pass language-level safety checks, because the model's own learned visual distribution still leans toward harmful content. DiSCO fixes this by building safe and unsafe image pools from the target model's own outputs and using beam search to append suffix tokens that push generation, in CLIP embedding space, toward the safe pool and away from the unsafe one, all without accessing model weights. Across 32 system-attack settings and five seeds, it cut average attack success rate from 23.6% to 2.4% under NudeNet and from 8.3% to 1.7% under Q16.

METAL MEDIA explanatory visual

A text-to-image prompt can look perfectly safe and still generate unsafe images, so this method rewrites the prompt itself, without touching the model, to steer it away

  1. 01Defines the 'benign adversarial' problem: prompts that pass text-level safety checks but still trigger unsafe image generation due to the model's learned data distribution
  2. 02Builds model-specific safe/unsafe reference image pools by generating images from I2P dataset prompts and keeping only cases where both NudeNet and Q16 classifiers agree, then uses a beam search with a language model (LLaMA-3-8B) to expand the prompt with suffix tokens that maximize similarity to the safe pool and minimize similarity to the unsafe pool in CLIP embedding space
  3. 03Requires no model retraining, fine-tuning, or access to internal weights or gradients, working purely by querying the model's outputs
  4. 04Applied on top of undefended models (SD v1.4, SD v2.0, Flux, SD3) and existing defenses (SLD-Max, SAFREE, RECE, ESD), it reduced attack success rate in all 32 tested combinations while also improving CLIP alignment and ImageReward quality scores
  5. 05Ablations show performance stays stable even when the reference pool is shrunk to 25% of its original size, and the moderate default beam search settings (K=4, T=16) balance safety and semantic fidelity
An explanatory diagram made by METAL MEDIA, not a figure supplied by the paper's authors.

What they did

  1. Defines the 'benign adversarial' problem: prompts that pass text-level safety checks but still trigger unsafe image generation due to the model's learned data distribution
  2. Builds model-specific safe/unsafe reference image pools by generating images from I2P dataset prompts and keeping only cases where both NudeNet and Q16 classifiers agree, then uses a beam search with a language model (LLaMA-3-8B) to expand the prompt with suffix tokens that maximize similarity to the safe pool and minimize similarity to the unsafe pool in CLIP embedding space
  3. Requires no model retraining, fine-tuning, or access to internal weights or gradients, working purely by querying the model's outputs
  4. Applied on top of undefended models (SD v1.4, SD v2.0, Flux, SD3) and existing defenses (SLD-Max, SAFREE, RECE, ESD), it reduced attack success rate in all 32 tested combinations while also improving CLIP alignment and ImageReward quality scores
  5. Ablations show performance stays stable even when the reference pool is shrunk to 25% of its original size, and the moderate default beam search settings (K=4, T=16) balance safety and semantic fidelity
Figure 2: Overview of DiSCO. Left: Model-specific safe and unsafe reference pools are constructed by generating images from the target model using the I2P dataset and filtering through classifier consensus (NudeNet and Q16). Right: DiSCO takes a benign-adversarial prompt, expands it with optimized suffix tokens via contrastive beam search in CLIP embedding space, and steers the target model from unsafe to safe generation without modifying the model itself.
Figure 2: Overview of DiSCO. Left: Model-specific safe and unsafe reference pools are constructed by generating images from the target model using the I2P dataset and filtering through classifier consensus (NudeNet and Q16). Right: DiSCO takes a benign-adversarial prompt, expands it with optimized suffix tokens via contrastive beam search in CLIP embedding space, and steers the target model from unsafe to safe generation without modifying the model itself.
Table 1: ASR (%, mean±std over 5 seeds) under four adversarial attacks, measured by NudeNet (NN) and Q16. Lower is better.
Ring-A-BellUnlearnDiffAtkMMA-DiffusionP4D
ModelNN↓Q16↓NN↓Q16↓NN↓Q16↓NN↓Q16↓
SD 1.4Base84.2±3.816.0±9.553.1±10.913.1±3.343.0±16.911.8±4.717.5±8.612.9±6.6
+ DiSCO7.8±3.04.0±2.96.2±2.53.4±2.58.0±1.73.2±1.15.5±1.45.3±1.9
SD 2.0Base75.4±7.026.5±4.724.2±9.27.4±1.79.6±6.69.5±6.210.1±6.310.4±7.4
+ DiSCO3.9±0.82.1±0.93.2±1.32.5±1.81.0±0.31.3±0.45.1±2.03.6±1.4
SD 3Base34.5±5.83.8±1.79.4±3.42.5±2.82.5±1.53.0±4.45.0±2.15.5±8.7
+ DiSCO2.1±1.60.4±0.50.2±0.50.2±0.50.1±0.10.0±0.12.7±1.20.9±0.5
FluxBase89.7±3.110.1±3.432.4±2.84.7±1.77.7±2.30.8±1.113.7±2.78.2±6.0
+ DiSCO5.0±1.00.0±0.00.0±0.00.3±0.60.6±0.30.1±0.16.0±2.33.3±1.4
SLD-MaxBase44.4±18.41.7±1.713.8±10.02.5±0.831.4±12.01.9±1.41.9±2.10.9±1.5
+ DiSCO0.3±0.70.0±0.01.2±0.81.2±0.15.5±1.80.1±0.10.3±0.20.0±0.1
SAFREEBase54.1±5.616.6±5.512.6±4.36.9±1.520.5±7.014.4±7.71.6±1.64.7±7.4
+ DiSCO0.4±0.51.2±0.42.2±0.60.8±0.92.5±1.14.2±1.20.5±0.40.6±0.4
RECEBase2.1±0.98.2±3.45.4±3.77.6±2.617.7±7.716.2±8.71.4±1.65.7±8.6
+ DiSCO0.0±0.00.7±0.40.2±0.52.3±1.32.6±0.95.6±1.20.4±0.31.0±0.5
ESDBase22.3±7.212.8±4.75.9±3.94.9±3.66.5±3.611.2±3.90.7±1.14.5±7.2
+ DiSCO0.2±0.41.5±1.32.2±1.01.5±1.41.0±0.82.7±0.80.1±0.10.4±0.3
Figure 3: Qualitative Results. Each row corresponds to an attack method. Left of the dashed line: undefended models (SD v2.0, Flux, SD3); right of the dashed line: defended models on SD v1.4 (SLD-Max, SAFREE, RECE, ESD). For each pair, the left image is generated without DiSCO and the right image is generated with DiSCO prepended. SD v1.4 results are shown alongside the defended models to avoid repetition. Unsafe regions are masked with black boxes. DiSCO consistently steers generation toward safe content while preserving semantic coherence across all models and defenses. Corresponding prompts can be found in Table 16.
Figure 3: Qualitative Results. Each row corresponds to an attack method. Left of the dashed line: undefended models (SD v2.0, Flux, SD3); right of the dashed line: defended models on SD v1.4 (SLD-Max, SAFREE, RECE, ESD). For each pair, the left image is generated without DiSCO and the right image is generated with DiSCO prepended. SD v1.4 results are shown alongside the defended models to avoid repetition. Unsafe regions are masked with black boxes. DiSCO consistently steers generation toward safe content while preserving semantic coherence across all models and defenses. Corresponding prompts can be found in Table 16.
Table 2: Generation quality and average ASR reduction. CLIP and ImageReward (IR) are measured against the original prompt over 5 seeds (higher is better); CLIP standard deviations are below 0.013 throughout and are omitted. ΔASR is the mean change across all four attacks.
CLIP ↑ImageReward ↑ΔASR (avg)
ModelBase+ DiSCOΔBase+ DiSCOΔΔNNΔQ16
SD 1.40.1840.270+0.086−2.01±0.07−0.34±0.12+1.67−42.6−9.5
SD 2.00.1810.263+0.082−1.88±0.10−0.14±0.14+1.74−26.5−11.1
SD 30.1900.260+0.070−1.42±0.120.45±0.07+1.87−11.6−3.3
Flux0.1990.264+0.065−1.54±0.080.68±0.07+2.22−33.0−5.0
SLD-Max0.1870.223+0.036−1.45±0.10−0.60±0.18+0.85−21.1−1.4
SAFREE0.2120.260+0.048−1.32±0.08−0.19±0.11+1.13−20.8−9.0
RECE0.1980.263+0.065−1.27±0.15−0.33±0.13+0.93−5.8−7.0
ESD0.2100.265+0.055−1.49±0.10−0.49±0.14+1.00−8.0−6.8
Figure 4: Attack Success Rate (ASR, %; lower is better) before (Base) and after applying DiSCO (+ DiSCO), across four attacks (Ring-A-Bell, UnlearnDiffAtk, MMA-Diffusion, P4D). Within each panel, the right group reports defended models (SLD-Max, SAFREE, RECE, ESD) and the left group undefended backbones (SD 1.4, SD 2.0, SD 3, FLUX). DiSCO substantially reduces ASR across every evaluated attack, defense, and backbone.
Figure 4: Attack Success Rate (ASR, %; lower is better) before (Base) and after applying DiSCO (+ DiSCO), across four attacks (Ring-A-Bell, UnlearnDiffAtk, MMA-Diffusion, P4D). Within each panel, the right group reports defended models (SLD-Max, SAFREE, RECE, ESD) and the left group undefended backbones (SD 1.4, SD 2.0, SD 3, FLUX). DiSCO substantially reduces ASR across every evaluated attack, defense, and backbone.
Table 3: Ablation on reference pool sampling size (R) on SD v1.4. We report ASR in %(↓) computed exclusively on prompts that still generate harmful content after the baseline defense, isolating the effect of DiSCO on the remaining challenging cases. R=8 is the default setting.
Pool Size (R)Ring-A-BellUnlearnDiffAtkMMA-DiffusionP4D
438.957.164.637.7
8 (default)33.342.960.838.3
1650.035.763.349.1
3238.942.962.937.7
Figure 5: Semantic drift from dominant concept injection. In rare cases, DiSCO’s suffix expansion introduces tokens corresponding to visually dominant concepts in the model’s training distribution, overriding the original prompt semantics or styles
Figure 5: Semantic drift from dominant concept injection. In rare cases, DiSCO’s suffix expansion introduces tokens corresponding to visually dominant concepts in the model’s training distribution, overriding the original prompt semantics or styles
Table 4: NudeNet ASR (%) as a function of reference pool size, on SD v1.4. Pool sizes are listed as (benign / harmful) image counts. The 100% counts correspond to the pool remaining after filtering the SD v1.4 outputs with the Q16 and NudeNet classifiers.
Attack100% pool (2613/84)50% pool (1306/42)25% pool (653/21)
Ring-A-Bell9.47.5±2.29.4±1.0
UnlearnDiffAtk10.710.1±2.28.9±1.5
MMA-Diffusion23.822.0±0.122.7±0.8
P4D38.337.7±1.441.4±4.4
Table 5: Ablation on scoring objective on SD v1.4. We compare the full contrastive objective against safe-only and unsafe-only single-pool variants. We report average ASR in %(↓) by NudeNet across attacks. ASR is computed on prompts that still generate harmful content from the undefended model.
Scoring ObjectiveRing-A-BellUnlearnDiffAtkMMAP4D
Safe-only (𝒥safe)10.610.720.045.0
Unsafe-only (𝒥unsafe)9.48.925.039.0
Contrastive (𝒥, default)4.712.514.031.0
Table 6: Ablation on beam search parameters on SD v1.4. We report average ASR (↓) by NudeNet across all attacks and CLIP (↑) for semantic alignment. ASR is computed on prompts that still generate harmful content from the SD v1.4. The default setting (K=4, T=16) is underlined.
K=2K=4K=8
T=8T=12T=16T=32T=8T=12T=16T=32T=8T=12T=16T=32
ASR(%)↓23.020.320.518.120.016.815.514.118.916.316.513.6
CLIP↑0.2810.2790.2800.2780.2800.2800.2810.2770.2800.2810.2780.280
Table 7: Attack success rate (%) under four nudity attacks, measured by NudeNet (pixel-level body-part detection) and ShieldGemma2-4B (semantic safety classification). ASR is computed over all attack prompts, using the same denominator for baseline and DiSCO. Lower is better.
Ring-A-BellUnlearnDiffAtkMMA-DiffusionP4D
VariantNudeNetShieldG2NudeNetShieldG2NudeNetShieldG2NudeNetShieldG2
SD v1.489.581.169.161.776.287.234.145.7
+ DiSCO8.45.37.44.911.012.213.114.5
SD v2.088.482.142.039.522.736.122.631.6
+ DiSCO4.24.22.57.42.12.510.610.2
FLUX93.770.537.528.411.918.618.818.8
+ DiSCO5.31.10.01.20.70.98.25.1
SD3-medium45.341.116.123.55.420.98.921.5
+ DiSCO2.10.00.01.20.10.43.32.0
SLD-Max72.651.633.322.253.161.66.27.0
+ DiSCO2.10.02.52.57.69.22.20.0
SAFREE52.652.621.013.633.550.24.44.3
+ DiSCO0.00.02.50.04.76.71.10.4
RECE3.26.39.912.332.351.44.47.4
+ DiSCO0.00.00.00.04.26.71.80.0
ESD23.226.313.613.66.038.11.85.9
+ DiSCO0.00.03.72.51.25.50.70.0
Table 8: Per-category ASR (%) under NudeNet and Q16: defense baselines vs. DiSCO-enhanced, on SD v1.4. Prompts are taken directly from I2P without augmentation by any attack tool, isolating how DiSCO performs across categories without introducing implementation bias from a specific attack method. “—” denotes no samples flagged at baseline.
MetricESDRECESAFREESLD-Max
/ CategoryNBase+DiSCOBase+DiSCOBase+DiSCOBase+DiSCO
NudeNet ASR (%)
Sexual9313.30.12.00.04.51.65.20.2
Violence7560.40.00.40.00.30.00.90.1
Hate2310.01.70.00.40.00.40.0
Harassment8240.70.00.70.00.10.00.60.0
Self-harm8011.80.00.40.00.90.00.90.0
Shocking8561.90.01.10.00.90.11.30.0
Illegal activity7270.40.00.80.00.10.00.70.0
Average1.20.01.00.01.00.21.40.0
Q16 ASR (%)
Sexual93115.20.118.90.114.00.24.30.0
Violence75639.00.040.10.034.90.014.50.0
Hate23131.235.90.423.80.010.40.0
Harassment82429.20.031.20.023.20.010.10.0
Self-harm80132.10.137.50.031.10.08.00.0
Shocking85644.20.150.60.043.00.015.50.1
Illegal activity72731.20.034.80.024.60.05.90.1
Average31.70.135.60.127.80.09.80.0
Table 9: Effect of DiSCO on benign generation on SD v1.4. We randomly sample 100 benign prompts from each attack that already produce safe images, and report CLIP (↑) for semantic fidelity and ImageReward (↑) for perceptual quality.
CLIP↑ImgRwd↑
Attack Sourcew/ow/w/ow/
Ring-A-Bell0.230.27-1.06-0.03
UnlearnDiffAtk0.260.28-0.390.06
MMA-Diffusion0.240.29-0.86-0.18
P4D0.260.270.570.42
Overall0.250.28-0.600.05
Table 10: Out-of-distribution generalization on 1,000 T2I-RiskyPrompt prompts. ASR (%) is reported with and without DiSCO, and reduction is relative. ASR denotes the percentage of naturally occurring risky prompts that generate unsafe images. No additional attack is applied.
ModelBase ASR+ DiSCO ASRReduction
SD 1.413.46.948.8
SD 2.08.53.460.2
SD 35.61.965.5
FLUX11.15.550.0
SLD-Max6.21.181.7
SAFREE4.00.684.6
ESD2.70.292.3
RECE1.60.193.8
Table 11: Sensitivity to confidence thresholds τlow and τhigh. Pool size reports the number of benign, harmful, and discarded candidates. Best results are shown in bold.
ConfigurationτlowτhighPool size (benign / harmful / discarded)NudeNet ASRQ16 ASR
Binary consensus9.4% (8/85)2.4% (2/85)
Strict0.100.801081 / 6 / 32534.7% (4/85)1.2% (1/85)
Moderate0.200.601579 / 41 / 27205.9% (5/85)2.4% (2/85)
Asymmetric harmful0.200.501579 / 65 / 26968.2% (7/85)3.5% (3/85)
Asymmetric benign0.150.701373 / 19 / 29489.4% (8/85)1.2% (1/85)
Relaxed0.300.401985 / 113 / 22428.2% (7/85)3.5% (3/85)
Table 12: Mean cosine similarity between original and DiSCO-suffixed prompt embeddings across attack settings and defenses.
GroupRing-A-BellMMA-DiffusionUnlearnDiffAtkP4D
Undefended0.900.880.910.90
ESD0.890.890.920.86
RECE0.880.890.880.89
SAFREE0.890.890.910.84
SLD0.890.890.910.86
Table 13: APT suffix-attack stress test across four undefended backbones and four defended systems. We report NudeNet ASR, Q16 ASR, and CLIP score for the unattacked baseline, APT-attacked prompts, and APT-attacked prompts followed by DiSCO.
NudeNet ASR (%)Q16 ASR (%)CLIP score
ModelBaseAPTAPT + DiSCOBaseAPTAPT + DiSCOBaseAPTAPT + DiSCO
SD 1.424.538.512.922.013.42.60.290.270.25
SD 2.012.326.313.622.919.94.70.270.250.25
SD 36.310.43.015.613.11.00.260.250.24
FLUX12.623.56.314.49.31.90.260.250.22
SLD-Max5.211.91.34.32.10.00.230.220.19
ESD3.35.21.015.211.80.10.260.250.23
RECE2.02.70.418.915.60.20.260.250.21
SAFREE4.57.71.214.09.20.10.280.260.24
Table 14: Per-prompt wall-clock time (seconds) on a single NVIDIA A100 GPU, decomposed into defense-pipeline generation and DiSCO suffix optimization.
Model / DefenseDefense gen (s)DiSCO opt. (s)Total (s)
ESD1.8210211.8
RECE4.2210214.2
SAFREE5.7210215.7
SLD5.8210215.8
SD 1.4 (undefended)1.6210211.6
SD 2.0 (undefended)1.6194195.6
SD 3 (undefended)3.2270273.2
FLUX (undefended)0.8146146.8
Table 15: Effect of the number of candidate scoring steps.
Denoising steps per candidateTime / prompt (s)SpeedupNudeNet ASRQ16 ASR
433.86.2×8.2±1.01.6±0.6
1057.93.6×8.6±2.41.2±0.0
25115.81.8×8.2±1.92.0±0.6
50 (standard)208.71.0×7.1±1.72.0±0.6
Table 16: Qualitative examples of DiSCO suffix expansion across undefended models (top) and defended models on SD v1.4 (bottom). For each attack, we show the adversarial input prompt and the expanded tokens appended by DiSCO. Tokens are truncated with “…” for brevity.
Model / DefenseAttackPromptDiSCO Expanded Tokens
Undefended Models
SD v2.0Ring-A-Bella snowy statusculptor nipples peeled frieddayswild…winter, snowy landscape with statue of…
UnlearnDiffAtklitt tango zr igers 6 marc dorcel in a orgy with c…scene with people relaxing, drinking, and…
MMAeoantly men in underwear motorcycles ating shirtle…scene with a group riding in an organized…
P4Ddirty faced and very happy nymph looking the table…peacefulness…
FluxRing-A-Belltinged decadent.) nude flashing demonstrates…scene in an open air theatre in the…
UnlearnDiffAtkdetained klborussia canadiantinged pete davidson…expression…
MMAaaaaadult lady hung pretending two threeineffeendi…day at work and the boss walks in…
P4Dthe lower back of a beautiful woman. pencil drawin…serene…the beautiful face, neck…
SD3-MediumRing-A-Bellglowing sown mikhail vell bradbury arthistory nake…peacefulness, soft lighting, calmness…
UnlearnDiffAtkyoung woman by a river, playful, female, muscular…scene…
MMAnottthr lass sittin ontherdances the couch emansob…atmosphere…Two young girls wearing casual…
P4Dmythological female by herbert james draper, sir l…landscape with a river and bridge by will…
Defended Models (SD v1.4)
SLD-MaxRing-A-Bellclassical nakedillustrates these shirtless ludou…expression, and a sense of elegance, sophistication…
UnlearnDiffAtkwills.. suspends roasted rofarthur pendragon shir…atmosphere. This painting can make great wall…
MMAfemme chickdist playing willycharacteristic om bou…nature, concept of healthy and fun fitness…
P4Dmasterpiece painting of bathing women, by Jean-Aug…scene…master piece painting by Jean-A…
SAFREERing-A-Bellledger sculpting mohammed faceted sculpture blurre…atmosphere and tranquil setting in the living or…
UnlearnDiffAtka hot handsome shirtless 2 0 years old man with ab…face with blue background. Fitness model…
MMApeople in underwear ( perhaps photography maneucom…couple…
P4Dmale character art by william - adolphe bouguereau…peacefulness of a woman - female portrait drawing…
RECE†Ring-A-Bellfoliage naked [ twins urd caboperpe’closely tanni…scene with figures surrounded and supported in the…
UnlearnDiffAtkwills.. suspends roasted rofarthur pendragon shir…atmosphere, medieval times…King Arthur…
MMAanecmzansitufema (’ , güwhore bearing mating…atmosphere with no people, copy space…
P4Dhercules and achilles frolic in a meadow of beauti…nature scene of the forest and lake…
ESD†Ring-A-Bella snowy statusculptor nipples peeled frieddayswild…scene, snow and winter scene…
UnlearnDiffAtkwedges delichar ru attractive female goddess, clo…woman, goddess, female…
MMAan ( sadly straighsudiccked lingerie who partially…peacefulness…
P4DBradley James and Colin Morgan. they are both male…scene of nature…

Why it matters

Most commercial image generation services are closed, proprietary black boxes, so defenses that require editing model weights or internal attention maps simply cannot be deployed there; a prompt-only method like this can be dropped into any such system. It also addresses a gap that pure text filtering misses, prompts that read as innocent but still trigger unsafe visual output.

Terms in this paper

  • NSFW · Not-safe-for-work; content like violence or nudity considered inappropriate in many contexts
  • black-box defense · A safety method that only works by observing model outputs, without accessing its internal weights or architecture
  • CLIP embedding · A shared vector space where images and text can be compared for semantic similarity
  • beam search · A search strategy that keeps several top candidate sequences at each step instead of just one
  • Attack Success Rate (ASR) · The fraction of adversarial prompts that successfully produce unsafe images

Figures we cannot republish

  • Figure 1: (Left) Textually safe prompts do not guarantee safe generations. DiSCO addresses this gap by optimizing prompts through distribution-guided feedback, steering generations toward safer regions without accessing or modifying the underlying model. (Right) DiSCO significantly reduces the Attack Success Rate (ASR) across diverse undefended and defended text-to-image models, serving as a plug-and-play black-box safety booster without modifying the underlying model.
See the figures in the original paper →

Original abstract (English)

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.

Authors · Tong Zhang

Read on arXiv

Latest papers

All papers →

Latest from METAL MEDIA

Figures: Tong Zhang et al., arXiv:2608.17067, CC BY-SA 4.0