Skip to content

Diffedit Zero-Shot Inpainting Pipeline #2837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 59 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
b218062
Update Pix2PixZero Auto-correlation Loss
clarencechen Mar 26, 2023
f953739
Add Stable Diffusion DiffEdit pipeline
clarencechen Mar 26, 2023
a37b6e0
Add draft documentation and import code
clarencechen Mar 28, 2023
1b8753b
Bugfixes and refactoring
clarencechen Mar 27, 2023
eda67c2
Add option to not decode latents in the inversion process
clarencechen Mar 28, 2023
b2598f1
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Mar 28, 2023
219793c
Harmonize preprocessing
clarencechen Mar 28, 2023
44d187e
Revert "Update Pix2PixZero Auto-correlation Loss"
clarencechen Mar 28, 2023
5fb209f
Update annotations
clarencechen Mar 28, 2023
3f74c41
rename `compute_mask` to `generate_mask`
clarencechen Mar 28, 2023
d459eb9
Update documentation
clarencechen Mar 28, 2023
f69551c
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Mar 28, 2023
e64fcba
Update docs
clarencechen Mar 28, 2023
1276d57
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Mar 29, 2023
1328b6a
Update Docs
clarencechen Mar 29, 2023
f7a12c8
Fix copy
clarencechen Mar 29, 2023
fcf26d4
Change shape of output latents to batch first
clarencechen Mar 30, 2023
2821aa4
Update docs
clarencechen Mar 30, 2023
dabd82f
Add first draft for tests
clarencechen Mar 30, 2023
18ee76a
Bugfix and update tests
clarencechen Mar 30, 2023
f31b7b8
Add `cross_attention_kwargs` support for all pipeline methods
clarencechen Mar 31, 2023
0b835b1
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Mar 31, 2023
ab79c4f
Fix Copies
clarencechen Mar 31, 2023
dba2a30
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Mar 31, 2023
a08f620
Add support for PIL image latents
clarencechen Apr 3, 2023
862ce2e
Enable MPS Tests
clarencechen Apr 3, 2023
7de158a
Move example docstrings
clarencechen Apr 4, 2023
79cf02b
Fix test
clarencechen Apr 4, 2023
e212d92
Refactor
clarencechen Apr 5, 2023
d4d8c84
Fix test
clarencechen Apr 5, 2023
ff0fbc2
fix pipeline inheritance
clarencechen Apr 5, 2023
0db442d
Harmonize `prepare_image_latents` with StableDiffusionPix2PixZeroPipe…
clarencechen Apr 5, 2023
a73f4db
Register modules set to `None` in config for `test_save_load_optional…
clarencechen Apr 5, 2023
1348e97
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 5, 2023
0f67d31
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 6, 2023
af62b30
Move fixed logic to specific test class
clarencechen Apr 6, 2023
55cacef
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 6, 2023
08280a7
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 7, 2023
8990159
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 10, 2023
c0e5413
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 12, 2023
135c390
Clean changes to other pipelines
clarencechen Apr 12, 2023
a46787d
Update new tests to coordinate with #2953
clarencechen Apr 12, 2023
37fb12d
Update slow tests for better results
clarencechen Apr 12, 2023
0c67b2f
Safety to avoid potential problems with torch.inference_mode
clarencechen Apr 13, 2023
0319d28
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 13, 2023
d79cac5
Add reference in SD Pipeline Overview
clarencechen Apr 13, 2023
b6a81e8
Fix tests again
clarencechen Apr 13, 2023
83e72d8
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 18, 2023
599750e
Enforce determinism in noise for generate_mask
clarencechen Apr 18, 2023
55cad5b
Fix copies
clarencechen Apr 18, 2023
a5986a9
Widen test tolerance for fp16 based on `test_stable_diffusion_upscale…
clarencechen Apr 18, 2023
c28a3f6
Merge branch 'main' of https://github.com/huggingface/diffusers into …
clarencechen Apr 19, 2023
baafe02
Merge remote-tracking branch 'upstream/main' into diffedit-inpainting…
clarencechen Apr 20, 2023
6101d4a
Add LoraLoaderMixin and update `prepare_image_latents`
clarencechen Apr 20, 2023
7224532
clean up repeat and reg
clarencechen Apr 22, 2023
d471f11
Merge remote-tracking branch 'upstream/main' into diffedit-inpainting…
clarencechen Apr 22, 2023
6b91ca6
bugfix
clarencechen Apr 22, 2023
9a49b01
Remove invalid args from docs
clarencechen Apr 26, 2023
9d37c33
Merge remote-tracking branch 'upstream/main' into diffedit-inpainting…
clarencechen Apr 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@
title: Text-to-Image Generation with ControlNet Conditioning
- local: api/pipelines/stable_diffusion/model_editing
title: Text-to-Image Model Editing
- local: api/pipelines/stable_diffusion/diffedit
title: DiffEdit
title: Stable Diffusion
- local: api/pipelines/stable_diffusion_2
title: Stable Diffusion 2
Expand Down
360 changes: 360 additions & 0 deletions docs/source/en/api/pipelines/stable_diffusion/diffedit.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
<!--Copyright 2023 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

# Zero-shot Diffusion-based Semantic Image Editing with Mask Guidance

## Overview

[DiffEdit: Diffusion-based semantic image editing with mask guidance](https://arxiv.org/abs/2210.11427) by Guillaume Couairon, Jakob Verbeek, Holger Schwenk, and Matthieu Cord.

The abstract of the paper is the following:

*Image generation has recently seen tremendous advances, with diffusion models allowing to synthesize convincing images for a large variety of text prompts. In this article, we propose DiffEdit, a method to take advantage of text-conditioned diffusion models for the task of semantic image editing, where the goal is to edit an image based on a text query. Semantic image editing is an extension of image generation, with the additional constraint that the generated image should be as similar as possible to a given input image. Current editing methods based on diffusion models usually require to provide a mask, making the task much easier by treating it as a conditional inpainting task. In contrast, our main contribution is able to automatically generate a mask highlighting regions of the input image that need to be edited, by contrasting predictions of a diffusion model conditioned on different text prompts. Moreover, we rely on latent inference to preserve content in those regions of interest and show excellent synergies with mask-based diffusion. DiffEdit achieves state-of-the-art editing performance on ImageNet. In addition, we evaluate semantic image editing in more challenging settings, using images from the COCO dataset as well as text-based generated images.*

Resources:

* [Paper](https://arxiv.org/abs/2210.11427).
* [Blog Post with Demo](https://blog.problemsolversguild.com/technical/research/2022/11/02/DiffEdit-Implementation.html).
* [Implementation on Github](https://github.com/Xiang-cd/DiffEdit-stable-diffusion/).

## Tips

* The pipeline can generate masks that can be fed into other inpainting pipelines. Check out the code examples below to know more.
* In order to generate an image using this pipeline, both an image mask (manually specified or generated using `generate_mask`)
and a set of partially inverted latents (generated using `invert`) _must_ be provided as arguments when calling the pipeline to generate the final edited image.
Refer to the code examples below for more details.
* The function `generate_mask` exposes two prompt arguments, `source_prompt` and `target_prompt`,
that let you control the locations of the semantic edits in the final image to be generated. Let's say,
you wanted to translate from "cat" to "dog". In this case, the edit direction will be "cat -> dog". To reflect
this in the generated mask, you simply have to set the embeddings related to the phrases including "cat" to
`source_prompt_embeds` and "dog" to `target_prompt_embeds`. Refer to the code example below for more details.
* When generating partially inverted latents using `invert`, assign a caption or text embedding describing the
overall image to the `prompt` argument to help guide the inverse latent sampling process. In most cases, the
source concept is sufficently descriptive to yield good results, but feel free to explore alternatives.
Please refer to [this code example](#generating-image-captions-for-inversion) for more details.
* When calling the pipeline to generate the final edited image, assign the source concept to `negative_prompt`
and the target concept to `prompt`. Taking the above example, you simply have to set the embeddings related to
the phrases including "cat" to `negative_prompt_embeds` and "dog" to `prompt_embeds`. Refer to the code example
below for more details.
* If you wanted to reverse the direction in the example above, i.e., "dog -> cat", then it's recommended to:
* Swap the `source_prompt` and `target_prompt` in the arguments to `generate_mask`.
* Change the input prompt for `invert` to include "dog".
* Swap the `prompt` and `negative_prompt` in the arguments to call the pipeline to generate the final edited image.
* Note that the source and target prompts, or their corresponding embeddings, can also be automatically generated. Please, refer to [this discussion](#generating-source-and-target-embeddings) for more details.

## Available Pipelines:

| Pipeline | Tasks
|---|---|
| [StableDiffusionDiffEditPipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_diffedit.py) | *Text-Based Image Editing*

<!-- TODO: add Colab -->

## Usage example

### Based on an input image with a caption

When the pipeline is conditioned on an input image, we first obtain partially inverted latents from the input image using a
`DDIMInverseScheduler` with the help of a caption. Then we generate an editing mask to identify relevant regions in the image using the source and target prompts. Finally,
the inverted noise and generated mask is used to start the generation process.

First, let's load our pipeline:

```py
import torch
from diffusers import DDIMScheduler, DDIMInverseScheduler, StableDiffusionPix2PixZeroPipeline

sd_model_ckpt = "stabilityai/stable-diffusion-2-1"
pipeline = StableDiffusionDiffEditPipeline.from_pretrained(
sd_model_ckpt,
torch_dtype=torch.float16,
safety_checker=None,
)
pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
pipeline.inverse_scheduler = DDIMInverseScheduler.from_config(pipeline.scheduler.config)
pipeline.enable_model_cpu_offload()
pipeline.enable_vae_slicing()
generator = torch.manual_seed(0)
```

Then, we load an input image to edit using our method:

```py
from diffusers.utils import load_image

img_url = "https://github.com/Xiang-cd/DiffEdit-stable-diffusion/raw/main/assets/origin.png"
raw_image = load_image(img_url).convert("RGB").resize((768, 768))
```

Then, we employ the source and target prompts to generate the editing mask:

```py
# See the "Generating source and target embeddings" section below to
# automate the generation of these captions with a pre-trained model like Flan-T5 as explained below.

source_prompt = "a bowl of fruits"
target_prompt = "a basket of fruits"
mask_image = pipeline.generate_mask(
image=raw_image,
source_prompt=source_prompt,
target_prompt=target_prompt,
generator=generator,
)
```

Then, we employ the caption and the input image to get the inverted latents:

```py
inv_latents = pipeline.invert(prompt=source_prompt, image=raw_image, generator=generator).latents
```

Now, generate the image with the inverted latents and semantically generated mask:

```py
image = pipeline(
prompt=target_prompt,
mask_image=mask_image,
image_latents=inv_latents,
generator=generator,
negative_prompt=source_prompt,
).images[0]
image.save("edited_image.png")
```

## Generating image captions for inversion

The authors originally used the source concept prompt as the caption for generating the partially inverted latents. However, we can also leverage open source and public image captioning models for the same purpose.
Below, we provide an end-to-end example with the [BLIP](https://huggingface.co/docs/transformers/model_doc/blip) model
for generating captions.

First, let's load our automatic image captioning model:

```py
import torch
from transformers import BlipForConditionalGeneration, BlipProcessor

captioner_id = "Salesforce/blip-image-captioning-base"
processor = BlipProcessor.from_pretrained(captioner_id)
model = BlipForConditionalGeneration.from_pretrained(captioner_id, torch_dtype=torch.float16, low_cpu_mem_usage=True)
```

Then, we define a utility to generate captions from an input image using the model:

```py
@torch.no_grad()
def generate_caption(images, caption_generator, caption_processor):
text = "a photograph of"

inputs = caption_processor(images, text, return_tensors="pt").to(device="cuda", dtype=caption_generator.dtype)
caption_generator.to("cuda")
outputs = caption_generator.generate(**inputs, max_new_tokens=128)

# offload caption generator
caption_generator.to("cpu")

caption = caption_processor.batch_decode(outputs, skip_special_tokens=True)[0]
return caption
```

Then, we load an input image for conditioning and obtain a suitable caption for it:

```py
from diffusers.utils import load_image

img_url = "https://github.com/Xiang-cd/DiffEdit-stable-diffusion/raw/main/assets/origin.png"
raw_image = load_image(img_url).convert("RGB").resize((768, 768))
caption = generate_caption(raw_image, model, processor)
```

Then, we employ the generated caption and the input image to get the inverted latents:

```py
from diffusers import DDIMInverseScheduler, DDIMScheduler

pipeline = StableDiffusionDiffEditPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16
)
pipeline = pipeline.to("cuda")
pipeline.enable_model_cpu_offload()
pipeline.enable_vae_slicing()

pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
pipeline.inverse_scheduler = DDIMInverseScheduler.from_config(pipeline.scheduler.config)

generator = torch.manual_seed(0)
inv_latents = pipeline.invert(prompt=caption, image=raw_image, generator=generator).latents
```

Now, generate the image with the inverted latents and semantically generated mask from our source and target prompts:

```py
source_prompt = "a bowl of fruits"
target_prompt = "a basket of fruits"

mask_image = pipeline.generate_mask(
image=raw_image,
source_prompt=source_prompt,
target_prompt=target_prompt,
generator=generator,
)

image = pipeline(
prompt=target_prompt,
mask_image=mask_image,
image_latents=inv_latents,
generator=generator,
negative_prompt=source_prompt,
).images[0]
image.save("edited_image.png")
```

## Generating source and target embeddings

The authors originally required the user to manually provide the source and target prompts for discovering
edit directions. However, we can also leverage open source and public models for the same purpose.
Below, we provide an end-to-end example with the [Flan-T5](https://huggingface.co/docs/transformers/model_doc/flan-t5) model
for generating source an target embeddings.

**1. Load the generation model**:

```py
import torch
from transformers import AutoTokenizer, T5ForConditionalGeneration

tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-xl")
model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-xl", device_map="auto", torch_dtype=torch.float16)
```

**2. Construct a starting prompt**:

```py
source_concept = "bowl"
target_concept = "basket"

source_text = f"Provide a caption for images containing a {source_concept}. "
"The captions should be in English and should be no longer than 150 characters."

target_text = f"Provide a caption for images containing a {target_concept}. "
"The captions should be in English and should be no longer than 150 characters."
```

Here, we're interested in the "bowl -> basket" direction.

**3. Generate prompts**:

We can use a utility like so for this purpose.

```py
@torch.no_grad
def generate_prompts(input_prompt):
input_ids = tokenizer(input_prompt, return_tensors="pt").input_ids.to("cuda")

outputs = model.generate(
input_ids, temperature=0.8, num_return_sequences=16, do_sample=True, max_new_tokens=128, top_k=10
)
return tokenizer.batch_decode(outputs, skip_special_tokens=True)
```

And then we just call it to generate our prompts:

```py
source_prompts = generate_prompts(source_text)
target_prompts = generate_prompts(target_text)
```

We encourage you to play around with the different parameters supported by the
`generate()` method ([documentation](https://huggingface.co/docs/transformers/main/en/main_classes/text_generation#transformers.generation_tf_utils.TFGenerationMixin.generate)) for the generation quality you are looking for.

**4. Load the embedding model**:

Here, we need to use the same text encoder model used by the subsequent Stable Diffusion model.

```py
from diffusers import StableDiffusionDiffEditPipeline

pipeline = StableDiffusionDiffEditPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16
)
pipeline = pipeline.to("cuda")
pipeline.enable_model_cpu_offload()
pipeline.enable_vae_slicing()

generator = torch.manual_seed(0)
```

**5. Compute embeddings**:

```py
import torch

@torch.no_grad()
def embed_prompts(sentences, tokenizer, text_encoder, device="cuda"):
embeddings = []
for sent in sentences:
text_inputs = tokenizer(
sent,
padding="max_length",
max_length=tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
text_input_ids = text_inputs.input_ids
prompt_embeds = text_encoder(text_input_ids.to(device), attention_mask=None)[0]
embeddings.append(prompt_embeds)
return torch.concatenate(embeddings, dim=0).mean(dim=0).unsqueeze(0)

source_embeddings = embed_prompts(source_prompts, pipeline.tokenizer, pipeline.text_encoder)
target_embeddings = embed_prompts(target_captions, pipeline.tokenizer, pipeline.text_encoder)
```

And you're done! Now, you can use these embeddings directly while calling the pipeline:

```py
from diffusers import DDIMInverseScheduler, DDIMScheduler
from diffusers.utils import load_image

pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
pipeline.inverse_scheduler = DDIMInverseScheduler.from_config(pipeline.scheduler.config)

img_url = "https://github.com/Xiang-cd/DiffEdit-stable-diffusion/raw/main/assets/origin.png"
raw_image = load_image(img_url).convert("RGB").resize((768, 768))


mask_image = pipeline.generate_mask(
image=raw_image,
source_prompt_embeds=source_embeds,
target_prompt_embeds=target_embeds,
generator=generator,
)

inv_latents = pipeline.invert(
prompt_embeds=source_embeds,
image=raw_image,
generator=generator,
).latents

images = pipeline(
mask_image=mask_image,
image_latents=inv_latents,
prompt_embeds=target_embeddings,
negative_prompt_embeds=source_embeddings,
generator=generator,
).images
images[0].save("edited_image.png")
```

## StableDiffusionDiffEditPipeline
[[autodoc]] StableDiffusionDiffEditPipeline
- all
- generate_mask
- invert
- __call__
1 change: 1 addition & 0 deletions docs/source/en/api/pipelines/stable_diffusion/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ For more details about how Stable Diffusion works and how it differs from the ba
| [StableDiffusionAttendAndExcitePipeline](./attend_and_excite) | **Experimental** – *Text-to-Image Generation * | | [Attend-and-Excite: Attention-Based Semantic Guidance for Text-to-Image Diffusion Models](https://huggingface.co/spaces/AttendAndExcite/Attend-and-Excite)
| [StableDiffusionPix2PixZeroPipeline](./pix2pix_zero) | **Experimental** – *Text-Based Image Editing * | | [Zero-shot Image-to-Image Translation](https://arxiv.org/abs/2302.03027)
| [StableDiffusionModelEditingPipeline](./model_editing) | **Experimental** – *Text-to-Image Model Editing * | | [Editing Implicit Assumptions in Text-to-Image Diffusion Models](https://arxiv.org/abs/2303.08084)
| [StableDiffusionDiffEditPipeline](./diffedit) | **Experimental** – *Text-Based Image Editing * | | [DiffEdit: Diffusion-based semantic image editing with mask guidance](https://arxiv.org/abs/2210.11427)



Expand Down
1 change: 1 addition & 0 deletions src/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
StableDiffusionAttendAndExcitePipeline,
StableDiffusionControlNetPipeline,
StableDiffusionDepth2ImgPipeline,
StableDiffusionDiffEditPipeline,
StableDiffusionImageVariationPipeline,
StableDiffusionImg2ImgPipeline,
StableDiffusionInpaintPipeline,
Expand Down
1 change: 1 addition & 0 deletions src/diffusers/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
StableDiffusionAttendAndExcitePipeline,
StableDiffusionControlNetPipeline,
StableDiffusionDepth2ImgPipeline,
StableDiffusionDiffEditPipeline,
StableDiffusionImageVariationPipeline,
StableDiffusionImg2ImgPipeline,
StableDiffusionInpaintPipeline,
Expand Down
Loading