Skip to Content
PyTorch によるコーダーのための AI および ML
book

PyTorch によるコーダーのための AI および ML

by Laurence Moroney
July 2025
Intermediate to advanced
444 pages
7h 8m
Japanese
O'Reilly Media, Inc.
Content preview from PyTorch によるコーダーのための AI および ML

第20章. LoRAと拡散器を使った生成画像モデルの調整

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

第19章では、ディフューザーのアイデアと、ディフューザーテクニックでトレーニングされたモデルが、どのようにプロンプトに基づいて画像を生成できるかを探った。テキストベースのモデル(第16章で検討)と同様に、テキストから画像へのモデル 、特定のタスクのために微調整することができる。拡散モデルのアーキテクチャとそれを微調整する方法については、それだけで1冊の本が書けるほどなので、この章では、これらの概念を高いレベルで探求することにしよう。そのためのテクニックはいくつかあり、DreamBooth、テキスト反転、最近の低順位適応 (LoRA)などがある。この最後のテクニックを使えば、わずかなデータで、特定のテーマやスタイルに合わせてモデルをカスタマイズすることができる。

トランスフォーマーと同様に、ディフューザー Hugging Face ライブラリ は、ディフューザーの使用や微調整をできるだけ簡単に行えるように設計されている。そのために、使用可能なビルド済みスクリプトが含まれている。

ここでは、Misatoと呼ばれる架空のデジタルインフルエンサーのデータセットを作成し、LoRAとディフューザーを使用して、Stable Diffusion 2と呼ばれるテキストから画像へのモデルを微調整する完全なサンプルを見ていく。次に、テキストから画像への推論を実行し、美里の新しい画像を作成する方法を示す(図20-1参照)。

図20-1. LoRAで調整された安定拡散2の画像

ディフューザーを使ったLoRAのトレーニング

ディフューザーを使ってLoRAをトレーニングするには、 、以下のステップを実行する必要がある。まず、diffusersのソースコードを入手し、あらかじめ用意されているトレーニングスクリプトにアクセスできるようにする。次に、Stable Diffusionの微調整に使用できるデータセットを入手または作成する。その後、トレーニングスクリプトを実行してモデルの微調整を行い、その微調整をHugging Faceに公開し、LoRAレイヤーを適用したベースモデルに対して推論を実行する。これが終われば、図20-1のような画像を作成できるはずだ。それでは、これらの各ステップを見ていこう。

ディフューザーを入手する

LoRAを使い始めるには、 、まずディフューザーのソースコードをクローンして、トレーニングスクリプトを入手するのが一番だと発見した。

git-cloneして、そのディレクトリに移動し、現在の場所で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 .

Colabや他のホストされたノートブックを使用している場合は、以下のような構文を使用する。テキストから画像へのLoRA微調整スクリプトは、 ...

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

生成 Deep Learning ―絵を描き、物語や音楽を作り、ゲームをプレイする

生成 Deep Learning ―絵を描き、物語や音楽を作り、ゲームをプレイする

David Foster, 松田 晃一, 小沼 千絵
ダイナミックリチーミング 第2版 ―5つのパターンによる効果的なチーム編成

ダイナミックリチーミング 第2版 ―5つのパターンによる効果的なチーム編成

Heidi Helfand, 永瀬 美穂, 吉羽 龍太郎, 原田 騎郎, 細澤 あゆみ

Publisher Resources

ISBN: 9798341662636