最終更新日:2023年7月12日
Pythonスクリプト
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler import torch from compel import Compel, DiffusersTextualInversionManager import argparse parser = argparse.ArgumentParser() parser.add_argument( '--seed', type=int, default=20000, help='the seed (for reproducible sampling)', ) parser.add_argument( '--n_samples', type=int, default=5, help='how many samples to produce for each given prompt', ) parser.add_argument( '--steps', type=int, default=25, help='num_inference_steps', ) args = parser.parse_args() seed = args.seed steps = args.steps scale = 7.0 model_id = "model/BracingEvoMix_v1.safetensors" pipe = StableDiffusionPipeline.from_single_file( model_id, load_safety_checker=False, extract_ema=True, torch_dtype=torch.float16) pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config) pipe.load_textual_inversion("embeddings", weight_name="EasyNegative.safetensors", token="EasyNegative") pipe.to("cuda") prompt = "(high resolution)++, 8k+, photorealistic+, attractive, highly detailed, photo of pretty Japanese woman, short hair" negative_prompt = "EasyNegative, (Worst Quality)++, (low quality)+" textual_inversion_manager = DiffusersTextualInversionManager(pipe) compel_proc = Compel( tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder, textual_inversion_manager=textual_inversion_manager, truncate_long_prompts=False) prompt_embeds = compel_proc([prompt]) negative_prompt_embeds = compel_proc([negative_prompt]) for i in range(args.n_samples): temp_seed = seed + i * 100 generator = torch.Generator(device="cuda").manual_seed(temp_seed) image = pipe( prompt_embeds=prompt_embeds, negative_prompt_embeds = negative_prompt_embeds, generator=generator, num_inference_steps=steps, guidance_scale=scale, width=768, height=768, ).images[0] image.save(f"./step{steps}_seed{temp_seed}.png")
長いpromptを書くのは苦手なので簡潔にしています。
実行
モデルはHugging Faceからダウンロード可能です。「EasyNegative.safetensors」はこちらからダウンロード可能です。
python txt2img.py --n_samples 20
いっきに20枚の画像が作成されます。
結果
一部のみ載せておきます。Python環境の構築
Windows 11 Python 3.10 CUDA 11.7
pip install torch --index-url https://download.pytorch.org/whl/cu117 pip install diffusers[torch] pip install transformers pip install compel pip install omegaconf