Skip to content

feat(nodes): faster heuristic resize, support resizing on upload, use it in canvas #8057

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 11 commits into from
May 29, 2025

Conversation

psychedelicious
Copy link
Collaborator

@psychedelicious psychedelicious commented May 28, 2025

Summary

Make heuristic_resize ~20x faster. This function is a high-quality image resizing function that preserves Canny edge maps and segmentation maps much better than traditional interpolation. This function was inherited from controlnet_aux.

  • Previously this kind of resize took ~4s to run on a 1024x1024 edge map with moderate edge density. Now it takes under 200ms.
  • The outputs are slightly different for Canny edge maps, but IMO the new algo looks a tiny bit better - fewer spurious dingleberries. Segmentation maps and "normal" images are handled in the same way as the previous algo.
  • Use the resize functionality with creating new layers & resizing at the same time, for example when using these actions:
    image
  • Add resize_to to upload endpoint. When dimensions are provided, the image is resized using heuristic_resize on upload.
  • Swap the opencv-python dep for opencv-contrib-python, which includes cv2.ximgproc.thinning. This function accounts for a good chunk of the perf improvement. The previous edge thinning code was mostly implemented in python and a lot slower.

A future enhancement will use this resize method for all canvas layer transformations, which will resolve #8004.

Related Issues / Discussions

n/a

QA Instructions

Before and after for new canvas from raster layer w/ resize. Note how the layer doesn't display at a smaller size for a moment before resizing, and how the resized layer is less pixelated.

Screen.Recording.2025-05-28.at.9.32.10.pm.mov

Screen.Recording.2025-05-28.at.9.34.56.pm.mov


Use the Heuristic Resize node to test the updated algorithm.

Tested against normal images, binary canny edge maps, grayscale HED edge maps, segmentation maps, and normal images.

Tested resizing up and down for each.

Comparisons

Some comparisons on Canny edge maps. Slightly different pixels, but in practically speaking the old and new methods give the same quality results.

Input image:
image

Old, new at various sizes:

Old New
Old New
Old New
Old New

Merge Plan

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • Documentation added / updated (if applicable)
  • Updated What's New copy (if doing a release after this PR)

Add `heuristic_resize_fast`, which does the same thing as `heuristic_resize`, except it's about 20x faster.

This is achieved by using opencv for the binary edge handling isntead of python, and checking only 100k pixels to determine what kind of image we are working with.

Besides being much faster, it results in cleaner lines for resized binary canny edge maps, and has results in fewer misidentified segmentation maps.

Tested against normal images, binary canny edge maps, grayscale HED edge maps, segmentation maps, and normal images.

Tested resizing up and down for each.

Besides the new utility function, I needed to swap the `opencv-python` dep for `opencv-contrib-python`, which includes `cv2.ximgproc.thinning`. This function accounts for a good chunk of the perf improvement.
better handling for smaller image sizes
@github-actions github-actions bot added api python PRs that change python files Root invocations PRs that change invocations frontend PRs that change frontend files python-deps PRs that change python dependencies labels May 28, 2025
@hipsterusername
Copy link
Member

You had me at spurious dingleberries.

@psychedelicious psychedelicious changed the title Psyche/feat/nodes/faster heuristic resize feat(nodes): faster heuristic resize, support resizing on upload, use it in canvas May 29, 2025
@psychedelicious psychedelicious enabled auto-merge (rebase) May 29, 2025 03:43
@psychedelicious psychedelicious merged commit b98abc2 into main May 29, 2025
12 checks passed
@psychedelicious psychedelicious deleted the psyche/feat/nodes/faster-heuristic-resize branch May 29, 2025 03:49
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
api frontend PRs that change frontend files invocations PRs that change invocations python PRs that change python files python-deps PRs that change python dependencies Root
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[enhancement]: Image resizing algorithm in layer transform
2 participants