![見出し画像](https://assets.st-note.com/production/uploads/images/107221257/rectangle_large_type_2_175a341d9dac2b507442b72005b9e3ce.png?width=1200)
【超簡単】Colaboratoryで画像生成の方法【コピペでOK】
おはようございます。
noteを書き始めて6か月ほどたった今、noteの機能にコードを挿入できるのを知ったので改めて更新しようと思います。
コラボラトリーのノートブックにコードをコピペするだけで遊べます。
コードの更新履歴
※23年10月22日
Colab環境のPyTorchが2.1.0にアップデートされいて画像生成できなくなったので、Colab側のPyTorchにxformersをあわせる(0.0.22.post4+cu118)コードを追記
※23年8月
コラボラトリーの無料だと5分程度で接続が切れてしまうため快適に遊ぶことはできません。
もし本格的に遊んでみたいな!って方は配信に来て「noteみて来ました!遊ばせてください!」って言えば作ってみたい画像を指示通りにプロンプトに記入して結果をお見せできますのでよければ遊びに来てください。
Youtubeチャンネル
→https://www.youtube.com/channel/UC6IB7yFWLS5ZBucQnSHX7xA
ここからがコピペ部分
黒い部分をコラボラトリーにコピペしてもらえば画像生成ができます。
簡単なコードの解説もありますが読まなくても大丈夫です。
Colab側のPyTorchにxformersをあわせる
!pip install https://download.pytorch.org/whl/cu118/xformers-0.0.22.post4%2Bcu118-cp310-cp310-manylinux2014_x86_64.whl
AUTOMATIC1111のインストール
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui /content/stable-diffusion-webui
%cd /content/stable-diffusion-webui
LoRAフォルダを作成&DL
#LoRAファイルを格納するフォルダを作成
!mkdir -p /content/stable-diffusion-webui/models/Lora
#フラット1
%cd /content/stable-diffusion-webui/models/Lora/
!wget https://huggingface.co/2vXpSwA7/iroiro-lora/resolve/main/release/flat1.safetensors -O /content/stable-diffusion-webui/models/Lora/flat1.safetensors
%cd /content/stable-diffusion-webui
あとは、使いたいLoraを自分で探して上記のコードを手直ししてください。
変更する部分は!wgetから始まるコードの中の3か所
1.使いたいLoraのURL
2.Loraのファイル名
(スペース、ドットが拡張子以外にもあれば修正してください)
3.Loraファイルの拡張子(.safetensorsや.ckptなど)
!wget 使いたいLoraのURL -O /content/stable-diffusion-webui/models/Lora/ファイル名.拡張子
モデルのダウンロード
#モデル名;Counterfeit-V3
!wget https://huggingface.co/gsdf/Counterfeit-V3.0/resolve/main/Counterfeit-V3.0_fp16.safetensors -O models/Counterfeit-V3_fp16.safetensors
ここもLoraと同じでURL、ファイル名、拡張子を修正等で使いたいモデルをお使いください。
CivitaiがモデルやLoraを探しやすいです。
それかモデル紹介をしてるのでそちらをご参考ください。
VAEやEasyNegative等の画質、精度の向上
#導入には設定のStableDiffisionにて変更する
#CounterfeitやAnythingで使えるVAE VAEとは(画像生成したときの、再度が低い、ぼやぼやした感じの除去)
%cd /content/stable-diffusion-webui/models/VAE
!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
!wget https://huggingface.co/andite/pastel-mix/resolve/main/pastel-waifu-diffusion.vae.pt -O /content/stable-diffusion-webui/models/VAE/pastel-waifu-diffusion.pt
!wget https://civitai.com/api/download/models/82365?type=VAE&format=Other -O /content/stable-diffusion-webui/models/VAE/animevae.pt
!wget https://civitai.com/api/download/models/80518 -O /content/stable-diffusion-webui/models/VAE/ClearVAE_V22.safetensors
%cd /content/stable-diffusion-webui/embeddings
#EasyNegative
!wget https://civitai.com/api/download/models/9208 -O /content/stable-diffusion-webui/embeddings/EasyNegative.safetensors
#negative_hand Negative Embedding
!wget https://civitai.com/api/download/models/60938 -O /content/stable-diffusion-webui/embeddings/negative_hand-neg.pt
#Deep Negative V1.x
!wget https://civitai.com/api/download/models/5637 -O /content/stable-diffusion-webui/embeddings/ng_deepnegative.pt
#Bad artist Negative embedding
!wget https://civitai.com/api/download/models/6056 -O /content/stable-diffusion-webui/embeddings/bad_artist_neg.pt
#veryBadImageNegative
!wget https://civitai.com/api/download/models/25820 -O /content/stable-diffusion-webui/embeddings/verybadimagenegative.pt
#Bad hand v4
!wget https://civitai.com/api/download/models/20068 -O /content/stable-diffusion-webui/embeddings/badhandv4.pt
%cd /content/stable-diffusion-webui
VAEはAUTOMATIC1111でsettingタブ>Stable Diffusionの中のVAEが「automatic」になってるので、そこを変更してください
起動
!COMMANDLINE_ARGS="--exit" REQS_FILE="requirements.txt" python launch.py
%cd /content/stable-diffusion-webui
!git pull
!python launch.py --share --xformers --enable-insecure-extension-access
↑23年6月27日更新
↓23年6月27日以前まで使えたコード
!COMMANDLINE_ARGS="--share --gradio-debug" REQS_FILE="requirements.txt" python launch.py
起動後にpublic URLをクリックする事でAUTOMATIC1111のページ飛び
画像生成で遊べるようになります。
以上となります。
シマたんVtuber
ペットの動画や育ててる草花の写真などYoutuberより?のVtuberです。たぶんVtuber。普段は配信で画像生成して遊んでいます、たまにゲームしたり。
AIで画像生成ガチャするのが好きでこんなnoteを書いてます。
Twitter▼https://twitter.com/poorbreastrelig
Youtube▼https://www.youtube.com/channel/UC6IB7yFWLS5ZBucQnSHX7xA
以下に、グーグルコラボラトリー事態を触ったことが無い人向けに
コラボラトリーの始め方の解説を簡単にのせておきます。
コラボラトリーの始め方
Googleコラボラトリーの始め方は、まずGoogleアカウントを作ります。
アカウントを作ったらGoogle検索のトップ画面の右上
Googleアプリをクリック
![](https://assets.st-note.com/img/1685750447702-haG6gUqKz6.png?width=1200)
ドライブをクリック
![](https://assets.st-note.com/img/1685750500329-xvqyUbWEnm.png?width=1200)
ドライブで新規をクリック
![](https://assets.st-note.com/img/1685750534751-3ztVumsYIw.png?width=1200)
その他からアプリを追加
![](https://assets.st-note.com/img/1685750550381-fM8gTvAbMv.png?width=1200)
コラボラトリーを探してインストール
![](https://assets.st-note.com/img/1685750628621-KmZLZSW8DP.png?width=1200)
インストールしたら新規>その他からGoogle Colabratoryをクリック
![](https://assets.st-note.com/img/1685750675619-ZGTx2MBZFZ.png?width=1200)
ノートブックが開くのでコードをコピペする
![](https://assets.st-note.com/img/1685750772898-ie3b7TTvD4.png?width=1200)
コードの追加は赤い矢印の所にカーソルを持ってくると「+コード」「+テキスト」がでるのでそこから追加
それか挿入タブからコードセルの追加でもできます
![](https://assets.st-note.com/img/1685751104940-TyIcYHMeil.png?width=1200)
ランタイムのタイプを変更
![](https://assets.st-note.com/img/1685751185337-HwCBXvcADL.png?width=1200)
NoneからGPUに変更
![](https://assets.st-note.com/img/1685751216634-TyIofi3fh5.png?width=1200)
まったく必須ではないですが自分が見やすいようにテキスト追加
![](https://assets.st-note.com/img/1685751278974-6AHxem0F10.png?width=1200)