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

[Feature Request] Attention Mask for LORAs? #2445

Open
MoonMoon82 opened this issue Jan 3, 2024 · 19 comments
Open

[Feature Request] Attention Mask for LORAs? #2445

MoonMoon82 opened this issue Jan 3, 2024 · 19 comments
Labels
Feature A new feature to add to ComfyUI.

Comments

@MoonMoon82
Copy link

Is it possible to add a Node to mask the area where a specific LORA will be applied to?
Matteos IPAdapter Node has got such a "attention mask". I found it very useful and it also would be greate to restrict the influence area of LORAs.
(I know, it's already similar by using "SetLatentNoiseMask", but it would need to add sample processes each LORA - and it's just not the same)

Idk if it's even possible to implement such mask, but at least I want to ask for it.

@jakeute
Copy link

jakeute commented Jan 4, 2024

masks are useful when there are multiple loras

@MoonMoon82
Copy link
Author

@jakeute Usually the output of a sampler is not very predictable, unless you're using controlnet modules to predefine the shape of the output. So in this case, masked loras in conjunction with controlnet modules would also be usefull.

@suede299
Copy link

suede299 commented Jan 5, 2024

The IPAdapter's ATTENTION MASK is the reference portion of the target.
For lora to work like this, it needs to be targeted to the generated image, but the images are all still in the process of being generated, so how do you know where a certain lora should be applied?
To achieve such an effect, I'm afraid that 2 samplers are needed, applying ConditioningSetMask, SetLatentNoiseMask to the 2nd sampler.

@MoonMoon82
Copy link
Author

@suede299 yes, that's exactly how IPAdapters "attention mask" works.
I'm totally aware of that it already can be achieved with 2 samplers in confunction with nodes like SetLatentNoiseMask. But the disadvantage of course is the increase of complexity and decrease of performance of your workflow.
Using this method IPAdapters "attention mask" would also be unnecessary.

You can be pretty sure where a certain lora should be applied to, as soon you're using controlnet like canny, openposen or segmentation controlnet for example.

I really don't have an idea if this is even possible. But I know that this would be very helpful!

@manzonif
Copy link

@MoonMoon82
Copy link
Author

@manzonif That's awesome! That means it is technically possible to implement such a feature?

Is there anyone out there who is capable of creating such a node out of this code?
To mask the target area of a lora would be very very helpful!

Unfortunately I don't even have an idea where to start and I can even barely read python :(

@manzonif
Copy link

technically yes.
I'm new to python, but I want to spend a few hours trying to implement it. I will hardly succeed :-(

@MoonMoon82
Copy link
Author

@manzonif That would be great! If I can help you in any kind of way please let me know! (e.g. testing a node)

@MoonMoon82
Copy link
Author

It seems there is an addon for A1111 to mask the output of LORAs:
https://github.com/lifeisboringsoprogramming/sd-webui-lora-masks

I would really really appreciate if someone could make a lora-attention-masking node for comfyui!

@MoonMoon82
Copy link
Author

I still have some hope this attention mask node could be developed..

@AlexD81
Copy link

AlexD81 commented Apr 22, 2024

+1

@MoonMoon82
Copy link
Author

My hope is fading :-(

comfyanonymous pushed a commit that referenced this issue May 9, 2024
…4472 and #2445 (#3422)

* Update node_helpers.py to use generic pillow wrapper to resolve multiple meta-data related issues.

replaced open_image function with a generic pillow function that takes Pil functions as a dependency injection and applies the ImageFile.LOAD_TRUNCATED_IMAGES try except fix to them. 

This provides an extensible function to handle related errors that can wrap offending functions when discovered without the need to repeat code.

* Update a few Pil functions to use node_helpers.pillow wrapper

Update a Pil function calls in a few locations to use the generic node_helpers.pillow wrapper that takes the function as a dependency injection and uses the try except method with ImageFIle.LOAD_TRUNCATED_IMAGES solution

* Corrected comment in issue #s fixed.

* Update node_helpers.py to remove import of Image from PIL

import of Image is no longer required as functions are Injected
@MoonMoon82
Copy link
Author

Hi @shawnington , how does this commit (0fecfd2) solve the missing attention mask for LORAs ?

@shawnington
Copy link
Contributor

Hi @shawnington , how does this commit (0fecfd2) solve the missing attention mask for LORAs ?

That is the issue number 2445 is for the PIL library, it was not intended to be linked to this feature request.

Lin1031 pushed a commit to Lin1031/ComfyUI that referenced this issue May 14, 2024
…omfyanonymous#4472 and comfyanonymous#2445 (comfyanonymous#3422)

* Update node_helpers.py to use generic pillow wrapper to resolve multiple meta-data related issues.

replaced open_image function with a generic pillow function that takes Pil functions as a dependency injection and applies the ImageFile.LOAD_TRUNCATED_IMAGES try except fix to them. 

This provides an extensible function to handle related errors that can wrap offending functions when discovered without the need to repeat code.

* Update a few Pil functions to use node_helpers.pillow wrapper

Update a Pil function calls in a few locations to use the generic node_helpers.pillow wrapper that takes the function as a dependency injection and uses the try except method with ImageFIle.LOAD_TRUNCATED_IMAGES solution

* Corrected comment in issue #s fixed.

* Update node_helpers.py to remove import of Image from PIL

import of Image is no longer required as functions are Injected
@MoonMoon82
Copy link
Author

@AlexD81 , @manzonif , @jakeute, @suede299

I found these two custom nodes in AnimageDiffEvolved which seem to do what I was searching for:
ADE_RegisterLoraHook
grafik

ADE_ConditioningSetMaskAndCombine
grafik

The first node loads the Lora but the lora is only activated for a specific masked area in the second node.
At the second node the Cond_ADD can be an empty/ZeroOut condition or a lora specific "trigger word".
All my tests confirm that this works the way I requested.

Could anyone also confirm?

Maybe this issue can be closed then.

@Phando
Copy link

Phando commented Jun 27, 2024

Do the LoraHooks only work with animated diff models? I am trying to add ADE_RegisterLoraHook and ADE_ConditioningSetMaskAndCombine with an SDXL workflow. Is it possible to use a standard sampler or do I need to set up a workflow with an AnimatedDiff sampler?

My goal is to generate an image with two characters, each coming from a different lora in the same generation. Regional conditioning is working, but the loras seem to be unmaskable.

@MoonMoon82
Copy link
Author

MoonMoon82 commented Jun 27, 2024

@Phando Yes, it works with usual Loras as well. Here's a basic workflow that can show you how it works in general:
multiple masked Loras.json

I had an issue in creating separate face masks, so I did it manually (upper right corner).
grafik

In this example the Loras have to same trigger words. So if your Loras have different trigger words, you should set up two different ADD_conditions. I'm not quite sure if I handle it right, but to me it seems it works just fine.

@Phando
Copy link

Phando commented Jun 27, 2024

Thank you for the sample workflow!
It looks like my Comfy was out of date, I kept getting tuple errors when the sampler was running. Your workflow helped me figure it out. Now it is time to have some fun with regional loras!

@mcmonkey4eva mcmonkey4eva added the Feature A new feature to add to ComfyUI. label Jun 27, 2024
@Phando
Copy link

Phando commented Jun 27, 2024

It worked!
By adding regional conditioning in conjunction with the LoRA hooks, its looking pretty sweet. Thank you. Uploading workflow (2).png…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature A new feature to add to ComfyUI.
Projects
None yet
Development

No branches or pull requests

8 participants