Latent Guard: a Safety Framework for Text-to-image Generation

Runtao Liu, Ashkan Khakzar, Jindong Gu, Qifeng Chen, Philip Torr, Fabio Pizzati · DOI

Introduction

The rapid advancement of text-to-image (T2I) models like Stable Diffusion and DALL-E 3 has introduced significant risks regarding the generation of unsafe content, including NSFW imagery, hate speech, and deepfakes. Current safety mechanisms generally fall into three categories: string-based blacklists, LLM-based filters, and post-generation image classifiers. However, these methods are often easily bypassed, computationally expensive, or reactive rather than proactive.

Latent Guard addresses these challenges by introducing a high-speed safety framework that maps text prompts into a learned latent space alongside a flexible "concept blacklist." This approach allows for the detection of harmful intent even when it is obscured by synonyms or adversarial attacks, all without the need to retrain the underlying diffusion model.

Methodology: The Embedding Mapping Layer

The fundamental innovation of Latent Guard is its Embedding Mapping Layer. Rather than analyzing raw text strings, the system operates on the embeddings produced by the T2I model’s text encoder (e.g., CLIP).

Workflow and Training

The framework follows a structured workflow:

  1. Data Generation: Using an LLM (Mixtral 8x7B), the authors generated a massive dataset of "unsafe" prompts centered on specific concepts (e.g., "violence") and paired them with "safe" counterparts that are semantically similar but harmless.
  2. Mapping Layer Architecture: This trainable component utilizes multi-head cross-attention. It processes the embedding of a user prompt alongside the embedding of a blacklisted concept, learning to "attend" to specific segments of the prompt that align with the harmful concept.
  3. Contrastive Learning: The model is trained using a supervised contrastive loss. This objective forces unsafe prompt embeddings to cluster near their corresponding blacklisted concepts in the latent space while pushing safe prompts away.
  4. Inference: During runtime, Latent Guard calculates the cosine similarity between the user's prompt and the concepts in the blacklist. If the similarity exceeds a predefined threshold, the prompt is blocked before the diffusion process begins.

The CoPro Dataset

To evaluate the framework, the authors introduced the CoPro (Concepts and Prompts) benchmark. This dataset contains 723 harmful concepts and over 226,000 safe/unsafe prompt pairs. It is categorized into:

  • Explicit Prompts: Containing direct harmful keywords.
  • Synonym Prompts: Rephrased harmful intent using non-blacklisted words.
  • Adversarial Prompts: Specifically optimized to bypass standard filters.

Additionally, the model was tested on existing benchmarks like Unsafe Diffusion (UD) and I2P++ to ensure generalization to unseen data.

Key Results and Insights

Latent Guard demonstrates superior performance compared to traditional baselines such as CLIPScore and BERTScore.

Performance Metrics

  • Accuracy: The model achieved approximately 87% accuracy on the CoPro dataset for in-distribution data.
  • AUC (Area Under Curve): On explicit prompts, Latent Guard reached an AUC of 0.985, significantly outperforming CLIPScore (0.697).
  • Adversarial Robustness: In scenarios involving optimized adversarial text, Latent Guard maintained an AUC of 0.908, whereas traditional methods saw a total collapse in performance.
  • Efficiency: Latent Guard adds negligible overhead, checking a prompt against 578 concepts in roughly 1ms. This is orders of magnitude faster than LLM-based filters or the image generation process itself.

Zero-Shot Adaptability

A key advantage of Latent Guard is its flexibility. Because it compares prompts to concept embeddings, the "blacklist" can be updated at test time. New harmful trends can be mitigated by simply adding the relevant concept to the list without requiring any model retraining. Visualization of the latent space via t-SNE further reveals that a clear "safe vs. unsafe" boundary naturally emerges, even though the model was only trained to identify specific concepts.

Discussion and Limitations

While Latent Guard represents a significant step forward, it is not without limitations. The system's effectiveness is inherently tied to the quality and coverage of its concept blacklist. If a harmful concept is not represented or semantically close to an existing entry, it may bypass the filter. Furthermore, because the training data was synthetically generated by an LLM, there is a potential for distribution shift when the model encounters human-written prompts that deviate from LLM patterns.

Finally, the authors note the "cat-and-mouse" nature of AI safety. Open-sourcing safety tools provides transparency but also allows malicious actors to study the model's weights to identify "blind spots" in the latent space.

Conclusion

Latent Guard provides a robust, efficient, and semantically aware safety layer for text-to-image generation. By operating in the latent space of text encoders, it bridges the gap between the speed of simple blacklists and the intelligence of large language models, offering a scalable solution for safer AI-driven content creation.

Summary updated

BibTeX
@inproceedings{liu2024latent,
  author = {Runtao Liu and Ashkan Khakzar and Jindong Gu and Qifeng Chen and Philip Torr and Fabio Pizzati},
  title = {{Latent Guard: a Safety Framework for Text-to-image Generation}},
  booktitle = {European Conference on Computer Vision},
  year = {2024},
  doi = {10.1007/978-3-031-73347-5\_6},
}
Figure