stable diffusion使ってみる

ローカルPCで画像生成ができるらしいのよ。

macとwindowsを去年の秋から今年の春にかけて刷新した理由は、こういうのを使ってみるためでもあるし。

stable diffusionやってみましょ。

ホンマはdockerコンテナのもあるらしいけど、普通にGPU使いたいから物理マシンのmac m4に入れてく。

入れ方

解説してくれてる人がいる。
作者さんありがとう。

【Stable Diffusion Web UI】Forge版をMacにインストールする方法(ローカルPC)
この記事では、Stable Diffusion WebUI(Forge版)をローカルPC(Mac)にダウンロード/インストールする方法を紹介しています。

brew使うみたいやから、コマンドラインを考える。

気に入らんかったらすぐにアンインストールできそうや。

解説読んで必要モジュールの確認。

brew install cmake protobuf rust python@3.10 git wget

んー、インストール対象はもう自分のmacに入ってるのんもある。

pythonが3.10なのは3.13でもええんとちゃうかって思うけど、pythonにはバージョン上限限定していることあるから真面目にバージョンそろえとこか。

gitはもう使ってるから入れんでもええ。

brewでインストールする箇所

いったんこうやってみよか。

brew install cmake protobuf rust python@3.10 wget 

やってみたらすんなり入る。

nari@narimac-mini ~ % brew install cmake protobuf rust wget

==> Downloading https://formulae.brew.sh/api/formula.jws.json
==> Downloading https://formulae.brew.sh/api/cask.jws.json
:(中略)
==> Pouring wget--1.25.0.arm64_sequoia.bottle.tar.gz
🍺  /opt/homebrew/Cellar/wget/1.25.0: 92 files, 4.5MB
==> Running `brew cleanup wget`...
==> Caveats
==> cmake
:(中略)

pythonのバージョン切り替え

今使ってるのをstable diffusion用にバージョン下げる。

brew unlink python@3.13 && brew link --force --overwrite python@3.10

3.10を使おうとしてるんやけど3.9.6になるのはなんでかわからん。いったんこのままやってみよか。

nari@narimac-mini StableDiffusion % python3 --version 
Python 3.13.3 ⭐️今のバージョンな
nari@narimac-mini StableDiffusion % brew unlink python@3.13 && brew link --force --overwrite python@3.10
Unlinking /opt/homebrew/Cellar/python@3.13/3.13.3... 23 symlinks removed.
Warning: Already linked: /opt/homebrew/Cellar/python@3.10/3.10.17
To relink, run:
  brew unlink python@3.10 && brew link python@3.10
nari@narimac-mini StableDiffusion % python3 --version 
Python 3.9.6 ⭐️なんじゃこりゃ? バージョン10になってへん
nari@narimac-mini StableDiffusion %

gitでクローン

作業用フォルダ作っておいて、Stable Diffusion web UIのリポジトリをローカルPCにcloneしてみる。

nari@narimac-mini StableDiffusion % pwd 
/Users/nari/Documents/personal/StableDiffusion
nari@narimac-mini StableDiffusion % git clone https://github.com/lllyasviel/stable-diffusion-webui-forge.git
Cloning into 'stable-diffusion-webui-forge'...
remote: Enumerating objects: 41922, done.
remote: Total 41922 (delta 0), reused 0 (delta 0), pack-reused 41922 (from 1)
Receiving objects: 100% (41922/41922), 46.30 MiB | 14.86 MiB/s, done.
Resolving deltas: 100% (29116/29116), done.
nari@narimac-mini StableDiffusion % ls
stable-diffusion-webui-forge
nari@narimac-mini StableDiffusion %

なんかいろいろ入ってるのな。

nari@narimac-mini StableDiffusion % cd stable-diffusion-webui-forge 
nari@narimac-mini stable-diffusion-webui-forge % ls  
_typos.toml            k_diffusion         requirements_versions.txt
backend                launch.py           script.js
CHANGELOG.md            LICENSE.txt         scripts
CITATION.cff            localizations           spaces.py
CODEOWNERS            models              style.css
download_supported_configs.py    modules             styles_integrated.csv
embeddings            modules_forge           webui-macos-env.sh
environment-wsl2.yaml        NEWS.md             webui-user.bat
extensions            package.json            webui-user.sh
extensions-builtin        packages_3rdparty       webui.bat
html                pyproject.toml          webui.py
javascript            README.md           webui.sh
nari@narimac-mini stable-diffusion-webui-forge % 

zshやなくてbashで動かすらしい。pipでダウンロード&インストールしてくれて警告もちょいちょいあるけど、いったんそのままにして2〜3分待つ。

nari@narimac-mini stable-diffusion-webui-forge % bash webui.sh ⭐️bashなんよ

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.
################################################################

################################################################
Running on nari user
################################################################

################################################################
Repo already cloned, using it as install directory
################################################################

################################################################
Create and activate python venv
################################################################
Requirement already satisfied: pip in ./venv/lib/python3.10/site-packages (25.0.1)
Collecting pip
  Using cached pip-25.1-py3-none-any.whl.metadata (3.6 kB)
Using cached pip-25.1-py3-none-any.whl (1.8 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 25.0.1
    Uninstalling pip-25.0.1:
      Successfully uninstalled pip-25.0.1
Successfully installed pip-25.1 ⭐️pipやっとるんやな、この後色々入れよる
:(中略)
Downloading torch-2.3.1-cp310-none-macosx_11_0_arm64.whl (61.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.0/61.0 MB 69.0 MB/s eta 0:00:00
Downloading torchvision-0.18.1-cp310-cp310-macosx_11_0_arm64.whl (1.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 78.0 MB/s eta 0:00:00
Downloading pillow-11.2.1-cp310-cp310-macosx_11_0_arm64.whl (3.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.0/3.0 MB 85.3 MB/s eta 0:00:00
:(中略)
Using sub quadratic optimization for cross attention
Using split attention for VAE
WARNING:matplotlib.font_manager:Matplotlib is building the font cache; this may take a moment.
Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled
ControlNet preprocessor location: /Users/nari/Documents/personal/StableDiffusion/stable-diffusion-webui-forge/models/ControlNetPreprocessor
2025-04-28 06:34:47,134 - ControlNet - INFO - ControlNet UI callback registered.
You do not have any model! ⭐️初回やからまだモデル何も入ってへんしな
Model selected: {'checkpoint_info': None, 'additional_modules': [], 'unet_storage_dtype': None}
Using online LoRAs in FP16: False
Running on local URL:  http://127.0.0.1:7860 ⭐️kubernetesのコンソールみたいにブラウザで使えるのが見えるようになる

To create a public link, set `share=True` in `launch()`.
Startup time: 114.4s (prepare environment: 70.0s, launcher: 1.1s, import torch: 31.9s, initialize shared: 0.3s, other imports: 7.1s, load scripts: 2.2s, reload hypernetworks: 0.2s, create ui: 0.9s, gradio launch: 0.7s).
Environment vars changed: {'stream': False, 'inference_memory': 1024.0, 'pin_shared_memory': False}
[GPU Setting] You will use 95.83% GPU memory (23552.00 MB) to load weights, and use 4.17% GPU memory (1024.00 MB) to do matrix computation.

初回起動では、プログラムのコンパイルみたいな感じでちょっとは負荷かかってる。

gvis-stablediff-mac

ブラウザに表示される。

gvis-stablediff-mac

モデルファイルってのが必要

ブラウザに表示されているCheckpointって箇所に指定が必要らしい。あとLoRAっていう追加ファイルとかvaeってのも使うんやて。

予め設定したモデルをベースにしてプロンプトに合わせた画像が生成されるんやな。

モデルは例えばこういう公開サイトあるそうな。

Models - Hugging Face
We’re on a journey to advance and democratize artificial intelligence through open source and open science.

実写風とか、アニメ調、油絵とかが扱えるらしい。

model cardってのがreadmeっぽく掲載されてのがあるんやけど、イメージが何も入ってないのとかあるし、どれ使うか選ぶ基準がイマイチわからんなぁ。

ライセンスのこととか禁止事項も書いてあるなぁ。別のサイトも見てみた。

Civitai Models | Discover Free Stable Diffusion & Flux Models
Browse from thousands of free Stable Diffusion & Flux models, spanning unique anime art styles, immersive 3D renders, st...

こっちはハデなサイト。いーっぱいある。9割女の子みたいな感じで、実写も混じってるけど、どうやって作ってるんかなぁ。

サイズもいろいろ。

絵の雰囲気を選んでみることにして、ベースモデルを適当に選んでダウンロードしてみましょか。

yayoi_mix - v2.5 | Stable Diffusion Checkpoint | Civitai
リアル系マージモデルです。 このマージモデルを公開するにあたり、使用したモデルの製作者の皆様に感謝申し上げます。 This is a realistic merge model. We would like to thank the cre...

さっきgit cloneしたモジュール置き場にmodelsってのがあるから置いとく。

gvis-stablediff-mac

描かせる

女の子描かせる

ベースモデルにもあった、女の子を指示してみる。

画像生成のhello worldってとこやね。ネガティブプロンプトってのがようわからんからいったん放置。

girl in the city

10秒ぐらいで描いてくれる。けっこう速いのな。

gvis-stablediff-mac

景色を変えて描かせる

もうちょっと指示足してみて描けたもの。画像は省略するけど、普通の名詞を指定したらそれなりに描いてくれる。

girl on the beach
girl at the mountain
girl in the room

海が行けたら、山も行けるし、名詞を指定するとイメージしてくれる。

固有名詞は行けるんかと思ったら描いてくれた。それっぽい景色にある看板の文字が、それぞれの地域の言語でできてた。

girl in the newyork
girl in the shanghai
girl in the tokyo

主体を変えて描かせる

cat in the city

絵の雰囲気は同じで猫いってみる。

gvis-stablediff-mac
ultraman in the city

空想科学のウルトラマンって指示したけど、ウルトラマンっぽくないのとアニメっぽい。

gvis-stablediff-mac
gundam in the city

ガンダムを指示したら、ショーウィンドウの前に立ってて人と同じサイズになってるなぁ。

gvis-stablediff-mac
mazinger Z in the city

色指定せずに指示したら、マジンガーZは黒っぽく描いてくれる。

gvis-stablediff-mac

固有名詞は空想科学でも行けるんか。結構もの知りやな。

せやけど、キングギドラはわからんみたいで街の景色しかなかったり、真っ黒画像になったりした。

前にキングギドラをgeminiに描いてもらったときの指示を出してみる。

A fantasy illustration of a three-headed gold dragon soaring through a blizzard of cherry blossoms.

キングギドラを固有名詞指定するんやなくて「金色の3つ首の龍」を指示する。

何回かやってると、なぜかポテっとした女の子が乗ってる絵を作ってくれたりした。

gvis-stablediff-mac

ベースになるモデルの指定ってのは、画風というか描画してくれる雰囲気の指定やから、stable diffusionのモデル指定が女の子画像ではこうなってまうんかもな。

そやけど「three-headed dragon(3つ首)」無視すんなよなぁ。わざとブレた結果作ってくれてるんかな。

何回かやってみると3つ首が反映されたのができたんやけど足の数多いやんけ。

gvis-stablediff-mac

イメージしてたものに一番近いパターンはこんな感じやったけど、geminiのときのほうがより近いものを描いてくれた気がする。

gvis-stablediff-mac

龍の目がないのと、羽がちょっと短い気がするのと、首の上のあたりの背景にいらんもん描いてるの外して欲しいなぁ。

gvis-stablediff-mac

足の数多いのを改善するのは、ネガティブプロンプトの練習したらええんやろか。

stable diffusionのログ

なんとなくターミナル画面見たら、画像作ってくれる途中ではこんな感じのエコーが出てる。

不足の警告とか出てへんし、リソース考えてやってくれてるんやな。

Total progress: 100%|█████████████████████████████████████████████████████████| 20/20 [00:17<00:00,  1.17it/s]
[Unload] Trying to free 1056.00 MB for mps with 1 models keep loaded ... Current free memory is 11790.25 MB ... Done.
100%|█████████████████████████████████████████████████████████████████████████| 20/20 [00:16<00:00,  1.20it/s]
[Unload] Trying to free 2722.50 MB for mps with 1 models keep loaded ... Current free memory is 8300.97 MB ... Done.
Total progress: 100%|█████████████████████████████████████████████████████████| 20/20 [00:17<00:00,  1.17it/s]
[Unload] Trying to free 1056.00 MB for mps with 1 models keep loaded ... Current free memory is 11866.20 MB ... Done.
100%|█████████████████████████████████████████████████████████████████████████| 20/20 [00:16<00:00,  1.20it/s]
[Unload] Trying to free 2722.50 MB for mps with 1 models keep loaded ... Current free memory is 8289.95 MB ... Done.
Total progress: 100%|█████████████████████████████████████████████████████████| 20/20 [00:17<00:00,  1.18it/s]
Total progress: 100%|█████████████████████████████████████████████████████████| 20/20 [00:17<00:00,  1.20it/s]