Created
July 28, 2023 18:10
-
-
Save Norod/d0e732516b453c986466fa2dfc3cd07a to your computer and use it in GitHub Desktop.
Testing stable-diffusion-xl 1.0 generation pipelines using simple random prompts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler | |
import torch | |
#//////////////////////////////////////////////////////////////// | |
guidance_scale=7.5 | |
steps=40 | |
width=1024 | |
height=1024 | |
base_model_id_str = "sdxl_10" | |
prompt_suffix = ", Very detailed, clean, high quality, sharp image" | |
neg_prompt = "text, watermark, grainy, blurry, unfocused, nsfw, naked, nude, noisy image, deformed, distorted, pixelated" | |
#//////////////////////////////////////////////////////////////// | |
base = None | |
refiner = None | |
#//////////////////////////////////////////////////////////////// | |
def load(): | |
global base, refiner | |
# load both base & refiner | |
base = DiffusionPipeline.from_pretrained( | |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True | |
) | |
base.to("cuda") | |
base.enable_xformers_memory_efficient_attention() | |
refiner = DiffusionPipeline.from_pretrained( | |
"stabilityai/stable-diffusion-xl-refiner-1.0", | |
text_encoder_2=base.text_encoder_2, | |
vae=base.vae, | |
torch_dtype=torch.float16, | |
use_safetensors=True, | |
variant="fp16", | |
) | |
refiner.to("cuda") | |
refiner.enable_xformers_memory_efficient_attention() | |
def generate(prompt, file_prefix ,samples, seed): | |
global base, refiner | |
torch.manual_seed(seed) | |
prompt += prompt_suffix | |
base_model_latents = base([prompt] * samples, | |
negative_prompt = [neg_prompt] * samples, | |
num_inference_steps=steps, | |
guidance_scale=guidance_scale, | |
height=height, width=width, | |
output_type="latent")["images"] | |
torch.manual_seed(seed) | |
refiner_model_images = refiner([prompt] * samples, | |
negative_prompt = [neg_prompt] * samples, | |
num_inference_steps=steps, | |
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", "13_AnimeTown", 2, 777) | |
generate("Family guy taking selfies at the beach", "14_FamilyGuy", 2, 555) | |
generate("Pikachu as Rick and morty, Eric Wallis", "15_PikachuRnM", 2, 777) | |
generate("Pikachu as Spongebob, Eric Wallis", "16_PikachuSpongeBob", 2, 42) | |
generate("An oil painting of Miss. Piggy from the muppets as the Mona Lisa", "17_MsPiggyMonaLisa", 2, 42) | |
generate("Rick Sanchez from the TV show \"Rick and Morty\"", "18_RickSanchez", 2, 42) | |
generate("A paiting of Southpark with rainbow", "19_Southpark", 2, 777) | |
generate("An oil painting of Phineas and Pherb hamering on a new machine, Eric Wallis", "20_PhineasPherb", 2, 777) | |
generate("Bender, Saturno Butto", "21_Bender", 2, 777) | |
generate("A psychedelic image of Bojack Horseman", "22_Bojack", 2, 777) | |
generate("A movie poster for Gravity Falls Cthulhu stories", "23_GravityFalls", 2, 777) | |
generate("A vibrant oil painting portrait of She-Ra", "24_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