|
| 1 | +<!--Copyright 2022 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 | +# Stable diffusion 2 |
| 14 | + |
| 15 | +Stable Diffusion 2 is a text-to-image _latent diffusion_ model built upon the work of [Stable Diffusion 1](https://stability.ai/blog/stable-diffusion-public-release). |
| 16 | +The project to train Stable Diffusion 2 was led by Robin Rombach and Katherine Crowson from [Stability AI](https://stability.ai/) and [LAION](https://laion.ai/). |
| 17 | + |
| 18 | +*The Stable Diffusion 2.0 release includes robust text-to-image models trained using a brand new text encoder (OpenCLIP), developed by LAION with support from Stability AI, which greatly improves the quality of the generated images compared to earlier V1 releases. The text-to-image models in this release can generate images with default resolutions of both 512x512 pixels and 768x768 pixels. |
| 19 | +These models are trained on an aesthetic subset of the [LAION-5B dataset](https://laion.ai/blog/laion-5b/) created by the DeepFloyd team at Stability AI, which is then further filtered to remove adult content using [LAION’s NSFW filter](https://openreview.net/forum?id=M3Y74vmsMcY).* |
| 20 | + |
| 21 | +For more details about how Stable Diffusion 2 works and how it differs from Stable Diffusion 1, please refer to the official [launch announcement post](https://stability.ai/blog/stable-diffusion-v2-release). |
| 22 | + |
| 23 | +## Tips |
| 24 | + |
| 25 | +### Avaiblable checkpoints: |
| 26 | + |
| 27 | +Note that the architecture is more or less identical to [Stable Diffusion 1](./api/pipelines/stable_diffusion) so please refer to [this page](./api/pipelines/stable_diffusion) for API documentation. |
| 28 | + |
| 29 | +- *Text-to-Image (512x512 resolution)*: [stabilityai/stable-diffusion-2-base](https://huggingface.co/stabilityai/stable-diffusion-2-base) with [`StableDiffusionPipeline`] |
| 30 | +- *Text-to-Image (768x768 resolution)*: [stabilityai/stable-diffusion-2](https://huggingface.co/stabilityai/stable-diffusion-2) with [`StableDiffusionPipeline`] |
| 31 | +- *Image Inpainting (512x512 resolution)*: [stabilityai/stable-diffusion-2-inpainting](https://huggingface.co/stabilityai/stable-diffusion-2-inpainting) with [`StableDiffusionInpaintPipeline`] |
| 32 | +- *Image Upscaling (x4 resolution resolution)*: [stable-diffusion-x4-upscaler](https://huggingface.co/stabilityai/stable-diffusion-x4-upscaler) [`StableDiffusionUpscalePipeline`] |
| 33 | + |
| 34 | +We recommend using the [`DPMSolverMultistepScheduler`] as it's currently the fastest scheduler there is. |
| 35 | + |
| 36 | +- *Text-to-Image (512x512 resolution)*: |
| 37 | + |
| 38 | +```python |
| 39 | +from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler |
| 40 | +import torch |
| 41 | + |
| 42 | +repo_id = "stabilityai/stable-diffusion-2-base" |
| 43 | +pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision="fp16") |
| 44 | + |
| 45 | +pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) |
| 46 | +pipe = pipe.to("cuda") |
| 47 | + |
| 48 | +prompt = "High quality photo of an astronaut riding a horse in space" |
| 49 | +image = pipe(prompt, num_inference_steps=25).images[0] |
| 50 | +image.save("astronaut.png") |
| 51 | +``` |
| 52 | + |
| 53 | +- *Text-to-Image (768x768 resolution)*: |
| 54 | + |
| 55 | +```python |
| 56 | +from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler |
| 57 | +import torch |
| 58 | + |
| 59 | +repo_id = "stabilityai/stable-diffusion-2" |
| 60 | +pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision="fp16") |
| 61 | + |
| 62 | +pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) |
| 63 | +pipe = pipe.to("cuda") |
| 64 | + |
| 65 | +prompt = "High quality photo of an astronaut riding a horse in space" |
| 66 | +image = pipe(prompt, guidance_scale=9, num_inference_steps=25).images[0] |
| 67 | +image.save("astronaut.png") |
| 68 | +``` |
| 69 | + |
| 70 | +- *Image Inpainting (512x512 resolution)*: |
| 71 | + |
| 72 | +```python |
| 73 | +import PIL |
| 74 | +import requests |
| 75 | +import torch |
| 76 | +from io import BytesIO |
| 77 | + |
| 78 | +from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler |
| 79 | + |
| 80 | + |
| 81 | +def download_image(url): |
| 82 | + response = requests.get(url) |
| 83 | + return PIL.Image.open(BytesIO(response.content)).convert("RGB") |
| 84 | + |
| 85 | + |
| 86 | +img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" |
| 87 | +mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" |
| 88 | + |
| 89 | +init_image = download_image(img_url).resize((512, 512)) |
| 90 | +mask_image = download_image(mask_url).resize((512, 512)) |
| 91 | + |
| 92 | +repo_id = "stabilityai/stable-diffusion-2-inpainting" |
| 93 | +pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision="fp16") |
| 94 | + |
| 95 | +pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) |
| 96 | +pipe = pipe.to("cuda") |
| 97 | + |
| 98 | +prompt = "Face of a yellow cat, high resolution, sitting on a park bench" |
| 99 | +image = pipe(prompt=prompt, image=init_image, mask_image=mask_image, num_inference_steps=25).images[0] |
| 100 | + |
| 101 | +image.save("yellow_cat.png") |
| 102 | +``` |
| 103 | + |
| 104 | +- *Image Upscaling (x4 resolution resolution)*: [stable-diffusion-x4-upscaler](https://huggingface.co/stabilityai/stable-diffusion-x4-upscaler) [`StableDiffusionUpscalePipeline`] |
| 105 | + |
| 106 | +```python |
| 107 | +import requests |
| 108 | +from PIL import Image |
| 109 | +from io import BytesIO |
| 110 | +from diffusers import StableDiffusionUpscalePipeline |
| 111 | +import torch |
| 112 | + |
| 113 | +# load model and scheduler |
| 114 | +model_id = "stabilityai/stable-diffusion-x4-upscaler" |
| 115 | +pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, revision="fp16", torch_dtype=torch.float16) |
| 116 | +pipeline = pipeline.to("cuda") |
| 117 | + |
| 118 | +# let's download an image |
| 119 | +url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale/low_res_cat.png" |
| 120 | +response = requests.get(url) |
| 121 | +low_res_img = Image.open(BytesIO(response.content)).convert("RGB") |
| 122 | +low_res_img = low_res_img.resize((128, 128)) |
| 123 | +prompt = "a white cat" |
| 124 | +upscaled_image = pipeline(prompt=prompt, image=low_res_img).images[0] |
| 125 | +upscaled_image.save("upsampled_cat.png") |
| 126 | +``` |
| 127 | + |
| 128 | +### How to load and use different schedulers. |
| 129 | + |
| 130 | +The stable diffusion pipeline uses [`DDIMScheduler`] scheduler by default. But `diffusers` provides many other schedulers that can be used with the stable diffusion pipeline such as [`PNDMScheduler`], [`LMSDiscreteScheduler`], [`EulerDiscreteScheduler`], [`EulerAncestralDiscreteScheduler`] etc. |
| 131 | +To use a different scheduler, you can either change it via the [`ConfigMixin.from_config`] method or pass the `scheduler` argument to the `from_pretrained` method of the pipeline. For example, to use the [`EulerDiscreteScheduler`], you can do the following: |
| 132 | + |
| 133 | +```python |
| 134 | +>>> from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler |
| 135 | + |
| 136 | +>>> pipeline = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2") |
| 137 | +>>> pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config) |
| 138 | +
|
| 139 | +>>> # or |
| 140 | +>>> euler_scheduler = EulerDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-2", subfolder="scheduler") |
| 141 | +>>> pipeline = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2", scheduler=euler_scheduler) |
| 142 | +``` |
0 commit comments