forked from MFaceTech/HyperDreamBooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT2I_inference_merge_lora.py
59 lines (46 loc) · 2.38 KB
/
T2I_inference_merge_lora.py
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
from diffusers import StableDiffusionPipeline, DDIMScheduler
import torch
import random
pretrain_model_path="stable-diffusion-models/realisticVisionV40_v40VAE"
noise_scheduler = DDIMScheduler(
num_train_timesteps=1000,
beta_start=0.00085,
beta_end=0.012,
beta_schedule="scaled_linear",
clip_sample=False,
set_alpha_to_one=False,
steps_offset=1
)
pipe = StableDiffusionPipeline.from_pretrained(pretrain_model_path,
torch_dtype=torch.float16,
scheduler=noise_scheduler,
requires_safety_checker=False)
# personal lora
dir1 = "projects/AIGC/lora_model_test"
lora_model_path1 = "pytorch_lora_weights.safetensors"
# prompt = "A [V] face"
dir2="projects/AIGC/experiments/style_lora"
lora_model_path2 = "pixarStyleModel_lora128.safetensors"
# lora_model_path2 = "Watercolor_Painting_by_vizsumit.safetensors"
# lora_model_path2 = "Professional_Portrait_1.5-000008.safetensors"
prompt = "A pixarstyle of a [V] face"
# prompt = "A watercolor paint of a [V] face"
# prompt = "A professional portrait of a [V] face"
negative_prompt = "nsfw,easynegative"
pipe.to("cuda")
pipe.load_lora_weights(dir1, weight_name=lora_model_path1, adapter_name="person")
pipe.load_lora_weights(dir2, weight_name=lora_model_path2, adapter_name="style")
# pipe.set_adapters(["person", "style"], adapter_weights=[0.6, 0.4]) #pixar
pipe.set_adapters(["person", "style"], adapter_weights=[0.4, 0.4]) #watercolor
# Fuses the LoRAs into the Unet
pipe.fuse_lora()
for i in range(10):
seed = random.randint(0, 100)
generator = torch.Generator(device="cuda").manual_seed(seed)
# image = pipe(prompt, negative_prompt=negative_prompt, height=512, width=512, num_inference_steps=30, guidance_scale=7.5,cross_attention_kwargs={"scale":1}).images[0]
# image = pipe(prompt, height=512, width=512, num_inference_steps=30, guidance_scale=7.5, cross_attention_kwargs={"scale": 1.0}, generator=torch.manual_seed(0)).images[0]
# image = pipe(prompt, height=512, width=512, num_inference_steps=30, guidance_scale=7.5, cross_attention_kwargs={"scale": 1.0}).images[0]
image = pipe(prompt, height=512, width=512, num_inference_steps=30, generator=generator).images[0]
image.save("aigc_samples/test_after_export_%d.jpg" % i)
# Gets the Unet back to the original state
pipe.unfuse_lora()