Skip to content

Commit

Permalink
make base/max size system disablable (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Interpause committed Jan 25, 2023
1 parent ff46139 commit ff82c1d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
12 changes: 10 additions & 2 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ def f_txt2img(req: Txt2ImgRequest):
args = process_script_args(script_ind, script, meta, req.script_args)

width, height = sddebz_highres_fix(
req.base_size, req.max_size, req.orig_width, req.orig_height
req.base_size,
req.max_size,
req.orig_width,
req.orig_height,
req.disable_sddebz_highres,
)

output = wrap_gradio_gpu_call(modules.txt2img.txt2img)(
Expand Down Expand Up @@ -218,7 +222,11 @@ def f_img2img(req: Img2ImgRequest):
width = height = req.base_size
else:
width, height = sddebz_highres_fix(
req.base_size, req.max_size, orig_width, orig_height
req.base_size,
req.max_size,
orig_width,
orig_height,
req.disable_sddebz_highres,
)

# NOTE:
Expand Down
2 changes: 2 additions & 0 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class GenerationOptions(BaseModel):
"""Native/base resolution of model used."""
max_size: int = 768
"""Max input resolution allowed to prevent image artifacts."""
disable_sddebz_highres: bool = False
"""Disable base size/max size system above."""
tiling: bool = False
"""Whether to generate a tileable image."""
highres_fix: bool = False
Expand Down
7 changes: 5 additions & 2 deletions backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def b64_to_img(enc: str):


def sddebz_highres_fix(
base_size: int, max_size: int, orig_width: int, orig_height: int
base_size: int, max_size: int, orig_width: int, orig_height: int, just_stride=False
):
"""Calculate an appropiate image resolution given the base input size of the
model and max input size allowed.
Expand Down Expand Up @@ -204,8 +204,11 @@ def rnd(r, x, z=64):

ratio = orig_width / orig_height

# don't apply correction; just stride to 64
if just_stride:
width, height = ceil(width / 64) * 64, ceil(height / 64) * 64
# height is smaller dimension
if orig_width > orig_height:
elif orig_width > orig_height:
width, height = rnd(ratio, base_size), base_size
if width > max_size:
width, height = max_size, rnd(1 / ratio, max_size)
Expand Down
1 change: 1 addition & 0 deletions frontends/krita/krita_diff/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def common_params(self, has_selection):
batch_size=self.cfg("sd_batch_size", int),
base_size=self.cfg("sd_base_size", int),
max_size=self.cfg("sd_max_size", int),
disable_sddebz_highres=self.cfg("disable_sddebz_highres", bool),
tiling=tiling,
upscaler_name=self.cfg("upscaler_name", str),
restore_faces=self.cfg("face_restorer_model", str) != "None",
Expand Down
1 change: 1 addition & 0 deletions frontends/krita/krita_diff/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Defaults:
alt_dock_behavior: bool = False
hide_layers: bool = True
no_groups: bool = False
disable_sddebz_highres: bool = False

sd_model_list: List[str] = field(default_factory=lambda: [ERROR_MSG])
sd_model: str = "model.ckpt"
Expand Down
20 changes: 19 additions & 1 deletion frontends/krita/krita_diff/pages/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def __init__(self, *args, **kwargs):

# Tiling mode
self.tiling = QCheckBox(script.cfg, "sd_tiling", "Tiling mode")
self.sddebz = QCheckBox(
script.cfg, "disable_sddebz_highres", "Disable base/max size"
)
checkboxes_layout = QHBoxLayout()
checkboxes_layout.addWidget(self.tiling)
checkboxes_layout.addWidget(self.sddebz)

# Interrupt button
self.interrupt_btn = QPushButton("Interrupt")
Expand All @@ -75,7 +81,7 @@ def __init__(self, *args, **kwargs):
layout.addLayout(self.upscaler_layout)
layout.addLayout(self.face_restorer_layout)
layout.addLayout(self.codeformer_weight_layout)
layout.addWidget(self.tiling)
layout.addLayout(checkboxes_layout)
layout.addLayout(self.sd_model_layout)
layout.addLayout(batch_layout)
layout.addLayout(size_layout)
Expand All @@ -94,6 +100,7 @@ def cfg_init(self):
self.face_restorer_layout.cfg_init()
self.codeformer_weight_layout.cfg_init()
self.tiling.cfg_init()
self.sddebz.cfg_init()

self.title.setVisible(not script.cfg("minimize_ui", bool))

Expand All @@ -107,6 +114,7 @@ def cfg_connect(self):
self.face_restorer_layout.cfg_connect()
self.codeformer_weight_layout.cfg_connect()
self.tiling.cfg_connect()
self.sddebz.cfg_connect()

# Hide codeformer_weight when model isnt codeformer
def toggle_codeformer_weights(visible):
Expand All @@ -120,4 +128,14 @@ def toggle_codeformer_weights(visible):
self.face_restorer_layout.qcombo.currentText() == "CodeFormer"
)

# hide base/max size when disabled
def toggle_sddebz_highres(visible):
self.base_size_layout.qspin.setVisible(visible)
self.base_size_layout.qlabel.setVisible(visible)
self.max_size_layout.qspin.setVisible(visible)
self.max_size_layout.qlabel.setVisible(visible)

self.sddebz.toggled.connect(lambda b: toggle_sddebz_highres(not b))
toggle_sddebz_highres(not script.cfg("disable_sddebz_highres", bool))

self.interrupt_btn.released.connect(lambda: script.action_interrupt())

0 comments on commit ff82c1d

Please sign in to comment.