-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Add notebook doc img2img #2472
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
Add notebook doc img2img #2472
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,17 @@ specific language governing permissions and limitations under the License. | |||||
|
|
||||||
| # Text-Guided Image-to-Image Generation | ||||||
|
|
||||||
| The [`StableDiffusionImg2ImgPipeline`] lets you pass a text prompt and an initial image to condition the generation of new images. | ||||||
| [[open-in-colab]] | ||||||
|
|
||||||
| The [`StableDiffusionImg2ImgPipeline`] lets you pass a text prompt and an initial image to condition the generation of new images. This tutorial shows how to use it for text-guided image-to-image generation with Stable Diffusion model. | ||||||
|
|
||||||
| Before you begin, make sure you have all the necessary libraries installed: | ||||||
|
|
||||||
| ```bash | ||||||
| !pip install diffusers transformers ftfy accelerate | ||||||
| ``` | ||||||
|
|
||||||
| Get started by creating a [`StableDiffusionImg2ImgPipeline`] with a pretrained Stable Diffusion model. | ||||||
|
|
||||||
| ```python | ||||||
| import torch | ||||||
|
|
@@ -21,25 +31,83 @@ from PIL import Image | |||||
| from io import BytesIO | ||||||
|
|
||||||
| from diffusers import StableDiffusionImg2ImgPipeline | ||||||
| ``` | ||||||
|
|
||||||
| # load the pipeline | ||||||
| Load the pipeline | ||||||
|
|
||||||
| ```python | ||||||
| device = "cuda" | ||||||
| pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to( | ||||||
| device | ||||||
| ) | ||||||
| ``` | ||||||
|
|
||||||
| # let's download an initial image | ||||||
| Download an initial image and preprocess it so we can pass it to the pipeline. | ||||||
|
|
||||||
| ```python | ||||||
| url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg" | ||||||
|
|
||||||
| response = requests.get(url) | ||||||
| init_image = Image.open(BytesIO(response.content)).convert("RGB") | ||||||
| init_image.thumbnail((768, 768)) | ||||||
| init_image | ||||||
| ``` | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
| Define the prompt and run the pipeline. | ||||||
|
|
||||||
| ```python | ||||||
| prompt = "A fantasy landscape, trending on artstation" | ||||||
| ``` | ||||||
|
|
||||||
| <Tip> | ||||||
|
|
||||||
| images = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images | ||||||
| `strength` is a value between 0.0 and 1.0, that controls the amount of noise that is added to the input image. Values that approach 1.0 allow for lots of variations but will also produce images that are not semantically consistent with the input. | ||||||
|
|
||||||
| images[0].save("fantasy_landscape.png") | ||||||
| </Tip> | ||||||
|
|
||||||
| Let's generate two images with same pipeline and seed, but with different values for `strength` | ||||||
|
|
||||||
| ```python | ||||||
| generator = torch.Generator(device=device).manual_seed(1024) | ||||||
| image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0] | ||||||
| ``` | ||||||
| You can also run this example on colab [](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/image_2_image_using_diffusers.ipynb) | ||||||
|
|
||||||
| ```python | ||||||
| image | ||||||
| ``` | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
|
|
||||||
| ```python | ||||||
| image = pipe(prompt=prompt, image=init_image, strength=0.5, guidance_scale=7.5, generator=generator).images[0] | ||||||
| image | ||||||
| ``` | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
|
|
||||||
| As you can see, when using a lower value for `strength`, the generated image is more closer to the original `image` | ||||||
|
|
||||||
| Now let's use a different scheduler - [LMSDiscreteScheduler](https://huggingface.co/docs/diffusers/api/schedulers#diffusers.LMSDiscreteScheduler) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's use maybe a different scheduler here, such as DPMSolverMultistepScheduler -> LMS requires another dependency and not the best scheduler anymore |
||||||
|
|
||||||
| ```python | ||||||
| from diffusers import LMSDiscreteScheduler | ||||||
|
|
||||||
| lms = LMSDiscreteScheduler.from_config(pipe.scheduler.config) | ||||||
| pipe.scheduler = lms | ||||||
| ``` | ||||||
|
|
||||||
| ```python | ||||||
| generator = torch.Generator(device=device).manual_seed(1024) | ||||||
| image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0] | ||||||
| ``` | ||||||
|
|
||||||
| ```python | ||||||
| image | ||||||
| ``` | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the corresponding generated google colab?