Abstract
Large-scale text-to-image generative models have shown remarkable ability to synthesize diverse and high-quality images. However, it is still challenging to directly apply these models for editing real images for two reasons. First, it is difficult for users to come up with a perfect text prompt that accurately describes every visual detail in the input image. Second, while existing models can introduce desirable changes in certain regions, they often dramatically alter the input content and introduce unexpected changes in unwanted regions. To address these challenges, we present Dual Contrastive Denoising Score, a simple yet powerful framework that leverages the rich generative prior of text-to-image diffusion models. Inspired by contrastive learning approaches for unpaired image-to-image translation, we introduce a straightforward dual contrastive loss within the proposed framework. Our approach utilizes the extensive spatial information from the intermediate representations of the self-attention layers in latent diffusion models without depending on auxiliary networks. Our method achieves both flexible content modification and structure preservation between input and output images, as well as zero-shot image-to-image translation. Through extensive experiments, we show that our approach outperforms existing methods in real image editing while maintaining the capability to directly utilize pretrained text-to-image diffusion models without further training.
Introduction: The Challenges of Real Image Editing
While large-scale text-to-image (T2I) models like Stable Diffusion are excellent at generating images from scratch, editing existing real images remains a "Goldilocks" problem. Current methods often struggle with the Prompting Gap, where it is nearly impossible to write a text prompt that describes every minute detail of a real-world photo, such as lighting, texture, or exact pose.
Furthermore, many techniques suffer from Structural Drift, where asking a model to change one element (e.g., turning a cat into a dog) results in unwanted changes to the background or the overall composition. Finally, the Masking Burden—the requirement for users to manually paint masks over target areas—is both tedious and often destructive to structural information at the boundaries. DualCDS addresses these issues by providing a zero-shot, training-free framework for high-fidelity editing.
Methodology: Dual Contrastive Denoising Score (DualCDS)
DualCDS is an optimization-based framework that iteratively updates a latent representation to minimize a total loss function composed of two primary signals: semantic guidance and structural anchoring.
1. The Semantic Guide: Delta Denoising Score (DDS)
To ensure the image accurately reflects the target text prompt (e.g., "cat" to "pig"), the framework utilizes DDS. DDS calculates a gradient direction by comparing the denoising score of the source image/prompt pair against the target image/prompt pair. This approach provides a significantly cleaner edit signal than standard Score Distillation Sampling (SDS), focusing the updates on the semantic changes requested by the user.
2. The Structural Anchor: Dual Contrastive Loss ($L_{dualcon}$)
Inspired by Contrastive Unpaired Translation (CUT), the authors introduce a patch-wise contrastive loss. Unlike previous methods that require auxiliary encoders, DualCDS extracts features directly from the internal self-attention layers of the pre-trained U-Net. This loss ensures that a feature patch at a specific location in the generated image remains most similar to the patch at the same location in the source image, effectively anchoring the geometry and preventing structural drift.
Key Insights: The Role of Key Tokens
A pivotal discovery in this research is that Key tokens ($K$) within the self-attention blocks are the most effective features for structural preservation. While "Values" ($V$) tend to carry more style and texture information, "Keys" act as the geometric blueprint of the image.
By adjusting the weight of the contrastive loss ($\lambda$), users can control the "creativity" of the model. A high $\lambda$ (e.g., 5.0) keeps the structure rigid for near-domain edits like "cat to dog," while a lower $\lambda$ (e.g., 1.5) allows the model more freedom for distant-domain transformations, such as "cat to cow."
Experimental Results
DualCDS was evaluated using datasets including LAION-5B, COCO, and FFHQ. It consistently outperformed baselines like SDEdit and Prompt-to-Prompt (P2P) in both quantitative and qualitative metrics:
- Quantitative Performance: In "cat-to-pig" transformation tasks, DualCDS achieved a 99.7% CLIP Accuracy for text alignment and the lowest Structure Distance (0.021), indicating superior layout preservation.
- User Study: In a study with 30 participants, DualCDS was ranked highest across all categories, including Text-to-Image Agreement (9.34/10) and Structural Preservation (9.42/10).
- Qualitative Excellence: Visual results demonstrate "pose-perfect" edits, such as modifying animals or objects while keeping complex backgrounds and specific item placements (like roller skates) entirely intact.
Conclusion and Future Work
DualCDS provides a powerful, training-free solution for real image editing by leveraging the internal representations of diffusion models. While the optimization process currently takes approximately 4 minutes per edit on an NVIDIA RTX-3090, the method sets a new standard for structural fidelity in zero-shot editing. Future work may extend this dual contrastive approach to video editing to maintain temporal consistency and to the generation of 3D assets.
Summary updated