Views
No views yet
1import random, os
2import numpy as np
3from pathlib import Path
4import torch, torchvision
5
6def setup_seed(seed):
7 random.seed(seed)
8 np.random.seed(seed)
9 torch.manual_seed(seed)
10 torch.cuda.manual_seed_all(seed)
11 torch.backends.cudnn.deterministic = True
12
13Path("demo").mkdir(parents=True, exist_ok=True)
14
15
16from diffusers import StableDiffusionXLPipeline
17pipe = StableDiffusionXLPipeline.from_pretrained("hansyan/perflow-sdxl-dreamshaper", torch_dtype=torch.float16, use_safetensors=True, variant="v0-fix")
18from src.scheduler_perflow import PeRFlowScheduler
19pipe.scheduler = PeRFlowScheduler.from_config(pipe.scheduler.config, prediction_type="ddim_eps", num_time_windows=4)
20pipe.to("cuda", torch.float16)
21
22
23prompts_list = [
24 ["photorealistic, uhd, high resolution, high quality, highly detailed; RAW photo, a handsome man, wearing a black coat, outside, closeup face",
25 "distorted, blur, low-quality, haze, out of focus",],
26 ["photorealistic, uhd, high resolution, high quality, highly detailed; masterpiece, A closeup face photo of girl, wearing a rain coat, in the street, heavy rain, bokeh,",
27 "distorted, blur, low-quality, haze, out of focus",],
28 ["photorealistic, uhd, high resolution, high quality, highly detailed; RAW photo, a red luxury car, studio light",
29 "distorted, blur, low-quality, haze, out of focus",],
30 ["photorealistic, uhd, high resolution, high quality, highly detailed; masterpiece, A beautiful cat bask in the sun",
31 "distorted, blur, low-quality, haze, out of focus",],
32]
33
34num_inference_steps = 6 # suggest steps >= num_win=4
35cfg_scale_list = [2.0] # suggest values [1.5, 2.0, 2.5]
36num_img = 2
37seed = 42
38
39for cfg_scale in cfg_scale_list:
40 for i, prompts in enumerate(prompts_list):
41 setup_seed(seed)
42 prompt, neg_prompt = prompts[0], prompts[1]
43 samples = pipe(
44 prompt = [prompt] * num_img,
45 negative_prompt = [neg_prompt] * num_img,
46 height = 1024,
47 width = 1024,
48 num_inference_steps = num_inference_steps,
49 guidance_scale = cfg_scale,
50 output_type = 'pt',
51 ).images
52
53 cfg_int = int(cfg_scale); cfg_float = int(cfg_scale*10 - cfg_int*10)
54 save_name = f'step_{num_inference_steps}_txt{i+1}_cfg{cfg_int}-{cfg_float}.png'
55 torchvision.utils.save_image(torchvision.utils.make_grid(samples, nrow = num_img), os.path.join("demo", save_name))
56