-
Notifications
You must be signed in to change notification settings - Fork 1
/
end2end_ddim_inv.py
230 lines (187 loc) · 8.2 KB
/
end2end_ddim_inv.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from pipelines.deepfloyd_pipeline import IFPipeline
from pipelines.deepfloyd_inv_pipeline import IFInvPipeline
from pipelines.deepfloyd_SR_pipeline import IFSuperResolutionPipeline
from pipelines.deepfloyd_SR_inv_pipeline import IFSuperResolutionInvPipeline
from pipelines.SDUP_pipeline import StableDiffusionUpscalePipeline
from pipelines.scheduler_ddim import DDIMScheduler
from pipelines.scheduler_ddpm import DDPMScheduler
from pipelines.scheduler_inv import DDIMInverseScheduler
from diffusers.utils import pt_to_pil, numpy_to_pil
import torch
from IPython.display import display
import numpy as np
from PIL import Image
# torch.cuda.set_device(3)
device = torch.device('cuda')
from diffusers.image_processor import VaeImageProcessor
import argparse
import os
### NOTE: image_processor can help to make the batch size ready
image_processor=VaeImageProcessor()
def pil_to_numpy_torch(pil_img: Image):
image_org = np.array(pil_img).astype(np.float32).transpose(2,0,1)/255.0
image_org = 2.0 * image_org - 1.0
image_org = torch.from_numpy(image_org)
return image_org
def arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--input_image', type=str, default='images/cat_hq.jpg')
parser.add_argument('--prompt_str', type=str, default='a cat in an astronaut suit')
parser.add_argument('--output_fold', type=str, default='')
args = parser.parse_args()
return args
if __name__=="__main__":
args = arguments()
print(args)
saving_path = f'DDIM_output/{args.output_fold}/{args.prompt_str}'
# os.makedirs(saving_path, exist_ok=True)
generator = torch.manual_seed(0)
# stages 1
stage_1 = IFInvPipeline.from_pretrained("DeepFloyd/IF-I-M-v1.0", variant="fp32", torch_dtype=torch.float32)
stage_1.enable_model_cpu_offload()
# stage 2
# stage_2 = IFSuperResolutionPipeline.from_pretrained("DeepFloyd/IF-II-M-v1.0",
# text_encoder=None, variant="fp32", torch_dtype=torch.float32)
# stage_2.enable_model_cpu_offload()
# stages 3for2
### NOTE: upscaler can only be float32
stage_2 = StableDiffusionUpscalePipeline.from_pretrained(
"stabilityai/stable-diffusion-x4-upscaler",
torch_dtype=torch.float32)
stage_2.enable_model_cpu_offload()
# stages 3
### NOTE: upscaler can only be float32
stage_3 = StableDiffusionUpscalePipeline.from_pretrained(
"stabilityai/stable-diffusion-x4-upscaler",
torch_dtype=torch.float32)
stage_3.enable_model_cpu_offload()
_inv_raw_image_2 = Image.open(args.input_image).convert("RGB").resize((1024,1024))
inv_raw_image_2 = pil_to_numpy_torch(_inv_raw_image_2)
_inv_raw_image_1 = Image.open(args.input_image).convert("RGB").resize((256,256))
inv_raw_image_1 = pil_to_numpy_torch(_inv_raw_image_1)
_inv_raw_image_0 = Image.open(args.input_image).convert("RGB").resize((64,64))
inv_raw_image_0 = pil_to_numpy_torch(_inv_raw_image_0)
# stage 1 inversion
# text embeds
stage_1.scheduler = DDIMInverseScheduler.from_config(stage_1.scheduler.config)
num_inference_steps = 50
prompt_embeds, negative_embeds = stage_1.encode_prompt(args.prompt_str)
output_inv_1, inter_img_list, uncond_embeddings_list = stage_1(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_embeds,
generator=generator,
num_inference_steps=num_inference_steps,
output_type="pt",
image_init=inv_raw_image_0,
guidance_scale=1.0,
)
# torch.save(inter_img_list, f'{saving_path}/inter_img_list_1.pt')
inv_noise_1 = output_inv_1.images
torch.cuda.empty_cache()
stage_1.to('cpu')
# stage 1 reconstraction
stage_1.scheduler = DDIMScheduler.from_config(stage_1.scheduler.config)
output_rec_1, inter_img_list, uncond_embeddings_list = stage_1(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_embeds,
generator=generator,
num_inference_steps=num_inference_steps,
output_type="pt",
image_init=inv_noise_1,
guidance_scale=7.5,
all_latents=inter_img_list,
null_inner_steps = 11,
is_NPI=False,
)
image_rec_1 = output_rec_1.images
torch.cuda.empty_cache()
stage_1.to('cpu')
# stage 2 upscale
# stage_2.scheduler = DDIMScheduler.from_config(stage_2.scheduler.config)
# noise_level=250
### NOTE: stage 2 is without text_encoder
# image_tuple_2_sr, _ = stage_2(
# image=image_rec_1,
# # image=[inv_raw_image_1],
# generator=generator,
# prompt_embeds=prompt_embeds,
# negative_prompt_embeds=negative_embeds,
# output_type="pt",
# guidance_scale=1.0,
# noise_level=noise_level
# )
# image_2_sr = image_tuple_2_sr.images
# # pil_image_2_sr = pt_to_pil(image_2_sr)
# stage_2.to('cpu')
# torch.cuda.empty_cache()
# image_2_rec = image_2_sr
# stage 2 inversion
with torch.no_grad():
latent = stage_2.prepare_image_latents(inv_raw_image_1.unsqueeze(0).to(device), 1, stage_2.vae.dtype, device, generator=generator)
torch.cuda.empty_cache()
stage_2.scheduler = DDIMInverseScheduler.from_config(stage_2.scheduler.config)
noise_level_2=100
image_tuple_2_inv, _ = stage_2(
prompt=args.prompt_str,
image=image_rec_1,
noise_level=noise_level_2,
generator=generator,
output_type="latent",
guidance_scale=1.0,
latents=latent,
num_inference_steps=100,
)
image_2_inv=image_tuple_2_inv.images
torch.cuda.empty_cache()
stage_2.to('cpu')
# stage 2 reconstraction
stage_2.scheduler = DDIMScheduler.from_config(stage_2.scheduler.config)
image_tuple_2_rec, _ = stage_2(
prompt=args.prompt_str,
image=image_rec_1,
noise_level=noise_level_2,
generator=generator,
output_type="pil",
guidance_scale=1.0,
num_inference_steps=100,
latents=image_2_inv
)
image_2_rec=image_tuple_2_rec.images
torch.cuda.empty_cache()
stage_2.to('cpu')
# stage 3 inversion
with torch.no_grad():
latent = stage_3.prepare_image_latents(inv_raw_image_2.unsqueeze(0).to(device), 1, stage_3.vae.dtype, device,generator=generator)
torch.cuda.empty_cache()
stage_3.scheduler = DDIMInverseScheduler.from_config(stage_3.scheduler.config)
noise_level_3=100
image_tuple_3_inv, _ = stage_3(
prompt=args.prompt_str,
image=image_2_rec,
noise_level=noise_level_3,
generator=generator,
output_type="latent",
guidance_scale=1.0,
latents=latent,
num_inference_steps=100,
)
image_3_inv=image_tuple_3_inv.images
torch.cuda.empty_cache()
stage_3.to('cpu')
# stage 3 reconstraction
stage_3.scheduler = DDIMScheduler.from_config(stage_3.scheduler.config)
noise_level_3=100
image_tuple_3_rec, _ = stage_3(
prompt=args.prompt_str,
image=image_2_rec,
noise_level=noise_level_3,
generator=generator,
output_type="pil",
guidance_scale=1.0,
num_inference_steps=100,
latents=image_3_inv
)
image_3_rec=image_tuple_3_rec.images
torch.cuda.empty_cache()
stage_3.to('cpu')
image_3_rec[0].save(f"{saving_path}_ddim_stage123_III_rec.png")