こんにちは! sonyakunです! 今日は
PythonでStableDiffusionを使う
性能
参考程度に...
- CPU: Intel Core i7 11700
- RAM: DDR4 8GBx2(16GB)
- GPU: NVIDIA GeForce RTX 3060 VRAM 19.8GB
これが使ったPCのスペックです
コード
まず、ターミナルで
pip install diffusers==0.12.1 transformers==4.19.2 ftfy accelerate
を実行する必要があります。
import torch
from diffusers import StableDiffusionPipeline
def gen(prompt):
model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"
pipe = StableDiffusionPipeline.from_pretrained(model_id, revision="fp16", torch_dtype=torch.float16)
pipe = pipe.to(device)
generator = torch.Generator(device).manual_seed(42)
with torch.autocast("cuda"):
image = pipe(prompt, guidance_scale=7.5, generator=generator).images[0]
image.save(prompt + ".png")
while True:
text = input(">")
gen(prompt=text)
実行すると、>がでてくるはずです。でてきたら、そこに生成したいテキストを入力して送信すると、その名前のpngが生成されます。
>Google
Fetching 16 files: 100%|████████████████████████| 16/16 [00:00, ?it/s]
100%|███████████████████████████████████| 50/50 [00:19<00:00, 2.61it/s]
>