私的AI研究会 > AI_Program
これまで検証してきた結果をもとに、Python で生成 AI プログラムを書く
ライブラリ名 | 概要 |
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]