Skip to content

Instantly share code, notes, and snippets.

@Norod
Last active July 29, 2023 20:12
Show Gist options
  • Select an option

  • Save Norod/76cf39f08e4f458e16fd2af111cc0af0 to your computer and use it in GitHub Desktop.

Select an option

Save Norod/76cf39f08e4f458e16fd2af111cc0af0 to your computer and use it in GitHub Desktop.
Compare two Stable-Diffusion diffuser models using a predifined set of prompts
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import torch
#////////////////////////////////////////////////////////////////
guidance_scale=8.0
steps=40
width=512
height=512
prompt_suffix = ", Very detailed, clean, high quality, sharp image"
#////////////////////////////////////////////////////////////////
base_model_id = "stabilityai/stable-diffusion-2-1-base"
custom_model_id = "Norod78/sd2-cartoon-blip"
#////////////////////////////////////////////////////////////////
base_model_id_str = base_model_id.replace("/","_")
custom_model_id_str = custom_model_id.replace("/","_")
base_model_pipe = None
custom_model_pipe = None
def generate(prompt, file_prefix ,samples, seed):
global base_model_pipe, custom_model_pipe
torch.manual_seed(seed)
prompt += prompt_suffix
file_prefix += "Compare"
base_model_images = base_model_pipe([prompt] * samples, num_inference_steps=steps, guidance_scale=guidance_scale, height=height, width=width)["images"]
for idx, image in enumerate(base_model_images):
image.save(f"{file_prefix}-{idx}-{seed}--{width}x{height}-{base_model_id_str}.jpg")
torch.manual_seed(seed)
custom_model_images = custom_model_pipe([prompt] * samples, num_inference_steps=steps, guidance_scale=guidance_scale, height=height, width=width)["images"]
for idx, image in enumerate(custom_model_images):
image.save(f"{file_prefix}-{idx}-{seed}--{width}x{height}-{custom_model_id_str}.jpg")
def load():
global base_model_pipe, custom_model_pipe
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,
)
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32
base_model_pipe = StableDiffusionPipeline.from_pretrained(base_model_id, scheduler=scheduler,torch_dtype=dtype).to(device)
custom_model_pipe = StableDiffusionPipeline.from_pretrained(custom_model_id, scheduler=scheduler,torch_dtype=dtype).to(device)
def main():
load()
generate("A livingroom", "01_LivingRoom", 4, 555)
generate("A nice town", "02_NiceTown", 4, 555)
generate("Nicolas Cage, in \"The Minions\" movie", "03_NicolasCage", 2, 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