当サイトにはアフィリエイト広告が含まれます。なおレビューは私の感想を書いており、内容を指示するご依頼はお断りしています

【無料でAI画像生成】PCのPython環境でStableDiffusionをインストールする

今回はこちらのサイトを参考に、AIイラスト界隈で有名な無料AIイラストツール「StableDiffusion」をインストールして、実際に使ってみたいと思います。

Hugging Faceのアカウントを作成する

Hugging Faceの公式サイトを開き、Hugging Faceのアカウントを作成します。

公式サイトを開くと右上に「SignUp」のボタンがあるので、クリックして手順に従っていくとアカウントが作成できます。

アカウントの仮作成が完了し、ログイン状態になったら、登録したメールアドレスに「Hugging Face」からメールが届いているので、メールアドレスの確認リンクを開きます。

そうしたら「Hugging Face」のアカウントの作成が完了です。

アクセストークンを取得する

Hugging Faceの公式サイトの右上に表示される、自身のアイコンをクリックして、「Settings」を開きます。

「Access Tokens」タブを開き、「READ」権限でアクセストークンを作成します。

トークンの名前は何でも良いです。

Google ColabでStable Diffusionを動かす

Google Colabを開き、「ファイル > ノートブックの新規作成」を行う。

「編集 > ノートブックの設定」から、「ハードウェア アクセラレータ」を 「GPU」に設定。

下記を実行して「Stable Diffusion」をインストール

!pip install diffusers==0.8.0 transformers scipy ftfy

下記を実行して、トークン変数を準備します

YOUR_TOKEN="<HugginFace Hubのトークン>"

下記を実行してStable Diffusionのパイプラインを準備します。

from diffusers import StableDiffusionPipeline

# StableDiffusionパイプラインの準備
pipe = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4", 
    use_auth_token=YOUR_TOKEN
).to("cuda")

私のPCにインストールするより圧倒的に早いです。

下記について、promptを書き換えて、画像を生成してみます。

from torch import autocast

# テキストからの画像生成
prompt = "生成したい画像のプロンプト"
with autocast("cuda"):
    images = pipe(prompt, guidance_scale=7.5).images
images[0].save("output.png")

左メニューの「ファイル」に「output.png」として保存されています。

怖っ!!!これはかなり改善を加えないといけなさそうですね💦💦

読み込むモデルを変更してみる

Google Colabで読み込ませるモデルを「anything-v4.0」に変更してみます。

from diffusers import StableDiffusionPipeline

# StableDiffusionパイプラインの準備
pipe = StableDiffusionPipeline.from_pretrained(
    "andite/anything-v4.0", 
    use_auth_token=YOUR_TOKEN
).to("cuda")

だいぶかわいくなりました。

その他のモデルは、以下で紹介されているので色々試してみるのがよさそうです。 huggingface.co

自分のPCにStableDiffusionのインストール手順(スペック不足により失敗)

モデルの作成

以下の内容の新規ファイル「create_art.py」を作成して実行します。

# ライブラリーのインポート
from diffusers import StableDiffusionPipeline
import matplotlib.pyplot as plt

# アクセストークンの設定
access_tokens="自身のアクセストークン" # @param {type:"string"}
 
# モデルのインスタンス化
model = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=access_tokens)
model.to("cuda")
$ python create_art.py

このファイルの実行がかなり重いです。15~20分くらいかかりました

画像生成用プログラムを作成する

ファイル「create_art.py」が以下になるように追記していきます。

# ライブラリーのインポート
from diffusers import StableDiffusionPipeline
import matplotlib.pyplot as plt
import re

# アクセストークンの設定
access_tokens="自身のアクセストークン" # @param {type:"string"}
 
# モデルのインスタンス化
model = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=access_tokens)
model.to("cuda")

# 画像のファイル名
prompt = "生成したい画像のプロンプト" #@param {type:"string"}
filename = re.sub(r'[\\/:*?"<>|,]+', '', prompt).replace(' ','_')

# 画像数
num = 4
 
for i in range(num):
  # モデルにpromptを入力し画像生成
  image = model(prompt,num_inference_steps=100)["sample"][0]
  # 保存
  outputfile = f'{filename} _{i:02} .png'
  image.save(f"outputfile/{outputfile}")
 
for i in range(num):
  outputfile = f'{filename} _{i:02} .png'
  plt.imshow(plt.imread(f"outputfile/{outputfile}"))
  plt.axis('off')
$ python create_art.py

※「生成したい画像のプロンプト」は、自分の作りたい画像に合わせてプロンプトを入力してください。

エラーを修正する

エラー① getattr(): attribute name must be string

この状態で実行すると

TypeError: getattr(): attribute name must be string

のエラーが発生します。同様のエラーが発生したというこちらの記事を参考にエラーを解決していきます。

$ pip install diffusers==0.8.0 transformers scipy ftfy

エラー② you might not have a CUDA gpu

もう一回「create_art.py」を実行すると、また違うエラーが発生しました。

$  python create_art.py
\Python\Python39\lib\site-packages\torch\cuda\__init__.py:52: UserWarning: CUDA initialization: CUDA driver initialization failed, you might not have a CUDA gpu. (Triggered internally at  ..\c10\cuda\CUDAFunctions.cpp:109.)
  return torch._C._cuda_getDeviceCount() > 0
Fetching 16 files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 16/16 [00:00<?, ?it/s]
\Python\Python39\lib\site-packages\transformers\models\clip\feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.
  warnings.warn(
Traceback (most recent call last):
  File "C:\Users\haru\Desktop\workspace\ai\create_art.py", line 11, in <module>
    model.to("cuda")
  File "\Python\Python39\lib\site-packages\diffusers\pipeline_utils.py", line 227, in to
    module.to(torch_device)
  File "\Python\Python39\lib\site-packages\transformers\modeling_utils.py", line 1749, in to
    return super().to(*args, **kwargs)
  File "\Python\Python39\lib\site-packages\torch\nn\modules\module.py", line 673, in to
    return self._apply(convert)
  File "\Python\Python39\lib\site-packages\torch\nn\modules\module.py", line 387, in _apply
    module._apply(fn)
  File "\Python\Python39\lib\site-packages\torch\nn\modules\module.py", line 387, in _apply
    module._apply(fn)
  File "\Python\Python39\lib\site-packages\torch\nn\modules\module.py", line 387, in _apply
    module._apply(fn)
  File "\Python\Python39\lib\site-packages\torch\nn\modules\module.py", line 409, in _apply
    param_applied = fn(param)
  File "\Python\Python39\lib\site-packages\torch\nn\modules\module.py", line 671, in convert
    return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)
  File "\Python\Python39\lib\site-packages\torch\cuda\__init__.py", line 170, in _lazy_init
    torch._C._cuda_init()
RuntimeError: CUDA driver initialization failed, you might not have a CUDA gpu.

クラス CLIPFeatureExtractor は非推奨であり、Transformers のバージョン 5 で削除されます。代わりに CLIPImageProcessor を使用してください。

CUDA ドライバーの初期化に失敗しました。CUDA GPU がない可能性があります。

こちらのURLから、「CUDA Toolkit」のインストールを行っていきます。

これもダウンロードに時間がかかりますね。

インストールが完了したので再度実行してみます。

エラー③ Expected one of cpu, cuda, xpu, mkldnn, opengl, opencl, ideep, hip, msnpu, xla, vulkan device type at start of device

$  python create_art.py
Fetching 16 files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 16/16 [00:00<?, ?it/s]
\Python\Python39\lib\site-packages\transformers\models\clip\feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.
  warnings.warn(
Traceback (most recent call last):
  File "C:\Users\haru\Desktop\workspace\ai\create_art.py", line 22, in <module>
    image = model(prompt,num_inference_steps=100)["sample"][0]
  File "\Python\Python39\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "\Python\Python39\lib\site-packages\diffusers\pipelines\stable_diffusion\pipeline_stable_diffusion.py", line 467, in __call__
    device = self._execution_device
  File "\Python\Python39\lib\site-packages\diffusers\pipelines\stable_diffusion\pipeline_stable_diffusion.py", line 205, in _execution_device
    if self.device != torch.device("meta") or not hasattr(self.unet, "_hf_hook"):
RuntimeError: Expected one of cpu, cuda, xpu, mkldnn, opengl, opencl, ideep, hip, msnpu, xla, vulkan device type at start of device string: meta

デバイス文字列の先頭には、cpu、cuda、xpu、mkldnn、opengl、opencl、ideeep、hip、msnpu、xla、vulkan デバイス タイプのいずれかが必要です

「pipeline_stable_diffusion.py」の205行目の「meta」を「cuda」に書き換えると解消できました。

エラー④ CUDA out of memory.

CUDA out of memory. Tried to allocate 10.00 MiB (GPU 0; 6.00 GiB total capacity; 5.18 GiB already allocated; 0 bytes free; 5.30 GiB reserved in total by PyTorch)

CUDA のメモリが不足しています。 10.00 MiB を割り当てようとしました (GPU 0; 6.00 GiB の合計容量; 5.18 GiB が既に割り当てられています; 0 バイトが空いています; PyTorch によって合計で 5.30 GiB が予約されています)

次はメモリ不足…。

$ nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 528.33       Driver Version: 528.33       CUDA Version: 12.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:01:00.0 Off |                  N/A |
| N/A   38C    P8     6W /  80W |      0MiB /  6144MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

実行中は最大まで振り切ってしまってますね…。

私のPCは「Stable Diffusion」を使用するにはスペックが低すぎるようです。