私的AI研究会 > AI_Program
これまで検証してきた結果をもとに、Python で生成 AI プログラムを書く
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' # パイプラインを作成 pipeline = StableDiffusionPipeline.from_single_file(model).to(device) # プロンプト prompt = "nature and waterfall photography" # 画像を生成 response = pipeline(prompt=prompt) image = response.images[0] image.save("image_001.png")・モデルのパスとデバイスを指定してパイプラインを作成する
モデルの種類 | パイプライン作成オブジェクト |
SD1.5 | StableDiffusionPipeline |
SDXL | StableDiffusionXLPipeline |
(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]
## 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' # パイプラインを作成 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("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' # パイプラインを作成 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("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' # パイプラインを作成 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("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' # パイプラインを作成 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('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' # パイプラインを作成 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('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' # プロンプト 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('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' # プロンプト 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('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' # プロンプト 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('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' # プロンプト 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('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' # プロンプト 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('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' 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("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' 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("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]