diffuxers学习--AutoPipeline

1. AutoPipeline 是什么?

diffusers 库提供了许多用于基本任务的管道(Pipeline),如生成图像、视频、音频和修复。除此之外,还有专门的管道用于适配器和功能,如放大、超分辨率等。

不同的管道类甚至可以使用相同的检查点(Checkpoint),因为它们共享相同的预训练模型组件。由于有如此多的管道,选择使用哪个管道类可能会让人感到不知所措。

为了解决这个问题,diffusers 提供了 AutoPipeline

AutoPipeline 让你只需要关心你要完成的任务类型 (例如:文生图、图生图、图像修复等),而不需要去记住和选择具体的 DiffusionPipeline 实现类。它会自动根据你加载的模型和传入的参数来选择最合适的管道。

2. 代码示例

示例 1: 文生图 (Text-to-Image) 与图生图 (Image-to-Image)

这个示例展示了如何使用 AutoPipeline 先进行文生图,然后利用已加载的模型高效地切换到图生图任务。

python 复制代码
import torch
from diffusers import AutoPipelineForText2Image,AutoPipelineForImage2Image,AutoPipelineForInpainting
from diffusers.utils import load_image
device="cuda"

# Diffusers 提供了许多用于基本任务的管道,如生成图像、视频、音频和修复。
# 除此之外,还有专门的管道用于适配器和功能,如放大、超分辨率等。
# 不同的管道类甚至可以使用相同的检查点,因为它们共享相同的预训练模型!由于有如此多的管道,选择使用哪个管道类可能会让人感到不知所措。
# 所以有了AutoPipeline 
# AutoPipeline 让你知道是什么任务就行 (文生图,图生图等),而不需要去记住DiffusionPipeline 这些实际的类
def demo1(): # 使用文生图的autopipeline和图生图的autopipeline
    pipeline=AutoPipelineForText2Image.from_pretrained(
         "dreamlike-art/dreamlike-photoreal-2.0",
         torch_dtype=torch.float16,
         use_safetensors=True
    ).to(device)
    prompt="cinematic photo of Godzilla eating sushi with a cat in a izakaya, 35mm photograph, film, professional, 4k, highly detailed"
    gen_seed=torch.Generator(device=device).manual_seed(37)
    image=pipeline(prompt,generator=gen_seed).images[0]
    image.save('./test.png')
    
    # 图生图autopipeline
    # 使用from_pipe而不是from_pretrained,因为checkpoint都是同一个,这样可以重用已加载到内存中的模型组件,避免重复下载和加载,效率更高。
    # 这种方法之所以可行,是因为像Stable Diffusion这样的基础模型本身就同时支持文生图和图生图两种任务。
    pipeline2=AutoPipelineForImage2Image.from_pipe(pipeline).to(device)
    init_image=load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/autopipeline-text2img.png")
    prompt2 = "cinematic photo of Godzilla eating burgers with a cat in a fast food restaurant, 35mm photograph, film, professional, 4k, highly detailed"
    gen_seed2=torch.Generator(device=device).manual_seed(53)
    image2=pipeline2(prompt2,image=init_image,generator=gen_seed2).images[0]
    image2.save("./test2.png")

示例 2: 图像重绘 (Inpainting)

这个示例展示了如何使用 AutoPipelineForInpainting 来修复或替换图像的特定部分。

python 复制代码
def demo2(): # 使用重绘的autopipeline
    pipeline=AutoPipelineForInpainting.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        torch_dtype=torch.float16,
        use_safetensors=True
    ).to(device)
    init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/autopipeline-img2img.png")
    mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/autopipeline-mask.png") # 重绘需要指明重绘的部分(遮罩)
    prompt = "cinematic photo of a owl, 35mm photograph, film, professional, 4k, highly detailed"
    
    # 修正:将 Generator 的设备与 pipeline 的设备保持一致
    generator = torch.Generator(device=device).manual_seed(38)   
    
    image=pipeline(prompt,image=init_image,mask_image=mask_image,generator=generator, strength=0.4).images[0]
    image.save("./test.png")
相关推荐
AI棒棒牛7 分钟前
YOLO26最新创新改进系列:融合 MVIGGraphFusionBlock,构建互视信息图注意力 Neck,嘎嘎创新!
人工智能·计算机视觉·yolo26
底层信号8 分钟前
沙箱管得住 Agent 的手,管不住它的目标 —— 从 OpenAI 7 月逃逸事件看 Agent 安全的真正瓶颈
人工智能·安全
桃西西呀9 分钟前
同一个模型 30% 到 100%?拆解 Harness 工程的 5 个机制,附 8 个坑的自检清单
人工智能·llm·ai编程
undsky9 分钟前
连买域名的钱都省了!将 ClawEmail 打造成临时邮箱
人工智能
AI视觉网奇15 分钟前
3d拆件 SegviGen 部署踩坑笔记
人工智能
吴佳浩17 分钟前
为什么现在越来越多的开源模型,都“毕业“于 Qwen?
人工智能·llm·ai编程
2601_9622937922 分钟前
【Python教程:自动化处理文件】
python·自动化·编程·文件处理·os模块
禹凕23 分钟前
快速幂算法详解与实战
python·算法
xxwl58527 分钟前
高斯消元法异或版
人工智能·算法·机器学习
嘿嘿-6630 分钟前
gpt-6-astra测试,测试你的模型有没有降智
人工智能·gpt·chatgpt·web