私的AI研究会 > AI_Program

生成 AI プログラミング == 編集中 ==

image_003_m.jpg

 これまで検証してきた結果をもとに、Python で生成 AI プログラムを書く

▲ 目 次
※ 最終更新:2025/06/26 

diffusersではじめめる Stable Diffusion (基本編)

 テキストから画像を生成する txt2img

 参考サイト:猫耳とdiffusersで始めるStable Diffusion入門

実行速度の目安

環境構築

  1. 「Anaconda」の動作する環境を構築しておく
    Anaconda 環境構築

  2. 「Python」バージョンを指定して仮想環境『sd_test』を構築する
    ・Python 3.11 で作成する
    (base) PS > conda create -n sd_test python=3.11 -y
  3. 仮想環境を有効にする
    (base) PS > conda activate sd_test
    (sd_test) PS >
  4. 環境に合わせた「PyTorch」をインストール
    ・オフィシャルサイト https://pytorch.org/ を開いてインストールコマンドを取得する →
    (sd_test) PS > pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
  5. その他のパッケージをインストールする
    (sd_test) PS > pip install transformers diffusers accelerate scipy

前提条件

Step 1:一番簡単な画像生成プログラム

  1. 「sd_001.py」
    ## sd_001.py「自然と滝の写真」~
    
    import torch
    from diffusers import StableDiffusionPipeline
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # 画像を生成
    response = pipeline(prompt=prompt)
    image = response.images[0]
    image.save("results/image_001.png")
    ・モデルのパスとデバイスを指定してパイプラインを作成する
    ・生成画像はリストとして出力されるが、1 枚しかないので.images[0]を付けて取得できる
    ・画像は PIL のオブジェクトで、save メソッドを使って保存できる

  2. プログラムを実行する(実行時間:約 5秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_001.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00,  8.43it/s]
    You have disabled the safety checker for <class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline'> by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .
    100%|██████████████████████████████████████████| 50/50 [00:05<00:00,  8.50it/s]
    ・警告メッセージ機械翻訳
    `safety_checker=None` を渡すことで、<class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline'> のセーフティチェッカーが無効化されました。Stable Diffusion ライセンスの条件を遵守し、フィルタリングされていない結果を公開サービスやアプリケーションに公開しないでください。diffusers チームと Hugging Face は、セーフティフィルターをすべての公開状況で有効にし、ネットワークの動作分析や結果の監査が必要な場合にのみ無効にすることを強く推奨しています。詳細については、https://github.com/huggingface/diffusers/pull/254 をご覧ください。
  3. 画像ファイル「image_001.png」が生成される

Step 2:不要な出力抑制と画像サイズの指定

  1. 「sd_002.py」
    ## sd_002.py「自然と滝の写真」(出力メッセージを抑制と画像サイズの指定)~
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # 画像を生成
    response = pipeline(prompt=prompt, width=768, height=512)   ## 出力サイズ 768x512
    image = response.images[0]
    image.save("results/image_002.png")
  2. プログラムを実行する(実行時間:約 9秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_002.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00,  8.99it/s]
    100%|██████████████████████████████████████████| 50/50 [00:09<00:00,  5.54it/s]
  3. 画像ファイル「image_002.png」が生成される

Step 3:半精度にして高速化とメモリー節約(GPU 動作のみ)

  1. 「sd_003.py」
    ## sd_003.py「自然と滝の写真」(半精度にして高速化とメモリー節約)~
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(
                    model,
                    torch_dtype=torch.float16
                    ).to(device)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # 画像を生成
    response = pipeline(prompt=prompt, width=768, height=512)   ## 出力サイズ 768x512
    image = response.images[0]
    image.save("results/image_003.png")
  2. プログラムを実行する(実行時間:約 4秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_003.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:01<00:00,  4.30it/s]
    100%|██████████████████████████████████████████| 50/50 [00:03<00:00, 14.21it/s]
  3. 画像ファイル「image_003.png」が生成される

Step 4:ステップ数を指定して高速化する

  1. 「sd_004.py」
    ## sd_004.py「自然と滝の写真」(ステップ数を指定する)~
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # 画像を生成
    response = pipeline(prompt=prompt,num_inference_steps=20, width=768, height=512)   ## 出力サイズ 768x512
    image = response.images[0]
    image.save("results/image_004.png")
  2. プログラムを実行する(実行時間:約 3秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_004.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 10.10it/s]
    100%|██████████████████████████████████████████| 20/20 [00:03<00:00,  5.28it/s]
  3. 画像ファイル「image_004.png」が生成される

Step 5:複数生成1 - 同じ条件で複数生成

  1. 「sd_005.py」
    ## sd_005.py「自然と滝の写真」(同じ条件で複数生成)~
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    from diffusers.utils import make_image_grid
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # 画像を生成
    response = pipeline(
                    prompt=prompt,
                    num_inference_steps = 20,
                    num_images_per_prompt = 6,
                    width = 512,
                    height = 512
                    ).images
    make_image_grid(response,2,3).save('results/image_005.png')
  2. プログラムを実行する(実行時間:約 12秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_005.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 11.17it/s]
    100%|██████████████████████████████████████████| 20/20 [00:12<00:00,  1.63it/s]
  3. 画像ファイル「image_005.png」が生成される

Step 6:複数生成2 - 複数プロンプトで生成

  1. 「sd_006.py」
    ## sd_006.py「自然と滝の写真/イラスト」(複数プロンプトで生成)~
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    from diffusers.utils import make_image_grid
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
    # プロンプト
    prompt = ["nature and waterfall photography", "nature and waterfall illustration"]
    
    # 画像を生成
    response = pipeline(
                    prompt=prompt,
                    num_inference_steps = 20
                    ).images
    make_image_grid(response,1,2).save('results/image_006.png')
  2. プログラムを実行する(実行時間:約 4秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_006.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00,  9.67it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.57it/s]
  3. 画像ファイル「image_006.png」が生成される

Step 7:複数生成3 - メモリーの開放(負荷を抑える)

  1. 「sd_007.py」
    ## sd_007.py「自然と滝の写真」(複数生成 負荷を抑えるメモリーの開放)
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    from diffusers.utils import make_image_grid
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # 画像生成
    def image_generation():
        # パイプラインを作成
        pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
        # 画像を生成
        img = pipeline(
                        prompt = prompt,
                        num_inference_steps = 20
                        ).images[0]
        return img
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # 複数画像を生成
    images = []
    for i in range(6):
        images.append(image_generation())
    
        # メモリー開放
        if device == 'cuda':
            torch.cuda.empty_cache()
        elif device == 'mps':
            torch.mps.empty_cache()
    
    make_image_grid(images,2,3).save('results/image_007.png')
    ・ループ処理で1回の画像生成が終わるたびにメモリーを開放する
     CUDA では torch.cuda.empty_cache()、MPS では torch.mps.empty_cache() を使用する

  2. プログラムを実行する(実行時間:約 12秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_007.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00,  8.96it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  7.94it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 16.52it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  7.97it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 11.52it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  7.73it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 16.98it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  7.82it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 17.09it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  7.84it/s]
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 10998.17it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 16.50it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  7.80it/s]
  3. 画像ファイル「image_007.png」が生成される

Step 8:同じ画像を生成してステップ数(num_inference_steps)の変化をみる

  1. 「sd_008.py」
    ## sd_008.py「自然と滝の写真」ステップ数(num_inference_steps)の変化
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    from diffusers.utils import make_image_grid
    import matplotlib.pyplot as plt
    
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # 画像生成
    def image_generation(n_steo):
        # パイプラインを作成
        pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
        # Generatorオブジェクト作成
        generator = torch.Generator(device).manual_seed(seed)
    
        # 画像を生成
        img = pipeline(
                        prompt = prompt,
                        num_inference_steps = n_step,
                        generator = generator
                        ).images[0]
        return img
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    seed = 1234         # seed 固定
    
    # 複数画像を生成
    plt.figure(figsize=[9.5, 6], dpi = 100)
    for i,n_step in enumerate(range(5, 31, 5)):
        img = image_generation(n_step)
        plt.subplot(2, 3, i + 1, title = "num_inference_steps=%d"%n_step)
        plt.imshow(img)
        plt.axis('off')
    
        # メモリー開放
        if device == 'cuda':
            torch.cuda.empty_cache()
        elif device == 'mps':
            torch.mps.empty_cache()
    
    plt.tight_layout()
    plt.savefig('results/image_008.png')
    plt.close()
    ・結果を固定したい場合は seed を指定して torch.Generator オブジェクトを作ってパイプラインを実行する時に generator パラメータを指定する
    ・seed 値は 0 より大きい整数
    ・seed と他の設定が同じなら全く同じ画像を生成する
    ・ステップ数(num_inference_steps)を変化して生成画像に与える影響を調べる

  2. 足りないパッケージをインストールする
    (sd_test) PS > pip install matplotlib
  3. プログラムを実行する(実行時間:約 10秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_008.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 14.85it/s]
    100%|████████████████████████████████████████████| 5/5 [00:00<00:00,  6.56it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.26it/s]
    100%|██████████████████████████████████████████| 10/10 [00:01<00:00,  8.18it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 35.28it/s]
    100%|██████████████████████████████████████████| 15/15 [00:01<00:00,  8.16it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.28it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.29it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 35.31it/s]
    100%|██████████████████████████████████████████| 25/25 [00:02<00:00,  8.55it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 22.39it/s]
    100%|██████████████████████████████████████████| 30/30 [00:03<00:00,  8.59it/s]
  4. 画像ファイル「image_008.png」が生成される

Step 9:プロンプトの重要度(guidance_scale)を変える

  1. 「sd_009.py」
    ## sd_009.py「自然と滝の写真」プロンプトの重要度(guidance_scale)
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    from diffusers.utils import make_image_grid
    import matplotlib.pyplot as plt
    
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # 画像生成
    def image_generation(g_scale):
        # パイプラインを作成
        pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
        # Generatorオブジェクト作成
        generator = torch.Generator(device).manual_seed(seed)
    
        # 画像を生成
        img = pipeline(
                        prompt = prompt,
                        num_inference_steps = 20,
                        guidance_scale = g_scale,
                        generator = generator
                        ).images[0]
        return img
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    seed = 1234         # seed 固定
    
    # 複数画像を生成
    plt.figure(figsize=[12.5, 6], dpi = 100)
    for i in range(1, 9):
        img = image_generation(i)
        plt.subplot(2, 4, i, title = "guidance_scale=%.1f"%i)
        plt.imshow(img)
        plt.axis('off')
    
        # メモリー開放
        if device == 'cuda':
            torch.cuda.empty_cache()
        elif device == 'mps':
            torch.mps.empty_cache()
    
    plt.tight_layout()
    plt.savefig('results/image_009.png')
    plt.close()
    ・guidance_scale パラメータは「どれくらいプロンプトを重視するか」を決める数値
    ・既定値は 7.5 で、0 から 10 で指定できる(数値が大きい程プロンプトが重視される)

  2. プログラムを実行する(実行時間:約 16秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_009.py
    
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 11003.42it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 15.43it/s]
    100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 13.50it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.54it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.59it/s]
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 12323.01it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.35it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.37it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.21it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.63it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.81it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.64it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 19.16it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.26it/s]
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 11008.67it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.43it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.44it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.80it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.34it/s]
  3. 画像ファイル「image_009.png」が生成される

Step 10:CLIPを飛ばす(clip_skip)

  1. 「sd_010.py」
    ## sd_010.py「自然と滝の写真」CLIPを飛ばす(clip_skip)
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    from diffusers.utils import make_image_grid
    import matplotlib.pyplot as plt
    
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # 画像生成
    def image_generation(c_skip):
        # パイプラインを作成
        pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
        # Generatorオブジェクト作成
        generator = torch.Generator(device).manual_seed(seed)
    
        # 画像を生成
        img = pipeline(
                        prompt = prompt,
                        num_inference_steps = 20,
                        guidance_scale = 7.5,
                        clip_skip = c_skip,
                        generator = generator
                        ).images[0]
        return img
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    seed = 1234         # seed 固定
    
    # 複数画像を生成
    plt.figure(figsize=[7, 10], dpi = 100)
    for i in range(12):
        img = image_generation(i)
        plt.subplot(4, 3, i + 1, title="clip_skip=%d"%i)
        plt.imshow(img)
        plt.axis('off')
    
        # メモリー開放
        if device == 'cuda':
            torch.cuda.empty_cache()
        elif device == 'mps':
            torch.mps.empty_cache()
    
    plt.tight_layout()
    plt.savefig('results/image_010.png')
    plt.close()

  2. プログラムを実行する(実行時間:約 25秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_010.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 14.60it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.27it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 32.96it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.55it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 32.60it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.43it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.40it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.40it/s]
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 10889.15it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.16it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.64it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 23.14it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.48it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.46it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.44it/s]
    Fetching 11 files: 100%|█████████████████████| 11/11 [00:00<00:00, 9123.46it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 32.64it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.48it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.62it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.35it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.45it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.63it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 22.02it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.41it/s]
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 10956.39it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 35.70it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.36it/s]
  3. 画像ファイル「image_010.png」が生成される

Step 11:スケジューラー(scheduler)を変える

  1. 「sd_011.py」
    ## sd_011.py「自然と滝の写真」スケジューラー(scheduler)を変更
    
    import torch
    import diffusers
    from diffusers import StableDiffusionPipeline, logging
    from diffusers.utils import make_image_grid
    import matplotlib.pyplot as plt
    
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # 画像生成
    def image_generation(scheduler):
        # パイプラインを作成
        pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
        pipeline.scheduler = scheduler.from_config(pipeline.scheduler.config)
    
        # Generatorオブジェクト作成
        generator = torch.Generator(device).manual_seed(seed)
    
        # 画像を生成
        img = pipeline(
                        prompt = prompt,
                        num_inference_steps = 20,
                        generator = generator
                        ).images[0]
        return img
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    seed = 1234         # seed 固定
    
    lis_schdl = [
        'DDIMScheduler',
        'DDPMScheduler',
        'PNDMScheduler',
        'DPMSolverSinglestepScheduler',
        'DPMSolverMultistepScheduler',
        'LMSDiscreteScheduler',
        'EulerDiscreteScheduler',
        'EulerAncestralDiscreteScheduler',
        'HeunDiscreteScheduler',
        'KDPM2AncestralDiscreteScheduler',
    ]
    
    # 複数画像を生成
    plt.figure(figsize=[6, 15.5], dpi = 100)
    for i, schdl in enumerate(lis_schdl):
        img = image_generation(getattr(diffusers, schdl))
        plt.subplot(5, 2, i + 1, title=schdl)
        plt.imshow(img)
        plt.axis('off')
    
        # メモリー開放
        if device == 'cuda':
            torch.cuda.empty_cache()
        elif device == 'mps':
            torch.mps.empty_cache()
    
    plt.tight_layout()
    plt.savefig('results/image_011.png')
    plt.close()
  2. プログラムを実行する(実行時間:約 20秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_011.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 14.83it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.68it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.87it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.96it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.43it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  8.36it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.74it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  9.15it/s]
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 11000.80it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.81it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  9.15it/s]
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 11008.67it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 23.26it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  9.11it/s]
    Fetching 11 files: 100%|█████████████████████| 11/11 [00:00<00:00, 8329.54it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 34.21it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  9.15it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 35.46it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  9.17it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.07it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.69it/s]
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.34it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.69it/s]
  3. 画像ファイル「image_011.png」が生成される

Step 12:日本語でプロンプト入力する

  1. 「sd_012.py」
    ## sd_012.py「自然と滝の写真」日本語でプロンプト入力~
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    from translate import Translator
    
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    seed = 5678
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
    # プロンプト
    prompt = "自然と滝の写真"
    
    # 英語に翻訳
    generator = torch.Generator(device).manual_seed(seed)
    trans = Translator('en','ja').translate
    en_prompt = trans(prompt)
    print(prompt, '→', en_prompt)
    
    # 画像を生成
    image = pipeline(
                prompt = en_prompt,
                num_inference_steps=20,
                width=768,
                height=512
                ).images[0]
    image.save("results/image_012.png")
  2. 足りないパッケージをインストールする
    (sd_test) PS > pip install translate
  3. プログラムを実行する(実行時間:約 3秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_012.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00,  8.67it/s]
    自然と滝の写真 → Photos of nature and waterfalls
    100%|██████████████████████████████████████████| 20/20 [00:03<00:00,  5.18it/s]
  4. 画像ファイル「image_012.png」が生成される

Step 13:生成したくないないものを指定(negative prompt)

  1. 「sd_013.py」
    ## sd_013.py「自然と滝の写真」生成したくないないものを指定(negative prompt)~
    
    import torch
    from diffusers import StableDiffusionPipeline, logging
    
    logging.set_verbosity_error()                               ## 不要なエラー出力の抑制
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    seed = 1234
    
    # パイプラインを作成
    pipeline = StableDiffusionPipeline.from_single_file(model).to(device)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    ng_prompt = "person"
    
    # Generatorオブジェクト作成
    generator = torch.Generator(device).manual_seed(seed)
    
    # 画像を生成
    image = pipeline(
                prompt = prompt,
                negative_prompt = ng_prompt,
                num_inference_steps=20,
                guidance_scale = 7.0,
                generator = generator
                ).images[0]
    image.save("results/image_013.png")
  2. プログラムを実行する(実行時間:約 2秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_013.py
    
    Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 11058.81it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00,  9.88it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  7.91it/s]
  3. 画像ファイル「image_013.png」が生成される
    ・ネガティブプロンプトを指定することで多少画像は変化する

Step 20:【SDXL】SDXL モデルを使用する

  1. 「sd_020.py」
    ## sd_020.py【SDXL】「自然と滝の写真」SDXL モデル~
    ## model:   sd_xl_base_1.0.safetensors
    ##          juggernautXL_v8Rundiffusion.safetensors
    ##          animexlXuebimix_v60LCM.safetensors
    
    import torch
    from diffusers import StableDiffusionXLPipeline, logging
    logging.set_verbosity_error()
    
    # モデルのフォルダーのパス
    model_path = "/StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # seed 値
    seed = 12345678
    
    # パイプラインを作成
    pipeline = StableDiffusionXLPipeline.from_single_file(
                        model_path,
                        torch_dtype = torch.float16,
                        ).to(device)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # Generatorオブジェクト作成
    generator = torch.Generator(device).manual_seed(seed)
    
    print(f'Seed: {seed}, Model: {model_path}')
    
    # 画像を生成
    image = pipeline(
                        prompt=prompt,
                        num_inference_steps=20,
                        guidance_scale = 7.5,
                        generator = generator
                        ).images[0]
    image.save("results/image_020.png")
  2. プログラムを実行する(実行時間:約 5秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_020.py
    
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00,  4.42it/s]
    Seed: 12345678, Model: /StabilityMatrix/Data/Models/StableDiffusion/sd_xl_base_1.0.safetensors
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.30it/s]
  3. 画像ファイル「image_020.png」が生成される

Step 21:【SDXL】VAE / スケジューラを設定する方法

  1. 「sd_021.py」
    ## sd_021.py【SDXL】「自然と滝の写真」VAE スケジューラ指定~
    ## model:   animexlXuebimix_v60LCM.safetensors
    
    import torch
    from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler, logging
    from diffusers.models import AutoencoderKL                  # VAEのクラス
    logging.set_verbosity_error()
    
    # モデル/VAE のフォルダーのパス
    model_path = "/StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors"
    vae_path = "/StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # seed 値
    seed = 12345678
    
    # VAEオブジェクトを作成
    vae = AutoencoderKL.from_single_file(
                        vae_path,
                        torch_dtype=torch.float16
                        )
    
    # パイプラインを作成
    pipeline = StableDiffusionXLPipeline.from_single_file(
                        model_path,
                        vae = vae,
                        torch_dtype = torch.float16,
                        ).to(device)
    
    # スケジューラ設定
    pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    
    # Generatorオブジェクト作成
    generator = torch.Generator(device).manual_seed(seed)
    
    print(f'Seed: {seed}, Model: {model_path}')
    print(f'VAE : {vae_path}')
    
    # 画像を生成
    image = pipeline(
                        prompt=prompt,
                        num_inference_steps=20,
                        guidance_scale = 7.5,
                        generator = generator
                        ).images[0]
    image.save("results/image_021.png")
  2. プログラムを実行する(実行時間:約 5秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_021.py
    
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00,  4.37it/s]
    Seed: 12345678, Model: /StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors
    VAE : /StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.23it/s]
  3. 画像ファイル「image_021.png」が生成される
    ・左の画像は VAE なしの場合、白いノイズなどがある
    ・モデルによって必要な場合は適切な VAE 屋スケジューラを設定する必要がある

Step 22:【SDXL】望ましくない結果を避ける(エンベディングモデル)

  1. 「sd_022.py」
    ## sd_022.py【SDXL】「自然と滝の写真」望ましくない結果を避ける~
    ## model:   animexlXuebimix_v60LCM.safetensors
    
    import torch
    from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler, logging
    from diffusers.models import AutoencoderKL                  # VAEのクラス
    logging.set_verbosity_error()
    
    # モデル/VAE/Embedding のフォルダーのパス
    model_path = "/StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors"
    vae_path = "/StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors"
    emb_path = "/StabilityMatrix/Data/Models/Embeddings/EasyNegativeV2.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # seed 値
    seed = 12345678
    
    # VAEオブジェクトを作成
    vae = AutoencoderKL.from_single_file(
                        vae_path,
                        torch_dtype=torch.float16
                        )
    
    # パイプラインを作成
    pipeline = StableDiffusionXLPipeline.from_single_file(
                        model_path,
                        vae = vae,
                        torch_dtype = torch.float16,
                        ).to(device)
    
    # スケジューラ設定
    pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
    
    # プロンプト
    prompt = "nature and waterfall photography"
    neg_prompt = "EasyNegative"
    
    # Generatorオブジェクト作成
    generator = torch.Generator(device).manual_seed(seed)
    
    # エンベディングをロード
    pipeline.load_textual_inversion(
        pretrained_model_name_or_path = emb_path,
        token='EasyNegative')
    
    print(f'Seed: {seed}, Model: {model_path}')
    print(f'Embeddings : {emb_path}')
    print(f'VAE : {vae_path}')
    
    # 画像を生成
    image = pipeline(
                        prompt = prompt,
                        negative_prompt = neg_prompt,
                        num_inference_steps=20,
                        guidance_scale = 7.5,
                        generator = generator
                        ).images[0]
    image.save("results/image_022.png")
  2. プログラムを実行する(実行時間:約 5秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_022.py
    
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00,  4.31it/s]
    The new embeddings will be initialized from a multivariate normal distribution that has old embeddings mean and covariance. As described in this article: https://nlp.stanford.edu/~johnhew/vocab-expansion.html. To disable this, use `mean_resizing=False`
    Seed: 12345678, Model: /StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors
    Embeddings : /StabilityMatrix/Data/Models/Embeddings/EasyNegativeV2.safetensors
    VAE : /StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.31it/s]
  3. 画像ファイル「image_022.png」が生成される
    ・モデルによって必ずしもいい結果になるとは限らない

Step 23:【SDXL】LoRA を使う

  1. 「sd_023.py」
    ## sd_023.py【SDXL】「自然と滝の写真」LoRA を使う~
    ## model:   animexlXuebimix_v60LCM.safetensors
    
    import torch
    from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler, logging
    from diffusers.models import AutoencoderKL                  # VAEのクラス
    logging.set_verbosity_error()
    
    # モデル/VAE/Embedding/LoRA のフォルダーのパス
    model_path = "/StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors"
    vae_path = "/StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors"
    emb_path = "/StabilityMatrix/Data/Models/Embeddings/EasyNegativeV2.safetensors"
    lora_path = "/StabilityMatrix/Data/Models/Lora/国风插画SDXL.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # seed 値
    seed = 12345678
    
    # VAEオブジェクトを作成
    vae = AutoencoderKL.from_single_file(
                        vae_path,
                        torch_dtype=torch.float16
                        )
    
    # パイプラインを作成
    pipeline = StableDiffusionXLPipeline.from_single_file(
                        model_path,
                        vae = vae,
                        torch_dtype = torch.float16,
                        ).to(device)
    
    # スケジューラ設定
    pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
    
    # プロンプト
    prompt = "guofeng, nature and waterfall photography"        # トリガーワードが含まれる
    neg_prompt = "EasyNegative"
    
    # Generatorオブジェクト作成
    generator = torch.Generator(device).manual_seed(seed)
    
    # エンベディングをロード
    pipeline.load_textual_inversion(
        pretrained_model_name_or_path = emb_path,
        token='EasyNegative')
    
    # LoRA をロード
    pipeline.load_lora_weights(".", weight_name = lora_path)
    
    print(f'Seed: {seed}, Model: {model_path}')
    print(f'Embeddings : {emb_path}')
    print(f'LoRA : {lora_path}')
    print(f'VAE : {vae_path}')
    
    # 画像を生成
    image = pipeline(
                        prompt = prompt,
                        negative_prompt = neg_prompt,
                        num_inference_steps=20,
                        guidance_scale = 7.5,
                        generator = generator
                        ).images[0]
    image.save("results/image_023.png")
  2. 足りないパッケージをインストールする
    (sd_test) PS > pip install -U peft
  3. プログラムを実行する(実行時間:約 7秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_023.py
    
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00,  4.34it/s]
    The new embeddings will be initialized from a multivariate normal distribution that has old embeddings mean and covariance. As described in this article: https://nlp.stanford.edu/~johnhew/vocab-expansion.html. To disable this, use `mean_resizing=False`
    Seed: 12345678, Model: /StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors
    Embeddings : /StabilityMatrix/Data/Models/Embeddings/EasyNegativeV2.safetensors
    LoRA : /StabilityMatrix/Data/Models/Lora/国风插画SDXL.safetensors
    VAE : /StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors
    100%|██████████████████████████████████████████| 20/20 [00:06<00:00,  3.17it/s]
  4. 画像ファイル「image_023.png」が生成される

Step 24:【SDXL】LoRAの比率を設定する(fuse_lora)

  1. 「sd_024.py」
    ## sd_024.py【SDXL】「自然と滝の写真」LoRA 比率の設定~
    ## model:   animexlXuebimix_v60LCM.safetensors
    
    import torch
    from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler, logging
    from diffusers.models import AutoencoderKL                  # VAEのクラス
    import matplotlib.pyplot as plt
    
    logging.set_verbosity_error()
    
    # 画像生成
    def image_generation(lora_s):
        # VAEオブジェクトを作成
        vae = AutoencoderKL.from_single_file(
                        vae_path,
                        torch_dtype=torch.float16
                        )
    
        # パイプラインを作成
        pipeline = StableDiffusionXLPipeline.from_single_file(
                        model_path,
                        vae = vae,
                        torch_dtype = torch.float16,
                        ).to(device)
    
        # スケジューラ設定
        pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
    
        # Generatorオブジェクト作成
        generator = torch.Generator(device).manual_seed(seed)
    
        # エンベディングをロード
        pipeline.load_textual_inversion(
                        pretrained_model_name_or_path = emb_path,
                        token='EasyNegative')
    
        # LoRA をロード
        pipeline.load_lora_weights(".", weight_name = lora_path)
        pipeline.fuse_lora(lora_scale = lora_s)                 # LoRA 比率
    
        # 画像を生成
        img = pipeline(
                        prompt = prompt,
                        negative_prompt = neg_prompt,
                        num_inference_steps=20,
                        guidance_scale = 7.5,
                        generator = generator
                        ).images[0]
        return img
    
    
    # モデル/VAE/Embedding/LoRA のフォルダーのパス
    model_path = "/StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors"
    vae_path = "/StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors"
    emb_path = "/StabilityMatrix/Data/Models/Embeddings/EasyNegativeV2.safetensors"
    lora_path = "/StabilityMatrix/Data/Models/Lora/国风插画SDXL.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    
    # seed 値
    seed = 12345678
    
    # プロンプト
    prompt = "guofeng, nature and waterfall photography"        # トリガーワードが含まれる
    neg_prompt = "EasyNegative"
    
    print(f'Seed: {seed}, Model: {model_path}')
    print(f'Embeddings : {emb_path}')
    print(f'LoRA : {lora_path}')
    print(f'VAE : {vae_path}')
    
    # 複数画像を生成
    plt.figure(figsize = [6, 12.5], dpi = 100)
    for i in range(8):
        lora_s = i * 0.2
        img = image_generation(lora_s)
        plt.subplot(4, 2, i + 1, title = "lora_scale=%.1f"%lora_s)
        plt.imshow(img)
        plt.axis('off')
    
        # メモリー開放
        if device == 'cuda':
            torch.cuda.empty_cache()
        elif device == 'mps':
            torch.mps.empty_cache()
    
    plt.tight_layout()
    plt.savefig('results/image_024.png')
    plt.close()
  2. プログラムを実行する(実行時間:約 32秒 RTX 4070 Ti 12GB)
    (sd_test) PS > python sd_024.py
    
    Seed: 12345678, Model: /StabilityMatrix/Data/Models/StableDiffusion/animexlXuebimix_v60LCM.safetensors
    Embeddings : /StabilityMatrix/Data/Models/Embeddings/EasyNegativeV2.safetensors
    LoRA : /StabilityMatrix/Data/Models/Lora/国风插画SDXL.safetensors
    VAE : /StabilityMatrix/Data/Models/VAE/sdxl_vae_fp16_fix.safetensors
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00,  9.53it/s]
    The new embeddings will be initialized from a multivariate normal distribution that has old embeddings mean and covariance. As described in this article: https://nlp.stanford.edu/~johnhew/vocab-expansion.html. To disable this, use `mean_resizing=False`
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.41it/s]
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00, 15.99it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.47it/s]
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00, 12.95it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.41it/s]
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00, 15.60it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.42it/s]
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00, 16.15it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.45it/s]
    Fetching 17 files: 100%|████████████████████| 17/17 [00:00<00:00, 17001.23it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00, 15.34it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.40it/s]
    Fetching 17 files: 100%|████████████████████| 17/17 [00:00<00:00, 17005.29it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00, 13.26it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.50it/s]
    Fetching 17 files: 100%|███████████████████████████████| 17/17 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 7/7 [00:00<00:00, 15.60it/s]
    100%|██████████████████████████████████████████| 20/20 [00:04<00:00,  4.40it/s]
  3. 画像ファイル「image_024.png」が生成される
 

忘備録

ValueError: PEFT backend is required for this method.

 

更新履歴

参考資料