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
19 changes: 19 additions & 0 deletions seed_all.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions simulacra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <seed>_<prompt>_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]),
Expand Down
18 changes: 12 additions & 6 deletions simulacra_imagen_sample.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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
from tqdm import tqdm, trange
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
Expand Down Expand Up @@ -148,16 +148,22 @@ 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"
opt.dyn = None
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}")

Expand Down Expand Up @@ -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)
Expand Down