Skip to content

Commit e5ff755

Browse files
patil-surajpcuencapatrickvonplaten
authored
Add InstructPix2Pix pipeline (#2040)
* being pix2pix * ifx * cfg image_latents * fix some docstr * fix * fix * hack * fix * Apply suggestions from code review Co-authored-by: Pedro Cuenca <pedro@huggingface.co> * add comments to explain the hack * move __call__ to the top * doc * remove height and width * remove depreications * fix doc str * quality * fast tests * chnage model id * fast tests * fix test * address Pedro's comments * copyright * Simple doc page. * Apply suggestions from code review * style * Remove import * address some review comments * Apply suggestions from code review Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com> * style Co-authored-by: Pedro Cuenca <pedro@huggingface.co> Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
1 parent 3ecbbd6 commit e5ff755

File tree

9 files changed

+1085
-0
lines changed

9 files changed

+1085
-0
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
title: Image-Variation
138138
- local: api/pipelines/stable_diffusion/upscale
139139
title: Super-Resolution
140+
- local: api/pipelines/stable_diffusion/pix2pix
141+
title: InstructPix2Pix
140142
title: Stable Diffusion
141143
- local: api/pipelines/stable_diffusion_2
142144
title: Stable Diffusion 2

docs/source/en/api/pipelines/stable_diffusion/overview.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ For more details about how Stable Diffusion works and how it differs from the ba
3131
| [StableDiffusionDepth2ImgPipeline](./depth2img) | **Experimental** *Depth-to-Image Text-Guided Generation * | | Coming soon
3232
| [StableDiffusionImageVariationPipeline](./image_variation) | **Experimental** *Image Variation Generation * | | [🤗 Stable Diffusion Image Variations](https://huggingface.co/spaces/lambdalabs/stable-diffusion-image-variations)
3333
| [StableDiffusionUpscalePipeline](./upscale) | **Experimental** *Text-Guided Image Super-Resolution * | | Coming soon
34+
| [StableDiffusionInstructPix2PixPipeline](./pix2pix) | **Experimental** *Text-Based Image Editing * | | [InstructPix2Pix: Learning to Follow Image Editing Instructions](https://huggingface.co/spaces/timbrooks/instruct-pix2pix)
3435

3536

3637

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
-->
12+
13+
# InstructPix2Pix: Learning to Follow Image Editing Instructions
14+
15+
## Overview
16+
17+
[InstructPix2Pix: Learning to Follow Image Editing Instructions](https://arxiv.org/abs/2211.09800) by Tim Brooks, Aleksander Holynski and Alexei A. Efros.
18+
19+
The abstract of the paper is the following:
20+
21+
*We propose a method for editing images from human instructions: given an input image and a written instruction that tells the model what to do, our model follows these instructions to edit the image. To obtain training data for this problem, we combine the knowledge of two large pretrained models -- a language model (GPT-3) and a text-to-image model (Stable Diffusion) -- to generate a large dataset of image editing examples. Our conditional diffusion model, InstructPix2Pix, is trained on our generated data, and generalizes to real images and user-written instructions at inference time. Since it performs edits in the forward pass and does not require per example fine-tuning or inversion, our model edits images quickly, in a matter of seconds. We show compelling editing results for a diverse collection of input images and written instructions.*
22+
23+
Resources:
24+
25+
* [Project Page](https://www.timothybrooks.com/instruct-pix2pix).
26+
* [Paper](https://arxiv.org/abs/2211.09800).
27+
* [Original Code](https://github.com/timothybrooks/instruct-pix2pix).
28+
* [Demo](https://huggingface.co/spaces/timbrooks/instruct-pix2pix).
29+
30+
31+
## Available Pipelines:
32+
33+
| Pipeline | Tasks | Demo
34+
|---|---|:---:|
35+
| [StableDiffusionInstructPix2PixPipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py) | *Text-Based Image Editing* | [🤗 Space](https://huggingface.co/spaces/timbrooks/instruct-pix2pix) |
36+
37+
<!-- TODO: add Colab -->
38+
39+
## Usage example
40+
41+
```python
42+
import PIL
43+
import requests
44+
import torch
45+
from diffusers import StableDiffusionInstructPix2PixPipeline
46+
47+
model_id = "timbrooks/instruct-pix2pix"
48+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
49+
model_id, torch_dtype=torch.float16, revision="fp16", safety_checker=None
50+
).to("cuda")
51+
52+
url = "https://raw.githubusercontent.com/timothybrooks/instruct-pix2pix/main/imgs/example.jpg"
53+
54+
55+
def download_image(url):
56+
image = PIL.Image.open(requests.get(url, stream=True).raw)
57+
image = PIL.ImageOps.exif_transpose(image)
58+
image = image.convert("RGB")
59+
return image
60+
61+
62+
image = download_image(url)
63+
64+
prompt = "turn him into a cyborg"
65+
images = pipe(prompt, image=image, num_inference_steps=10, guidance_scale=1.1, image_guidance_scale=1).images
66+
images[0].save("david_cyborg.png")
67+
```
68+
69+
## StableDiffusionInstructPix2PixPipeline
70+
[[autodoc]] StableDiffusionInstructPix2PixPipeline
71+
- __call__
72+
- all

src/diffusers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
StableDiffusionImg2ImgPipeline,
115115
StableDiffusionInpaintPipeline,
116116
StableDiffusionInpaintPipelineLegacy,
117+
StableDiffusionInstructPix2PixPipeline,
117118
StableDiffusionPipeline,
118119
StableDiffusionPipelineSafe,
119120
StableDiffusionUpscalePipeline,

src/diffusers/pipelines/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
StableDiffusionImg2ImgPipeline,
5252
StableDiffusionInpaintPipeline,
5353
StableDiffusionInpaintPipelineLegacy,
54+
StableDiffusionInstructPix2PixPipeline,
5455
StableDiffusionPipeline,
5556
StableDiffusionUpscalePipeline,
5657
)

src/diffusers/pipelines/stable_diffusion/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class StableDiffusionPipelineOutput(BaseOutput):
4343
from .pipeline_stable_diffusion_img2img import StableDiffusionImg2ImgPipeline
4444
from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline
4545
from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy
46+
from .pipeline_stable_diffusion_instruct_pix2pix import StableDiffusionInstructPix2PixPipeline
4647
from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
4748
from .safety_checker import StableDiffusionSafetyChecker
4849

0 commit comments

Comments
 (0)