Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lets Finetune Stable Diffusion
Search
payanotty
April 06, 2023
Technology
0
1.3k
Lets Finetune Stable Diffusion
payanotty
April 06, 2023
Tweet
Share
More Decks by payanotty
See All by payanotty
トークナイザー入門
payanotty
4
1.9k
LLM_Prompt_Recovery
payanotty
3
980
Embeddingモデルを使ったベクトル化のしくみ、fine-tuning手法を解説
payanotty
15
5.8k
Transformerによるテキストベクトル化を解説
payanotty
5
3.8k
Kaggle_LLMコンペの攻略法を解説.pdf
payanotty
1
1.5k
ManimMLでイケてるアニメーションを作ろう
payanotty
0
760
Lets Finetune LLM
payanotty
3
1.3k
Stable Diffusion Web UI, Let Your Fave Eat Ramen
payanotty
1
1.1k
Deffusion解説
payanotty
3
890
Other Decks in Technology
See All in Technology
ニッポンの人に知ってもらいたいGISスポット
sakaik
0
170
AWS Control Tower に学ぶ! IAM Identity Center 権限設計の第一歩 / IAM Identity Center with Control Tower
y___u
1
200
Digitization部 紹介資料
sansan33
PRO
1
5.6k
ガバメントクラウド(AWS)へのデータ移行戦略の立て方【虎の巻】 / 20251011 Mitsutosi Matsuo
shift_evolve
PRO
2
200
OAuthからOIDCへ ― 認可の仕組みが認証に拡張されるまで
yamatai1212
0
140
なぜAWSを活かしきれないのか?技術と組織への処方箋
nrinetcom
PRO
5
980
Introdução a Service Mesh usando o Istio
aeciopires
0
210
All About Sansan – for New Global Engineers
sansan33
PRO
1
1.2k
Introduction to Sansan Meishi Maker Development Engineer
sansan33
PRO
0
310
プレーリーカードを活用しよう❗❗デジタル名刺交換からはじまるイベント会場交流のススメ
tsukaman
0
180
能登半島災害現場エンジニアクロストーク 【JAWS FESTA 2025 in 金沢】
ditccsugii
0
900
AIツールでどこまでデザインを忠実に実装できるのか
oikon48
6
3.5k
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
GraphQLとの向き合い方2022年版
quramy
49
14k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
YesSQL, Process and Tooling at Scale
rocio
173
14k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
115
20k
Speed Design
sergeychernyshev
32
1.2k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Transcript
Stable Diffusionで 自分だけの お絵描きAIを作ろう 早野康太
自己紹介 • 名前 ◦ 早野 康太 • お仕事 ◦ 自然言語モデルの改善
• 春アニメおすすめ ◦ 僕ヤバ ◦ 水星の魔女 第2期 • 映画 ◦ BLUE GIANT → 神でした (原作一気買い)
Stable Diffusionとは • 文章を与えると画像を生成してくれるモデル • 2022年にStability AIが公開 ◦ Google Colaboratoryを使えばサクッと試せる
▪ https://huggingface.co/stabilityai/stable-diffusion-2
Stable Diffusionとは • モデルを誰でも利用できるため Stable Diffusionを使ったサービスや派生モデルの公開など さかんに行われている ◦ 画像生成bot ▪
Line, Discordなど ◦ 派生モデル ▪ NovelAI Diffusion ▪ Waifu Diffusion ▪ ほか、Huggingfaceで検索したら無限に出てくる • Models - Hugging Face
Stable Diffusionとは • Stable Diffusion ◦ 誰でも利用できる → 自分だけのオリジナルモデルを作成可能 ◦
lambdalabs/sd-pokemon-diffusers · Hugging Face
今回の発表では • Stable Diffusionをfine-tuneして オリジナルモデルを作成するデモを Google Colaboratoryで実践します ◦ データセットの作成→学習→モデル保存&ロードまでを ノートブックで完結させます
▪ 学習用notebook
前提とする知識 • PyTorch ◦ 今日お見せする学習コードはPyTorchで書かれています ◦ 最低限PyTorchで簡単なNN学習を実装できる程度の知識は必要です • 機械学習 ◦
深層学習の基本的な用語の意味については知っている前提で 進めます
アジェンダ • データセットの作成 ◦ 画像の収集 ◦ キャプション付け ▪ BLIPを利用して自動でキャプションをつけよう •
学習 ◦ Stable Diffusionの学習のやり方 ◦ 学習コードの解説 • 推論 ◦ 作ったモデルで画像を生成してみる
データセットの作成 • データセットの構成 ◦ 画像とキャプションが1対1に対応したもの 出典:ぼっち・ざ・ろっく! CloverWorks、アニプレックス、芳文社、 ©はまじあき/芳文社・アニプレックス キュビズムみたいな作画になった 後藤ひとり
画像 キャプション ×N
データセットの作成 • 画像収集 ◦ Safebooru (画像投稿サイト) のAPIを利用して収集 ▪ “umamusume”タグで絞り込んで15,000枚 •
キャプション付け ◦ BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation ▪ 2022年1月に論文で発表されたモデル ▪ 画像を入力として その画像に即したキャプションを出力できる
データセットの作成 image file caption path/to/image1.png a group of cartoon
animals sitting next to ... path/to/image2.png an anime girl with long hair and a bow in her ... path/to/image3.png a couple of anime girls laying on top of a bed csv 画像ファイル
Stable Diffusionの学習 VAE VAE Denoising Autoencoder (UNET) Text Encoder input
text 出典:ぼっち・ざ・ろっく! CloverWorks、アニプレックス、芳文社、 ©はまじあき/芳文社・アニプレックス
Stable Diffusionの学習 VAE VAE Denoising Autoencoder (UNET) Text Encoder input
text 今回はここだけ 学習する 出典:ぼっち・ざ・ろっく! CloverWorks、アニプレックス、芳文社、 ©はまじあき/芳文社・アニプレックス
Stable Diffusionの学習 x 0 x 1 = x
0 + ε 1 x 2 = x 1 + ε 2 x 3 = x 2 + ε 3 z 1 = x 0 - ε θ (z 0 , 0) z 2 = z 1 - ε θ (z 1 , 1) z 3 = z 2 - ε θ (z 2 , 2) 出典:ぼっち・ざ・ろっく! CloverWorks、アニプレックス、芳文社、 ©はまじあき/芳文社・アニプレックス z 0
Stable Diffusionの学習 x 0 x 1 = x
0 + ε 1 x 2 = x 1 + ε 2 x 3 = x 2 + ε 3 z 1 = x 0 - ε θ (z 0 , 0) z 2 = z 1 - ε θ (z 1 , 1) z 3 = z 2 - ε θ (z 2 , 2) 出典:ぼっち・ざ・ろっく! CloverWorks、アニプレックス、芳文社、 ©はまじあき/芳文社・アニプレックス z 0 各タイムステップにおいて 付加したノイズをターゲットとして ロスを計算する
Stable Diffusionの学習 raw pixel value VAE latent noisy
latent + noise input text Text Encoder embedding UNET prediction loss velocity
Stable Diffusionの学習 raw pixel value VAE latent noisy
latent + noise input text Text Encoder embedding UNET prediction loss velocity 前処理
余談: velocityってなんぞ? x : 画像の値 ε : ノイズの値
z = αx + σε • Stable Diffusion 1.0 ◦ εがターゲット • Stable Diffusion 2.0 ◦ vがターゲット ※理屈がわかっていないため深入りしません α, σ : ノイズ付与のハイパラ (タイムステップごとに変動) v = dz / dφ Progressive Distillation for Fast Sampling of Diffusion Models
学習してみた結果 • 作りたかったモデル ◦ ウマ娘風の絵柄でイラストを生成するAI (3期も始まるので備えたい) • データセット
◦ 画像 ▪ Safebooru APIを使い”umamusume”タグで 収集した画像(10,000 - 15,000枚) ◦ キャプション ▪ 収集した画像をもとにBLIPで生成 • モデル ◦ backbone ▪ stabilityai/stable-diffusion-2
素のStable Diffusion • プロンプト ◦ “a girl in a
maid outfit holding a teapot”
UMA Diffusion • プロンプト ◦ “a girl in a
maid outfit holding a teapot” • 画像数: 10,000 • ハイパラ ◦ epochs: 30 ◦ batch_size: 4 ◦ lr: 5e-6 ◦ lr_scheduler: cosine ◦ warmup_ratio: 0.06 ◦ resolution: 512x512 ◦ transforms ▪ RandomCrop
UMA Diffusion • プロンプト ◦ “a girl in a
maid outfit holding a teapot” • 画像数: 15,000 • ハイパラ ◦ epochs: 30 ◦ batch_size: 4 ◦ lr: 5e-6 ◦ lr_scheduler: cosine ◦ warmup_ratio: 0.06 ◦ resolution: 512x512 ◦ transforms ▪ RandomCrop
UMA Diffusion • プロンプト ◦ “a girl in a
maid outfit holding a teapot” • 画像数: 10,000 • ハイパラ ◦ epochs: 30 ◦ batch_size: 4 ◦ lr: 5e-6 ◦ lr_scheduler: cosine ◦ warmup_ratio: 0.06 ◦ resolution: 512x384 ◦ transforms ▪ RandomCrop
UMA Diffusion • プロンプト ◦ “a girl in a
maid outfit holding a teapot” • 画像数: 15,000 • ハイパラ ◦ epochs: 30 ◦ batch_size: 4 ◦ lr: 5e-6 ◦ lr_scheduler: cosine ◦ warmup_ratio: 0.06 ◦ resolution: 512x384 ◦ transforms ▪ RandomCrop
finetune方法発展 • 今回はシンプルなfinetuneを紹介したが さまざまなfinetune手法が提案されている ◦ Text Inversion ▪ An
Image is Worth One Word: Personalizing Text-to-Image Generation using Textual Inversion ◦ DreamBooth ▪ DreamBooth: Fine Tuning Text-to-Image Diffusion Models for Subject-Driven Generation ◦ Hypernetworks ▪ NovelAI Improvements on Stable Diffusion ◦ LoRA ▪ GitHub - cloneofsimo/lora: Using Low-rank adaptation to quickly fine-tune diffusion models.
やってみた所感 • DNNの学習を触ったことがあるなら 学習コードを回すこと自体は全然難しくない ◦ 計算資源の確保が大変 (GPUメモリ16GB以上推奨) • 10,000枚程度の画像でもある程度のクオリティの
モデルを学習できた ◦ とはいえ安定したクオリティの出力は難しい ◦ 特に手の作画は崩れてしまう • よりクオリティを追求しようとするなら 発展的な手法を使ったfinetuneを試したいところ (いずれ挑戦したい)