-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable_diffusion.py
275 lines (206 loc) · 11.6 KB
/
stable_diffusion.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from huggingface_hub import hf_hub_download
from torchvision import transforms
from transformers import CLIPTextModel, CLIPTokenizer, CLIPVisionModel,logging,CLIPProcessor
from diffusers import AutoencoderKL, UNet2DConditionModel, PNDMScheduler
# suppress partial model loading warning
logging.set_verbosity_error()
import torch
import torch.nn as nn
import torch.nn.functional as F
from loguru import logger
from PIL import Image
import cv2
import time
class StableDiffusion(nn.Module):
def __init__(self, device, model_name='CompVis/stable-diffusion-v1-4',concept_name=None, latent_mode=True, half=True):
super().__init__()
try:
with open('./TOKEN', 'r') as f:
self.token = f.read().replace('\n', '') # remove the last \n!
logger.info(f'loaded hugging face access token from ./TOKEN!')
except FileNotFoundError as e:
self.token = True
logger.warning(f'try to load hugging face access token from the default place, make sure you have run `huggingface-cli login`.')
self.device = device
self.latent_mode = latent_mode
self.num_train_timesteps = 1000
self.min_step = int(self.num_train_timesteps * 0.02)
self.max_step = int(self.num_train_timesteps * 0.98)
logger.info(f'loading stable diffusion with {model_name}...')
# 1. Load the autoencoder model which will be used to decode the latents into image space.
# self.vae = AutoencoderKL.from_pretrained(model_name, subfolder="vae", use_auth_token=self.token).to(self.device)
if half:
self.vae = AutoencoderKL.from_pretrained(model_name, subfolder="vae", use_auth_token=self.token).half().to(self.device)
else:
self.vae = AutoencoderKL.from_pretrained(model_name, subfolder="vae", use_auth_token=self.token).to(self.device)
# 2. Load the tokenizer and text encoder to tokenize and encode the text.
# self.text_encoder = CLIPTextModel.from_pretrained("openai/clip-vit-large-patch14").to(self.device)
self.tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
self.text_encoder = CLIPTextModel.from_pretrained("openai/clip-vit-large-patch14").to(self.device)
self.image_encoder = None
self.image_processor = None
# 3. The UNet model for generating the latents.
# self.unet = UNet2DConditionModel.from_pretrained(model_name, subfolder="unet", use_auth_token=self.token).to(self.device)
if half:
self.unet = UNet2DConditionModel.from_pretrained(model_name, subfolder="unet", use_auth_token=self.token).half().to(self.device)
else:
self.unet = UNet2DConditionModel.from_pretrained(model_name, subfolder="unet", use_auth_token=self.token).to(self.device)
# 4. Create a scheduler for inference
self.scheduler = PNDMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=self.num_train_timesteps)
# self.alphas = self.scheduler.alphas_cumprod.to(self.device) # for convenience
if half:
self.alphas = self.scheduler.alphas_cumprod.half().to(self.device) # for convenience
else:
self.alphas = self.scheduler.alphas_cumprod.half().to(self.device) # for convenience
if concept_name is not None:
self.load_concept(concept_name)
logger.info(f'\t successfully loaded stable diffusion!')
def load_concept(self, concept_name):
repo_id_embeds = f"sd-concepts-library/{concept_name}"
learned_embeds_path = hf_hub_download(repo_id=repo_id_embeds, filename="learned_embeds.bin")
token_path = hf_hub_download(repo_id=repo_id_embeds, filename="token_identifier.txt")
with open(token_path, 'r') as file:
placeholder_token_string = file.read()
loaded_learned_embeds = torch.load(learned_embeds_path, map_location="cpu")
# separate token and the embeds
trained_token = list(loaded_learned_embeds.keys())[0]
embeds = loaded_learned_embeds[trained_token]
# cast to dtype of text_encoder
dtype = self.text_encoder.get_input_embeddings().weight.dtype
embeds.to(dtype)
# add the token in tokenizer
token = trained_token
num_added_tokens = self.tokenizer.add_tokens(token)
if num_added_tokens == 0:
raise ValueError(
f"The tokenizer already contains the token {token}. Please pass a different `token` that is not already in the tokenizer.")
# resize the token embeddings
self.text_encoder.resize_token_embeddings(len(self.tokenizer))
# get the id for the token and assign the embeds
token_id = self.tokenizer.convert_tokens_to_ids(token)
self.text_encoder.get_input_embeddings().weight.data[token_id] = embeds
def get_text_embeds(self, prompt):
# Tokenize text and get embeddings
text_input = self.tokenizer(prompt, padding='max_length', max_length=self.tokenizer.model_max_length, truncation=True, return_tensors='pt')
with torch.no_grad():
text_embeddings = self.text_encoder(text_input.input_ids.to(self.device))[0]
# Do the same for unconditional embeddings
uncond_input = self.tokenizer([''] * len(prompt), padding='max_length', max_length=self.tokenizer.model_max_length, return_tensors='pt')
with torch.no_grad():
uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(self.device))[0]
# Cat for final embeddings
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
return text_embeddings
def train_step(self, text_embeddings, inputs, guidance_scale=100, gpu_tracker=None, params_to_train=None):
# interp to 512x512 to be fed into vae.
# _t = time.time()
if not self.latent_mode:
# latents = F.interpolate(latents, (64, 64), mode='bilinear', align_corners=False)
pred_rgb_512 = F.interpolate(inputs, (512, 512), mode='bilinear', align_corners=False)
latents = self.encode_imgs(pred_rgb_512)
else:
# latents = inputs
latents = F.interpolate(inputs, (64, 64), mode='bilinear', align_corners=False)
# torch.cuda.synchronize(); print(f'[TIME] guiding: interp {time.time() - _t:.4f}s')
# timestep ~ U(0.02, 0.98) to avoid very high/low noise level
t = torch.randint(self.min_step, self.max_step + 1, [1], dtype=torch.long, device=self.device)
# encode image into latents with vae, requires grad!
# _t = time.time()
# torch.cuda.synchronize(); print(f'[TIME] guiding: vae enc {time.time() - _t:.4f}s')
# predict the noise residual with unet, NO grad!
# _t = time.time()
with torch.no_grad():
# add noise
noise = torch.randn_like(latents)
latents_noisy = self.scheduler.add_noise(latents, noise, t)
# pred noise
latent_model_input = torch.cat([latents_noisy] * 2)
noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample
# torch.cuda.synchronize(); print(f'[TIME] guiding: unet {time.time() - _t:.4f}s')
# perform guidance (high scale from paper!)
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
# w(t), alpha_t * sigma_t^2
# w = (1 - self.alphas[t])
w = self.alphas[t] ** 0.5 * (1 - self.alphas[t])
grad = w * (noise_pred - noise)
# clip grad for stable training?
grad = grad.clamp(-1, 1)
# manually backward, since we omitted an item in grad and cannot simply autodiff.
# _t = time.time()
# for param in params_to_train:
# print(param.grad)
# print("gradient: ", grad)
# with torch.autograd.detect_anomaly():
# latents.backward(gradient=grad, retain_graph=True)
latents.backward(gradient=grad, retain_graph=True)
# torch.autograd.grad(outputs=latents, inputs=params_to_train, grad_outputs=grad)
# for param in params_to_train:
# print(param.grad)
# raise NotImplementedError
# torch.cuda.synchronize(); print(f'[TIME] guiding: backward {time.time() - _t:.4f}s')
return 0 # dummy loss value
def produce_latents(self, text_embeddings, height=512, width=512, num_inference_steps=50, guidance_scale=7.5, latents=None):
if latents is None:
latents = torch.randn((text_embeddings.shape[0] // 2, self.unet.in_channels, height // 8, width // 8), device=self.device)
self.scheduler.set_timesteps(num_inference_steps)
with torch.autocast('cuda'):
for i, t in enumerate(self.scheduler.timesteps):
# expand the latents if we are doing classifier-free guidance to avoid doing two forward passes.
latent_model_input = torch.cat([latents] * 2)
# predict the noise residual
with torch.no_grad():
noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings)['sample']
# perform guidance
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
# compute the previous noisy sample x_t -> x_t-1
latents = self.scheduler.step(noise_pred, t, latents)['prev_sample']
return latents
def decode_latents(self, latents):
# latents = F.interpolate(latents, (64, 64), mode='bilinear', align_corners=False)
latents = 1 / 0.18215 * latents
with torch.no_grad():
imgs = self.vae.decode(latents).sample
imgs = (imgs / 2 + 0.5).clamp(0, 1)
return imgs
def encode_imgs(self, imgs):
# imgs: [B, 3, H, W]
imgs = 2 * imgs - 1
posterior = self.vae.encode(imgs).latent_dist
latents = posterior.sample() * 0.18215
return latents
def prompt_to_img(self, prompts, height=512, width=512, num_inference_steps=50, guidance_scale=7.5, latents=None):
if isinstance(prompts, str):
prompts = [prompts]
# Prompts -> text embeds
text_embeds = self.get_text_embeds(prompts) # [2, 77, 768]
# Text embeds -> img latents
latents = self.produce_latents(text_embeds, height=height, width=width, latents=latents, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale) # [1, 4, 64, 64]
# Img latents -> imgs
imgs = self.decode_latents(latents.half()) # [1, 3, 512, 512]
# Img to Numpy
imgs = imgs.detach().cpu().permute(0, 2, 3, 1).squeeze(0).numpy()
imgs = (imgs * 255).round().astype('uint8')
return imgs
if __name__ == '__main__':
import argparse
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument('--prompt', type=str)
parser.add_argument('-H', type=int, default=512)
parser.add_argument('-W', type=int, default=512)
parser.add_argument('--steps', type=int, default=50)
parser.add_argument('--num', type=int, default=1)
opt = parser.parse_args()
device = torch.device('cuda:0')
sd = StableDiffusion(device)
imgs = []
for i in range(opt.num):
img = sd.prompt_to_img(opt.prompt, opt.H, opt.W, opt.steps)
imgs.append(img)
for i, img in enumerate(imgs):
cv2.imwrite(f'stb_{i}.png', cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
# visualize image
# plt.imshow(imgs[0])
# plt.show()