additional picture for prompt matrix
proper seeds for img2img a bit of refactoring
This commit is contained in:
parent
60e95f1d8c
commit
61bfa6c16b
3 changed files with 63 additions and 60 deletions
|
@ -83,9 +83,11 @@ For example, if you use `a house in a field of grass|at dawn|illustration` promp
|
||||||
- `a house in a field of grass, at dawn, illustration`
|
- `a house in a field of grass, at dawn, illustration`
|
||||||
|
|
||||||
Four images will be produced, in this order, all with same seed and each with corresponding prompt:
|
Four images will be produced, in this order, all with same seed and each with corresponding prompt:
|
||||||
|
|
||||||
![](images/prompt-matrix.png)
|
![](images/prompt-matrix.png)
|
||||||
|
|
||||||
|
Another example, this time with 5 prompts and 16 variations, (text added manually):
|
||||||
|
![](images/prompt_matrix.jpg)
|
||||||
|
|
||||||
### Flagging
|
### Flagging
|
||||||
Click the Flag button under the output section, and generated images will be saved to `log/images` directory, and generation parameters
|
Click the Flag button under the output section, and generated images will be saved to `log/images` directory, and generation parameters
|
||||||
will be appended to a csv file `log/log.csv` in the `/sd` directory.
|
will be appended to a csv file `log/log.csv` in the `/sd` directory.
|
||||||
|
|
BIN
images/prompt_matrix.jpg
Normal file
BIN
images/prompt_matrix.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 770 KiB |
119
webui.py
119
webui.py
|
@ -106,6 +106,30 @@ class CFGDenoiser(nn.Module):
|
||||||
return uncond + (cond - uncond) * cond_scale
|
return uncond + (cond - uncond) * cond_scale
|
||||||
|
|
||||||
|
|
||||||
|
class KDiffusionSampler:
|
||||||
|
def __init__(self, m):
|
||||||
|
self.model = m
|
||||||
|
self.model_wrap = K.external.CompVisDenoiser(m)
|
||||||
|
|
||||||
|
def sample(self, S, conditioning, batch_size, shape, verbose, unconditional_guidance_scale, unconditional_conditioning, eta, x_T):
|
||||||
|
sigmas = self.model_wrap.get_sigmas(S)
|
||||||
|
x = x_T * sigmas[0]
|
||||||
|
model_wrap_cfg = CFGDenoiser(self.model_wrap)
|
||||||
|
samples_ddim = K.sampling.sample_lms(model_wrap_cfg, x, sigmas, extra_args={'cond': conditioning, 'uncond': unconditional_conditioning, 'cond_scale': unconditional_guidance_scale}, disable=False)
|
||||||
|
|
||||||
|
return samples_ddim, None
|
||||||
|
|
||||||
|
|
||||||
|
def create_random_tensors(seed, shape, count, same_seed=False):
|
||||||
|
xs = []
|
||||||
|
for i in range(count):
|
||||||
|
current_seed = seed if same_seed else seed + i
|
||||||
|
torch.manual_seed(current_seed)
|
||||||
|
xs.append(torch.randn(shape, device=device))
|
||||||
|
x = torch.stack(xs)
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
def load_GFPGAN():
|
def load_GFPGAN():
|
||||||
model_name = 'GFPGANv1.3'
|
model_name = 'GFPGANv1.3'
|
||||||
model_path = os.path.join(GFPGAN_dir, 'experiments/pretrained_models', model_name + '.pth')
|
model_path = os.path.join(GFPGAN_dir, 'experiments/pretrained_models', model_name + '.pth')
|
||||||
|
@ -166,22 +190,15 @@ def dream(prompt: str, ddim_steps: int, sampler_name: str, use_GFPGAN: bool, pro
|
||||||
seed = int(seed)
|
seed = int(seed)
|
||||||
keep_same_seed = False
|
keep_same_seed = False
|
||||||
|
|
||||||
is_PLMS = sampler_name == 'PLMS'
|
if sampler_name == 'PLMS':
|
||||||
is_DDIM = sampler_name == 'DDIM'
|
|
||||||
is_Kdif = sampler_name == 'k-diffusion'
|
|
||||||
|
|
||||||
sampler = None
|
|
||||||
if is_PLMS:
|
|
||||||
sampler = PLMSSampler(model)
|
sampler = PLMSSampler(model)
|
||||||
elif is_DDIM:
|
elif sampler_name == 'DDIM':
|
||||||
sampler = DDIMSampler(model)
|
sampler = DDIMSampler(model)
|
||||||
elif is_Kdif:
|
elif sampler_name == 'k-diffusion':
|
||||||
pass
|
sampler = KDiffusionSampler(model)
|
||||||
else:
|
else:
|
||||||
raise Exception("Unknown sampler: " + sampler_name)
|
raise Exception("Unknown sampler: " + sampler_name)
|
||||||
|
|
||||||
model_wrap = K.external.CompVisDenoiser(model)
|
|
||||||
|
|
||||||
os.makedirs(outpath, exist_ok=True)
|
os.makedirs(outpath, exist_ok=True)
|
||||||
|
|
||||||
batch_size = n_samples
|
batch_size = n_samples
|
||||||
|
@ -238,21 +255,9 @@ def dream(prompt: str, ddim_steps: int, sampler_name: str, use_GFPGAN: bool, pro
|
||||||
batch_seed = seed if keep_same_seed else seed + n * len(prompts)
|
batch_seed = seed if keep_same_seed else seed + n * len(prompts)
|
||||||
|
|
||||||
# we manually generate all input noises because each one should have a specific seed
|
# we manually generate all input noises because each one should have a specific seed
|
||||||
xs = []
|
x = create_random_tensors(batch_seed, shape, count=len(prompts), same_seed=keep_same_seed)
|
||||||
for i in range(len(prompts)):
|
|
||||||
current_seed = seed if keep_same_seed else batch_seed + i
|
|
||||||
torch.manual_seed(current_seed)
|
|
||||||
xs.append(torch.randn(shape, device=device))
|
|
||||||
x = torch.stack(xs)
|
|
||||||
|
|
||||||
if is_Kdif:
|
samples_ddim, _ = sampler.sample(S=ddim_steps, conditioning=c, batch_size=len(prompts), shape=shape, verbose=False, unconditional_guidance_scale=cfg_scale, unconditional_conditioning=uc, eta=ddim_eta, x_T=x)
|
||||||
sigmas = model_wrap.get_sigmas(ddim_steps)
|
|
||||||
x = x * sigmas[0]
|
|
||||||
model_wrap_cfg = CFGDenoiser(model_wrap)
|
|
||||||
samples_ddim = K.sampling.sample_lms(model_wrap_cfg, x, sigmas, extra_args={'cond': c, 'uncond': uc, 'cond_scale': cfg_scale}, disable=False)
|
|
||||||
|
|
||||||
elif sampler is not None:
|
|
||||||
samples_ddim, _ = sampler.sample(S=ddim_steps, conditioning=c, batch_size=len(prompts), shape=shape, verbose=False, unconditional_guidance_scale=cfg_scale, unconditional_conditioning=uc, eta=ddim_eta, x_T=x)
|
|
||||||
|
|
||||||
x_samples_ddim = model.decode_first_stage(samples_ddim)
|
x_samples_ddim = model.decode_first_stage(samples_ddim)
|
||||||
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
||||||
|
@ -274,9 +279,6 @@ def dream(prompt: str, ddim_steps: int, sampler_name: str, use_GFPGAN: bool, pro
|
||||||
output_images.append(image)
|
output_images.append(image)
|
||||||
base_count += 1
|
base_count += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if not opt.skip_grid:
|
if not opt.skip_grid:
|
||||||
# additionally, save as grid
|
# additionally, save as grid
|
||||||
grid = image_grid(output_images, batch_size, round_down=prompt_matrix)
|
grid = image_grid(output_images, batch_size, round_down=prompt_matrix)
|
||||||
|
@ -380,13 +382,11 @@ def translation(prompt: str, init_img, ddim_steps: int, use_GFPGAN: bool, ddim_e
|
||||||
batch_size = n_samples
|
batch_size = n_samples
|
||||||
|
|
||||||
assert prompt is not None
|
assert prompt is not None
|
||||||
data = [batch_size * [prompt]]
|
|
||||||
|
|
||||||
sample_path = os.path.join(outpath, "samples")
|
sample_path = os.path.join(outpath, "samples")
|
||||||
os.makedirs(sample_path, exist_ok=True)
|
os.makedirs(sample_path, exist_ok=True)
|
||||||
base_count = len(os.listdir(sample_path))
|
base_count = len(os.listdir(sample_path))
|
||||||
grid_count = len(os.listdir(outpath)) - 1
|
grid_count = len(os.listdir(outpath)) - 1
|
||||||
seedit = 0
|
|
||||||
|
|
||||||
image = init_img.convert("RGB")
|
image = init_img.convert("RGB")
|
||||||
image = image.resize((width, height), resample=PIL.Image.Resampling.LANCZOS)
|
image = image.resize((width, height), resample=PIL.Image.Resampling.LANCZOS)
|
||||||
|
@ -407,43 +407,44 @@ def translation(prompt: str, init_img, ddim_steps: int, use_GFPGAN: bool, ddim_e
|
||||||
t_enc = int(denoising_strength * ddim_steps)
|
t_enc = int(denoising_strength * ddim_steps)
|
||||||
|
|
||||||
for n in range(n_iter):
|
for n in range(n_iter):
|
||||||
for batch_index, prompts in enumerate(data):
|
prompts = batch_size * [prompt]
|
||||||
uc = None
|
|
||||||
if cfg_scale != 1.0:
|
|
||||||
uc = model.get_learned_conditioning(batch_size * [""])
|
|
||||||
if isinstance(prompts, tuple):
|
|
||||||
prompts = list(prompts)
|
|
||||||
c = model.get_learned_conditioning(prompts)
|
|
||||||
|
|
||||||
sigmas = model_wrap.get_sigmas(ddim_steps)
|
uc = None
|
||||||
|
if cfg_scale != 1.0:
|
||||||
|
uc = model.get_learned_conditioning(batch_size * [""])
|
||||||
|
if isinstance(prompts, tuple):
|
||||||
|
prompts = list(prompts)
|
||||||
|
c = model.get_learned_conditioning(prompts)
|
||||||
|
|
||||||
current_seed = seed + n * len(data) + batch_index
|
batch_seed = seed + n * len(prompts)
|
||||||
torch.manual_seed(current_seed)
|
|
||||||
|
|
||||||
noise = torch.randn_like(x0) * sigmas[ddim_steps - t_enc - 1] # for GPU draw
|
sigmas = model_wrap.get_sigmas(ddim_steps)
|
||||||
xi = x0 + noise
|
noise = create_random_tensors(batch_seed, x0.shape[1:], count=len(prompts))
|
||||||
sigma_sched = sigmas[ddim_steps - t_enc - 1:]
|
noise = noise * sigmas[ddim_steps - t_enc - 1]
|
||||||
model_wrap_cfg = CFGDenoiser(model_wrap)
|
|
||||||
extra_args = {'cond': c, 'uncond': uc, 'cond_scale': cfg_scale}
|
|
||||||
|
|
||||||
samples_ddim = K.sampling.sample_lms(model_wrap_cfg, xi, sigma_sched, extra_args=extra_args, disable=False)
|
xi = x0 + noise
|
||||||
x_samples_ddim = model.decode_first_stage(samples_ddim)
|
sigma_sched = sigmas[ddim_steps - t_enc - 1:]
|
||||||
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
model_wrap_cfg = CFGDenoiser(model_wrap)
|
||||||
|
extra_args = {'cond': c, 'uncond': uc, 'cond_scale': cfg_scale}
|
||||||
|
|
||||||
if not opt.skip_save or not opt.skip_grid:
|
samples_ddim = K.sampling.sample_lms(model_wrap_cfg, xi, sigma_sched, extra_args=extra_args, disable=False)
|
||||||
for x_sample in x_samples_ddim:
|
x_samples_ddim = model.decode_first_stage(samples_ddim)
|
||||||
x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
|
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
||||||
x_sample = x_sample.astype(np.uint8)
|
|
||||||
|
|
||||||
if use_GFPGAN and GFPGAN is not None:
|
if not opt.skip_save or not opt.skip_grid:
|
||||||
cropped_faces, restored_faces, restored_img = GFPGAN.enhance(x_sample, has_aligned=False, only_center_face=False, paste_back=True)
|
for i, x_sample in enumerate(x_samples_ddim):
|
||||||
x_sample = restored_img
|
x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
|
||||||
|
x_sample = x_sample.astype(np.uint8)
|
||||||
|
|
||||||
image = Image.fromarray(x_sample)
|
if use_GFPGAN and GFPGAN is not None:
|
||||||
image.save(os.path.join(sample_path, f"{base_count:05}-{current_seed}_{prompt.replace(' ', '_').translate({ord(x): '' for x in invalid_filename_chars})[:128]}.png"))
|
cropped_faces, restored_faces, restored_img = GFPGAN.enhance(x_sample, has_aligned=False, only_center_face=False, paste_back=True)
|
||||||
|
x_sample = restored_img
|
||||||
|
|
||||||
output_images.append(image)
|
image = Image.fromarray(x_sample)
|
||||||
base_count += 1
|
image.save(os.path.join(sample_path, f"{base_count:05}-{batch_seed+i}_{prompt.replace(' ', '_').translate({ord(x): '' for x in invalid_filename_chars})[:128]}.png"))
|
||||||
|
|
||||||
|
output_images.append(image)
|
||||||
|
base_count += 1
|
||||||
|
|
||||||
if not opt.skip_grid:
|
if not opt.skip_grid:
|
||||||
# additionally, save as grid
|
# additionally, save as grid
|
||||||
|
|
Loading…
Reference in a new issue