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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
payanotty
April 06, 2023
Technology
1.3k
0
Share
Lets Finetune Stable Diffusion
payanotty
April 06, 2023
More Decks by payanotty
See All by payanotty
トークナイザー入門
payanotty
5
2.4k
LLM_Prompt_Recovery
payanotty
3
1k
Embeddingモデルを使ったベクトル化のしくみ、fine-tuning手法を解説
payanotty
15
6.3k
Transformerによるテキストベクトル化を解説
payanotty
6
4.2k
Kaggle_LLMコンペの攻略法を解説.pdf
payanotty
1
1.6k
ManimMLでイケてるアニメーションを作ろう
payanotty
0
830
Lets Finetune LLM
payanotty
3
1.4k
Stable Diffusion Web UI, Let Your Fave Eat Ramen
payanotty
1
1.2k
Deffusion解説
payanotty
3
980
Other Decks in Technology
See All in Technology
AI와 협업하는 조직으로의 여정
arawn
0
590
小さいVue.jsを30分で作る
hal_spidernight
0
140
毎日の作業を Claude Code 経由にしたら、 ノウハウがコードになった
kossykinto
0
770
インターネットのガバナンスと応用 / Internet Governance and Applications
ks91
PRO
0
110
AIの揺らぎに“コシ”を与える階層化品質設計
ickx
0
230
M5Stack CoreS3とZephyr(RTOS)で Edge AIっぽいことしてみた
iotengineer22
0
430
AgentCore Managed Harness を使ってみよう
yakumo
2
320
雑談は、センサーだった
bitkey
PRO
2
200
知ってた?JavaScriptの"正しさ"を検証するテストが5万以上もあること(Test262)
riyaamemiya
1
150
ブラウザの投機的読み込みと投機ルールAPIを理解し、Webサービスのパフォーマンスを最適化する
shuta13
3
280
国内外の生成AIセキュリティの最新動向 & AIガードレール製品「chakoshi」のご紹介 / Latest Trends in Generative AI Security (Domestic & International) & Introduction to AI Guardrail Product "chakoshi"
nttcom
4
1.8k
Oracle Cloud Infrastructure:2026年4月度サービス・アップデート
oracle4engineer
PRO
0
340
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.5k
Docker and Python
trallard
47
3.8k
4 Signs Your Business is Dying
shpigford
187
22k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
290
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
270
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
170
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
230
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
110
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を試したいところ (いずれ挑戦したい)