Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting seeds to random after exhausted #1904

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
52 changes: 48 additions & 4 deletions gen_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,7 @@ def scale_and_round(x):
metadata.add_text("crop-left", str(crop_left))

if filename is not None:
fln = filename
fln = first_available_filename(args.outdir, filename) #Checks to make sure is not existing file, else returns first available sequential filename
else:
if args.use_original_file_name and init_images is not None:
if type(init_images) is list:
Expand Down Expand Up @@ -2586,7 +2586,8 @@ def scale_and_round(x):
negative_scale = args.negative_scale
steps = args.steps
seed = None
seeds = None
if pi == 0:
seeds = None
strength = 0.8 if args.strength is None else args.strength
negative_prompt = ""
clip_prompt = None
Expand Down Expand Up @@ -2670,6 +2671,8 @@ def scale_and_round(x):

m = re.match(r"d ([\d,]+)", parg, re.IGNORECASE)
if m: # seed
if pi > 0 and len(raw_prompts) > 1: #Bypass od 2nd loop for dynamic prompts
continue
seeds = [int(d) for d in m.group(1).split(",")]
logger.info(f"seeds: {seeds}")
continue
Expand Down Expand Up @@ -2802,7 +2805,11 @@ def scale_and_round(x):
logger.error(f"Exception in parsing / 解析エラー: {parg}")
logger.error(f"{ex}")

# override Deep Shrink
# override filename to add index number if more than one image per prompt
if filename is not None and (args.images_per_prompt > 1 or len(raw_prompts) > 1):
filename = filename + "_%s" % pi

# override Deep Shrink
if ds_depth_1 is not None:
if ds_depth_1 < 0:
ds_depth_1 = args.ds_depth_1 or 3
Expand Down Expand Up @@ -2835,8 +2842,16 @@ def scale_and_round(x):
# prepare seed
if seeds is not None: # given in prompt
# num_images_per_promptが多い場合は足りなくなるので、足りない分は前のを使う
if len(seeds) > 0:
# Previous implementation may result in unexpected behaviour when number of seeds is lesss than number of repeats. Last seed is taken for rest of repeated prompts. Add condition if last element is -1, to start randomizing seed.
if len(seeds) > 1:
seed = seeds.pop(0)
elif len(seeds) == 1:
if seeds[0] == -1:
seeds = None
else:
seed = seeds.pop(0)


else:
if args.iter_same_seed:
seed = iter_seed
Expand All @@ -2847,6 +2862,7 @@ def scale_and_round(x):
seed = seed_random.randint(0, 2**32 - 1)
if args.interactive:
logger.info(f"seed: {seed}")
# logger.info(f"seed: {seed}") #debugging logger. Uncomment to verify if expected seed is added correctly.

# prepare init image, guide image and mask
init_image = mask_image = guide_image = None
Expand Down Expand Up @@ -2935,7 +2951,35 @@ def scale_and_round(x):

logger.info("done!")

def first_available_filename(path, filename):
"""
Checks if filename is in use.
if filename is in use, appends a running number
e.g. filename = 'file.png':

file.png
file_1.png
file_2.png

Runs in log(n) time where n is the number of existing files in sequence
"""
i = 1
if not os.path.exists(os.path.join(path, filename)):
return filename
fileext = os.path.splitext(filename)
filename = fileext[0] + "_%s" + fileext[1]
# First do an exponential search
while os.path.exists(os.path.join(path,filename % i)):
i = i * 2

# Result lies somewhere in the interval (i/2..i]
# We call this interval (a..b] and narrow it down until a + 1 = b
a, b = (i // 2, i)
while a + 1 < b:
c = (a + b) // 2 # interval midpoint
a, b = (c, b) if os.path.exists(os.path.join(path,filename % c)) else (a, c)

return filename % b
def setup_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()

Expand Down