Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/scripts/sd_upscale.py
3055 views
1
import math
2
3
import modules.scripts as scripts
4
import gradio as gr
5
from PIL import Image
6
7
from modules import processing, shared, images, devices
8
from modules.processing import Processed
9
from modules.shared import opts, state
10
11
12
class Script(scripts.Script):
13
def title(self):
14
return "SD upscale"
15
16
def show(self, is_img2img):
17
return is_img2img
18
19
def ui(self, is_img2img):
20
info = gr.HTML("<p style=\"margin-bottom:0.75em\">Will upscale the image by the selected scale factor; use width and height sliders to set tile size</p>")
21
overlap = gr.Slider(minimum=0, maximum=256, step=16, label='Tile overlap', value=64, elem_id=self.elem_id("overlap"))
22
scale_factor = gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label='Scale Factor', value=2.0, elem_id=self.elem_id("scale_factor"))
23
upscaler_index = gr.Radio(label='Upscaler', choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index", elem_id=self.elem_id("upscaler_index"))
24
25
return [info, overlap, upscaler_index, scale_factor]
26
27
def run(self, p, _, overlap, upscaler_index, scale_factor):
28
if isinstance(upscaler_index, str):
29
upscaler_index = [x.name.lower() for x in shared.sd_upscalers].index(upscaler_index.lower())
30
processing.fix_seed(p)
31
upscaler = shared.sd_upscalers[upscaler_index]
32
33
p.extra_generation_params["SD upscale overlap"] = overlap
34
p.extra_generation_params["SD upscale upscaler"] = upscaler.name
35
36
initial_info = None
37
seed = p.seed
38
39
init_img = p.init_images[0]
40
init_img = images.flatten(init_img, opts.img2img_background_color)
41
42
if upscaler.name != "None":
43
img = upscaler.scaler.upscale(init_img, scale_factor, upscaler.data_path)
44
else:
45
img = init_img
46
47
devices.torch_gc()
48
49
grid = images.split_grid(img, tile_w=p.width, tile_h=p.height, overlap=overlap)
50
51
batch_size = p.batch_size
52
upscale_count = p.n_iter
53
p.n_iter = 1
54
p.do_not_save_grid = True
55
p.do_not_save_samples = True
56
57
work = []
58
59
for _y, _h, row in grid.tiles:
60
for tiledata in row:
61
work.append(tiledata[2])
62
63
batch_count = math.ceil(len(work) / batch_size)
64
state.job_count = batch_count * upscale_count
65
66
print(f"SD upscaling will process a total of {len(work)} images tiled as {len(grid.tiles[0][2])}x{len(grid.tiles)} per upscale in a total of {state.job_count} batches.")
67
68
result_images = []
69
for n in range(upscale_count):
70
start_seed = seed + n
71
p.seed = start_seed
72
73
work_results = []
74
for i in range(batch_count):
75
p.batch_size = batch_size
76
p.init_images = work[i * batch_size:(i + 1) * batch_size]
77
78
state.job = f"Batch {i + 1 + n * batch_count} out of {state.job_count}"
79
processed = processing.process_images(p)
80
81
if initial_info is None:
82
initial_info = processed.info
83
84
p.seed = processed.seed + 1
85
work_results += processed.images
86
87
image_index = 0
88
for _y, _h, row in grid.tiles:
89
for tiledata in row:
90
tiledata[2] = work_results[image_index] if image_index < len(work_results) else Image.new("RGB", (p.width, p.height))
91
image_index += 1
92
93
combined_image = images.combine_grid(grid)
94
result_images.append(combined_image)
95
96
if opts.samples_save:
97
images.save_image(combined_image, p.outpath_samples, "", start_seed, p.prompt, opts.samples_format, info=initial_info, p=p)
98
99
processed = Processed(p, result_images, seed, initial_info)
100
101
return processed
102
103