File size: 6,939 Bytes
ab2369a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import sys
import torch
import os
import argparse
sys.path.append(os.getcwd())
from PIL import Image
import torchvision.transforms as transforms
import numpy as np
from diffusers import StableDiffusionPipeline, DDIMScheduler
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
from diffusers.utils import load_image
from diffusers import PNDMScheduler, UniPCMultistepScheduler,DDIMScheduler
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel
from scheduler.scheduling_dpmsolver_multistep_lm import DPMSolverMultistepLMScheduler
from scheduler.scheduling_ddim_lm import DDIMLMScheduler
from transformers import pipeline
import cv2
import numpy as np
def main():
parser = argparse.ArgumentParser(description="sampling script for ControlNet-depth.")
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--num_inference_steps', type=int, default=20)
parser.add_argument('--guidance', type=float, default=7.5)
parser.add_argument('--sampler_type', type = str,default='lag')
parser.add_argument('--model', type=str, default='sd2_base', choices=['sd15', 'sd2_base'])
parser.add_argument('--prompt', type=str, default='an asian girl')
parser.add_argument('--lamb', type=float, default=5.0)
parser.add_argument('--kappa', type=float, default=0.0)
parser.add_argument('--freeze', type=float, default=0.0)
parser.add_argument('--prompt_list', nargs='+', type=str,
default=['an asian girl'])
parser.add_argument('--save_dir', type=str, default='/xxx/xxx/result/0402')
parser.add_argument('--controlnet_dir', type=str, default="/xxx/xxx/control_v11f1p_sd15_depth")
parser.add_argument('--sd_dir', type=str, default="/xxx/xxx/stable-diffusion-v1-5")
args = parser.parse_args()
if args.sampler_type in ['bdia']:
parser.add_argument('--bdia_gamma', type=float, default=0.5)
if args.sampler_type in ['edict']:
parser.add_argument('--edict_p', type=float, default=0.93)
args = parser.parse_args()
device = 'cuda'
sampler_type = args.sampler_type
guidance_scale = args.guidance
num_inference_steps = args.num_inference_steps
lamb = args.lamb
freeze = args.freeze
kappa = args.kappa
save_dir = args.save_dir
if not os.path.exists(save_dir):
os.makedirs(save_dir, exist_ok=True)
# torch.manual_seed(args.seed)
controlnet = ControlNetModel.from_pretrained(args.controlnet_dir, torch_dtype=torch.float16, use_safetensors=True)
control_pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
args.sd_dir,
controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True
)
control_pipe.enable_model_cpu_offload()
control_pipe.safety_checker = None
if sampler_type in ['dpm_lm']:
control_pipe.scheduler = DPMSolverMultistepLMScheduler.from_config(control_pipe.scheduler.config)
control_pipe.scheduler.config.solver_order = 3
control_pipe.scheduler.config.algorithm_type = "dpmsolver"
control_pipe.scheduler.lamb = lamb
control_pipe.scheduler.lm = True
elif sampler_type in ['dpm']:
control_pipe.scheduler = DPMSolverMultistepLMScheduler.from_config(control_pipe.scheduler.config)
control_pipe.scheduler.config.solver_order = 3
control_pipe.scheduler.config.algorithm_type = "dpmsolver"
control_pipe.scheduler.lamb = lamb
control_pipe.scheduler.lm = False
elif sampler_type in ['dpm++']:
control_pipe.scheduler = DPMSolverMultistepLMScheduler.from_config(control_pipe.scheduler.config)
control_pipe.scheduler.config.solver_order = 3
control_pipe.scheduler.config.algorithm_type = "dpmsolver++"
control_pipe.scheduler.lamb = lamb
control_pipe.scheduler.lm = False
elif sampler_type in ['dpm++_lm']:
control_pipe.scheduler = DPMSolverMultistepLMScheduler.from_config(control_pipe.scheduler.config)
control_pipe.scheduler.config.solver_order = 3
control_pipe.scheduler.config.algorithm_type = "dpmsolver++"
control_pipe.scheduler.lamb = lamb
control_pipe.scheduler.lm = True
elif sampler_type in ['pndm']:
control_pipe.scheduler = PNDMScheduler.from_config(control_pipe.scheduler.config)
elif sampler_type in ['ddim']:
control_pipe.scheduler = DDIMScheduler.from_config(control_pipe.scheduler.config)
# control_pipe.scheduler.lamb = lamb
# control_pipe.scheduler.lm = False
# control_pipe.scheduler.kappa = kappa
elif sampler_type in ['ddim_lm']:
control_pipe.scheduler = DDIMLMScheduler.from_config(control_pipe.scheduler.config)
control_pipe.scheduler.lamb = lamb
control_pipe.scheduler.lm = True
control_pipe.scheduler.kappa = kappa
control_pipe.scheduler.freeze = freeze
elif sampler_type in ['unipc']:
control_pipe.scheduler = UniPCMultistepScheduler.from_config(control_pipe.scheduler.config)
image = load_image(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/controlnet-img2img.jpg"
)
def get_depth_map(image, depth_estimator):
image = depth_estimator(image)["depth"]
image = np.array(image)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
detected_map = torch.from_numpy(image).float() / 255.0
depth_map = detected_map.permute(2, 0, 1)
return depth_map
depth_estimator = pipeline("depth-estimation")
depth_map = get_depth_map(image, depth_estimator).unsqueeze(0).half().to("cuda")
transforms.ToPILImage()(depth_map[0]).save(os.path.join(save_dir,
f"depth_map.png"))
for prompt, negative_prompt in [["lego batman and robin",''],
["Spider-Man and Superman", ''],
["A girl and a boy", ''],
["asian woman and asian man", ''],
["American Indian woman and American Indian man", ''],
["A girl and a girl, monalisa style", ''],
["Elsa and Anna, in the movie Frozen", ''],
["A woman and a man, wearing suit", ''],
]:
for seed in range(20):
torch.manual_seed(seed)
res = control_pipe(
prompt = prompt, image=image, control_image=depth_map,num_inference_steps=num_inference_steps,
).images[0]
res.save(os.path.join(save_dir,
f"{prompt[:20]}_seed{seed}_{sampler_type}_infer{num_inference_steps}_g{guidance_scale}_lamb{args.lamb}.png"))
if __name__ == '__main__':
main() |