私的AI研究会 > AI_Program3
これまで検証してきた結果をもとに、Python で生成 AI プログラムを書く
画像から画像を生成する instruct-pix2pix と controlnet instruct-pix2pix |
参考サイト:instruct-pix2pixで画像を指示した通り変更したり
Step | プログラム | GPU | CPU | |||||
RTX 4070Ti | RTX 4060 | RTX 4060L | RTX 3050 | GTX 1050 | i7-1260P | |||
40 | 「instruct-pix2pix」で画像を変換 | sd_040.py | 00:03 | 00:08 | 00:50 | 05:32 | ||
sd_040a.py | 00:08 | 00:31 | 18:19 | 24:11 | ||||
41 | image_guidance_scale パラメータによる変化 | sd_041.py | 00:12 | 00:24 | 04:52 | 14:23 | ||
sd_041a.py | 00:42 | 02:00 | 02:40:30 | 03:38:17 | ||||
42 | 「controlnet instruct-pix2pix」で画像を変換 | sd_042.py | 00:02 | 00:14 | 00:54 | 06:30 | ||
43 | controlnet_conditioning_scale パラメータによる変化 | sd_043.py | 00:06 | 00:24 | 04:56 | 17:01 | ||
44 | 「controlnet inpaint」で画像の一部を変換 | sd_044.py | 00:01 | 00:10 | 00:45 | 05:17 | ||
45 | strength パラメータによる変化 | sd_045.py | 00:05 | 00:15 | 03:53 | 12:12 | ||
46 | 「outpaint」画像の外側を書き加える | sd_046.py | 00:01 | 00:12 | 00:45 | 05:15 | ||
47 | 「controlnet scribble」手描きの線画から画像を生成 | sd_047.py | 00:01 | 00:12 | 00:53 | 05:36 | ||
48 | 「controlnet openpose」画像から同じ姿勢の画像を生成 | sd_048.py | 00:02 | 00:10 | 01:17 | 05:25 |
コマンドオプション | 引数 | 初期値 | 意味 |
--result_image | str | './sd_results/sd.png' | 保存するファイルパスとヘッダ名の指定 |
--cpu | bool | False | cpu mode の時に設定する(パラメータ不要) |
--log | int | 3 | Log level(-1/0/1/2/3/4/5) |
--model_dir | str | '/StabilityMatrix/Data/Models/StableDiffusion' | モデルフォルダのパス |
--model_path | str | 'SD1.5/beautifulRealistic_brav5.safetensors' | モデルファイル |
--ctrl_model_dir | str | '/StabilityMatrix/Data/Models/ControlNet' | コントロールネット・モデルフォルダのパス |
--ctrl_model_path | str | 'control_v11e_sd15_ip2p_fp16.safetensors' | コントロールネット・モデルファイル |
--image_path | str | 'images/StableDiffusion_247.png' | 入力画像のファイル・パス名 |
--max_size | int | 0 | 入力画像リサイズの最大値(0=入力画像サイズ) |
--prompt | str | '黒髪で短い髪の女性' | 画像生成のためのプロンプト(日本語/英語) |
--seed | int | -1 | シード値(-1の時はランダムに生成) |
--width | int | 512 | 画像サイズの横幅 |
--height | int | 512 | 画像サイズの高さ |
--step | int | 30 | 生成ステップ数 |
--scale | float | 7.0 | ガイダンススケール値 |
--image_scale | float | 1.5 | イメージ・ガイダンススケール値 |
--cc_scale | float | 1.0 | controlnet conditioning scale |
--strength | float | 0.5 | 変化の強さを表すパラメータ |
名称 | 機能 | 処理内容 | プロンプトの書き方 | モデルの場所 |
instruct-pix2pix | 元画像をから新しい画像を作る | 指示された内容との関係がある部分だけ変えられる | 「これに変えたい」と書く | 【SD1.5】instruct-pix2pix |
【SDXL】sdxl-instructpix2pix-768 | ||||
controlnet instruct-pix2pix | 元画像を改造する | 元画像全体を変えられる | 欲しい結果画像の姿を描写する | 【SD1.5】control_v11e_sd15_ip2p |
(base) PS > conda activate sd_test (sd_test) PS > cd workspace_3/sd_test
python sd_040.py生成画像(左) image_040.png 元になる画像(右) sd_040_test.png →
(sd_test) > python sd_040.py Stable Diffusion with diffusers(040) Ver 0.01: Starting application... --result_image : results/image_040.png --cpu : False --log : 3 --model_path : timbrooks/instruct-pix2pix --image_path : images/sd_040_test.png --max_size : 0 --prompt : 雪の中の場面にする --seed : 0 --width : 512 --height : 512 --step : 20 --scale : 7.0 --image_scale : 1.5 prompt: Make it a scene in the snow width: 512, height: 512 seed: 0 Loading pipeline components...: 100%|████████████| 7/7 [00:02<00:00, 2.97it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 16.75it/s] result_file: results/image_040.png Finished.
python sd_040.py --model_path 'diffusers/sdxl-instructpix2pix-768' --width 768 --height 768 --result_image 'results/image_040a.png'生成画像 image_040a.png 元になる画像は同じ sd_040_test.png →
(sd_test) PS > python sd_040.py --model_path 'diffusers/sdxl-instructpix2pix-768' --width 768 --height 768 --result_image 'results/image_040a.png' Stable Diffusion with diffusers(040) Ver 0.01: Starting application... --result_image : results/image_040a.png --cpu : False --log : 3 --model_path : diffusers/sdxl-instructpix2pix-768 --image_path : images/sd_040_test.png --max_size : 0 --prompt : 雪の中の場面にする --seed : 0 --width : 768 --height : 768 --step : 20 --scale : 7.0 --image_scale : 1.5 prompt: Make it a scene in the snow width: 768, height: 768 seed: 0 Loading pipeline components...: 100%|████████████| 7/7 [00:04<00:00, 1.67it/s] 100%|██████████████████████████████████████████| 20/20 [00:03<00:00, 5.27it/s] result_file: results/image_040a.png Finished.
モデルの種類 | 基本画像サイズ | パイプライン作成オブジェクト |
SD1.5 | 512x512 | StableDiffusionInstructPix2PixPipeline |
SDXL | 768x768 | StableDiffusionXLInstructPix2PixPipeline |
python sd_041.py
(sd_test) PS > python sd_041.py Stable Diffusion with diffusers(041) Ver 0.01: Starting application... --result_image : results/image_041.png --cpu : False --log : 3 --model_path : timbrooks/instruct-pix2pix --image_path : images/sd_040_test.png --max_size : 0 --prompt : 雪の中の場面にする --seed : 0 --width : 512 --height : 512 --step : 20 --scale : 7.0 --image_scale : 1.5 prompt: Make it a scene in the snow width: 512, height: 512 seed: 0 Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00, 4.87it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 18.16it/s] Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00, 5.65it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 18.43it/s] Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00, 5.70it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 18.44it/s] Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00, 5.35it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 16.70it/s] Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00, 5.65it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 18.42it/s] Loading pipeline components...: 100%|████████████| 7/7 [00:01<00:00, 5.66it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 18.43it/s] result_file: results/image_041.png Finished.
python sd_041.py --model_path 'diffusers/sdxl-instructpix2pix-768' --width 768 --height 768 --result_image 'results/image_041a.png'
(sd_test) PS > python sd_041.py --model_path 'diffusers/sdxl-instructpix2pix-768' --width 768 --height 768 --result_image 'results/image_041a.png' Stable Diffusion with diffusers(041) Ver 0.01: Starting application... --result_image : results/image_041a.png --cpu : False --log : 3 --model_path : diffusers/sdxl-instructpix2pix-768 --image_path : images/sd_040_test.png --max_size : 0 --prompt : 雪の中の場面にする --seed : 0 --width : 768 --height : 768 --step : 20 --scale : 7.0 --image_scale : 1.5 prompt: Make it a scene in the snow width: 768, height: 768 seed: 0 Loading pipeline components...: 100%|█████████████| 7/7 [00:03<00:00, 1.76it/s] 100%|███████████████████████████████████████████| 20/20 [00:03<00:00, 5.36it/s] Loading pipeline components...: 100%|█████████████| 7/7 [00:04<00:00, 1.70it/s] 100%|███████████████████████████████████████████| 20/20 [00:03<00:00, 5.45it/s] Loading pipeline components...: 100%|█████████████| 7/7 [00:04<00:00, 1.73it/s] 100%|███████████████████████████████████████████| 20/20 [00:03<00:00, 5.44it/s] Loading pipeline components...: 100%|█████████████| 7/7 [00:04<00:00, 1.66it/s] 100%|███████████████████████████████████████████| 20/20 [00:03<00:00, 5.45it/s] Loading pipeline components...: 100%|█████████████| 7/7 [00:03<00:00, 1.80it/s] 100%|███████████████████████████████████████████| 20/20 [00:03<00:00, 5.44it/s] Loading pipeline components...: 100%|█████████████| 7/7 [00:03<00:00, 1.78it/s] 100%|███████████████████████████████████████████| 20/20 [00:03<00:00, 5.45it/s] result_file: results/image_041a.png Finished.
python sd_042.py生成画像(左) image_042.png 元になる画像(右) sd_040_test.png →
(sd_test) PS D:\anaconda_win\workspace_3\sd_test> python sd_042.py Stable Diffusion with diffusers(042) Ver 0.01: Starting application... --result_image : results/image_042.png --cpu : False --log : 3 --model_dir : /StabilityMatrix/Data/Models/StableDiffusion --model_path : SD1.5/beautifulRealistic_brav5.safetensors --ctrl_model_dir : /StabilityMatrix/Data/Models/ControlNet --ctrl_model_path : control_v11e_sd15_ip2p_fp16.safetensors --image_path : images/sd_040_test.png --max_size : 0 --prompt : 浜辺の場面にする --seed : 12345678 --width : 512 --height : 512 --step : 20 --scale : 7.0 --cc_scale : 1.0 prompt: Set the scene on the beach width: 512, height: 512 seed: 12345678 Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 17.47it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 10.55it/s] result_file: results/image_042.png Finished.
python sd_042.py --prompt '雪の中の場面にする'・ベースモデル「beautifulRealistic_brav5.safetensors(リアル系)」
浜辺の場面にする | 雪の中の場面にする | 炎の中の場面にする | 森の中の場面にする | 山中の場面にする | 砂漠の場面にする |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
着物姿に着替える | イラスト画像にする | アニメ画像にする | 微笑んだ顔のアニメ画像 | 泣き顔のアニメ画像にする | 嬉しそうな顔のアニメ画像 |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
python sd_043.py
(sd_test) PS D:\anaconda_win\workspace_3\sd_test> python sd_043.py Stable Diffusion with diffusers(043) Ver 0.01: Starting application... --result_image : results/image_043.png --cpu : False --log : 3 --model_dir : /StabilityMatrix/Data/Models/StableDiffusion --model_path : SD1.5/beautifulRealistic_brav5.safetensors --ctrl_model_dir : /StabilityMatrix/Data/Models/ControlNet --ctrl_model_path : control_v11e_sd15_ip2p_fp16.safetensors --image_path : images/sd_040_test.png --max_size : 0 --prompt : 浜辺の場面にする --seed : 12345678 --width : 512 --height : 512 --step : 20 --scale : 7.0 --cc_scale : 1.0 prompt: Set the scene on the beach width: 512, height: 512 seed: 12345678 Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 30.96it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 14.83it/s] Fetching 11 files: 100%|█████████████████████| 11/11 [00:00<00:00, 7498.35it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.70it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 17.03it/s] Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 11016.56it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.90it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 16.16it/s] Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 21.80it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 16.25it/s] Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 33.68it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 16.56it/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:01<00:00, 16.98it/s] result_file: results/image_043.png Finished.
種類 | パイプライン作成オブジェクト |
従来のinpaint | StableDiffusionInpaintPipeline |
controlnet inpaint | StableDiffusionControlNetInpaintPipeline |
controlnet (参考) | StableDiffusionControlNetPipeline |
python sd_044.pyマスク画像(左) sd_038_test_mask.png 元画像(右) sd_038_test.png →
(sd_test) PS > python sd_044.py Stable Diffusion with diffusers(044) Ver 0.01: Starting application... --result_image : results/image_044.png --cpu : False --log : 3 --model_dir : /StabilityMatrix/Data/Models/StableDiffusion --model_path : SD1.5/beautifulRealistic_brav5.safetensors --ctrl_model_dir : /StabilityMatrix/Data/Models/ControlNet --ctrl_model_path : control_v11p_sd15_inpaint_fp16.safetensors --image_path : images/sd_038_test.png --ctrl_image_path : images/sd_038_test_mask.png --max_size : 0 --prompt : 微笑んでいる女性 --seed : 12345678 --width : 512 --height : 512 --step : 20 --scale : 7.0 --cc_scale : 1.0 --strength : 0.6 prompt: Woman smiling width: 512, height: 512 seed: 12345678 Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 16.91it/s] 100%|██████████████████████████████████████████| 12/12 [00:01<00:00, 10.44it/s] result_file: results/image_044.png Finished.
python sd_044.py --prompt '見つめている女性'・元画像
python sd_045.py
(sd_test) PS > python sd_045.py Stable Diffusion with diffusers(045) Ver 0.01: Starting application... --result_image : results/image_045.png --cpu : False --log : 3 --model_dir : /StabilityMatrix/Data/Models/StableDiffusion --model_path : SD1.5/beautifulRealistic_brav5.safetensors --ctrl_model_dir : /StabilityMatrix/Data/Models/ControlNet --ctrl_model_path : control_v11p_sd15_inpaint_fp16.safetensors --image_path : images/sd_038_test.png --ctrl_image_path : images/sd_038_test_mask.png --max_size : 0 --prompt : 微笑んでいる女性 --seed : 12345678 --width : 512 --height : 512 --step : 20 --scale : 7.0 --cc_scale : 1.0 --strength : 0.6 prompt: Woman smiling width: 512, height: 512 seed: 12345678 Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 32.00it/s] 100%|█████████████████████████████████████████████| 2/2 [00:00<00:00, 9.69it/s] Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 33.38it/s] 100%|█████████████████████████████████████████████| 4/4 [00:00<00:00, 15.51it/s] Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 33.10it/s] 100%|█████████████████████████████████████████████| 6/6 [00:00<00:00, 16.51it/s] Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 22.99it/s] 100%|█████████████████████████████████████████████| 8/8 [00:00<00:00, 15.07it/s] Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 33.78it/s] 100%|███████████████████████████████████████████| 10/10 [00:00<00:00, 15.35it/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%|███████████████████████████████████████████| 12/12 [00:00<00:00, 16.26it/s] Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 21.80it/s] 100%|███████████████████████████████████████████| 14/14 [00:00<00:00, 15.63it/s] Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 34.68it/s] 100%|███████████████████████████████████████████| 16/16 [00:00<00:00, 16.55it/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%|███████████████████████████████████████████| 18/18 [00:01<00:00, 16.38it/s] Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 33.47it/s] 100%|███████████████████████████████████████████| 20/20 [00:01<00:00, 15.80it/s] result_file: results/image_045.png Finished.
python sd_046.py
(sd_test) PS > python sd_046.py Stable Diffusion with diffusers(046) Ver 0.01: Starting application... --result_image : results/image_046.png --cpu : False --log : 3 --model_dir : /StabilityMatrix/Data/Models/StableDiffusion --model_path : SD1.5/beautifulRealistic_brav5.safetensors --ctrl_model_dir : /StabilityMatrix/Data/Models/ControlNet --ctrl_model_path : control_v11p_sd15_inpaint_fp16.safetensors --image_path : images/sd_046_test.png --max_size : 0 --prompt : 庭に立って微笑んでいる女性 --seed : 12345678 --width : 512 --height : 512 --step : 20 --scale : 7.0 --cc_scale : 1.0 prompt: Woman standing in a garden smiling width: 512, height: 512 seed: 12345678 Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00, 22.32it/s] 100%|███████████████████████████████████████████| 20/20 [00:01<00:00, 11.69it/s] result_file: results/image_046.png
python sd_047.py線画イラスト(左) sd_047.png 生成画像(右) image_047.png →
(sd_test) PS > python sd_047.py Stable Diffusion with diffusers(047) Ver 0.01: Starting application... --result_image : results/image_047.png --cpu : False --log : 3 --model_dir : /StabilityMatrix/Data/Models/StableDiffusion --model_path : SD1.5/v1-5-pruned-emaonly.safetensors --ctrl_model_dir : /StabilityMatrix/Data/Models/ControlNet --ctrl_model_path : control_v11p_sd15_scribble_fp16.safetensors --image_path : images/sd_047.png --max_size : 0 --prompt : テーブル上の白いコーヒーカップ --seed : 12345678 --step : 20 prompt: White coffee cup on the table seed: 12345678 Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:01<00:00, 5.70it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 10.11it/s] result_file: results/image_047.png Finished.
python sd_047.py --prompt '木製のテーブルの上に置かれた白いコーヒーカップ'・ベースモデル「v1-5-pruned-emaonly.safetensors
(sd_test) PS > pip install controlnet_aux
python sd_048.py推定された姿勢(左) sd_048_test1_pose.png 元画像(右) sd_048_test1.png →
(sd_test) PS > python sd_048.py Stable Diffusion with diffusers(048) Ver 0.01: Starting application... --result_image : results/image_048.png --cpu : False --log : 3 --model_dir : /StabilityMatrix/Data/Models/StableDiffusion --model_path : SD1.5/beautifulRealistic_brav5.safetensors --ctrl_model_dir : /StabilityMatrix/Data/Models/ControlNet --ctrl_model_path : control_v11p_sd15_openpose_fp16.safetensors --image_path : images/sd_048_test1.png --max_size : 0 --prompt : ダンスを踊る女性 --seed : -1 --step : 20 prompt: Dancing Woman seed: 2142389556 Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 15.00it/s] 100%|██████████████████████████████████████████| 20/20 [00:01<00:00, 10.27it/s] result_file: results/image_048_2142389556.png Finished.
(sd_test) PS > python sd_048.py --seed 1595966935・ベースモデル「beautifulRealistic_brav5.safetensors(リアル系)」/「animePastelDream_softBakedVae.safetensors(イラスト系)」
: cv2.error: OpenCV(4.12.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1284: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvNamedWindow'
pip uninstall opencv-python-headless pip uninstall opencv-python
: File "C:\Users\XXXX\anaconda3\envs\sd_test\Lib\site-packages\controlnet_aux\midas\midas\transforms.py", line 6, in <module> def apply_min_size(sample, size, image_interpolation_method=cv2.INTER_AREA): ^^^^^^^^^^^^^^ AttributeError: module 'cv2' has no attribute 'INTER_AREA'
pip install opencv-contrib-python