私的AI研究会 > AI_Program

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

_00020-3231355271_m.jpg

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

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

diffusersで始める Stable Diffusion

環境構築

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

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

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

  1. 「sd_001.py」
    ## sd_001.py「自然と滝の写真」~
    
    import torch
    from diffusers import StableDiffusionPipeline
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda'
    
    # パイプラインを作成
    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")
  2. プログラムを実行する
    (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]
  3. 画像ファイル「image_001.png」が生成される

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

  1. 「sd_002.py」
    ## sd_002.py「自然と滝の写真」(出力メッセージを抑制と画像サイズの指定)~
    
    import torch
    from diffusers import StableDiffusionPipeline,logging       ## 不要なエラー出力の抑制
    logging.set_verbosity_error()                               ##
    
    # モデルのフォルダーのパス
    model = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors"
    
    # GPUを使う場合は"cuda" 使わない場合は"cpu"
    device = 'cuda'
    
    # パイプラインを作成
    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")
  2. プログラムを実行する
    (sd_test) PS > python sd_002.py
    
    Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s]
    Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00,  8.99it/s]
    100%|██████████████████████████████████████████| 50/50 [00:09<00:00,  5.54it/s]
  3. 画像ファイル「image_002.png」が生成される
 

更新履歴

参考資料