私的AI研究会 > AI_Program4

生成 AI プログラミング4

image_050_1_m.jpg

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

▲ 目 次
※ 最終更新:2025/08/20 

diffusersではじめめる Stable Diffusion (応用編2)

 顔の崩れを修正する face_recognitionADetailer controlnet Paint-By-Example / コントロールネット機能のまとめ

動作環境

概要

Step 50:顔の崩れを修正する1

  1. プログラムを実行する(実行時間:約 2秒 RTX 4070 Ti 12GB)
     python sd_050.py
    (sd_test) PS > python sd_050.py
    
    Stable Diffusion with diffusers(050)  Ver 0.06: Starting application...
    
     --result_image             :   results/image_050.png
     --cpu                      :   False
     --log                      :   3
     --model_dir                :   /StabilityMatrix/Data/Models/StableDiffusion
     --model_path               :   SD1.5/beautifulRealistic_brav5.safetensors
     --image_path               :   images/sd_050_test.jpg
     --max_size                 :   0
     --prompt                   :   masterpiece, high quality, very_high_resolution, large_filesize, full color, an extremely cute face, woman, symmetrical, HDR, real, realistic
     --seed                     :   12345678
     --width                    :   512
     --height                   :   512
     --step                     :   20
     --scale                    :   8.5
     --strength                 :   0.4
     --neg_prompt               :   lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name, multiple legs, malformation
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 19.56it/s]
    100%|██████████████████████████████████████████| 20/20 [00:02<00:00,  9.50it/s]
    result_file: results/image_050.png
    
    Finished.
  2. 画像ファイル「image_050.png」が生成される

  3. 実行例
    元画像顔の抽出顔の修正完成画像
    sd_050_test1_src_m.jpg image_050_testa_1_m.jpg image_050_testb_1_m.jpg image_050_1_m.jpg
    sd_050_test2_src_m.jpg image_050_testa_2_m.jpg image_050_testb_2_m.jpg image_050_2_m.jpg
    sd_050_test3_src_m.jpg image_050_testa_3_m.jpg image_050_testb_3_m.jpg image_050_3_m.jpg
    sd_050_test4_src_m.jpg image_050_testa_4_m.jpg image_050_testb_4_m.jpg image_050_4_m.jpg

Step 51:顔の崩れを修正する2「ADetailer」

  1. プログラムを実行する(実行時間:約 3秒 RTX 4070 Ti 12GB)
     python sd_051.py
    (sd_test) PS > python sd_051.py
    
    Stable Diffusion with diffusers(sd_051)     Ver 0.06: Starting application...
    
     --result_image             :   results/image_051.png
     --cpu                      :   False
     --log                      :   3
     --model_dir                :   /StabilityMatrix/Data/Models/StableDiffusion
     --model_path               :   SD1.5/beautifulRealistic_brav5.safetensors
     --image_path               :   D:/anaconda_win/workspace_3/sd_test/images/sd_050_test.jpg
     --max_size                 :   0
     --prompt                   :   masterpiece, best quality, 1girl
     --seed                     :   12345678
     --step                     :   30
    
    = ADetailer =   prompt: 'masterpiece, best quality, 1girl'  model: 'face_yolov8s.pt'
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 19.93it/s]
    
    0: 640x512 1 face, 36.0ms
    Speed: 2.3ms preprocess, 36.0ms inference, 61.3ms postprocess per image at shape (1, 3, 640, 512)
    100%|██████████████████████████████████████████| 12/12 [00:01<00:00,  9.07it/s]
    
    0: 640x512 1 face, 23.9ms
    Speed: 1.3ms preprocess, 23.9ms inference, 2.0ms postprocess per image at shape (1, 3, 640, 512)
    100%|██████████████████████████████████████████| 12/12 [00:01<00:00,  9.17it/s]
    result_file: results/image_051-sd_050_test.png
    
    Finished.
  2. 画像ファイル「image_051-XXXXXXXX.png」が生成される(XXXXXXXX は入力ファイル名)

Step 52:画像の一部を変換する「Paint-By-Example」

  1. プログラムを実行する(実行時間:約 5秒 RTX 4070 Ti 12GB)
     python sd_052.py
    (sd_test) PS > python sd_052.py
    
    Stable Diffusion with diffusers(052)  Ver 0.06: Starting application...
    
     --result_image             :   results/image_052.png
     --cpu                      :   False
     --log                      :   3
     --model_path               :   Fantasy-Studio/Paint-by-Example
     --image_path               :   images/heron.jpg
     --ctrl_image_path          :   images/kamo.jpg
     --max_size                 :   0
     --seed                     :   -1
     --width                    :   512
     --height                   :   512
     --step                     :   20
    
    Loading pipeline components...:   0%|                                                            | 0/5 [00:00<?, ?it/s]An error occurred while trying to fetch C:\Users\izuts\.cache\huggingface\hub\models--Fantasy-Studio--Paint-by-Example\snapshots\351e6427d8c28a3b24f7c751d43eb4b6735127f7\vae: Error no file named diffusion_pytorch_model.safetensors found in directory C:\Users\izuts\.cache\huggingface\hub\models--Fantasy-Studio--Paint-by-Example\snapshots\351e6427d8c28a3b24f7c751d43eb4b6735127f7\vae.
    An error occurred while trying to fetch C:\Users\izuts\.cache\huggingface\hub\models--Fantasy-Studio--Paint-by-Example\snapshots\351e6427d8c28a3b24f7c751d43eb4b6735127f7\unet: Error no file named diffusion_pytorch_model.safetensors found in directory C:\Users\izuts\.cache\huggingface\hub\models--Fantasy-Studio--Paint-by-Example\snapshots\351e6427d8c28a3b24f7c751d43eb4b6735127f7\unet.
    Loading pipeline components...:  40%|████████████████████▊                               | 2/5 [00:00<00:00,  7.84it/s]You are using a model of type clip_vision_model to instantiate a model of type clip. This is not supported for all configurations of models and can yield errors.
    Loading pipeline components...: 100%|█████████████| 5/5 [00:00<00:00,  5.47it/s]
    100%|███████████████████████████████████████████| 20/20 [00:05<00:00,  3.76it/s]
    result_file: results/image_052-388208840.png
    
    Finished.
  2. 画像ファイル「image_052-XXXXXXXX.png」が生成される(XXXXXXXX はシード値)

Step 53:コントロールネットの機能をまとめる

  canny / inpaint / outpaint / scribble / openpose / pix2pix / txt2img / img2img /
  lineart / softedge / shuffle / depth / seg / normal / anime / mlsd / tile

  1. プログラムを実行する(実行時間:約 2秒 RTX 4070 Ti 12GB)
     python sd_053.py --mode 'canny']
    (sd_test) PS > python sd_053.py --mode 'canny'
    
    Stable Diffusion with diffusers(053)  Ver 0.02: Starting application...
    
     --result_image             :   results/image_053.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_canny_fp16.safetensors
     --image_path               :   images/vermeer.png
     --max_size                 :   0
     --prompt                   :   微笑んでいる女性
     --seed                     :   12345678
     --width                    :   512
     --height                   :   512
     --step                     :   20
     --scale                    :   7.0
     --cc_scale                 :   1.0
     --strength                 :   0.6
     --neg_prompt               :   最悪の品質、おかしい人体構造
     --ip_scale                 :   0.5
     --scheduler                :   euler
     --mode                     :   canny
    
    Fetching 11 files: 100%|████████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|█████████████| 6/6 [00:00<00:00,  9.42it/s]
    100%|███████████████████████████████████████████| 20/20 [00:06<00:00,  3.16it/s]
    result_file: results/image_053-vermeer-canny.png
    
    Finished.
  2. 画像ファイル「image_053-XXXXXXXX-YYYY.png」が生成される(XXXXXXXX は入力ファイル名, YYYY は処理名)
 

忘備録

 

更新履歴

 

参考資料