Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions ldm/modules/diffusionmodules/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand All @@ -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


Expand Down
20 changes: 17 additions & 3 deletions optimizedSD/optimized_txt2img.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
from datetime import datetime
import os
import random
import re
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -190,14 +192,23 @@ 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")
while torch.cuda.memory_allocated() / 1e6 >= mem:
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
Expand Down Expand Up @@ -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}")