Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Forked from lmmx/REPRO_CONDA_SETUP.md
Created August 25, 2022 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loretoparisi/153a926d47544d7b4728b540003f6426 to your computer and use it in GitHub Desktop.
Save loretoparisi/153a926d47544d7b4728b540003f6426 to your computer and use it in GitHub Desktop.
Demo of Stable Diffusion usage, storing the prompt in metadata https://twitter.com/permutans/status/1562471438501548034
import piexif
from torch import autocast
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
use_auth_token=True,
).to("cuda")
prompt = "a photo of a lilac and tan French Bulldog puppy frolicking in a park filled with tennis balls and butterflies, DSLR camera, high quality, sunny day, joyful feeling"
prompt_short = "fb-park"
def make_prompt_exif(prompt):
"""https://stackoverflow.com/a/63649983/"""
exif_ifd = {piexif.ExifIFD.UserComment: f"Prompt: {prompt}".encode()}
exif_dict = {"0th": {}, "Exif": exif_ifd, "1st": {}, "thumbnail": None, "GPS": {}}
return piexif.dump(exif_dict)
for i in range(1):
with autocast("cuda"):
size = int(512 * 1.5)
image = pipe(
prompt,
height=size,
width=size,
num_inference_steps=100,
guidance_scale=11.0,
)["sample"][0]
image.save(f"{prompt_short}_{i:03d}.png", exif=make_prompt_exif(prompt))
from pathlib import Path
import piexif
import torch
from PIL import Image
from torch import autocast
from image_to_image import StableDiffusionImg2ImgPipeline, preprocess
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
revision="fp16",
torch_dtype=torch.float16,
use_auth_token=True,
).to("cuda")
prompt = "drawing of a dog, pencil sketch"
prompt_short = "draw-the-dog"
size = 512 # int(512 * 1.5)
def make_prompt_exif(prompt):
"""https://stackoverflow.com/a/63649983/"""
exif_ifd = {piexif.ExifIFD.UserComment: f"Prompt: {prompt}".encode()}
exif_dict = {"0th": {}, "Exif": exif_ifd, "1st": {}, "thumbnail": None, "GPS": {}}
return piexif.dump(exif_dict)
def load_and_preprocess_init_image(image_path):
init_image = Image.open(image_path).convert("RGB").resize((size, size))
return preprocess(init_image)
image_path = Path("sketches") / "dog-ipad-sketch.jpg"
init_image = load_and_preprocess_init_image(image_path)
for i in range(100):
with autocast("cuda"):
image = pipe(
prompt,
init_image=init_image,
num_inference_steps=100,
guidance_scale=11.0,
)["sample"][0]
image.save(f"{prompt_short}_{i:03d}.png", exif=make_prompt_exif(prompt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment