text stringlengths 0 5.54k |
|---|
Using Diffusers with other modalities |
Diffusers is in the process of expanding to modalities other than images. |
Example type |
Colab |
Pipeline |
Molecule conformation generation |
❌ |
More coming soon! |
Token merging Token merging (ToMe) merges redundant tokens/patches progressively in the forward pass of a Transformer-based network which can speed-up the inference latency of StableDiffusionPipeline. Install ToMe from pip: Copied pip install tomesd You can use ToMe from the tomesd library with the apply_patch functi... |
import torch |
import tomesd |
pipeline = StableDiffusionPipeline.from_pretrained( |
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True, |
).to("cuda") |
+ tomesd.apply_patch(pipeline, ratio=0.5) |
image = pipeline("a photo of an astronaut riding a horse on mars").images[0] The apply_patch function exposes a number of arguments to help strike a balance between pipeline inference speed and the quality of the generated tokens. The most important argument is ratio which controls the number of tokens that are merge... |
- Python version: 3.8.16 |
- PyTorch version (GPU?): 1.13.1+cu116 (True) |
- Huggingface_hub version: 0.13.2 |
- Transformers version: 4.27.2 |
- Accelerate version: 0.18.0 |
- xFormers version: 0.0.16 |
- tomesd version: 0.1.2 To reproduce this benchmark, feel free to use this script. The results are reported in seconds, and where applicable we report the speed-up percentage over the vanilla pipeline when using ToMe and ToMe + xFormers. GPU Resolution Batch size Vanilla ToMe ToMe + xFormers A100 512 10 6.88 5.26 (+23.... |
Stable Diffusion XL Stable Diffusion XL (SDXL) is a powerful text-to-image generation model that iterates on the previous Stable Diffusion models in three key ways: the UNet is 3x larger and SDXL combines a second text encoder (OpenCLIP ViT-bigG/14) with the original text encoder to significantly increase th... |
#!pip install -q diffusers transformers accelerate invisible-watermark>=0.2.0 We recommend installing the invisible-watermark library to help identify images that are generated. If the invisible-watermark library is installed, it is used by default. To disable the watermarker: Copied pipeline = StableDiffusionXLPipel... |
import torch |
pipeline = StableDiffusionXLPipeline.from_pretrained( |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True |
).to("cuda") |
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained( |
"stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, use_safetensors=True, variant="fp16" |
).to("cuda") You can also use the from_single_file() method to load a model checkpoint stored in a single file format (.ckpt or .safetensors) from the Hub or locally: Copied from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline |
import torch |
pipeline = StableDiffusionXLPipeline.from_single_file( |
"https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0.safetensors", torch_dtype=torch.float16, variant="fp16", use_safetensors=True |
).to("cuda") |
refiner = StableDiffusionXLImg2ImgPipeline.from_single_file( |
"https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0/blob/main/sd_xl_refiner_1.0.safetensors", torch_dtype=torch.float16, use_safetensors=True, variant="fp16" |
).to("cuda") Text-to-image For text-to-image, pass a text prompt. By default, SDXL generates a 1024x1024 image for the best results. You can try setting the height and width parameters to 768x768 or 512x512, but anything below 512x512 is not likely to work. Copied from diffusers import AutoPipelineForText2Image |
import torch |
pipeline_text2image = AutoPipelineForText2Image.from_pretrained( |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True |
).to("cuda") |
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" |
image = pipeline_text2image(prompt=prompt).images[0] |
image Image-to-image For image-to-image, SDXL works especially well with image sizes between 768x768 and 1024x1024. Pass an initial image, and a text prompt to condition the image with: Copied from diffusers import AutoPipelineForImage2Image |
from diffusers.utils import load_image, make_image_grid |
# use from_pipe to avoid consuming additional memory when loading a checkpoint |
pipeline = AutoPipelineForImage2Image.from_pipe(pipeline_text2image).to("cuda") |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-text2img.png" |
init_image = load_image(url) |
prompt = "a dog catching a frisbee in the jungle" |
image = pipeline(prompt, image=init_image, strength=0.8, guidance_scale=10.5).images[0] |
make_image_grid([init_image, image], rows=1, cols=2) Inpainting For inpainting, you’ll need the original image and a mask of what you want to replace in the original image. Create a prompt to describe what you want to replace the masked area with. Copied from diffusers import AutoPipelineForInpainting |
from diffusers.utils import load_image, make_image_grid |
# use from_pipe to avoid consuming additional memory when loading a checkpoint |
pipeline = AutoPipelineForInpainting.from_pipe(pipeline_text2image).to("cuda") |
img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-text2img.png" |
mask_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-inpaint-mask.png" |
init_image = load_image(img_url) |
mask_image = load_image(mask_url) |
prompt = "A deep sea diver floating" |
image = pipeline(prompt=prompt, image=init_image, mask_image=mask_image, strength=0.85, guidance_scale=12.5).images[0] |
make_image_grid([init_image, mask_image, image], rows=1, cols=3) Refine image quality SDXL includes a refiner model specialized in denoising low-noise stage images to generate higher-quality images from the base model. There are two ways to use the refiner: use the base and refiner models together to produce a refine... |
import torch |
base = DiffusionPipeline.from_pretrained( |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True |
).to("cuda") |
refiner = DiffusionPipeline.from_pretrained( |
"stabilityai/stable-diffusion-xl-refiner-1.0", |
text_encoder_2=base.text_encoder_2, |
vae=base.vae, |
torch_dtype=torch.float16, |
use_safetensors=True, |
variant="fp16", |
).to("cuda") To use this approach, you need to define the number of timesteps for each model to run through their respective stages. For the base model, this is controlled by the denoising_end parameter and for the refiner model, it is controlled by the denoising_start parameter. The denoising_end and denoising_start p... |
image = base( |
prompt=prompt, |
num_inference_steps=40, |
denoising_end=0.8, |
output_type="latent", |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.