muchkanensys
commited on
Commit
•
4ad7a34
1
Parent(s):
c52b395
Upload png_model_grid.py
Browse files- png_model_grid.py +156 -0
png_model_grid.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import namedtuple
|
2 |
+
from copy import copy
|
3 |
+
from itertools import permutations, chain
|
4 |
+
import random
|
5 |
+
import csv
|
6 |
+
from io import StringIO
|
7 |
+
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
+
import os
|
10 |
+
|
11 |
+
import modules.scripts as scripts
|
12 |
+
import gradio as gr
|
13 |
+
from modules import images, sd_samplers
|
14 |
+
from modules.paths import models_path
|
15 |
+
from modules.hypernetworks import hypernetwork
|
16 |
+
from modules.processing import process_images, Processed, StableDiffusionProcessingTxt2Img
|
17 |
+
from modules.shared import opts, cmd_opts, state
|
18 |
+
import modules.shared as shared
|
19 |
+
import modules.sd_samplers
|
20 |
+
import modules.sd_models
|
21 |
+
import modules.generation_parameters_copypaste as parameters_copypaste
|
22 |
+
|
23 |
+
|
24 |
+
def edit_prompt(p,x):
|
25 |
+
img = Image.open(x)
|
26 |
+
params={}
|
27 |
+
params["Prompt"] = ""
|
28 |
+
params["Negative prompt"] = ""
|
29 |
+
params["Seed"] = ""
|
30 |
+
if img.text['parameters']:
|
31 |
+
params = parameters_copypaste.parse_generation_parameters(img.text['parameters'])
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
p.prompt = params["Prompt"]
|
36 |
+
p.negative_prompt = params["Negative prompt"]
|
37 |
+
p.seed = params["Seed"]
|
38 |
+
|
39 |
+
|
40 |
+
def apply_checkpoint(p, x, xs):
|
41 |
+
info = modules.sd_models.get_closet_checkpoint_match(x)
|
42 |
+
if info is None:
|
43 |
+
raise RuntimeError(f"Unknown checkpoint: {x}")
|
44 |
+
modules.sd_models.reload_model_weights(shared.sd_model, info)
|
45 |
+
|
46 |
+
def confirm_checkpoints(p, xs):
|
47 |
+
for x in xs:
|
48 |
+
print(f'confirm_checkpoint {x}')
|
49 |
+
if modules.sd_models.get_closet_checkpoint_match(x) is None:
|
50 |
+
raise RuntimeError(f"Unknown checkpoint: {x}")
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
def draw_xy_grid(p, input_dir, ms, cell):
|
55 |
+
ps = os.listdir(input_dir)
|
56 |
+
|
57 |
+
ver_texts = [[images.GridAnnotation(y)] for y in ms]
|
58 |
+
hor_texts = [[images.GridAnnotation(x)] for x in ps]
|
59 |
+
|
60 |
+
# Temporary list of all the images that are generated to be populated into the grid.
|
61 |
+
# Will be filled with empty images for any individual step that fails to process properly
|
62 |
+
image_cache = []
|
63 |
+
|
64 |
+
processed_result = None
|
65 |
+
cell_mode = "P"
|
66 |
+
cell_size = (1,1)
|
67 |
+
|
68 |
+
state.job_count = len(ms) * len(ps) * p.n_iter
|
69 |
+
|
70 |
+
for iy, y in enumerate(ms):
|
71 |
+
for ix, x in enumerate(ps):
|
72 |
+
state.job = f"{ix + iy * len(ps) + 1} out of {len(ps) * len(ms)}"
|
73 |
+
processed:Processed = cell(os.path.join(input_dir,x), y)
|
74 |
+
try:
|
75 |
+
# this dereference will throw an exception if the image was not processed
|
76 |
+
# (this happens in cases such as if the user stops the process from the UI)
|
77 |
+
processed_image = processed.images[0]
|
78 |
+
|
79 |
+
if processed_result is None:
|
80 |
+
# Use our first valid processed result as a template container to hold our full results
|
81 |
+
processed_result = copy(processed)
|
82 |
+
cell_mode = processed_image.mode
|
83 |
+
cell_size = processed_image.size
|
84 |
+
processed_result.images = [Image.new(cell_mode, cell_size)]
|
85 |
+
|
86 |
+
image_cache.append(processed_image)
|
87 |
+
|
88 |
+
except:
|
89 |
+
image_cache.append(Image.new(cell_mode, cell_size))
|
90 |
+
|
91 |
+
if not processed_result:
|
92 |
+
print("Unexpected error: draw_xy_grid failed to return even a single processed image")
|
93 |
+
return Processed()
|
94 |
+
|
95 |
+
grid = images.image_grid(image_cache, rows=len(ms))
|
96 |
+
grid = images.draw_grid_annotations(grid, cell_size[0], cell_size[1], hor_texts, ver_texts)
|
97 |
+
|
98 |
+
processed_result.images[0] = grid
|
99 |
+
|
100 |
+
return processed_result
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
class SharedSettingsStackHelper(object):
|
105 |
+
def __enter__(self):
|
106 |
+
self.CLIP_stop_at_last_layers = opts.CLIP_stop_at_last_layers
|
107 |
+
self.vae = opts.sd_vae
|
108 |
+
|
109 |
+
def __exit__(self, exc_type, exc_value, tb):
|
110 |
+
opts.data["sd_vae"] = self.vae
|
111 |
+
modules.sd_models.reload_model_weights()
|
112 |
+
modules.sd_vae.reload_vae_weights()
|
113 |
+
|
114 |
+
opts.data["CLIP_stop_at_last_layers"] = self.CLIP_stop_at_last_layers
|
115 |
+
|
116 |
+
|
117 |
+
class Script(scripts.Script):
|
118 |
+
def title(self):
|
119 |
+
return "PNG/Model Grid"
|
120 |
+
|
121 |
+
def ui(self, is_img2img):
|
122 |
+
|
123 |
+
with gr.Row():
|
124 |
+
input_dir = gr.Textbox(label="PNG input dir", lines=1)
|
125 |
+
with gr.Row():
|
126 |
+
models = gr.Textbox(label="Checkpoint names", lines=1)
|
127 |
+
|
128 |
+
return [input_dir ,models]
|
129 |
+
|
130 |
+
def run(self, p, input_dir ,models):
|
131 |
+
|
132 |
+
p.batch_count = 1
|
133 |
+
p.batch_size = 1
|
134 |
+
|
135 |
+
ms = [x.strip() for x in chain.from_iterable(csv.reader(StringIO(models)))]
|
136 |
+
|
137 |
+
confirm_checkpoints(p,ms)
|
138 |
+
|
139 |
+
def cell(x, y):
|
140 |
+
pc = copy(p)
|
141 |
+
edit_prompt(pc, x)
|
142 |
+
apply_checkpoint(pc, y, ms)
|
143 |
+
return process_images(pc)
|
144 |
+
|
145 |
+
with SharedSettingsStackHelper():
|
146 |
+
processed = draw_xy_grid(
|
147 |
+
p,
|
148 |
+
input_dir=input_dir,
|
149 |
+
ms=ms,
|
150 |
+
cell=cell
|
151 |
+
)
|
152 |
+
|
153 |
+
if opts.grid_save:
|
154 |
+
images.save_image(processed.images[0], p.outpath_grids, "xy_grid", prompt=p.prompt, seed=processed.seed, grid=True, p=p)
|
155 |
+
|
156 |
+
return processed
|