Diffusers documentation
Stable Diffusion 2
Stable Diffusion 2
Stable Diffusion 2 es un modelo de latent diffusion de texto a imagen construido sobre el trabajo del Stable Diffusion original, y fue liderado por Robin Rombach y Katherine Crowson de Stability AI y LAION.
La versión de Stable Diffusion 2.0 incluye modelos robustos de texto a imagen entrenados con un codificador de texto completamente nuevo (OpenCLIP), desarrollado por LAION con el apoyo de Stability AI, que mejora considerablemente la calidad de las imágenes generadas en comparación con las versiones anteriores V1. Los modelos de texto a imagen de esta versión pueden generar imágenes con resoluciones predeterminadas de 512x512 píxeles y 768x768 píxeles. Estos modelos se entrenan en un subconjunto estético del conjunto de datos LAION-5B creado por el equipo DeepFloyd de Stability AI, que luego se filtra para eliminar contenido para adultos mediante el filtro NSFW de LAION.
Para más detalles sobre cómo funciona Stable Diffusion 2 y en qué se diferencia de la versión original de Stable Diffusion, consulte el post de anuncio oficial.
La arquitectura de Stable Diffusion 2 es más o menos idéntica a la del modelo original Stable Diffusion, por lo que consulta su documentación de API para saber cómo usar Stable Diffusion 2. Recomendamos usar DPMSolverMultistepScheduler, ya que ofrece un equilibrio razonable entre velocidad y calidad y puede ejecutarse con tan solo 20 pasos.
Stable Diffusion 2 está disponible para tareas como texto a imagen, inpainting, superresolución y profundidad a imagen:
| Tarea | Repositorio |
|---|---|
| texto a imagen (512x512) | stabilityai/stable-diffusion-2-base |
| texto a imagen (768x768) | stabilityai/stable-diffusion-2 |
| inpainting | stabilityai/stable-diffusion-2-inpainting |
| super-resolución | stable-diffusion-x4-upscaler |
| depth-to-image | stabilityai/stable-diffusion-2-depth |
Aquí hay algunos ejemplos de cómo usar Stable Diffusion 2 para cada tarea:
Asegúrate de consultar la sección Consejos de Stable Diffusion para aprender a explorar el equilibrio entre la velocidad y la calidad del programador, y a reutilizar los componentes del pipeline de manera eficiente.
Si te interesa utilizar uno de los puntos de control oficiales para una tarea, explora las organizaciones CompVis y Stability AI en Hub.
Texto a imagen
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import torch
repo_id = "stabilityai/stable-diffusion-2-base"
pipe = DiffusionPipeline.from_pretrained(repo_id, dtype=torch.float16, variant="fp16")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda") # or "mps", "xpu", "cpu"
prompt = "High quality photo of an astronaut riding a horse in space"
image = pipe(prompt, num_inference_steps=25).images[0]
imageInpainting
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.utils import load_image, make_image_grid
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
init_image = load_image(img_url).resize((512, 512))
mask_image = load_image(mask_url).resize((512, 512))
repo_id = "stabilityai/stable-diffusion-2-inpainting"
pipe = DiffusionPipeline.from_pretrained(repo_id, dtype=torch.float16, variant="fp16")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda") # or "mps", "xpu", "cpu"
prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image, num_inference_steps=25).images[0]
make_image_grid([init_image, mask_image, image], rows=1, cols=3)Superresolución
from diffusers import StableDiffusionUpscalePipeline
from diffusers.utils import load_image, make_image_grid
import torch
# load model and scheduler
model_id = "stabilityai/stable-diffusion-x4-upscaler"
pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, dtype=torch.float16)
pipeline = pipeline.to("cuda") # or "mps", "xpu", "cpu"
# let's download an image
url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale/low_res_cat.png"
low_res_img = load_image(url)
low_res_img = low_res_img.resize((128, 128))
prompt = "a white cat"
upscaled_image = pipeline(prompt=prompt, image=low_res_img).images[0]
make_image_grid([low_res_img.resize((512, 512)), upscaled_image.resize((512, 512))], rows=1, cols=2)De profundidad a imagen
import torch
from diffusers import StableDiffusionDepth2ImgPipeline
from diffusers.utils import load_image, make_image_grid
pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-depth",
dtype=torch.float16,
).to("cuda") # or "mps", "xpu", "cpu"
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
init_image = load_image(url)
prompt = "two tigers"
negative_prompt = "bad, deformed, ugly, bad anotomy"
image = pipe(prompt=prompt, image=init_image, negative_prompt=negative_prompt, strength=0.7).images[0]
make_image_grid([init_image, image], rows=1, cols=2)