diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..00c591c03 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ + +*.png +*.pyc +*.csv +CodeFormer/ +src/clip/ +src/taming-transformers/ +latent_diffusion.egg-info/dependency_links.txt +latent_diffusion.egg-info/PKG-INFO +latent_diffusion.egg-info/requires.txt +latent_diffusion.egg-info/SOURCES.txt +latent_diffusion.egg-info/top_level.txt +log.txt +models/ldm/stable-diffusion-v1/model.ckpt +.vscode/settings.json +tqdm.txt diff --git a/ldm/modules/diffusionmodules/model.py b/ldm/modules/diffusionmodules/model.py index 8dcfce963..53bb4638e 100644 --- a/ldm/modules/diffusionmodules/model.py +++ b/ldm/modules/diffusionmodules/model.py @@ -184,7 +184,7 @@ def __init__(self, in_channels): @torch.jit.script -def fused_t(x, c): +def fused_t(x, c: int): return x * (int(c) ** (-0.5)) @@ -197,7 +197,7 @@ def fused_memory_opt(mem_reserved, mem_active, mem_free_cuda, b, h, w, c): # s3 = (b * c * h * w * 3) * 2 # zeros_like, empty_like, empty_strided # s = 2 * (s1 + s2 + s3) # 2 because of small allocations which don't really matter s = 16 * b * ((h * w) ** 2) + 12 * b * c * h * w - s = (s // mem_free_total) + 1 if s > mem_free_total else torch.tensor(1) + s = int((s // mem_free_total) + 1) if s > mem_free_total else 1 return s diff --git a/optimizedSD/optimized_txt2img.py b/optimizedSD/optimized_txt2img.py index 9776097d4..bd100116e 100644 --- a/optimizedSD/optimized_txt2img.py +++ b/optimizedSD/optimized_txt2img.py @@ -1,4 +1,5 @@ import argparse +from datetime import datetime import os import random import re @@ -117,6 +118,7 @@ def get_image(opt, model, modelCS, modelFS, prompt=None, save=True, callback_fn= negative_prompt = "" with torch.no_grad(): all_samples = list() + count_iter = 0 for _ in trange(opt.n_iter, desc="Sampling"): for prompts in tqdm(data, desc="data"): sample_path = os.path.join(opt.outpath, "_".join(re.split(":| ", prompts[0])))[:150] @@ -147,7 +149,7 @@ def get_image(opt, model, modelCS, modelFS, prompt=None, save=True, callback_fn= c = modelCS.get_learned_conditioning(prompts) shape = [opt.num_images, opt.C, opt.height // opt.f, opt.width // opt.f] - + seed_at_begin = opt.seed if opt.device != "cpu": mem = torch.cuda.memory_allocated() / 1e6 modelCS.to("cpu") @@ -190,6 +192,15 @@ def get_image(opt, model, modelCS, modelFS, prompt=None, save=True, callback_fn= opt.seed += 1 base_count += 1 + count = 0 + for x_index in range(count_iter * batch_size, len(all_samples)): + x_sample = 255. * rearrange(all_samples[x_index][0].numpy(), 'c h w -> h w c') + now = datetime.now() + dt_string = now.strftime("%d-%m-%Y %H_%M_%S ") + Image.fromarray(x_sample.astype(np.uint8)).save( + os.path.join(sample_path, f"{dt_string}s{seed_at_begin}d{opt.ddim_steps}-{str(count)}.png")) + count += 1 + if opt.device != "cpu": mem = torch.cuda.memory_allocated() / 1e6 modelFS.to("cpu") @@ -197,7 +208,7 @@ def get_image(opt, model, modelCS, modelFS, prompt=None, save=True, callback_fn= time.sleep(1) del samples_ddim print("memory_final = ", torch.cuda.memory_allocated() / 1e6) - + count_iter += 1 toc = time.time() time_taken = (toc - tic) / 60.0 @@ -430,7 +441,10 @@ def get_image(opt, model, modelCS, modelFS, prompt=None, save=True, callback_fn= grid = torch.cat(all_samples, 0) grid = make_grid(grid, nrow=opt.n_iter) grid = 255.0 * rearrange(grid, "c h w -> h w c").cpu().numpy() + + now = datetime.now() + dt_string = now.strftime(" %d-%m-%Y %H_%M_%S ") Image.fromarray(grid.astype(np.uint8)).save( - os.path.join(outpath + "/" + str(opt.prompt).replace("/", "")[:100] + f".{opt.format}") + os.path.join(outpath + "/" + str(opt.prompt).replace("/", "")[:100] + dt_string + f".{opt.format}") ) print("exported to", outpath + "/" + str(opt.prompt).replace("/", "")[:100] + f".{opt.format}")