私的AI研究会 > AI_Program3
#ref(): File not found: "image_037_512_m.jpg" at page "AI_Program2"
これまで検証してきた結果をもとに、Python で生成 AI プログラムを書く
画像生成のプログラムを書く''' |
## sd_txt2img.py【SD1.5】 テキストから画像生成(txt2img)テンプレート import torch from diffusers import StableDiffusionPipeline, logging from translate import Translator logging.set_verbosity_error() # モデルのフォルダーのパス model_path = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors" # モデル # GPUを使う場合は"cuda" 使わない場合は"cpu" device = 'cuda' # seed 値 seed = 12345678 # パイプラインを作成 pipeline = StableDiffusionPipeline.from_single_file(model_path).to(device) # プロンプト trans = Translator('en','ja').translate prompt_jp = '満開の蘭' # プロンプト prompt = trans(prompt_jp) # Generatorオブジェクト作成 generator = torch.Generator(device).manual_seed(seed) print(f'Seed: {seed}, Model: {model_path}') print(f'prompt : {prompt_jp} → {prompt}') # 画像を生成 image = pipeline( prompt=prompt, num_inference_steps = 30, generator = generator, ).images[0] image.save("results/sd_txt2img.png") # 生成画像
(sd_test) PS > python sd_txt2img.py Fetching 11 files: 100%|████████████████████| 11/11 [00:00<00:00, 11048.21it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:00<00:00, 8.85it/s] Seed: 12345678, Model: /StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors prompt : 満開の蘭 → Orchid in full bloom 100%|██████████████████████████████████████████| 30/30 [00:03<00:00, 8.31it/s]
## sd_img2img.py【SD1.5】 画像から画像生成(img2img)テンプレート import torch from PIL import Image from diffusers import StableDiffusionImg2ImgPipeline,DPMSolverMultistepScheduler, logging from translate import Translator logging.set_verbosity_error() # モデルフォルダーのパス model_path = "/StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors" # モデル image_path = "images/kamo.jpg" # 元画像 # GPUを使う場合は"cuda" 使わない場合は"cpu" device = 'cuda' # seed 値 seed = 12345678 # パイプラインを作成 pipeline = StableDiffusionImg2ImgPipeline.from_single_file( model_path, torch_dtype = torch.float16, ).to(device) # スケジューラ設定 pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config) # プロンプト trans = Translator('en','ja').translate prompt_jp = '兎' # プロンプト prompt = trans(prompt_jp) src_image = Image.open(image_path) # Generatorオブジェクト作成 generator = torch.Generator(device).manual_seed(seed) print(f'Seed: {seed}, Model: {model_path}') print(f'prompt : {prompt_jp} → {prompt}') # 画像を生成 image = pipeline( prompt = prompt, image = src_image, num_inference_steps = 30, guidance_scale = 7, strength = 0.6, generator = generator ).images[0] image.save("results/sd_img2img.png") # 生成画像
(sd_test) PS > python sd_img2img.py Fetching 11 files: 100%|███████████████████████████████| 11/11 [00:00<?, ?it/s] Loading pipeline components...: 100%|████████████| 6/6 [00:01<00:00, 3.51it/s] Seed: 12345678, Model: /StabilityMatrix/Data/Models/StableDiffusion/SD1.5/v1-5-pruned-emaonly.safetensors prompt : 兎 → Domestic Rabbit 100%|██████████████████████████████████████████| 18/18 [00:04<00:00, 3.78it/s] (sd_test) PS D:\anaconda_win\workspace_3\sd_test> python sd_037.py