私的AI研究会 > AI_Program
これまで検証してきた結果をもとに、Python で生成 AI プログラムを書く
テキストから画像を生成する txt2img |
参考サイト:猫耳とdiffusersで始めるStable Diffusion入門
Step | プログラム | GPU | CPU | |||||
RTX 4070Ti | RTX 4060 | RTX 4060L | RTX 3050 | GTX 1050 | i7-1260P | |||
1 | 一番簡単なテキストからの画像生成 | sd_001.py | 00:05 | 00:13 | 00:18 | 09:49 | ||
2 | 不要な出力抑制と画像サイズの指定 | sd_002.py | 00:09 | 00:22 | 00:32 | 15:23 | ||
3 | 半精度にして高速化とメモリー節 | sd_003.py | 00:04 | 00:08 | 00:18 | × | ||
4 | ステップ数を指定して高速化する | sd_004.py | 00:03 | 00:09 | 00:14 | 06:22 | ||
5 | 複数生成1 - 同じ条件で複数生成 | sd_005.py | 00:12 | 00:30 | 00:43 | 22:19 | ||
6 | 複数生成2 - 複数プロンプトで生成 | sd_006.py | 00:04 | 00:10 | 00:16 | 07:52 | ||
7 | 複数生成3 - メモリーの開放 | sd_007.py | 00:12 | 00:30 | 00:51 | 24:14 | ||
8 | 同じ画像を生成してステップ数の変化をみる | sd_008.py | 00:10 | 00:23 | 00:35 | 09:23 | ||
9 | プロンプトの重要度を変える | sd_009.py | 00:16 | 00:38 | 00:53 | 25:58 | ||
10 | CLIPを飛ばす | sd_010.py | 00:25 | 01:00 | 01:24 | 48:24 | ||
11 | スケジューラー(scheduler)を変える | sd_scheduler.py | 00:00 | 00:00 | 00:00 | |||
sd_010.py | 00:20 | 00:58 | 01:05 | 45:27 | ||||
12 | 日本語でプロンプト入力する | sd_012.py | 00:03 | 00:09 | 00:14 | 06:24 | ||
13 | 生成したくないないものを指定 | sd_013.py | 00:02 | 00:05 | 00:09 | 04:02 | ||
20 | 【SDXL】SDXL モデルを使用する | sd_020.py | 00:05 | 04:15 | 00:25 | × | ||
21 | 【SDXL】VAE / スケジューラを設定する方法 | sd_021.py | 00:05 | 00:26 | 00:24 | × | ||
22 | 【SDXL】望ましくない結果を避ける | sd_022.py | 00:05 | 03:11 | 00:20 | × | ||
23 | 【SDXL】LoRA を使う | sd_023.py | 00:07 | 03:36 | 00:59 | × | ||
24 | 【SDXL】LoRAの比率を設定する(fuse_lora) | sd_024.py | 00:32 | 10:47 | 01:53 | × | ||
30 | 一番簡単な画像から画像生成 | sd_030.py | 00:01 | 00:01 | 00:10 | × | ||
31 | 変化の強さを調整する(strength) | sd_031.py | 00:10 | 00:12 | 00:15 | × | ||
32 | プロンプトの重さ(guidance_scale) | sd_032.py | 00:18 | 00:53 | 01:06 | × | ||
33 | 【SDXL】モデル合成(refiner) | sd_033.py | 06:00 | 07:03 | 08:21 | × | ||
34 | 【SDXL】モデル合成(refiner)パラメータを比較 | sd_034.py | 50:00 | 66:59 | 56:26 | × | ||
35 | 潜在空間の変換(latent) | sd_035.py | 00:02 | 00:02 | 00:27 | × | ||
36 | 元画像を4倍拡大(x4 upscaler) | sd_036.py | 00:05 | 02:07 | 03:53 | × | ||
37 | 潜在空間で2倍拡大(x2 latent upscaler) | sd_037.py | 00:04 | 00:07 | 02:42 | × | ||
38 | 特定の部分だけ修正(inpaint) | sd_038.py | 00:01 | 00:55 | 03:05 | × | ||
40 | テキストから画像生成(txt2img)」 | sd_040.py | 00:03 | 00:08 | 06:05 | |||
41 | ~ コマンドラインからパラメータ入力 ~ | sd_041.py | ||||||
42 | ~ GUI プログラム ~ | sd_042.py |
PS > nvidia-smi Thu Jun 12 10:30:34 2025 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 576.52 Driver Version: 576.52 CUDA Version: 12.9 | |-----------------------------------------+------------------------+----------------------+ | GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================| | 0 NVIDIA GeForce RTX 4070 Ti WDDM | 00000000:01:00.0 On | N/A | | 31% 31C P8 4W / 285W | 1013MiB / 12282MiB | 1% Default | | | | N/A | +-----------------------------------------+------------------------+----------------------+
ライブラリ名 | 概要 |
PyTorch | 深層学習向けの機械学習フレームワーク |
Transformers | 自然言語処理の Transformer 系モデルの学習と推論用のライブラリ |
Diffusers | 画像生成などに使われる拡散モデルのライブラリ |
Accelerrate | PyTorch で分散学習や高速化を簡単にするためのライブラリ |
SciPy | 数値計算用のライブラリ |
(base) PS > conda create -n sd_test python=3.11 -y
(base) PS > conda activate sd_test (sd_test) PS >
(sd_test) PS > pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
(sd_test) PS > pip install transformers diffusers accelerate scipy
:\(ドライブ・ルート) ├─anaconda_win/ │ ├─workspace_3/ │ │ ├─sd_test/ ← プロジェクトの実行フォルダ : ├─StabilityMatrix/ │ └─Data/ │ ├─Models/ │ │ ├─StableDiffusion/ │ │ │ ├─SD1.5/ ← SD1.5 モデルの場所 │ │ │ └─・・・・・・ ← SDXL モデルの場所・「workspace_3/sd_test/」フォルダが配置されているドライブ直下に「StabilityMatrix」フォルダが存在すること
(sd_test) PS > python -c 'import torch;print(torch.cuda.is_available())'
## 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")・モデルのパスとデバイスを指定してパイプラインを作成する
(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 をご覧ください。
## 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")
(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]
## 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")
(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]
## 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")
(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]
## 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')
(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]
## 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')
(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]
## 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回の画像生成が終わるたびにメモリーを開放する
(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]
## 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 パラメータを指定する
(sd_test) PS > pip install matplotlib
(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]
## 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 パラメータは「どれくらいプロンプトを重視するか」を決める数値
(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]
## 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()
(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]
## sd_scheduler.py スケジューラー(scheduler)を調べる from diffusers import StableDiffusionPipeline, logging logging.set_verbosity_error() ## 不要なエラー出力の抑制 model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors" pipeline = StableDiffusionPipeline.from_single_file(model) print(pipeline.scheduler)・実行結果
(sd_test) PS > python sd_scheduler.py Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 9.80it/s] PNDMScheduler { "_class_name": "PNDMScheduler", "_diffusers_version": "0.33.1", "beta_end": 0.012, "beta_schedule": "scaled_linear", "beta_start": 0.00085, "clip_sample": false, "num_train_timesteps": 1000, "prediction_type": "epsilon", "set_alpha_to_one": false, "skip_prk_steps": true, "steps_offset": 1, "timestep_spacing": "leading", "trained_betas": null }
A1111/k-diffusion | Diffusers | Usage |
DPM++ 2M | DPMSolverMultistepScheduler | |
DPM++ 2M Karras | DPMSolverMultistepScheduler | init with use_karras_sigmas=True |
DPM++ 2M SDE | DPMSolverMultistepScheduler | init with algorithm_type="sde-dpmsolver++" |
DPM++ 2M SDE Karras | DPMSolverMultistepScheduler | init with use_karras_sigmas=True and algorithm_type="sde-dpmsolver++" |
DPM++ 2S a | N/A | very similar to DPMSolverSinglestepScheduler |
DPM++ 2S a Karras | N/A | very similar to DPMSolverSinglestepScheduler(use_karras_sigmas=True, ...) |
DPM++ SDE | DPMSolverSinglestepScheduler | |
DPM++ SDE Karras | DPMSolverSinglestepScheduler | init with use_karras_sigmas=True |
DPM2 | KDPM2DiscreteScheduler | |
DPM2 Karras | KDPM2DiscreteScheduler | init with use_karras_sigmas=True |
DPM2 a | KDPM2AncestralDiscreteScheduler | |
DPM2 a Karras | KDPM2AncestralDiscreteScheduler | init with use_karras_sigmas=True |
DPM adaptive | N/A | |
DPM fast | N/A | |
Euler | EulerDiscreteScheduler | |
Euler a | EulerAncestralDiscreteScheduler | |
Heun | HeunDiscreteScheduler | |
LMS | LMSDiscreteScheduler | |
LMS Karras | LMSDiscreteScheduler | init with use_karras_sigmas=True |
N/A | DEISMultistepScheduler | |
N/A | UniPCMultistepScheduler |
A1111/k-diffusion | Diffusers |
Karras | init with use_karras_sigmas=True |
sgm_uniform | init with timestep_spacing="trailing" |
simple | init with timestep_spacing="trailing" |
exponential | init with timestep_spacing="linspace", use_exponential_sigmas=True |
beta | init with timestep_spacing="linspace", use_beta_sigmas=True |
## 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()
(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]
## 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")
(sd_test) PS > pip install translate
(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]
## 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")
(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]
モデルの種類 | 基本画像サイズ | パイプライン作成オブジェクト |
SD1.5 | 512x512 | StableDiffusionPipeline |
SDXL | 1024x1024 | StableDiffusionXLPipeline |
## 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")
(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]
## 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")
(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]
## 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")
(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]
## 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")
(sd_test) PS > pip install -U peft
(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]
## 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()
(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]
: raise ValueError("PEFT backend is required for this method.") ValueError: PEFT backend is required for this method.
(sd_test) PS > pip install -U peft transformers