Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f87d710d14 | ||
|
|
85da4a4d0e |
@@ -2,6 +2,16 @@
|
||||
|
||||
All notable changes to ComfyUI-AusBoss are documented here.
|
||||
|
||||
## Unreleased
|
||||
|
||||
- **Box edits on big photos start much sooner.** Crop For Inpaint,
|
||||
Image Crop + Rotate + Pad and Stitch Inpaint grew and softened their
|
||||
masks across the whole picture, even when the box was small or there was
|
||||
nothing to do. On a 17 MP photo that took about 5 seconds before the
|
||||
model started. The work now covers only the area around the mask:
|
||||
Crop For Inpaint went from 3.1 s to 0.14 s, and Image Crop + Rotate + Pad
|
||||
with nothing to do from 2.2 s to 0.5 s. The results are the same pixels.
|
||||
|
||||
## 2.26.4 - 2026-11-18
|
||||
|
||||
- **A video upload stops at 4 GB.** Dropping a video on Load Video had no
|
||||
|
||||
@@ -966,15 +966,12 @@ def inner_tone_offset(
|
||||
diff = new - old
|
||||
frames = diff.shape[0]
|
||||
whole = (blend > 0.999).to(diff.dtype)
|
||||
size = 2 * INNER_RIM_PX + 1
|
||||
# A pixel is deeper than the rim when nothing within INNER_RIM_PX of it
|
||||
# is outside the whole paste. The crop's own border does not count as
|
||||
# outside: the painted area simply runs off the crop there.
|
||||
# One pass along each axis: the same square reach at a fraction of the cost.
|
||||
outside = (1.0 - whole).unsqueeze(1)
|
||||
outside = functional.max_pool2d(outside, (1, size), stride=1, padding=(0, INNER_RIM_PX))
|
||||
outside = functional.max_pool2d(outside, (size, 1), stride=1, padding=(INNER_RIM_PX, 0))
|
||||
deep = 1.0 - outside.squeeze(1)
|
||||
outside = grow_shrink_mask(1.0 - whole, INNER_RIM_PX)
|
||||
deep = 1.0 - outside
|
||||
rim = (whole > 0) & (deep < 0.5)
|
||||
rim = torch.where(rim.flatten(1).any(dim=1).view(-1, 1, 1), rim, whole > 0)
|
||||
pasted = whole > 0
|
||||
|
||||
+73
-11
@@ -63,6 +63,41 @@ def _as_bhw(mask: torch.Tensor) -> torch.Tensor:
|
||||
MASK_CHUNK = 8
|
||||
|
||||
|
||||
def _within_support(mask: torch.Tensor, reach: int, run) -> torch.Tensor:
|
||||
"""Run a neighbourhood filter only where it can change anything.
|
||||
|
||||
``run`` is a grow, shrink or blur whose output pixel reads the input no
|
||||
further than ``reach`` pixels away, and gives exactly 0 where all it
|
||||
reads is 0. Outside the nonzero pixels grown by ``reach`` the result is
|
||||
therefore 0, so the filter runs on a window around them, grown by twice
|
||||
the reach: every pixel kept from it reads only what it would read on the
|
||||
whole frame, and at the frame's own edge the window ends there too, so
|
||||
edge padding behaves the same. A box drawn on a big photo then costs the
|
||||
box, not the photo; an empty mask costs one scan.
|
||||
"""
|
||||
if mask.ndim != 3 or mask.numel() == 0:
|
||||
return run(mask)
|
||||
height, width = int(mask.shape[1]), int(mask.shape[2])
|
||||
nonzero = (mask != 0).any(dim=0)
|
||||
rows = torch.nonzero(nonzero.any(dim=1)).flatten()
|
||||
if rows.numel() == 0:
|
||||
return torch.zeros_like(mask)
|
||||
cols = torch.nonzero(nonzero.any(dim=0)).flatten()
|
||||
top, bottom = int(rows[0]), int(rows[-1]) + 1
|
||||
left, right = int(cols[0]), int(cols[-1]) + 1
|
||||
wy0, wy1 = max(0, top - 2 * reach), min(height, bottom + 2 * reach)
|
||||
wx0, wx1 = max(0, left - 2 * reach), min(width, right + 2 * reach)
|
||||
# Most of the frame anyway: the copies would cost more than they save.
|
||||
if (wy1 - wy0) * (wx1 - wx0) * 2 > height * width:
|
||||
return run(mask)
|
||||
window = run(mask[:, wy0:wy1, wx0:wx1].contiguous())
|
||||
oy0, oy1 = max(0, top - reach), min(height, bottom + reach)
|
||||
ox0, ox1 = max(0, left - reach), min(width, right + reach)
|
||||
result = torch.zeros_like(mask)
|
||||
result[:, oy0:oy1, ox0:ox1] = window[:, oy0 - wy0 : oy1 - wy0, ox0 - wx0 : ox1 - wx0]
|
||||
return result
|
||||
|
||||
|
||||
def grow_shrink_mask(mask: torch.Tensor, pixels: int) -> torch.Tensor:
|
||||
"""Dilate (positive) or erode (negative) by whole pixels; soft values survive.
|
||||
|
||||
@@ -77,17 +112,40 @@ def grow_shrink_mask(mask: torch.Tensor, pixels: int) -> torch.Tensor:
|
||||
steps = abs(int(pixels))
|
||||
if steps == 0:
|
||||
return mask
|
||||
return _within_support(mask, steps, lambda part: _grow_shrink_full(part, pixels, steps))
|
||||
|
||||
|
||||
def _grow_shrink_full(mask: torch.Tensor, pixels: int, steps: int) -> torch.Tensor:
|
||||
if mask.shape[0] > MASK_CHUNK:
|
||||
return torch.cat([grow_shrink_mask(part, pixels) for part in mask.split(MASK_CHUNK)])
|
||||
size = 2 * steps + 1
|
||||
grown = mask.unsqueeze(1)
|
||||
return torch.cat([_grow_shrink_full(part, pixels, steps) for part in mask.split(MASK_CHUNK)])
|
||||
if pixels > 0:
|
||||
grown = functional.max_pool2d(grown, kernel_size=(1, size), stride=1, padding=(0, steps))
|
||||
grown = functional.max_pool2d(grown, kernel_size=(size, 1), stride=1, padding=(steps, 0))
|
||||
else:
|
||||
grown = -functional.max_pool2d(-grown, kernel_size=(1, size), stride=1, padding=(0, steps))
|
||||
grown = -functional.max_pool2d(-grown, kernel_size=(size, 1), stride=1, padding=(steps, 0))
|
||||
return grown.squeeze(1)
|
||||
return _running_max(_running_max(mask, steps, 2), steps, 1)
|
||||
return -_running_max(_running_max(-mask, steps, 2), steps, 1)
|
||||
|
||||
|
||||
def _running_max(values: torch.Tensor, steps: int, dim: int) -> torch.Tensor:
|
||||
"""Max over the ``2 * steps + 1`` pixels centred on each pixel along ``dim``.
|
||||
|
||||
The same numbers as a max pool with that kernel and ``steps`` of -inf
|
||||
padding, but by doubling: after k passes each pixel holds the max of the
|
||||
2**k pixels from it on, and two such overlapping runs cover the whole
|
||||
window. Max is exact in any order, so this matches the pool to the bit,
|
||||
in about log2 of the window passes instead of one read per window pixel.
|
||||
"""
|
||||
size = 2 * steps + 1
|
||||
length = values.shape[dim]
|
||||
pad_shape = list(values.shape)
|
||||
pad_shape[dim] = steps
|
||||
edge = values.new_full(pad_shape, float("-inf"))
|
||||
run = torch.cat((edge, values, edge), dim=dim)
|
||||
width = 1
|
||||
while width * 2 <= size:
|
||||
count = run.shape[dim] - width
|
||||
run = torch.maximum(run.narrow(dim, 0, count), run.narrow(dim, width, count))
|
||||
width *= 2
|
||||
if width == size:
|
||||
return run.narrow(dim, 0, length).contiguous()
|
||||
return torch.maximum(run.narrow(dim, 0, length), run.narrow(dim, size - width, length))
|
||||
|
||||
|
||||
def _torch_fill_holes(solid: torch.Tensor) -> torch.Tensor:
|
||||
@@ -194,9 +252,13 @@ def fill_mask_holes(mask: torch.Tensor, max_hole_size: float = 0.0) -> torch.Ten
|
||||
def blur_mask(mask: torch.Tensor, sigma: float) -> torch.Tensor:
|
||||
if sigma <= 0.0:
|
||||
return mask
|
||||
if mask.shape[0] > MASK_CHUNK:
|
||||
return torch.cat([blur_mask(part, sigma) for part in mask.split(MASK_CHUNK)])
|
||||
radius = max(1, int(math.ceil(float(sigma) * 3.0)))
|
||||
return _within_support(mask, radius, lambda part: _blur_full(part, sigma, radius))
|
||||
|
||||
|
||||
def _blur_full(mask: torch.Tensor, sigma: float, radius: int) -> torch.Tensor:
|
||||
if mask.shape[0] > MASK_CHUNK:
|
||||
return torch.cat([_blur_full(part, sigma, radius) for part in mask.split(MASK_CHUNK)])
|
||||
coords = torch.arange(-radius, radius + 1, dtype=torch.float32, device=mask.device)
|
||||
kernel = torch.exp(-(coords**2) / (2.0 * float(sigma) ** 2))
|
||||
kernel = kernel / kernel.sum()
|
||||
|
||||
@@ -393,8 +393,9 @@ def transform_pil_batch(
|
||||
)
|
||||
image_array = np.asarray(output, dtype=np.float32) / 255.0
|
||||
mask_array = np.asarray(mask, dtype=np.float32) / 255.0
|
||||
frames.append(torch.from_numpy(image_array.copy()))
|
||||
masks.append(torch.from_numpy(mask_array.copy()))
|
||||
# The division already made fresh arrays, so no copy is needed.
|
||||
frames.append(torch.from_numpy(image_array))
|
||||
masks.append(torch.from_numpy(mask_array))
|
||||
views.append(prompt_view)
|
||||
|
||||
if not frames or first_geometry is None:
|
||||
@@ -575,6 +576,15 @@ def resize_then_pad(output, mask, geometry, fill_color, megapixels, method, step
|
||||
return place(output), resized, None if view is None else place(view)
|
||||
|
||||
|
||||
def has_see_through(images: Iterable[Image.Image]) -> bool:
|
||||
"""True when any frame has see-through parts (:func:`see_through_kept`),
|
||||
so :func:`original_image_batch` would differ with ``keep_see_through``."""
|
||||
return any(
|
||||
see_through_kept(image if image.mode == "RGBA" else image.convert("RGBA")) is not None
|
||||
for image in images
|
||||
)
|
||||
|
||||
|
||||
def original_image_batch(images: Iterable[Image.Image], keep_see_through: bool = False) -> torch.Tensor:
|
||||
"""Untransformed, EXIF-oriented RGB source frames for reference outputs.
|
||||
|
||||
@@ -586,7 +596,7 @@ def original_image_batch(images: Iterable[Image.Image], keep_see_through: bool =
|
||||
"""
|
||||
frames = []
|
||||
for image in images:
|
||||
array = np.asarray(image.convert("RGB"), dtype=np.float32).copy()
|
||||
array = np.array(image.convert("RGB"), dtype=np.float32)
|
||||
kept = None if keep_see_through else see_through_kept(image if image.mode == "RGBA" else image.convert("RGBA"))
|
||||
if kept is not None:
|
||||
array[~kept] = SEE_THROUGH_BACKDROP
|
||||
|
||||
@@ -6,6 +6,7 @@ from ._execution_helpers import ignore_extra_inputs
|
||||
from ._media_helpers import list_input_images, load_image_frames, resolve_input_path
|
||||
from ._inpaint_crop_helpers import build_transform_stitcher, remember_original
|
||||
from ._transform_engine import (
|
||||
has_see_through,
|
||||
original_image_batch,
|
||||
resize_then_pad,
|
||||
stable_file_fingerprint,
|
||||
@@ -138,13 +139,20 @@ class AusBossImageCropRotatePad:
|
||||
source="Image Crop + Rotate + Pad",
|
||||
)
|
||||
# The picture as stored: a MaskEditor stroke keeps what is under it.
|
||||
remember_original(stitcher, original_image_batch(frames, True), geometry, spec.normalized().rotation_degrees)
|
||||
stored = original_image_batch(frames, True)
|
||||
remember_original(stitcher, stored, geometry, spec.normalized().rotation_degrees)
|
||||
# With nothing see-through both copies hold the same pixels: copy
|
||||
# the one already made instead of converting the picture again.
|
||||
if keep_picture or not has_see_through(frames):
|
||||
original = stored.clone()
|
||||
else:
|
||||
original = original_image_batch(frames, False)
|
||||
# Nothing see-through: the prompt view is the image itself, as a copy
|
||||
# so no consumer can change one through the other.
|
||||
if prompt_image is None:
|
||||
prompt_image = output.clone()
|
||||
return (
|
||||
output, mask, stitcher, original_image_batch(frames, keep_picture),
|
||||
output, mask, stitcher, original,
|
||||
int(output.shape[2]), int(output.shape[1]), prompt_image,
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
"""The box-edit path costs the box, not the picture, and gives the same pixels.
|
||||
|
||||
Crop + Rotate + Pad, Crop For Inpaint and Stitch Inpaint used to grow and
|
||||
blur masks across the whole frame: a 17 MP photo with a small box drawn on it
|
||||
spent about 5 s on CPU before the sampler started. The filters now run on a
|
||||
window around the mask's nonzero pixels, and grows use an exact doubling
|
||||
running max instead of a wide max pool.
|
||||
|
||||
Two kinds of test:
|
||||
|
||||
- Pixel match. The old full-frame filters live on here as references and are
|
||||
patched back into the helpers. Grows and shrinks must match to the bit.
|
||||
A blur on a window can round its last bit differently from the same blur
|
||||
on the whole frame (conv2d picks its kernel by size), so blurred values,
|
||||
and node outputs that use them, match within FLOAT_ROUNDING; pixels the
|
||||
stitch leaves alone stay bit-identical either way.
|
||||
- Timing. A 17 MP no-op Crop + Rotate + Pad, a box Crop For Inpaint and its
|
||||
Stitch Inpaint, each with a bound several times the fixed cost and well
|
||||
under the old one, so a slow runner passes and a full-frame regression
|
||||
does not.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import ExitStack
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
import torch
|
||||
import torch.nn.functional as functional
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
COMFY_ROOT = os.environ.get("AUSBOSS_COMFY_ROOT")
|
||||
if COMFY_ROOT:
|
||||
sys.path.append(COMFY_ROOT)
|
||||
HAS_COMFY = importlib.util.find_spec("comfy") is not None
|
||||
if "nodes" in sys.modules and not hasattr(sys.modules["nodes"], "__path__"):
|
||||
del sys.modules["nodes"]
|
||||
|
||||
from nodes import _mask_helpers as mask_helpers
|
||||
from nodes._inpaint_crop_helpers import stitch_blend_from_mask
|
||||
from nodes._mask_helpers import blur_mask, grow_shrink_mask
|
||||
from nodes.node_image_crop_rotate_pad import AusBossImageCropRotatePad
|
||||
from nodes.node_inpaint_crop_stitch import AusBossCropForInpaint, AusBossStitchInpaint
|
||||
|
||||
|
||||
# --- the old full-frame filters, kept as references ---------------------------
|
||||
|
||||
|
||||
def pooled_running_max(values: torch.Tensor, steps: int, dim: int) -> torch.Tensor:
|
||||
"""The max pool grow_shrink_mask used before, one axis at a time."""
|
||||
size = 2 * steps + 1
|
||||
grown = values.unsqueeze(1)
|
||||
if dim == 2:
|
||||
grown = functional.max_pool2d(grown, kernel_size=(1, size), stride=1, padding=(0, steps))
|
||||
else:
|
||||
grown = functional.max_pool2d(grown, kernel_size=(size, 1), stride=1, padding=(steps, 0))
|
||||
return grown.squeeze(1)
|
||||
|
||||
|
||||
def whole_frame(mask, reach, run):
|
||||
return run(mask)
|
||||
|
||||
|
||||
def old_filters():
|
||||
"""Patch the old behaviour back in: whole-frame filters, max pool grows."""
|
||||
stack = ExitStack()
|
||||
stack.enter_context(patch.object(mask_helpers, "_within_support", whole_frame))
|
||||
stack.enter_context(patch.object(mask_helpers, "_running_max", pooled_running_max))
|
||||
return stack
|
||||
|
||||
|
||||
def photo(height: int, width: int, seed: int = 0) -> np.ndarray:
|
||||
"""A smooth picture with grain, like a photo, as HWC uint8."""
|
||||
rng = np.random.default_rng(seed)
|
||||
rows, cols = np.mgrid[0:height, 0:width]
|
||||
picture = np.stack(
|
||||
[cols * 255 // max(1, width), rows * 255 // max(1, height), (rows + cols) * 127 // max(1, height + width)],
|
||||
axis=-1,
|
||||
).astype(np.int16)
|
||||
return np.clip(picture + rng.integers(-20, 20, picture.shape), 0, 255).astype(np.uint8)
|
||||
|
||||
|
||||
def box_mask(height: int, width: int, top: int, left: int, box_h: int, box_w: int, value: float = 1.0):
|
||||
mask = torch.zeros((1, height, width), dtype=torch.float32)
|
||||
mask[:, top : top + box_h, left : left + box_w] = value
|
||||
return mask
|
||||
|
||||
|
||||
# One float32 step at 1.0 is 1.2e-7; a tenth of an 8-bit level is 4e-4.
|
||||
FLOAT_ROUNDING = 1e-6
|
||||
|
||||
|
||||
def assert_same(case: unittest.TestCase, new, old, where: str = "", tolerance: float = 0.0) -> None:
|
||||
if isinstance(new, torch.Tensor):
|
||||
case.assertTrue(isinstance(old, torch.Tensor), where)
|
||||
case.assertEqual(tuple(new.shape), tuple(old.shape), where)
|
||||
if tolerance and new.dtype.is_floating_point:
|
||||
diff = float((new - old).abs().max()) if new.numel() else 0.0
|
||||
case.assertLessEqual(diff, tolerance, f"{where}: max diff {diff}")
|
||||
else:
|
||||
case.assertTrue(torch.equal(new, old), f"{where}: max diff {float((new - old).abs().max())}")
|
||||
elif isinstance(new, dict):
|
||||
case.assertEqual(sorted(new), sorted(old), where)
|
||||
for key in new:
|
||||
assert_same(case, new[key], old[key], f"{where}.{key}", tolerance)
|
||||
elif isinstance(new, (list, tuple)):
|
||||
case.assertEqual(len(new), len(old), where)
|
||||
for index, (a, b) in enumerate(zip(new, old)):
|
||||
assert_same(case, a, b, f"{where}[{index}]", tolerance)
|
||||
else:
|
||||
case.assertEqual(new, old, where)
|
||||
|
||||
|
||||
# --- pixel match ---------------------------------------------------------------
|
||||
|
||||
|
||||
class FilterMatchTests(unittest.TestCase):
|
||||
def masks(self, height, width):
|
||||
generator = torch.Generator().manual_seed(height * 1000 + width)
|
||||
noise = torch.rand((2, height, width), generator=generator)
|
||||
yield "noise", noise
|
||||
yield "sparse soft", (noise > 0.9).float() * torch.rand((2, height, width), generator=generator)
|
||||
yield "inner box", box_mask(height, width, height // 3, width // 3, max(1, height // 5), max(1, width // 6), 0.7)
|
||||
yield "box on the left edge", box_mask(height, width, height // 4, 0, max(1, height // 3), max(1, width // 7))
|
||||
yield "box in a corner", box_mask(height, width, height - max(1, height // 6), width - max(1, width // 6), height, width)
|
||||
yield "one pixel", box_mask(height, width, height // 2, width // 2, 1, 1)
|
||||
yield "empty", torch.zeros((1, height, width))
|
||||
frames = torch.zeros((3, height, width))
|
||||
frames[0, : height // 4, : width // 4] = 1.0
|
||||
frames[2, -max(1, height // 5) :, width // 2 :] = 0.4
|
||||
yield "frames in different places", frames
|
||||
|
||||
def test_grow_and_shrink_match_the_max_pool(self):
|
||||
for height, width in ((1, 1), (5, 9), (64, 48), (211, 307)):
|
||||
for name, mask in self.masks(height, width):
|
||||
for pixels in (1, 2, 3, 5, 8, 16, 31, 48, 100, -1, -3, -8, -48):
|
||||
with self.subTest(size=(height, width), mask=name, pixels=pixels):
|
||||
new = grow_shrink_mask(mask, pixels)
|
||||
with old_filters():
|
||||
old = grow_shrink_mask(mask, pixels)
|
||||
assert_same(self, new, old)
|
||||
|
||||
def test_blur_matches_the_whole_frame_blur(self):
|
||||
for height, width in ((1, 1), (5, 9), (64, 48), (211, 307)):
|
||||
for name, mask in self.masks(height, width):
|
||||
for sigma in (0.5, 2.0, 16.0, 48 / 3.0):
|
||||
with self.subTest(size=(height, width), mask=name, sigma=sigma):
|
||||
new = blur_mask(mask, sigma)
|
||||
with old_filters():
|
||||
old = blur_mask(mask, sigma)
|
||||
assert_same(self, new, old, tolerance=FLOAT_ROUNDING)
|
||||
# Where the old blur gave exactly 0, so does the new one.
|
||||
self.assertTrue(torch.equal(new == 0, old == 0))
|
||||
|
||||
def test_the_stitch_blend_matches(self):
|
||||
mask = box_mask(300, 220, 40, 60, 90, 70)
|
||||
for blend, grow in ((0, 0), (16, 0), (48, 8), (32, -6)):
|
||||
with self.subTest(blend=blend, grow=grow):
|
||||
new = stitch_blend_from_mask(mask, blend, grow)
|
||||
with old_filters():
|
||||
old = stitch_blend_from_mask(mask, blend, grow)
|
||||
assert_same(self, new, old, tolerance=FLOAT_ROUNDING)
|
||||
self.assertTrue(torch.equal(new == 0, old == 0))
|
||||
|
||||
|
||||
class NodeMatchTests(unittest.TestCase):
|
||||
"""Whole nodes, every output and the stitcher, before and after."""
|
||||
|
||||
def crop_and_stitch(self, image, mask, **settings):
|
||||
crop = AusBossCropForInpaint().crop(image, mask, **settings)
|
||||
generated = (crop[0] * 0.85 + 0.1).clamp(0.0, 1.0)
|
||||
outputs = [crop]
|
||||
for options in ({}, {"color_match": 1.0}):
|
||||
outputs.append(AusBossStitchInpaint().stitch(crop[2], generated, **options))
|
||||
return outputs
|
||||
|
||||
def test_crop_for_inpaint_and_stitch_match(self):
|
||||
height, width = 420, 300
|
||||
image = torch.from_numpy(photo(height, width)).float().unsqueeze(0) / 255.0
|
||||
cases = {
|
||||
"the box-edit settings": dict(
|
||||
context_factor=1.0, blend_pixels=48, output_multiple=32, context_pixels=96, mask_grow=8,
|
||||
mask_blur=2.0, target_megapixels=0.05, rescale_algorithm="bicubic", keep_inside=True,
|
||||
),
|
||||
"defaults": dict(context_factor=1.2, blend_pixels=16, output_multiple=32),
|
||||
"shrink and pad past the edge": dict(
|
||||
context_factor=2.0, blend_pixels=24, output_multiple=64, mask_grow=-4, mask_blur=6.0,
|
||||
keep_inside=False,
|
||||
),
|
||||
"inverted": dict(context_factor=1.0, blend_pixels=8, output_multiple=8, invert_mask=True, mask_grow=3),
|
||||
"outpaint": dict(context_factor=1.0, blend_pixels=16, output_multiple=8, extend_left=40, extend_down=24, mask_grow=4),
|
||||
}
|
||||
masks = {
|
||||
"inner box": box_mask(height, width, 150, 90, 80, 60),
|
||||
"box on the edge": box_mask(height, width, 0, 200, 70, 100),
|
||||
"empty": torch.zeros((1, height, width)),
|
||||
}
|
||||
for case, settings in cases.items():
|
||||
for name, mask in masks.items():
|
||||
with self.subTest(settings=case, mask=name):
|
||||
new = self.crop_and_stitch(image, mask, **settings)
|
||||
with old_filters():
|
||||
old = self.crop_and_stitch(image, mask, **settings)
|
||||
assert_same(self, new, old, tolerance=FLOAT_ROUNDING)
|
||||
# The stitched picture is the original wherever the old
|
||||
# blend was 0, to the bit.
|
||||
stitcher = old[0][2]
|
||||
ox, oy, ow, oh = stitcher["canvas_to_original"]
|
||||
untouched = stitcher["blend"][0, oy : oy + oh, ox : ox + ow] == 0
|
||||
source = stitcher["canvas"][0, oy : oy + oh, ox : ox + ow]
|
||||
self.assertTrue(torch.equal(new[1][0][0][untouched], source[untouched]))
|
||||
|
||||
def load(self, frames, **values):
|
||||
with patch("nodes.node_image_crop_rotate_pad.resolve_input_path", return_value=Path("source.png")), patch(
|
||||
"nodes.node_image_crop_rotate_pad.load_image_frames", return_value=frames
|
||||
):
|
||||
return AusBossImageCropRotatePad().load_transform("source.png", **values)
|
||||
|
||||
def test_crop_rotate_pad_matches(self):
|
||||
opaque = Image.fromarray(photo(180, 140, seed=1), "RGB").convert("RGBA")
|
||||
cutout = opaque.copy()
|
||||
alpha = np.full((180, 140), 255, np.uint8)
|
||||
alpha[30:90, 20:70] = 0
|
||||
cutout.putalpha(Image.fromarray(alpha))
|
||||
layouts = {
|
||||
"nothing to do": {},
|
||||
"padded": dict(pad_left=24, pad_bottom=40),
|
||||
"turned and cropped": dict(rotation_degrees=-11.0, crop_x=8, crop_y=6, crop_width=120, crop_height=150),
|
||||
"feathered pad, wide blend": dict(pad_top=32, pad_right=16, feather=6, stitch_blend=48, stitch_grow=-4),
|
||||
}
|
||||
if HAS_COMFY: # the megapixel resize runs through comfy.utils
|
||||
layouts["resized"] = dict(pad_left=16, resize_to_megapixels=True, megapixels=0.05)
|
||||
for picture_name, picture in (("opaque", opaque), ("see-through", cutout)):
|
||||
for painted in ("fill", "keep picture"):
|
||||
for layout, values in layouts.items():
|
||||
with self.subTest(picture=picture_name, painted_area=painted, layout=layout):
|
||||
new = self.load([picture], painted_area=painted, **values)
|
||||
with old_filters():
|
||||
old = self.load([picture], painted_area=painted, **values)
|
||||
assert_same(self, new, old, tolerance=FLOAT_ROUNDING)
|
||||
# Two outputs, two tensors: no consumer changes one through the other.
|
||||
self.assertIsNot(new[3], new[2]["original"]["frames"])
|
||||
|
||||
|
||||
# --- timing ----------------------------------------------------------------------
|
||||
|
||||
# 3360 x 5024, the size of the photo the slowdown was found on.
|
||||
BIG_W, BIG_H = 3360, 5024
|
||||
|
||||
|
||||
def best_of(runs: int, call):
|
||||
best, result = None, None
|
||||
for _ in range(runs):
|
||||
start = time.perf_counter()
|
||||
result = call()
|
||||
took = time.perf_counter() - start
|
||||
best = took if best is None else min(best, took)
|
||||
return best, result
|
||||
|
||||
|
||||
class BigPictureTimingTests(unittest.TestCase):
|
||||
"""Bounds for a CI runner; on two CPU threads the old code took several
|
||||
times each one (see the PR for the before and after numbers)."""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.picture = Image.fromarray(photo(BIG_H, BIG_W), "RGB").convert("RGBA")
|
||||
cls.image = torch.from_numpy(np.asarray(cls.picture.convert("RGB"), dtype=np.float32) / 255.0).unsqueeze(0)
|
||||
cls.mask = box_mask(BIG_H, BIG_W, BIG_H // 3, BIG_W // 3, BIG_H // 6, BIG_W // 5)
|
||||
|
||||
def test_a_no_op_crop_rotate_pad_is_quick(self):
|
||||
def run():
|
||||
with patch("nodes.node_image_crop_rotate_pad.resolve_input_path", return_value=Path("source.png")), patch(
|
||||
"nodes.node_image_crop_rotate_pad.load_image_frames", return_value=[self.picture]
|
||||
):
|
||||
return AusBossImageCropRotatePad().load_transform("source.png")
|
||||
|
||||
took, outputs = best_of(2, run)
|
||||
self.assertFalse(bool(outputs[1].any()))
|
||||
self.assertLess(took, 2.5, f"Crop + Rotate + Pad with nothing to do took {took:.2f} s at 17 MP")
|
||||
|
||||
def test_a_box_crop_and_stitch_are_quick(self):
|
||||
settings = dict(
|
||||
context_factor=1.0, blend_pixels=48, output_multiple=32, context_pixels=96, mask_grow=8,
|
||||
mask_blur=2.0, target_megapixels=1.9, rescale_algorithm="bicubic", keep_inside=True,
|
||||
)
|
||||
took, crop = best_of(2, lambda: AusBossCropForInpaint().crop(self.image, self.mask, **settings))
|
||||
self.assertLess(took, 1.5, f"Crop For Inpaint took {took:.2f} s for a box on a 17 MP picture")
|
||||
generated = (crop[0] * 0.9 + 0.05).contiguous()
|
||||
took, _ = best_of(2, lambda: AusBossStitchInpaint().stitch(crop[2], generated))
|
||||
self.assertLess(took, 1.0, f"Stitch Inpaint took {took:.2f} s for a box on a 17 MP picture")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user