https://huggingface.co/docs/diffusers/tutorials/autopipelinehttps://huggingface.co/docs/diffusers/tutorials/autopipelineAutoPipeline会自动检测要使用的正确流程类,这样可以更轻松地加载与任务相对应的检查点,而无需知道具体的流程类名称。
1.choose an autopipeline for your task
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True
).to("cuda")
prompt = "peasant and dragon combat, wood cutting style, viking era, bevel with rune"
image = pipeline(prompt, num_inference_steps=25).images[0]
AutoPipelineForText2Image:
1.自动从model_index.json文件中检测到一个名为"stable-diffusion"的类。2.基于"stable-diffusion"类名加载相应的文本到图像的StableDiffusionPipeline。
from diffusers import AutoPipelineForImage2Image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16,
use_safetensors=True,
).to("cuda")
prompt = "a portrait of a dog wearing a pearl earring"
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/1665_Girl_with_a_Pearl_Earring.jpg/800px-1665_Girl_with_a_Pearl_Earring.jpg"
response = requests.get(url)
image = Image.open(BytesIO(response.content)).convert("RGB")
image.thumbnail((768, 768))
image = pipeline(prompt, image, num_inference_steps=200, strength=0.75, guidance_scale=10.5).images[0]