From efa4be22849190a3ade104f2812a6ba7ee5e2dc9 Mon Sep 17 00:00:00 2001 From: spuun Date: Thu, 30 Jun 2022 06:58:37 +0700 Subject: [PATCH 1/4] Add length fix and seed patch `seed_all` suggested by Zippy Length is calculated based on current length --- seed_all.py | 19 +++++++++++++++++++ simulacra.py | 6 +++--- simulacra_imagen_sample.py | 10 +++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 seed_all.py diff --git a/seed_all.py b/seed_all.py new file mode 100644 index 0000000..52db51e --- /dev/null +++ b/seed_all.py @@ -0,0 +1,19 @@ +# Zippy's seed_all function +from numpy.random import MT19937, RandomState, SeedSequence +import random, numpy as np, time +import torch + +def seed_all(seed_value=None): + new_state = RandomState(MT19937(SeedSequence(int(time.time_ns()) % (2**32 - 1)))) + np.random.set_state(new_state.get_state()) + if seed_value is None: + seed_value = np.random.randint(1, 2**32 - 1) + random.seed(seed_value) # Python + np.random.seed(seed_value) # cpu vars + torch.manual_seed(seed_value) # cpu vars + if torch.cuda.is_available(): + torch.cuda.manual_seed(seed_value) + torch.cuda.manual_seed_all(seed_value) # gpu vars + torch.backends.cudnn.deterministic = True # needed + torch.backends.cudnn.benchmark = False + return seed_value diff --git a/simulacra.py b/simulacra.py index 2e9d682..fabea44 100644 --- a/simulacra.py +++ b/simulacra.py @@ -1065,11 +1065,11 @@ async def add(interaction: nextcord.Interaction): await interaction.send("'{}' is not an allowed word by the content filter".format(word)) return prompt = interaction.message.content.split(".add")[1].strip() - if len(prompt) > 246: # Must be able to fit _grid.png on end. + seed = generations.get_next_seed() + if len(prompt) > (255 - 10 - len(str(seed))): # Must be able to fit __grid.png so inference doesn't crash await interaction.send("The length of the prompt wouldn't fit on the " - "filesystem. Please shorten it and try again.") + "filesystem, current max length is {}. Please shorten it and try again.".format((255 - 10 - len(str(seed))))) return - seed = generations.get_next_seed() job = Job(prompt=prompt, cloob_checkpoint='cloob_laion_400m_vit_b_16_16_epochs', scale=5., diff --git a/simulacra_imagen_sample.py b/simulacra_imagen_sample.py index 9790a2c..61d783a 100644 --- a/simulacra_imagen_sample.py +++ b/simulacra_imagen_sample.py @@ -7,6 +7,7 @@ from itertools import islice from einops import rearrange from torchvision.utils import make_grid +from seed_all import seed_all from ldm.util import instantiate_from_config from ldm.models.diffusion.ddim import DDIMSampler @@ -147,7 +148,14 @@ def main(opt): default="logs/f8-kl-clip-encoder-256x256-run1/checkpoints/last.ckpt", help="path to checkpoint of model", ) - #opt = parser.parse_args() + + parser.add_argument( + "--seed", + type=int, + help="seed for seed_all", + ) + # opt = parser.parse_args() + seed_all(opt.seed) opt.n_rows = 1 opt.config = "stable-diffusion/configs/stable-diffusion/txt2img-1p4B-multinode-clip-encoder-high-res-512-inference.yaml" opt.ckpt = "256f8ft512-2022-06-15-pruned.ckpt" From 0477b41283a7df02b64f3a568ac3d47dc1f95d38 Mon Sep 17 00:00:00 2001 From: spuun Date: Thu, 30 Jun 2022 09:07:39 +0700 Subject: [PATCH 2/4] `.add` regex --- simulacra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulacra.py b/simulacra.py index fabea44..2801b03 100644 --- a/simulacra.py +++ b/simulacra.py @@ -1064,7 +1064,7 @@ async def add(interaction: nextcord.Interaction): if (' ' + word.lower()) in interaction.message.content.lower(): await interaction.send("'{}' is not an allowed word by the content filter".format(word)) return - prompt = interaction.message.content.split(".add")[1].strip() + prompt = re.sub("\.add\s+", "", interaction.message.content).strip() seed = generations.get_next_seed() if len(prompt) > (255 - 10 - len(str(seed))): # Must be able to fit __grid.png so inference doesn't crash await interaction.send("The length of the prompt wouldn't fit on the " From 826d7c48c2aaa13ec066bcb2fbc995923ff76dc4 Mon Sep 17 00:00:00 2001 From: spuun Date: Thu, 30 Jun 2022 12:58:42 +0700 Subject: [PATCH 3/4] Replace seed call, prefix cond_scale to image out --- simulacra.py | 2 +- simulacra_imagen_sample.py | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/simulacra.py b/simulacra.py index 1a4b67c..2c0334d 100644 --- a/simulacra.py +++ b/simulacra.py @@ -1066,7 +1066,7 @@ async def add(interaction: nextcord.Interaction): return prompt = re.sub("\.add\s+", "", interaction.message.content).strip() seed = generations.get_next_seed() - if len(prompt) > (255 - 10 - len(str(seed))): # Must be able to fit __grid.png so inference doesn't crash + if len(prompt) > (255 - 12 - len(str(seed))): # Must be able to fit __grid.png so inference doesn't crash await interaction.send("The length of the prompt wouldn't fit on the " "filesystem, current max length is {}. Please shorten it and try again.".format((255 - 10 - len(str(seed))))) return diff --git a/simulacra_imagen_sample.py b/simulacra_imagen_sample.py index c692db2..a329938 100644 --- a/simulacra_imagen_sample.py +++ b/simulacra_imagen_sample.py @@ -1,6 +1,5 @@ import argparse, os, sys, glob import torch -from pytorch_lightning import seed_everything import numpy as np from omegaconf import OmegaConf from PIL import Image @@ -157,6 +156,7 @@ def main(opt): ) # opt = parser.parse_args() seed_all(opt.seed) + opt.n_rows = 1 opt.config = "stable-diffusion/configs/stable-diffusion/txt2img-multinode-clip-encoder-f16-768-laion-hr-inference.yaml" opt.ckpt = "f16-33k+12k-hr_pruned.ckpt" @@ -164,8 +164,6 @@ def main(opt): opt.C = 16 opt.f = 16 - seed_everything(opt.seed) - config = OmegaConf.load(f"{opt.config}") model = load_model_from_config(config, f"{opt.ckpt}") @@ -215,11 +213,11 @@ def main(opt): outs = all_samples[0] for index, out in enumerate(outs): print(out.shape) - outpath = str(opt.seed) + "_" + opt.prompt.replace(" ", "_").replace("/","_") + "_" + str(index + 1) + ".png" + outpath = str(opt.scale) + "_" + str(opt.seed) + "_" + opt.prompt.replace(" ", "_").replace("/","_") + "_" + str(index + 1) + ".png" out = 255. * rearrange(out.cpu().numpy(), 'c h w -> h w c') Image.fromarray(out.astype(np.uint8)).save(outpath) - gridpath = str(opt.seed) + "_" + opt.prompt.replace(" ", "_").replace("/","_") + "_grid" + ".png" + gridpath = str(opt.scale) + "_" + opt.str(opt.seed) + "_" + opt.prompt.replace(" ", "_").replace("/","_") + "_grid" + ".png" grid = torch.stack(all_samples, 0) grid = rearrange(grid, 'n b c h w -> (n b) c h w') grid = make_grid(grid, nrow=opt.n_samples) From 380d3dad6492edfb93534e4ccfb7fefceaa014e4 Mon Sep 17 00:00:00 2001 From: spuun Date: Fri, 1 Jul 2022 14:41:47 +0700 Subject: [PATCH 4/4] Just noticed this. --- simulacra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulacra.py b/simulacra.py index 047c2de..3c97e69 100644 --- a/simulacra.py +++ b/simulacra.py @@ -1068,7 +1068,7 @@ async def add(interaction: nextcord.Interaction): seed = generations.get_next_seed() if len(prompt) > (255 - 12 - len(str(seed))): # Must be able to fit __grid.png so inference doesn't crash await interaction.send("The length of the prompt wouldn't fit on the " - "filesystem, current max length is {}. Please shorten it and try again.".format((255 - 10 - len(str(seed))))) + "filesystem, current max length is {}. Please shorten it and try again.".format((255 - 12 - len(str(seed))))) return job = Job(prompt=prompt, cloob_checkpoint='cloob_laion_400m_vit_b_16_16_epochs',