-
Notifications
You must be signed in to change notification settings - Fork 6k
add load textual inversion embeddings to stable diffusion #2009
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
Conversation
The documentation is not available anymore as the PR was closed or merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for working on this @piEsposito , this will make loading embeddings very easy!
Instead of adding methods to every pipeline
, we could create TextualInversionLoaderMixin
class with a method load_textual_inversion_embeddings
in pipelines/loaders.py
, and the pipelines
which can do text inversion can subclass from that mixin
.
Also, left some comments below, more specifically.
- We should follow the
embeddings
format of the textual inversion script so that we can load all the embeddings in https://huggingface.co/sd-concepts-library - We could also support loading single vector embedding from
auto1111
and then extend it to multiple embeddings.
Thanks!
src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py
Outdated
Show resolved
Hide resolved
src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py
Outdated
Show resolved
Hide resolved
src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py
Outdated
Show resolved
Hide resolved
@patil-suraj I'm addressing your review on the next few days, thanks! |
@patil-suraj Github somehow un-requested review from a bunch of HF people. Can you please add them again? |
@sayakpaul @pcuenca @williamberman could you take a final look here? Made the PR now ready for diffusers design - should work for all use cases. |
image = pipe( | ||
"An logo of a turtle in Style-Winter with <low-poly-hd-logos-icons>", generator=generator, output_type="np" | ||
).images[0] | ||
# np.save("/home/patrick/diffusers-images/text_inv/winter_logo_style.npy", image) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# np.save("/home/patrick/diffusers-images/text_inv/winter_logo_style.npy", image) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we could squash and rebase on main, that would be nice.
Also assuming tests pass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's ship this thing!
Excellent tests, btw. Let's make them pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the API!
embedding = state_dict["string_to_param"]["*"] | ||
|
||
if token is not None and loaded_token != token: | ||
logger.warn(f"The loaded token: {loaded_token} is overwritten by the passed token {token}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't we want to do the opposite override? (What comes in the state_dict is what gets added)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I'd say what gets passed has priority! If you do:
load_textual_inversion("./textual_inversion", token="<special-token>")
I think the token should be "<special-token>"
no matter what's in the dict - it's similar to how we do from_pretrained(unet=unet)
overrides
Come on folks everything but an unrelated test in MPS is passing let's get this thing merged! |
embeddings = [e for e in embedding] # noqa: C416 | ||
else: | ||
tokens = [token] | ||
embeddings = [embedding] if len(embedding.shape) > 1 else [embedding[0]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying the latest version and I wasn't getting anything related to the embeddings, after changing this I was able to get good results. I'm not sure how this would work with len(embedding.shape)
greater than 1 but at least when the shape has only one dimension this seems to fix it.
embeddings = [embedding] if len(embedding.shape) > 1 else [embedding[0]] | |
embeddings = [embedding[0]] if len(embedding.shape) > 1 else [embedding] |
embeddings = [embedding] if len(embedding.shape) > 1 else [embedding[0]] | |
embeddings = [embedding] if len(embedding.shape) <= 1 else [embedding[0]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm cannot reproduce this one - my tests are passing just fine on this branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed diffusers/examples/textual_inversion
to train my own embedding and got learned_embeds.bin
file in the end, then use the example here.
pretrained_path = 'xxx'
embedding_path = 'learned_embeds.bin'
pipe = DiffusionPipeline.from_pretrained(pretrained_path, torch_dtype=torch.float16)
pipe.load_textual_inversion(embedding_path)
It is not working to show any concept from my enbedding token. I have to modify the same with @GuiyeC for the file src/diffusers/loaders.py
to make it correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a PR with the fix for this where I try to explain the problem a bit more.
Co-authored-by: Pedro Cuenca <[email protected]>
Ok let's merge it 🚀 Puuh big PR - thanks so much for kickstarting this @piEsposito and for everybody involved here. Hope that the final solution / design works for everybody |
Thanks for getting this over the finish line guys! WIsh I could've been of
more help. Great work -- I look forward to using the feature.
Best
Evan Jones
Website: www.ea-jones.com
…On Thu, Mar 30, 2023 at 1:08 PM Patrick von Platen ***@***.***> wrote:
Merged #2009 <#2009> into
main.
—
Reply to this email directly, view it on GitHub
<#2009 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJ2T6AKXJ6GJT7NP6LQGKNLW6W42LANCNFSM6AAAAAAT4ZUCKY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Gosh forgot the most important part: Docs! 😅 In case someone has some spare time for a quick PR on docs on how to use it (both A1111 and |
@piEsposito @patrickvonplaten Cheers this is done! But yes Docs are needed to show newbies like me how to use it, the discuss here is too long to read them all lol. |
@blx0102 would you be up for contributing a PR? :) |
Opened a quick PR here: #3068 |
…e#2009) * add load textual inversion embeddings draft * fix quality * fix typo * make fix copies * move to textual inversion mixin * make it accept from sd-concept library * accept list of paths to embeddings * fix styling of stable diffusion pipeline * add dummy TextualInversionMixin * add docstring to textualinversionmixin * add load textual inversion embeddings draft * fix quality * fix typo * make fix copies * move to textual inversion mixin * make it accept from sd-concept library * accept list of paths to embeddings * fix styling of stable diffusion pipeline * add dummy TextualInversionMixin * add docstring to textualinversionmixin * add case for parsing embedding from auto1111 UI format Co-authored-by: Evan Jones <[email protected]> Co-authored-by: Ana Tamais <[email protected]> * fix style after rebase * move textual inversion mixin to loaders * move mixin inheritance to DiffusionPipeline from StableDiffusionPipeline) * update dummy class name * addressed allo comments * fix old dangling import * fix style * proposal * remove bogus * Apply suggestions from code review Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Will Berman <[email protected]> * finish * make style * up * fix code quality * fix code quality - again * fix code quality - 3 * fix alt diffusion code quality * fix model editing pipeline * Apply suggestions from code review Co-authored-by: Pedro Cuenca <[email protected]> * Finish --------- Co-authored-by: Evan Jones <[email protected]> Co-authored-by: Ana Tamais <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Will Berman <[email protected]> Co-authored-by: Pedro Cuenca <[email protected]>
…e#2009) * add load textual inversion embeddings draft * fix quality * fix typo * make fix copies * move to textual inversion mixin * make it accept from sd-concept library * accept list of paths to embeddings * fix styling of stable diffusion pipeline * add dummy TextualInversionMixin * add docstring to textualinversionmixin * add load textual inversion embeddings draft * fix quality * fix typo * make fix copies * move to textual inversion mixin * make it accept from sd-concept library * accept list of paths to embeddings * fix styling of stable diffusion pipeline * add dummy TextualInversionMixin * add docstring to textualinversionmixin * add case for parsing embedding from auto1111 UI format Co-authored-by: Evan Jones <[email protected]> Co-authored-by: Ana Tamais <[email protected]> * fix style after rebase * move textual inversion mixin to loaders * move mixin inheritance to DiffusionPipeline from StableDiffusionPipeline) * update dummy class name * addressed allo comments * fix old dangling import * fix style * proposal * remove bogus * Apply suggestions from code review Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Will Berman <[email protected]> * finish * make style * up * fix code quality * fix code quality - again * fix code quality - 3 * fix alt diffusion code quality * fix model editing pipeline * Apply suggestions from code review Co-authored-by: Pedro Cuenca <[email protected]> * Finish --------- Co-authored-by: Evan Jones <[email protected]> Co-authored-by: Ana Tamais <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Will Berman <[email protected]> Co-authored-by: Pedro Cuenca <[email protected]>
…e#2009) * add load textual inversion embeddings draft * fix quality * fix typo * make fix copies * move to textual inversion mixin * make it accept from sd-concept library * accept list of paths to embeddings * fix styling of stable diffusion pipeline * add dummy TextualInversionMixin * add docstring to textualinversionmixin * add load textual inversion embeddings draft * fix quality * fix typo * make fix copies * move to textual inversion mixin * make it accept from sd-concept library * accept list of paths to embeddings * fix styling of stable diffusion pipeline * add dummy TextualInversionMixin * add docstring to textualinversionmixin * add case for parsing embedding from auto1111 UI format Co-authored-by: Evan Jones <[email protected]> Co-authored-by: Ana Tamais <[email protected]> * fix style after rebase * move textual inversion mixin to loaders * move mixin inheritance to DiffusionPipeline from StableDiffusionPipeline) * update dummy class name * addressed allo comments * fix old dangling import * fix style * proposal * remove bogus * Apply suggestions from code review Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Will Berman <[email protected]> * finish * make style * up * fix code quality * fix code quality - again * fix code quality - 3 * fix alt diffusion code quality * fix model editing pipeline * Apply suggestions from code review Co-authored-by: Pedro Cuenca <[email protected]> * Finish --------- Co-authored-by: Evan Jones <[email protected]> Co-authored-by: Ana Tamais <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Will Berman <[email protected]> Co-authored-by: Pedro Cuenca <[email protected]>
Should close #1985