@@ -258,7 +258,13 @@ def image_grid(imgs, batch_size, round_down=False, force_n_rows=None):
258
258
259
259
return grid
260
260
261
-
261
+ def seed_to_int (s ):
262
+ if s == 'random' :
263
+ return random .randint (0 ,2 ** 32 )
264
+ n = abs (int (s ) if s .isdigit () else hash (s ))
265
+ while n > 2 ** 32 :
266
+ n = n >> 32
267
+ return n
262
268
263
269
def draw_prompt_matrix (im , width , height , all_prompts ):
264
270
def wrap (text , d , font , line_length ):
@@ -390,10 +396,6 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
390
396
mem_mon = MemUsageMonitor ('MemMon' )
391
397
mem_mon .start ()
392
398
393
- if seed == - 1 :
394
- seed = random .randrange (4294967294 )
395
- seed = int (seed )
396
-
397
399
os .makedirs (outpath , exist_ok = True )
398
400
399
401
sample_path = os .path .join (outpath , "samples" )
@@ -468,6 +470,7 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
468
470
cropped_faces , restored_faces , restored_img = GFPGAN .enhance (x_sample , has_aligned = False , only_center_face = False , paste_back = True )
469
471
x_sample = restored_img
470
472
473
+
471
474
image = Image .fromarray (x_sample )
472
475
filename = f"{ base_count :05} -{ seeds [i ]} _{ prompts [i ].replace (' ' , '_' ).translate ({ord (x ): '' for x in invalid_filename_chars })[:128 ]} .png"
473
476
if not skip_save :
@@ -518,6 +521,7 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
518
521
def txt2img (prompt : str , ddim_steps : int , sampler_name : str , use_GFPGAN : bool , prompt_matrix : bool , skip_grid : bool , skip_save : bool , ddim_eta : float , n_iter : int , batch_size : int , cfg_scale : float , seed : int , height : int , width : int ):
519
522
outpath = opt .outdir or "outputs/txt2img-samples"
520
523
err = False
524
+ seed = seed_to_int (seed )
521
525
522
526
if sampler_name == 'PLMS' :
523
527
sampler = PLMSSampler (model )
@@ -624,7 +628,7 @@ def flag(self, flag_data, flag_option=None, flag_index=None, username=None):
624
628
gr .Slider (minimum = 1 , maximum = 16 , step = 1 , label = 'Batch count (how many batches of images to generate)' , value = 1 ),
625
629
gr .Slider (minimum = 1 , maximum = 8 , step = 1 , label = 'Batch size (how many images are in a batch; memory-hungry)' , value = 1 ),
626
630
gr .Slider (minimum = 1.0 , maximum = 15.0 , step = 0.5 , label = 'Classifier Free Guidance Scale (how strongly the image should follow the prompt)' , value = 7.0 ),
627
- gr .Number (label = ' Seed' , value = - 1 ),
631
+ gr .Textbox (label = " Seed ('random' to randomize)" , lines = 1 , value = "random" ),
628
632
gr .Slider (minimum = 64 , maximum = 2048 , step = 64 , label = "Height" , value = 512 ),
629
633
gr .Slider (minimum = 64 , maximum = 2048 , step = 64 , label = "Width" , value = 512 ),
630
634
],
@@ -644,6 +648,7 @@ def flag(self, flag_data, flag_option=None, flag_index=None, username=None):
644
648
def img2img (prompt : str , init_img , ddim_steps : int , sampler_name : str , use_GFPGAN : bool , prompt_matrix , loopback : bool , skip_grid : bool , skip_save : bool , n_iter : int , batch_size : int , cfg_scale : float , denoising_strength : float , seed : int , height : int , width : int , resize_mode : int ):
645
649
outpath = opt .outdir or "outputs/img2img-samples"
646
650
err = False
651
+ seed = seed_to_int (seed )
647
652
648
653
if sampler_name == 'DDIM' :
649
654
sampler = DDIMSampler (model )
@@ -725,12 +730,14 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
725
730
seed = seed + 1
726
731
denoising_strength = max (denoising_strength * 0.95 , 0.1 )
727
732
history .append (init_img )
733
+
728
734
if not skip_grid :
729
735
grid_count = len (os .listdir (outpath )) - 1
730
736
grid = image_grid (history , batch_size , force_n_rows = 1 )
731
737
grid_file = f"grid-{ grid_count :05} -{ seed } _{ prompt .replace (' ' , '_' ).translate ({ord (x ): '' for x in invalid_filename_chars })[:128 ]} .jpg"
732
738
grid .save (os .path .join (outpath , grid_file ), 'jpeg' , quality = 100 , optimize = True )
733
739
740
+
734
741
output_images = history
735
742
seed = initial_seed
736
743
@@ -787,7 +794,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
787
794
gr .Slider (minimum = 1 , maximum = 8 , step = 1 , label = 'Batch size (how many images are in a batch; memory-hungry)' , value = 1 ),
788
795
gr .Slider (minimum = 1.0 , maximum = 15.0 , step = 0.5 , label = 'Classifier Free Guidance Scale (how strongly the image should follow the prompt)' , value = 7.0 ),
789
796
gr .Slider (minimum = 0.0 , maximum = 1.0 , step = 0.01 , label = 'Denoising Strength' , value = 0.75 ),
790
- gr .Number (label = ' Seed' , value = - 1 ),
797
+ gr .Textbox (label = " Seed ('random' to randomize)" , lines = 1 , value = "random" ),
791
798
gr .Slider (minimum = 64 , maximum = 2048 , step = 64 , label = "Height" , value = 512 ),
792
799
gr .Slider (minimum = 64 , maximum = 2048 , step = 64 , label = "Width" , value = 512 ),
793
800
gr .Radio (label = "Resize mode" , choices = ["Just resize" , "Crop and resize" , "Resize and fill" ], type = "index" , value = "Just resize" )
0 commit comments