Skip to Content
PyTorch 中的 AI 和 ML 编码
book

PyTorch 中的 AI 和 ML 编码

by Laurence Moroney
July 2025
Beginner to intermediate
444 pages
6h 20m
Chinese
O'Reilly Media, Inc.
Content preview from PyTorch 中的 AI 和 ML 编码

第 20 章 利用 LoRA 和扩散器调整生成图像模型

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

第 19 章中,我们探讨了扩散器的概念,以及使用扩散技术训练的模型如何根据提示生成图像。与基于文本的模型(我们在第 16 章中探讨过)一样,文本到图像模型 也可以针对特定任务进行微调。关于扩散模型的结构以及如何对其进行微调,本身就足以写成一本完整的书,因此在本章中,我们将只是在高层次上探讨这些概念。有几种技术可以做到这一点,包括DreamBooth、文本反转和最新的低排序自适应 (LoRA),你将在本章中逐步了解这些技术。最后一种技术可以让您用很少的数据为特定主题或风格定制模型。

与变换器一样,漫反射拥抱脸部库 也是为了让漫反射的使用和微调变得尽可能简单。为此,它包含了可供您使用的预置脚本。

我们将通过一个完整的示例,创建一个名为美里(Misato)的虚构数字影响者的数据集,使用 LoRA 和扩散器为她微调一个名为稳定扩散 2 的文本到图像模型。然后,我们将执行文本到图像推理,演示如何创建美里的新图像(见图 20-1)。

图 20-1. 经过 LoRA 调整的稳定扩散 2 图像

使用扩散器训练 LoRA

要使用扩散器训练 LoRA, ,您需要执行以下步骤。首先,您需要获取扩散器的源代码,以便访问其预制的训练脚本。然后,获取或创建一个数据集,用于微调 Stable Diffusion。然后,运行训练脚本对模型进行微调,将微调结果发布到 Hugging Face,并在应用 LoRA 层的基础模型上运行推理。完成后,您就可以创建如图 20-1 所示的图像了。下面我们逐一介绍这些步骤。

获取扩散器

要开始使用 LoRA, ,我发现最好的办法是首先克隆扩散器的源代码以获取训练脚本。

要做到这一点非常简单,只需克隆 Git,进入目录,然后在当前位置运行pip install 即可:

git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .

如果您使用的是 Colab 或其他托管笔记本,则需要使用类似的语法:

!git clone https://github.com/huggingface/diffusers
%cd diffusers
!pip install .

这将为您提供一个本地版本的 diffusers,供您使用。文本到图像 LoRA 微调脚本位于/diffusers/examples/text_too_image目录中,你需要像这样安装它们的依赖项:

%cd /content/diffusers/examples/text_to_image # or whatever your dir is
!pip install -r requirements.txt

这些依赖项包括 accelerate、transformers 和 torchvision 等工具的特定版本。最好从源代码 Git 克隆,这样你就能获得最新版本的requirements.txt,让你的生活更轻松!

最后,你还需要 xformers 库,它旨在提高转换器的效率,从而加快转换过程。你可以这样获取它:

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

在企业中实施 MLOps

在企业中实施 MLOps

Yaron Haviv, Noah Gift
图解大模型 : 生成式AI 原理与实战

图解大模型 : 生成式AI 原理与实战

Jay Alammar, Maarten Grootendorst

Publisher Resources

ISBN: 9798341662599