![見出し画像](https://assets.st-note.com/production/uploads/images/106368414/rectangle_large_type_2_f50ad5f86de528ef903fe57301cc5d4b.png?width=800)
Stable Diffusion + ControlNetをgoogle colabへズバッと構築するコピペコード
ControlNetを使ったAI画像生成をやりたくてネット上の記事を読み漁ったのですが、必要な情報がバラバラで手順がまとまっておらず、環境構築に手間取ってしまったのでここにまとめておきます。
記事が面白かったらフォロー&♥よろしくお願いしますm(_ _)m
環境構築手順
環境構築に必要な材料は下記。
この構築をスマートに行う手順は次の通り。
google colab proに課金(1,179円/月)
必須。GPUを使わないとまともに動作しないのでまずは課金。注意すべきは課金した所で使い放題ではないという事。100ユニット/1,179円/月で毎月自動で継続購入するイメージ。
つまり使い切ったら100ユニット/1,179円で追加購入が必要。
google colab pro+(5,767円/月)という最上位モデルもあるが、こっちは500ユニットを分毎月購入するだけ。
選択するGPUのスペックに応じて消費するユニット数の速度が異なる。良いやつを使うと多くユニットを消費し、しょぼいやつはユニット数の消費が抑えられる。(A100 > V100 > T4)
GPUの変更は「ランタイム > ランタイムのタイプを変更」からできる。
![](https://assets.st-note.com/img/1684899924494-mP05UD4KSQ.png?width=800)
![](https://assets.st-note.com/img/1684899755589-rpCnx0mgnG.png?width=800)
T4は2.05ユニット/時間
![](https://assets.st-note.com/img/1684899761286-2uVYbqg4Gs.png?width=800)
V100は5.45ユニット/時間
![](https://assets.st-note.com/img/1684899774013-TOYdd6X8BN.png?width=800)
A100は13.08ユニット/時間
![](https://assets.st-note.com/img/1684899765599-F2u5tAgoLh.png?width=800)
お試しやら設定を弄る時なんかはT4にしつつ、生成する設定やプロンプトが固まったらA100を使うみたいな運用をするとユニット数の節約になるかなと思います。
Google Oneに課金(250円/月)
デフォルトのgoogleアカウントだと15GBまで保存できるけど、Stable Diffusionの生成モデルが1個で4GBとかのサイズ感なので、一番安いプランを課金して100GBにアップグレードしておくのをオススメします。
容量をカツカツに節約すれば課金しなくても画像生成はできます。足りなくなったら課金でいいかも。
google colabに構築
すぐに使いたい方は下記の共有版をどうぞ。
自前で構築したい方は下記をコピペ。
#ライブラリインポート
%pip install torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchtext torchaudio torchdata==0.6.0 --index-url https://download.pytorch.org/whl/cu118
#webUIの本体ダウンロード
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
#プラグインの格納場所へ移動
%cd /content/stable-diffusion-webui/extensions
#controlnetプラグインインストール
!git clone https://github.com/Mikubill/sd-webui-controlnet
#生成モデルのインストール(自分で使うモデルをインストールして下さい)
#/content/stable-diffusion-webui/models/Stable-diffusion/に置く
!wget https://huggingface.co/nuigurumi/basil_mix/resolve/main/Basil_mix_fixed.safetensors -O /content/stable-diffusion-webui/models/Stable-diffusion/Basil_mix_fixed.safetensors
!wget https://huggingface.co/BanKaiPls/AsianModel/resolve/main/BraV4.safetensors -O /content/stable-diffusion-webui/models/Stable-diffusion/BraV4.safetensors
!wget https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors -O /content/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors
#controlnetモデルのダウンロード
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_canny-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_canny-fp16.safetensors
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_depth-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_depth-fp16.safetensors
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_hed-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_hed-fp16.safetensors
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_mlsd-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_mlsd-fp16.safetensors
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_normal-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_normal-fp16.safetensors
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_openpose-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_openpose-fp16.safetensors
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_scribble-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_scribble-fp16.safetensors
!wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_seg-fp16.safetensors -O /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models/control_seg-fp16.safetensors
#webUI本体へ移動
%cd /content/stable-diffusion-webui
!python launch.py --share --xformers --enable-insecure-extension-access
上から順番に実行していけばセットアップ完了です。Stable Diffusion Web Uiが起動するのでlaunch.pyの実行結果からRunning on public URLのリンクをクリック。
![](https://assets.st-note.com/img/1684901193582-XUiq8mDYlJ.png?width=800)
Web Uiが開きます、ControlNetと各モデルがインストール済みなのを確認して後は画像生成をエンジョイして下さい。
![](https://assets.st-note.com/img/1684901345057-RqGpYlcfpW.png?width=800)
この記事が面白かったら♡・フォローよろしくお願いしますm(_ _)m
下記のメンバーシップでは日々の活動報告・裏話なんかも掲示板で更新しています、一部の有料記事も読めちゃって初月無料ですので良かったらお試しを!
よろしければサポートお願いします、頂いたサポートは活動費として使用させて頂きより有意義な記事を書けるように頑張ります!