Skip to content

Instantly share code, notes, and snippets.

@Norod
Created July 8, 2023 20:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Norod/2eed459148e9deaa27f5ca6fa064b1b9 to your computer and use it in GitHub Desktop.
Save Norod/2eed459148e9deaa27f5ca6fa064b1b9 to your computer and use it in GitHub Desktop.
Testing stable-diffusion-xl 0.9 generation pipelines using simple random prompts (Script needs official HF repo access permission to work)
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import torch
#////////////////////////////////////////////////////////////////
guidance_scale=7.5
steps_base=50
steps_refiner=50
width=1024
height=1024
base_model_id_str = "sdxl_09"
prompt_suffix = ", Very detailed, clean, high quality, sharp image"
neg_prompt = "text, watermark, grainy, blurry, unfocused, nsfw, naked, nude, noisy image, deformed, distorted, pixelated"
#////////////////////////////////////////////////////////////////
pipe1 = None
pipe2 = None
#////////////////////////////////////////////////////////////////
def load():
global pipe1, pipe2
# scheduler = DPMSolverMultistepScheduler(
# beta_start=0.00085,
# beta_end=0.012,
# beta_schedule="scaled_linear",
# num_train_timesteps=1000,
# trained_betas=None,
# thresholding=False,
# algorithm_type="dpmsolver++",
# solver_type="midpoint",
# lower_order_final=True,
# )
pipe1 = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16")#, scheduler=scheduler)
pipe1.to("cuda")
pipe1.enable_xformers_memory_efficient_attention()
pipe2 = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", torch_dtype=torch.float16, variant="fp16")
pipe2.to("cuda")
pipe2.enable_xformers_memory_efficient_attention()
def generate(prompt, file_prefix ,samples, seed):
global pipe1, pipe2
torch.manual_seed(seed)
prompt += prompt_suffix
base_model_latents = pipe1([prompt] * samples, negative_prompt = [neg_prompt] * samples, num_inference_steps=steps_base, guidance_scale=guidance_scale, height=height, width=width, output_type="latent")["images"]
torch.manual_seed(seed)
refiner_model_images = pipe2([prompt] * samples, negative_prompt = [neg_prompt] * samples, num_inference_steps=steps_refiner, image=base_model_latents)["images"]
for idx, image in enumerate(refiner_model_images):
image.save(f"{file_prefix}-{idx}-{seed}--{width}x{height}--{guidance_scale}--{base_model_id_str}.jpg")
def main():
load()
generate("A livingroom", "01_LivingRoom", 2, 7777)
generate("A nice town", "02_NiceTown", 2, 555)
generate("Nicolas Cage, in \"The Minions\" movie", "03_NicolasCage", 4, 42)
generate("Gal Gadot as wonderwoman", "04_GalGadot", 2, 42)
generate("Marge Simpson", "05_MargeSimpson", 2, 42)
generate("A beautiful woman", "06_BeautifulWoman", 2, 42)
generate("A magical landscape", "07_MagicalLandscape", 4, 42)
generate("Cute dog, will lick you to sleep", "08_CuteDog", 2, 42)
generate("An oil on canvas portrait of Snoop Dogg, Mark Ryden", "09_SnoopDog", 2, 777)
generate("A flemish baroque painting of Kermit from the muppet show", "10_KermitFlemishBaroque", 2, 42)
generate("Gal Gadot in Avatar", "11_GalGadotAvatar", 2, 777)
generate("Ninja turtles, Naoto Hattori", "12_TMNT", 2, 312)
generate("An anime town", "12_AnimeTown", 2, 777)
generate("Family guy taking selfies at the beach", "13_FamilyGuy", 2, 555)
generate("Pikachu as Rick and morty, Eric Wallis", "14_PikachuRnM", 2, 777)
generate("Pikachu as Spongebob, Eric Wallis", "15_PikachuSpongeBob", 2, 42)
generate("An oil painting of Miss. Piggy from the muppets as the Mona Lisa", "16_MsPiggyMonaLisa", 2, 42)
generate("Rick Sanchez from the TV show \"Rick and Morty\"", "17_RickSanchez", 2, 42)
generate("An paiting of Southpark with rainbow", "18_Southpark", 2, 777)
generate("An oil painting of Phineas and Pherb hamering on a new machine, Eric Wallis", "19_PhineasPherb", 2, 777)
generate("Bender, Saturno Butto", "20_Bender", 2, 777)
generate("A psychedelic image of Bojack Horseman", "21_Bojack", 2, 777)
generate("A movie poster for Gravity Falls Cthulhu stories", "22_GravityFalls", 2, 777)
generate("A vibrant oil painting portrait of She-Ra", "23_Shira", 2, 512)
#
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment