Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(nodes): better controlnet processors #6021

Merged
merged 11 commits into from Mar 21, 2024

Conversation

psychedelicious
Copy link
Collaborator

Summary

I also added detect_resolution to:

  • Canny
  • Mediapipe
  • Midas

I didn't fully understand what detect_resolution and image_resolution did until I read through the code. These call the same function to resize the image to a given "resolution", maintaining the aspect ratio.

  • detect_resolution: This takes the input image and resizes it to the given resolution before processing.
  • image_resolution: This does the same thing, but after the image was processed.

Both detect_resolution and image_resolution really need to be correct for the control image to work as it should. Ideally, these numbers match exactly the control image and output image, respectively, but the logic helpfully allows for some fudging.

Examples

Here's my input control image:
image

And my settings (I will be changing only the two resolution settings) - I did a bit of tweaking to get the edges to not be too busy:
image

detect_resolution 512, image_resolution 512

image
image

detect_resolution 512, image_resolution 1024

Note that the control image is still very low quality - that's because it starts out @ 512 and is then resized to 1024.
image
image

detect_resolution 1024, image_resolution 1024

Note that the control image is now very sharp.
image
image

Removing dependency on controlnet_aux

This package provides the controlnet processors, but it bundles up model loading in a rather inconvenient, inaccessible way. We will want to manage the loading of the controlnet processor models ourselves at some point, which may be tricky.

The package is also very messy, being a copy-paste from the OG controlnet repo (unmaintained). controlnet_aux has seen some activity from non-HF contributors, but with Patrick no longer at HF, it's not clear how much support it will get.

As I was working on adding detect_resolution, I looked at controlnet_aux's source and recoiled in horror. I was compelled to clean it up a bit.

So far, I've adapted these things:

  • Canny
  • HED
  • Lineart
  • Lineart Anime
  • A number of cryptic helper functions

The code is cleaned up, with type annotations as best as I could figure. I did back-to-back tests of the processors before and after my adaptations and confirmed the output images were perfect matches, so I'm confident I didn't break anything.

Maybe we should contribute this back to controlnet_aux at some point? Or make a separate package?

Related Issues / Discussions

It's possible that the lack of image and detect resolution contributed to the issues noted in #5943.

QA Instructions

Try out the new settings on Canny. You should get markedly better results for controlnet and T2I adapter if you have the resolutions correct.

Merge Plan

N/A

Checklist

  • The PR has a short but descriptive title
  • Tests added / updated (N/A)
  • Documentation added / updated (N/A)

Some processors, like Canny, didn't use `detect_resolution`. The resultant control images were then resized by the processors from 512x512 to the desired dimensions. The result is that the control images are the right size, but very low quality.

Using detect_resolution fixes this.
These all support controlnet processors.

- `pil_to_cv2`
- `cv2_to_pil`
- `pil_to_np`
- `np_to_pil`
- `normalize_image_channel_count` (a readable version of `HWC3` from the controlnet repo)
- `fit_image_to_resolution` (a readable version of `resize_image` from the controlnet repo)
- `non_maximum_suppression` (a readable version of `nms` from the controlnet repo)
- `safe_step` (a readable version of `safe_step` from the controlnet repo)
Adapted from controlnet processors package

fix: do final resize in canny processor

canny
Adapted from controlnet repo
@github-actions github-actions bot added python PRs that change python files invocations PRs that change invocations backend PRs that change backend files frontend PRs that change frontend files labels Mar 21, 2024
@hipsterusername
Copy link
Member

Nailed it!

@hipsterusername hipsterusername merged commit a6283b9 into main Mar 21, 2024
14 checks passed
@hipsterusername hipsterusername deleted the psyche/feat/nodes/better-cnet-processors branch March 21, 2024 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend PRs that change backend files frontend PRs that change frontend files invocations PRs that change invocations python PRs that change python files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants