Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing-su committed Sep 2, 2024
2 parents 25e7509 + 5ff72ad commit 03ec9d0
Show file tree
Hide file tree
Showing 20 changed files with 455 additions and 81 deletions.
4 changes: 1 addition & 3 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ body:
label: Steps to reproduce
description: |
Description of how we can reproduce this issue.
validations:
required: true
- type: textarea
attributes:
Expand All @@ -38,7 +36,7 @@ body:
attributes:
label: Console logs, from start to end.
description: |
The full console log of your terminal.
The FULL console log of your terminal.
placeholder: |
Python ...
Version: ...
Expand Down
23 changes: 0 additions & 23 deletions .github/workflows/lgtm.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
test:
name: test
runs-on: macos-14
runs-on: macos-latest
strategy:
matrix:
python-version:
Expand All @@ -27,7 +27,7 @@ jobs:

- name: Install dependencies
run: |
uv pip install --system . pytest
uv pip install --system ".[test]"
- name: Run tests
run: pytest -v
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Test on PR

on:
pull_request:
paths:
- "adetailer/**.py"

jobs:
test:
name: Test on python ${{ matrix.python-version }}
runs-on: macos-latest
strategy:
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- uses: yezz123/setup-uv@v4

- name: Install dependencies
run: |
uv pip install --system ".[test]"
- name: Run tests
run: pytest -v
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,4 @@ pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
*.ipynb
node_modules
modules
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ci:
autoupdate_branch: "dev"

exclude: ^modules/

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
Expand All @@ -22,7 +24,7 @@ repos:
- id: prettier

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.6
rev: v0.5.7
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 2024-09-02

- v24.9.0
- Dynamic Denoising, Inpaint bbox sizing 기능 (PR #678)
- `ad_save_images_dir` 옵션 추가 - ad 이미지를 저장하는 장소 지정 (PR #689)

- forge와 관련된 버그 몇 개 수정
- pydantic validation에 실패해도 에러를 일으키지 않고 넘어가도록 수정

## 2024-08-03

- v24.8.0
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tasks:

update:
cmds:
- "{{.PYTHON}} -m uv pip install -U ultralytics mediapipe ruff pre-commit black devtools pytest"
- "{{.PYTHON}} -m uv pip install -U ultralytics mediapipe ruff pre-commit black devtools pytest hypothesis"

update-torch:
cmds:
Expand Down
6 changes: 6 additions & 0 deletions aaaaaa/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import TYPE_CHECKING, Any, Union

import torch
from PIL import Image
from typing_extensions import Protocol

from modules import safe
from modules.shared import opts
Expand Down Expand Up @@ -57,3 +59,7 @@ def preserve_prompts(p: PT):

def copy_extra_params(extra_params: dict[str, Any]) -> dict[str, Any]:
return {k: v for k, v in extra_params.items() if not callable(v)}


class PPImage(Protocol):
image: Image.Image
2 changes: 1 addition & 1 deletion aaaaaa/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def get_table(title: str, data: dict[str, Any]) -> Table:
table.add_column("Value")
for key, value in data.items():
if not isinstance(value, str):
value = repr(value)
value = repr(value) # noqa: PLW2901
table.add_row(key, value)

return table
Expand Down
4 changes: 3 additions & 1 deletion aaaaaa/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def one_ui_group(n: int, is_img2img: bool, webui_info: WebuiInfo):
with gr.Group():
with gr.Row(elem_id=eid("ad_toprow_prompt")):
w.ad_prompt = gr.Textbox(
value="",
label="ad_prompt" + suffix(n),
show_label=False,
lines=3,
Expand All @@ -230,6 +231,7 @@ def one_ui_group(n: int, is_img2img: bool, webui_info: WebuiInfo):

with gr.Row(elem_id=eid("ad_toprow_negative_prompt")):
w.ad_negative_prompt = gr.Textbox(
value="",
label="ad_negative_prompt" + suffix(n),
show_label=False,
lines=2,
Expand Down Expand Up @@ -369,7 +371,7 @@ def mask_preprocessing(w: Widgets, n: int, is_img2img: bool):
)


def inpainting(w: Widgets, n: int, is_img2img: bool, webui_info: WebuiInfo):
def inpainting(w: Widgets, n: int, is_img2img: bool, webui_info: WebuiInfo): # noqa: PLR0915
eid = partial(elem_id, n=n, is_img2img=is_img2img)

with gr.Group():
Expand Down
2 changes: 1 addition & 1 deletion adetailer/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "24.8.0"
__version__ = "24.9.0"
15 changes: 15 additions & 0 deletions adetailer/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections import UserList
from dataclasses import dataclass
from enum import Enum
from functools import cached_property, partial
from typing import Any, Literal, NamedTuple, Optional

Expand Down Expand Up @@ -262,6 +263,7 @@ def need_skip(self) -> bool:
"Position (center to edge)",
"Area (large to small)",
]

MASK_MERGE_INVERT = ["None", "Merge", "Merge and Invert"]

_script_default = (
Expand All @@ -276,3 +278,16 @@ def need_skip(self) -> bool:

_builtin_script = ("soft_inpainting", "hypertile_script")
BUILTIN_SCRIPT = ",".join(sorted(_builtin_script))


class InpaintBBoxMatchMode(Enum):
OFF = "Off"
STRICT = "Strict (SDXL only)"
FREE = "Free"


INPAINT_BBOX_MATCH_MODES = [
InpaintBBoxMatchMode.OFF.value,
InpaintBBoxMatchMode.STRICT.value,
InpaintBBoxMatchMode.FREE.value,
]
2 changes: 1 addition & 1 deletion adetailer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def create_bbox_from_mask(
"""
bboxes = []
for mask in masks:
mask = mask.resize(shape)
mask = mask.resize(shape) # noqa: PLW2901
bbox = mask.getbbox()
if bbox is not None:
bboxes.append(list(bbox))
Expand Down
101 changes: 101 additions & 0 deletions adetailer/opts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import ClassVar, TypeVar

import numpy as np

T = TypeVar("T", int, float)


def dynamic_denoise_strength(
denoise_power: float,
denoise_strength: float,
bbox: Sequence[T],
image_size: tuple[int, int],
) -> float:
if len(bbox) != 4:
msg = f"bbox length must be 4, got {len(bbox)}"
raise ValueError(msg)

if np.isclose(denoise_power, 0.0) or len(bbox) != 4:
return denoise_strength

width, height = image_size

image_pixels = width * height
bbox_pixels = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])

normalized_area = bbox_pixels / image_pixels
denoise_modifier = (1.0 - normalized_area) ** denoise_power

return denoise_strength * denoise_modifier


class _OptimalCropSize:
sdxl_res: ClassVar[list[tuple[int, int]]] = [
(1024, 1024),
(1152, 896),
(896, 1152),
(1216, 832),
(832, 1216),
(1344, 768),
(768, 1344),
(1536, 640),
(640, 1536),
]

def sdxl(
self, inpaint_width: int, inpaint_height: int, bbox: Sequence[T]
) -> tuple[int, int]:
if len(bbox) != 4:
msg = f"bbox length must be 4, got {len(bbox)}"
raise ValueError(msg)

bbox_width = bbox[2] - bbox[0]
bbox_height = bbox[3] - bbox[1]
bbox_aspect_ratio = bbox_width / bbox_height

resolutions = [
res
for res in self.sdxl_res
if (res[0] >= bbox_width and res[1] >= bbox_height)
and (res[0] >= inpaint_width or res[1] >= inpaint_height)
]

if not resolutions:
return inpaint_width, inpaint_height

return min(
resolutions,
key=lambda res: abs((res[0] / res[1]) - bbox_aspect_ratio),
)

def free(
self, inpaint_width: int, inpaint_height: int, bbox: Sequence[T]
) -> tuple[int, int]:
if len(bbox) != 4:
msg = f"bbox length must be 4, got {len(bbox)}"
raise ValueError(msg)

bbox_width = bbox[2] - bbox[0]
bbox_height = bbox[3] - bbox[1]
bbox_aspect_ratio = bbox_width / bbox_height

scale_size = max(inpaint_width, inpaint_height)

if bbox_aspect_ratio > 1:
optimal_width = scale_size
optimal_height = scale_size / bbox_aspect_ratio
else:
optimal_width = scale_size * bbox_aspect_ratio
optimal_height = scale_size

# Round up to the nearest multiple of 8 to make the dimensions friendly for upscaling/diffusion.
optimal_width = ((optimal_width + 8 - 1) // 8) * 8
optimal_height = ((optimal_height + 8 - 1) // 8) * 8

return int(optimal_width), int(optimal_height)


optimal_crop_size = _OptimalCropSize()
3 changes: 2 additions & 1 deletion controlnet_ext/controlnet_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def init_controlnet(self):
models = self.external_cn.get_models()
self.cn_models.extend(m for m in models if cn_model_regex.search(m))

def update_scripts_args(
def update_scripts_args( # noqa: PLR0913
self,
p,
model: str,
Expand Down Expand Up @@ -78,6 +78,7 @@ def update_scripts_args(
guidance_start=guidance_start,
guidance_end=guidance_end,
pixel_perfect=True,
enabled=True,
)
]

Expand Down
2 changes: 1 addition & 1 deletion controlnet_ext/controlnet_ext_forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self):
def init_controlnet(self):
self.cn_available = True

def update_scripts_args(
def update_scripts_args( # noqa: PLR0913
self,
p,
model: str,
Expand Down
Loading

0 comments on commit 03ec9d0

Please sign in to comment.