今回は2次元キャラクターに特化したモデルWaifu Diffusionを、イラスト描画AIであるStable Diffusionの最新バージョン2.xで動作させる方法を解説します。
ローカル環境やGoogle ColabでPythonコードから使用できますので、ぜひトライしてみてください。
また、StableDiffusionに関連する記事を以下のページでまとめていますので、あわせてご覧ください。

以下のページで自然言語処理モデルGPT-3を使ったライティングツールCatchyで、Stable DiffusionのPromptを自動生成する方法について解説していますので、あわせてご覧ください。

Stable Diffusionとは
Stable Diffusion(ステーブル・ディフュージョン)は2022年8月に無償公開された描画AIです。ユーザーがテキストでキーワードを指定することで、それに応じた画像が自動生成される仕組みとなっています。
NVIDIAのGPUを搭載していれば、ユーザ自身でStable Diffusionをインストールし、ローカル環境で実行することも可能です。
(出典:wikipedia)
2023年1月現在、Stable DiffusionはVersion 2.x系がリリースされていますので、今回はStable Diffusion 2.0を使用します。Stable Diffusion 2.0の使い方については、以下のページでも解説しています。



Waifu Diffusionとは
ハイクオリティなイラストを生成できるお絵かきAI、Stable Diffusionをさらに2次元イラストに特化させるためのチューニングが行われたモデルがWaifu Diffusionです。
2023年1月現在のWaifu Diffusionのバージョンはv1.4となります。
Waifu Diffuisonのリポジトリは以下となります。今回はこのモデルを使って2次元キャラクターを描かせてみようと思います。

StableDiffusion 2.xのセットアップ
以下のコマンドを実行してパッケージをインストールします。
pip install diffusers[torch]==0.9 transformers
pip install --upgrade --pre triton
StableDiffusionのリポジトリを取得してインストールします。
pip install --upgrade git+https://github.com/huggingface/diffusers.git transformers accelerate scipy
以上でStableDiffusion 2.0のセットアップは完了です。
作成したソースコード
Waifu Diffusionのモデルを指定
Waifu Diffusionを使用する方法は、モデルの指定箇所を変更するだけです。
model_id = "hakurei/waifu-diffusion" #waifu diffusionのモデルを指定
作成した全体のソースコード
作成した全体のソースコードは以下の通りです。
モデルIDの指定箇所以外は通常のStable Diffusion 2.0と同じです。
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
import torch
from datetime import datetime
#model_id = "stabilityai/stable-diffusion-2"
model_id = "hakurei/waifu-diffusion" #waifu diffusionのモデルを指定
# Use the Euler scheduler here instead
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, revision="fp16", torch_dtype=torch.float16)
pipe = pipe.to("cuda") #GPUがない場合は「cpu」を指定
#画像生成の指示(呪文)
prompt = "(イラストに対する指示を設定)"
#描画する回数を設定
num_images = 5
#イラスト生成
for i in range(num_images):
#推論実行
image = pipe(prompt, height=768, width=768).images[0]
#生成日時をファイル名にして保存
date = datetime.now().strftime("%Y%m%d_%H%M%S")
path = date + ".png"
image.save(path)
実行結果
今回は動作確認のため、chiliさんがTwitterで公開されているPrompt(呪文)をお借りしました。
(画像の「ALT」をクリックするとPromptが確認できます)
お借りしたPromptで先ほどのPythonプログラムを実行したところ以下のような画像が生成されました。

以下のページで自然言語処理モデルGPT-3を使ったライティングツールCatchyで、Stable DiffusionのPromptを自動生成する方法について解説していますので、あわせてご覧ください。

Stable Diffusionのテクニックを効率よく学ぶには?

Stable Diffusionを使ってみたいけど、ネットで調べた情報を試してもうまくいかない…



そんな時は、操作方法の説明が動画で見られるUdemyがおすすめだよ!
動画学習プラットフォームUdemyでは、画像生成AIで高品質なイラストを生成する方法や、AIの内部で使われているアルゴリズムについて学べる講座が用意されています。
Udemyは講座単体で購入できるため安価で(セール時1500円くらいから購入できます)、PCが無くてもスマホでいつでもどこでも手軽に学習できます。
Stable Diffusionに特化して学ぶ
Stable Diffusionに特化し、クラウドコンピューティングサービスPaperspaceでの環境構築方法から、モデルのマージ方法、ControlNetを使った構図のコントロールなど、中級者以上のレベルを目指したい方に最適な講座です。


画像生成AIの仕組みを学ぶ
画像生成AIの仕組みについて学びたい方には、以下の講座がおすすめです。
画像生成AIで使用される変分オートエンコーダやGANのアーキテクチャを理解することで、よりクオリティの高いイラストを生成することができます。


まとめ
Stable Diffusion 2.xでもWaifu Diffusionを使ってイラストを生成することができました。
現在Stable Diffusion 1.xは動作しなくなってしまっているようですので、2.x系で活用してみてください。
以下のページで自然言語処理モデルGPT-3を使ったライティングツールCatchyで、Stable DiffusionのPromptを自動生成する方法について解説していますので、あわせてご覧ください。


また、以下の記事で効率的にPythonのプログラミングスキルを学べるプログラミングスクールの選び方について解説しています。最近ではほとんどのスクールがオンラインで授業を受けられるようになり、仕事をしながらでも自宅で自分のペースで学習できるようになりました。
スキルアップや副業にぜひ活用してみてください。


スクールではなく、自分でPythonを習得したい方には、いつでもどこでも学べる動画学習プラットフォームのUdemyがおすすめです。
講座単位で購入できるため、スクールに比べ非常に安価(セール時1200円程度~)に学ぶことができます。私も受講しているおすすめの講座を以下の記事でまとめていますので、ぜひ参考にしてみてください。


それでは、また次の記事でお会いしましょう。






コメント