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 59cceae..3c97e69 100644 --- a/simulacra.py +++ b/simulacra.py @@ -1064,12 +1064,12 @@ 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() - if len(prompt) > 246: # Must be able to fit _grid.png on end. + prompt = re.sub("\.add\s+", "", interaction.message.content).strip() + 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. Please shorten it and try again.") + "filesystem, current max length is {}. Please shorten it and try again.".format((255 - 12 - len(str(seed))))) return - seed = generations.get_next_seed() job = Job(prompt=prompt, cloob_checkpoint='cloob_laion_400m_vit_b_16_16_epochs', scale=secrets.choice([7,8,9,10]), diff --git a/simulacra_imagen_sample.py b/simulacra_imagen_sample.py index ac75e1f..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 @@ -8,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 @@ -148,7 +148,15 @@ 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-multinode-clip-encoder-f16-768-laion-hr-inference.yaml" opt.ckpt = "f16-33k+12k-hr_pruned.ckpt" @@ -156,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}") @@ -207,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)