要将LCM用于图像到图像,需要将支持的LCM模型的Checkpoint加载到[UNet2DConditionModel]中,并用[LCM scheduler]替换scheduler 程序。然后,可以像往常一样使用管道,并传递文本提示和初始图像,只需4个步骤即可生成图像。
# 以下代码为程序运行进行设置
import os os.environ["HF_ENDPOINT"] = "https://hf-mirror.com" mport torch from diffusers import AutoPipelineForImage2Image, UNet2DConditionModel, LCMScheduler from diffusers.utils import load_image
# 加载支持LCM模型的Checkpoint
unet = UNet2DConditionModel.from_pretrained(
"SimianLuo/LCM_Dreamshaper_v7",
subfolder="unet",
torch_dtype=torch.float16,
)
pipe = AutoPipelineForImage2Image.from_pretrained(
"Lykon/dreamshaper-7",
unet=unet,
torch_dtype=torch.float16,
variant="fp16",
).to("cuda")
# 并用[LCM scheduler]替换scheduler 程序
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
# 加载原始图片
init_image = load_image(
"https://hf-mirror.com/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png")
prompt = "Astronauts in a jungle, cold color palette, muted colors, detailed, 8k"
generator = torch.manual_seed(0)
image = pipe(
prompt,
image=init_image,
num_inference_steps=4,
guidance_scale=7.5,
strength=0.5,
generator=generator
).images[0]
image.show()
以下为原始图片
以下为LCM图生图的输出
标签:图生,image,torch,Diffusers,pipe,unet,scheduler,LCM From: https://blog.csdn.net/duhaining1976/article/details/139740822