forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable-diffusion-txt2img.py
580 lines (464 loc) · 18.3 KB
/
stable-diffusion-txt2img.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import os
import sys
import time
import numpy as np
import cv2
from transformers import CLIPTokenizer, CLIPTextModel
import ailia
# import original modules
sys.path.append('../../util')
from arg_utils import get_base_parser, update_parser, get_savepath # noqa
from model_utils import check_and_download_models # noqa
# logger
from logging import getLogger # noqa
from constants import alphas_cumprod
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_DFSN_EMB_PATH = 'diffusion_emb.onnx'
MODEL_DFSN_EMB_PATH = 'diffusion_emb.onnx.prototxt'
WEIGHT_DFSN_MID_PATH = 'diffusion_mid.onnx'
MODEL_DFSN_MID_PATH = 'diffusion_mid.onnx.prototxt'
WEIGHT_DFSN_OUT_PATH = 'diffusion_out.onnx'
MODEL_DFSN_OUT_PATH = 'diffusion_out.onnx.prototxt'
WEIGHT_AUTO_ENC_PATH = 'autoencoder.onnx'
MODEL_AUTO_ENC_PATH = 'autoencoder.onnx.prototxt'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/stable-diffusion-txt2img/'
SAVE_IMAGE_PATH = 'output.png'
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'Stable Diffusion', None, SAVE_IMAGE_PATH
)
parser.add_argument(
"-i", "--input", metavar="TEXT", type=str,
default="a photograph of an astronaut riding a horse",
help="the prompt to render"
)
parser.add_argument(
"--n_iter", type=int, default=1,
help="sample this often",
)
parser.add_argument(
"--n_samples", type=int, default=1,
help="how many samples to produce for the given prompt",
)
parser.add_argument(
"--ddim_steps", type=int, default=50,
help="number of ddim sampling steps",
)
parser.add_argument(
"--ddim_eta", type=float, default=0.0,
help="ddim eta (eta=0.0 corresponds to deterministic sampling)",
)
parser.add_argument(
"--H", metavar="height", type=int, default=512,
help="image height, in pixel space",
)
parser.add_argument(
"--W", metavar="width", type=int, default=512,
help="image width, in pixel space",
)
parser.add_argument(
"--C", type=int, default=4,
help="latent channels",
)
parser.add_argument(
"--f", type=int, default=8,
help="downsampling factor",
)
parser.add_argument(
"--scale", type=float, default=7.5,
help="unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) - eps(x, empty))",
)
parser.add_argument(
"--seed", type=int, default=1001,
help="random seed",
)
parser.add_argument(
'--onnx',
action='store_true',
help='execute onnxruntime version.'
)
args = update_parser(parser, check_input_type=False)
# ======================
# Options
# ======================
FIX_CONSTANT_CONTEXT = True
# ======================
# Secondaty Functions
# ======================
def make_ddim_timesteps(num_ddim_timesteps, num_ddpm_timesteps):
c = num_ddpm_timesteps // num_ddim_timesteps
ddim_timesteps = np.asarray(list(range(0, num_ddpm_timesteps, c)))
# add one to get the final alpha values right (the ones from first scale to data during sampling)
steps_out = ddim_timesteps + 1
return steps_out
def make_ddim_sampling_parameters(alphacums, ddim_timesteps, eta):
# select alphas for computing the variance schedule
alphas = alphacums[ddim_timesteps]
alphas_prev = np.asarray([alphacums[0]] + alphacums[ddim_timesteps[:-1]].tolist())
# according the the formula provided in https://arxiv.org/abs/2010.02502
sigmas = eta * np.sqrt((1 - alphas_prev) / (1 - alphas) * (1 - alphas / alphas_prev))
return sigmas, alphas, alphas_prev
# ======================
# Main functions
# ======================
"""
ddim_timesteps
"""
ddim_num_steps = args.ddim_steps
ddpm_num_timesteps = 1000
ddim_timesteps = make_ddim_timesteps(
ddim_num_steps, ddpm_num_timesteps)
"""
ddim sampling parameters
"""
ddim_eta = args.ddim_eta
ddim_sigmas, ddim_alphas, ddim_alphas_prev = \
make_ddim_sampling_parameters(
alphacums=alphas_cumprod,
ddim_timesteps=ddim_timesteps,
eta=ddim_eta)
ddim_sqrt_one_minus_alphas = np.sqrt(1. - ddim_alphas)
# encoder
class FrozenCLIPEmbedder:
"""Uses the CLIP transformer encoder for text (from Hugging Face)"""
def __init__(self, version="openai/clip-vit-large-patch14", max_length=77):
self.tokenizer = CLIPTokenizer.from_pretrained(version)
self.transformer = CLIPTextModel.from_pretrained(version)
self.max_length = max_length
def encode(self, text):
batch_encoding = self.tokenizer(
text, truncation=True, max_length=self.max_length, return_length=True,
return_overflowing_tokens=False, padding="max_length", return_tensors="pt")
tokens = batch_encoding["input_ids"]
outputs = self.transformer(input_ids=tokens)
z = outputs.last_hidden_state
z = z.detach().numpy()
return z
# plms
def plms_sampling(
models,
cond, shape,
unconditional_guidance_scale=1.0,
unconditional_conditioning=None):
img = np.random.randn(shape[0] * shape[1] * shape[2] * shape[3]).reshape(shape)
timesteps = ddim_timesteps
time_range = np.flip(timesteps)
total_steps = timesteps.shape[0]
logger.info(f"Running PLMS Sampling with {total_steps} timesteps")
try:
from tqdm import tqdm
iterator = tqdm(time_range, desc='PLMS Sampler', total=total_steps)
except ModuleNotFoundError:
def iter_func(a):
for i, x in enumerate(a):
print("PLMS Sampler: %s/%s" % (i + 1, len(a)))
yield x
iterator = iter_func(time_range)
b = shape[0]
old_eps = []
if args.benchmark:
logger.info('BENCHMARK mode')
total_time_estimation = 0
for i, step in enumerate(iterator):
index = total_steps - i - 1
ts = np.full((b,), step, dtype=np.int64)
ts_next = np.full((b,), time_range[min(i + 1, len(time_range) - 1)], dtype=np.int64)
if args.benchmark:
start = int(round(time.time() * 1000))
outs = p_sample_plms(
models,
img, cond, ts,
update_context=(i==0),
index=index,
unconditional_guidance_scale=unconditional_guidance_scale,
unconditional_conditioning=unconditional_conditioning,
old_eps=old_eps, t_next=ts_next,
)
if args.benchmark:
end = int(round(time.time() * 1000))
estimation_time = (end - start)
logger.info(f'\tailia processing estimation time {estimation_time} ms')
total_time_estimation = total_time_estimation + estimation_time
img, pred_x0, e_t = outs
old_eps.append(e_t)
if len(old_eps) >= 4:
old_eps.pop(0)
if args.benchmark:
logger.info(f'\ttotal time estimation {total_time_estimation} ms')
return img
def p_sample_plms(
models, x, c, t, update_context, index,
temperature=1.,
unconditional_guidance_scale=1.,
unconditional_conditioning=None,
old_eps=None, t_next=None):
b, *_ = x.shape
def get_model_output(x, t):
x_in = np.concatenate([x] * 2)
t_in = np.concatenate([t] * 2)
c_in = np.concatenate([unconditional_conditioning, c])
x_recon = apply_model(models, x_in, t_in, c_in, update_context)
e_t_uncond, e_t = np.split(x_recon, 2)
e_t = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond)
return e_t
def get_x_prev_and_pred_x0(e_t, index):
alphas = ddim_alphas
alphas_prev = ddim_alphas_prev
sqrt_one_minus_alphas = ddim_sqrt_one_minus_alphas
sigmas = ddim_sigmas
# select parameters corresponding to the currently considered timestep
a_t = np.full((b, 1, 1, 1), alphas[index])
a_prev = np.full((b, 1, 1, 1), alphas_prev[index])
sigma_t = np.full((b, 1, 1, 1), sigmas[index])
sqrt_one_minus_at = np.full((b, 1, 1, 1), sqrt_one_minus_alphas[index])
# current prediction for x_0
pred_x0 = (x - sqrt_one_minus_at * e_t) / np.sqrt(a_t)
# direction pointing to x_t
dir_xt = np.sqrt(1. - a_prev - sigma_t ** 2) * e_t
noise = sigma_t * np.random.randn(x.size).reshape(x.shape) * temperature
x_prev = np.sqrt(a_prev) * pred_x0 + dir_xt + noise
return x_prev, pred_x0
e_t = get_model_output(x, t)
if len(old_eps) == 0:
# Pseudo Improved Euler (2nd order)
x_prev, pred_x0 = get_x_prev_and_pred_x0(e_t, index)
e_t_next = get_model_output(x_prev, t_next)
e_t_prime = (e_t + e_t_next) / 2
elif len(old_eps) == 1:
# 2nd order Pseudo Linear Multistep (Adams-Bashforth)
e_t_prime = (3 * e_t - old_eps[-1]) / 2
elif len(old_eps) == 2:
# 3nd order Pseudo Linear Multistep (Adams-Bashforth)
e_t_prime = (23 * e_t - 16 * old_eps[-1] + 5 * old_eps[-2]) / 12
elif len(old_eps) >= 3:
# 4nd order Pseudo Linear Multistep (Adams-Bashforth)
e_t_prime = (55 * e_t - 59 * old_eps[-1] + 37 * old_eps[-2] - 9 * old_eps[-3]) / 24
x_prev, pred_x0 = get_x_prev_and_pred_x0(e_t_prime, index)
return x_prev, pred_x0, e_t
# ddim
def ddim_sampling(
models,
cond, shape,
unconditional_guidance_scale=1.0,
unconditional_conditioning=None):
img = np.random.randn(shape[0] * shape[1] * shape[2] * shape[3]).reshape(shape)
timesteps = ddim_timesteps
time_range = np.flip(timesteps)
total_steps = timesteps.shape[0]
logger.info(f"Running DDIM Sampling with {total_steps} timesteps")
try:
from tqdm import tqdm
iterator = tqdm(time_range, desc='DDIM Sampler', total=total_steps)
except ModuleNotFoundError:
def iter_func(a):
for i, x in enumerate(a):
print("DDIM Sampler: %s/%s" % (i + 1, len(a)))
yield x
iterator = iter_func(time_range)
for i, step in enumerate(iterator):
index = total_steps - i - 1
ts = np.full((shape[0],), step, dtype=np.int64)
img, pred_x0 = p_sample_ddim(
models,
img, cond, ts,
index=index,
unconditional_guidance_scale=unconditional_guidance_scale,
unconditional_conditioning=unconditional_conditioning,
)
return img
def p_sample_ddim(
models, x, c, t, index,
temperature=1.,
unconditional_guidance_scale=1.,
unconditional_conditioning=None):
x_in = np.concatenate([x] * 2)
t_in = np.concatenate([t] * 2)
c_in = np.concatenate([unconditional_conditioning, c])
x_recon = apply_model(models, x_in, t_in, c_in, True)
e_t_uncond, e_t = np.split(x_recon, 2)
e_t = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond)
alphas = ddim_alphas
alphas_prev = ddim_alphas_prev
sqrt_one_minus_alphas = ddim_sqrt_one_minus_alphas
sigmas = ddim_sigmas
# select parameters corresponding to the currently considered timestep
b, *_ = x.shape
a_t = np.full((b, 1, 1, 1), alphas[index])
a_prev = np.full((b, 1, 1, 1), alphas_prev[index])
sigma_t = np.full((b, 1, 1, 1), sigmas[index])
sqrt_one_minus_at = np.full((b, 1, 1, 1), sqrt_one_minus_alphas[index])
# current prediction for x_0
pred_x0 = (x - sqrt_one_minus_at * e_t) / np.sqrt(a_t)
# direction pointing to x_t
dir_xt = np.sqrt(1. - a_prev - sigma_t ** 2) * e_t
noise = sigma_t * np.random.randn(x.size).reshape(x.shape) * temperature
x_prev = np.sqrt(a_prev) * pred_x0 + dir_xt + noise
return x_prev, pred_x0
# ddpm
def apply_model(models, x, t, cc, update_context):
diffusion_emb = models["diffusion_emb"]
diffusion_mid = models["diffusion_mid"]
diffusion_out = models["diffusion_out"]
x = x.astype(np.float16)
if not args.onnx:
if not FIX_CONSTANT_CONTEXT or update_context:
output = diffusion_emb.predict([x, t, cc])
else:
output = diffusion_emb.run({'x': x, 'timesteps': t})
else:
output = diffusion_emb.run(None, {'x': x, 'timesteps': t, 'context': cc})
h, emb, *hs = output
if not args.onnx:
if not FIX_CONSTANT_CONTEXT or update_context:
output = diffusion_mid.predict([h, emb, cc, *hs[6:]])
else:
output = diffusion_mid.run({
'h': h, 'emb': emb,
'h6': hs[6], 'h7': hs[7], 'h8': hs[8],
'h9': hs[9], 'h10': hs[10], 'h11': hs[11],
})
else:
output = diffusion_mid.run(None, {
'h': h, 'emb': emb, 'context': cc,
'h6': hs[6], 'h7': hs[7], 'h8': hs[8],
'h9': hs[9], 'h10': hs[10], 'h11': hs[11],
})
h = output[0]
if not args.onnx:
if not FIX_CONSTANT_CONTEXT or update_context:
output = diffusion_out.predict([h, emb, cc, *hs[:6]])
else:
output = diffusion_out.run({
'h': h, 'emb': emb,
'h0': hs[0], 'h1': hs[1], 'h2': hs[2],
'h3': hs[3], 'h4': hs[4], 'h5': hs[5],
})
else:
output = diffusion_out.run(None, {
'h': h, 'emb': emb, 'context': cc,
'h0': hs[0], 'h1': hs[1], 'h2': hs[2],
'h3': hs[3], 'h4': hs[4], 'h5': hs[5],
})
out = output[0]
return out
# decoder
def decode_first_stage(models, z):
scale_factor = 0.18215
z = z / scale_factor
z = z.astype(np.float32)
autoencoder = models['autoencoder']
if not args.onnx:
output = autoencoder.predict([z])
else:
output = autoencoder.run(None, {'input': z})
dec = output[0]
return dec
def predict(
models, cond_stage_model,
prompt, uc):
n_samples = args.n_samples
scale = args.scale
H = args.H
W = args.W
C = args.C
factor = args.f
c = cond_stage_model.encode([prompt] * n_samples)
shape = [n_samples, C, H // factor, W // factor]
plms = True
if plms:
samples_ddim = plms_sampling(
models, c, shape,
unconditional_guidance_scale=scale,
unconditional_conditioning=uc)
else:
samples_ddim = ddim_sampling(
models, c, shape,
unconditional_guidance_scale=scale,
unconditional_conditioning=uc)
x_samples_ddim = decode_first_stage(models, samples_ddim)
x_samples_ddim = np.clip((x_samples_ddim + 1.0) / 2.0, a_min=0.0, a_max=1.0)
x_samples = []
for x_sample in x_samples_ddim:
x_sample = x_sample.transpose(1, 2, 0) # CHW -> HWC
x_sample = x_sample * 255
img = x_sample.astype(np.uint8)
img = img[:, :, ::-1] # RGB -> BGR
x_samples.append(img)
return x_samples
def recognize_from_text(models):
n_iter = 1 if args.benchmark else args.n_iter
n_samples = args.n_samples
scale = args.scale
cond_stage_model = FrozenCLIPEmbedder()
prompt = args.input if isinstance(args.input, str) else args.input[0]
logger.info("prompt: %s" % prompt)
sample_path = os.path.join('outputs', prompt.replace(" ", "-"))
os.makedirs(sample_path, exist_ok=True)
base_count = len(os.listdir(sample_path))
logger.info('Start inference...')
uc = None
if scale != 1.0:
uc = cond_stage_model.encode([""] * n_samples)
all_samples = []
for i in range(n_iter):
logger.info("iteration: %s" % (i + 1))
x_samples = predict(models, cond_stage_model, prompt, uc)
for img in x_samples:
sample_file = os.path.join(sample_path, f"{base_count:04}.png")
cv2.imwrite(sample_file, img)
base_count += 1
x_samples = np.concatenate(x_samples, axis=1)
all_samples.append(x_samples)
grid_img = np.concatenate(all_samples, axis=0)
# plot result
savepath = get_savepath(args.savepath, "", ext='.png')
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, grid_img)
logger.info('Script finished successfully.')
def main():
check_and_download_models(WEIGHT_DFSN_EMB_PATH, MODEL_DFSN_EMB_PATH, REMOTE_PATH)
check_and_download_models(WEIGHT_DFSN_MID_PATH, MODEL_DFSN_MID_PATH, REMOTE_PATH)
check_and_download_models(WEIGHT_DFSN_OUT_PATH, MODEL_DFSN_OUT_PATH, REMOTE_PATH)
check_and_download_models(WEIGHT_AUTO_ENC_PATH, MODEL_AUTO_ENC_PATH, REMOTE_PATH)
env_id = args.env_id
# initialize
if not args.onnx:
# disable FP16
if "FP16" in ailia.get_environment(args.env_id).props:
logger.warning('This model do not work on FP16. So use CPU mode.')
env_id = 0
logger.info("This model requires 10GB or more memory.")
memory_mode = ailia.get_memory_mode(
reduce_constant=True, ignore_input_with_initializer=True,
reduce_interstage=False, reuse_interstage=True)
diffusion_emb = ailia.Net \
(MODEL_DFSN_EMB_PATH, WEIGHT_DFSN_EMB_PATH, env_id=env_id, memory_mode=memory_mode)
diffusion_mid = ailia.Net(
MODEL_DFSN_MID_PATH, WEIGHT_DFSN_MID_PATH, env_id=env_id, memory_mode=memory_mode)
diffusion_out = ailia.Net(
MODEL_DFSN_OUT_PATH, WEIGHT_DFSN_OUT_PATH, env_id=env_id, memory_mode=memory_mode)
autoencoder = ailia.Net(
MODEL_AUTO_ENC_PATH, WEIGHT_AUTO_ENC_PATH, env_id=env_id, memory_mode=memory_mode)
else:
import onnxruntime
diffusion_emb = onnxruntime.InferenceSession(WEIGHT_DFSN_EMB_PATH)
diffusion_mid = onnxruntime.InferenceSession(WEIGHT_DFSN_MID_PATH)
diffusion_out = onnxruntime.InferenceSession(WEIGHT_DFSN_OUT_PATH)
autoencoder = onnxruntime.InferenceSession(WEIGHT_AUTO_ENC_PATH)
seed = args.seed
if seed is not None:
np.random.seed(seed)
models = dict(
diffusion_emb=diffusion_emb,
diffusion_mid=diffusion_mid,
diffusion_out=diffusion_out,
autoencoder=autoencoder,
)
recognize_from_text(models)
if __name__ == '__main__':
main()