Skip to content

add models for T2I-Adapter-XL #4696

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

Merged
merged 36 commits into from
Aug 29, 2023
Merged

add models for T2I-Adapter-XL #4696

merged 36 commits into from
Aug 29, 2023

Conversation

MC-E
Copy link
Contributor

@MC-E MC-E commented Aug 21, 2023

What does this PR do?

Fixes # (issue)

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@MC-E
Copy link
Contributor Author

MC-E commented Aug 21, 2023

We add T2I-Adapter-XL models to support the control on SDXL.

@sayakpaul
Copy link
Member

@MC-E this looks good to me! Do we have any checkpoints that we can test this with?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

Copy link
Contributor

@patil-suraj patil-suraj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool and minimal. just left two small comments.

@sayakpaul
Copy link
Member

@MC-E I tried loading an existing checkpoint here: https://colab.research.google.com/gist/sayakpaul/e9ed999df5714a6c9bb9c0a06cc9922a/scratchpad.ipynb. But I am currently facing issues. Could you look into it?

@MC-E
Copy link
Contributor Author

MC-E commented Aug 22, 2023

@MC-E I tried loading an existing checkpoint here: https://colab.research.google.com/gist/sayakpaul/e9ed999df5714a6c9bb9c0a06cc9922a/scratchpad.ipynb. But I am currently facing issues. Could you look into it?

It seems that all the key prefixes have an additional "adapter."?

@sayakpaul
Copy link
Member

@MC-E I did the following:

final_state_dict = {f"adapter.{k}": v for k, v in state_dict_new.items()}

xl_adapter = T2IAdapter(adapter_type="full_adapter_xl", downscale_factor=16)
xl_adapter.load_state_dict(final_state_dict)

It leads to:

RuntimeError: Error(s) in loading state_dict for T2IAdapter:
	size mismatch for adapter.conv_in.weight: copying a param with shape torch.Size([320, 256, 3, 3]) from checkpoint, the shape in current model is torch.Size([320, 768, 3, 3]).

Anything I am missing out on?

@MC-E
Copy link
Contributor Author

MC-E commented Aug 22, 2023

Please follow the configs here https://github.com/TencentARC/T2I-Adapter/tree/XL/configs/inference; The number of input channels for OpenPose is three times that of others.

@sayakpaul
Copy link
Member

I am using canny here:

from huggingface_hub import hf_hub_download

repo_id = "TencentARC/T2I-Adapter"
filepath = "models_XL/adapter-xl-canny.pth"

state_dict_path = hf_hub_download(repo_id=repo_id, filename=filepath)

@MC-E
Copy link
Contributor Author

MC-E commented Aug 22, 2023

@sayakpaul The input channels need to be 1. It seems that you set it as 3

@MC-E
Copy link
Contributor Author

MC-E commented Aug 22, 2023

Happy to assist you. Feel free to ask any questions:)

@sayakpaul
Copy link
Member

Yup, that fixed it :)

@sayakpaul
Copy link
Member

sayakpaul commented Aug 22, 2023

@MC-E I created this conversion script for easy sharing and distribution: https://gist.github.com/sayakpaul/9a888466865bc0844d8979a2b2821c63. Let's add this maybe to the scripts directory. This will give a config file and the state dict file in safetensors resembling what we have here: https://huggingface.co/TencentARC/t2iadapter_seg_sd14v1/tree/main.

Also, since you have already incorporated the feedback provided by @patil-suraj, I think for newer checkpoints (assuming we would use these blocks) we won't require any conversion.

@sayakpaul
Copy link
Member

I think the next step would be to:

Pt 2 mostly involves:

We can add a new pipeline for this: pipeline_stable_diffusion_xl_adapter.py.

Let me know if you face any difficulties here.

Then in a follow-up PR, we can add the training script to start our full-length experiments.

@MC-E
Copy link
Contributor Author

MC-E commented Aug 23, 2023

@sayakpaul The XL pipeline is added. The hf models are uploaded at: https://huggingface.co/Adapter/t2iadapter/tree/main

@MC-E
Copy link
Contributor Author

MC-E commented Aug 23, 2023

@sayakpaul The test code is:

import torch

from diffusers import (
    T2IAdapter,
    StableDiffusionXLAdapterPipeline,
    DDPMScheduler,
)
from diffusers.utils import load_image

sketch_image = load_image('https://huggingface.co/Adapter/t2iadapter/resolve/main/sketch.png')
model_id = 'stabilityai/stable-diffusion-xl-base-1.0'

adapter = T2IAdapter.from_pretrained("Adapter/t2iadapter", subfolder='sketch_sdxl_1.0',torch_dtype=torch.float16, adapter_type="full_adapter_xl")
scheduler = DDPMScheduler.from_pretrained(model_id, subfolder="scheduler")

pipe = StableDiffusionXLAdapterPipeline.from_pretrained(
    model_id, adapter=adapter, torch_dtype=torch.float16, variant="fp16", scheduler=scheduler
)

pipe.to('cuda')
generator = torch.manual_seed(42)
sketch_image_out = pipe(prompt='a photo of a dog in real world, high quality', negative_prompt='extra digit, fewer digits, cropped, worst quality, low quality', image=sketch_image, generator=generator, guidance_scale=7.5).images[0]

sketch_image_out.save('sketch_image_out.png')

@sayakpaul
Copy link
Member

@MC-E thanks for providing the code snippet and for your contributions!

A couple of things:

  • I simplified your snippet so that it's easier to follow. I hope that's okay. Could you please also leave a link to edge_dog.png so that we can try it out?
  • Do we need to ensure the inputs images of (1024, 1024) resolution as SDXL base model is typically good at that resolution?
  • The default Euler Discrete scheduler of SDXL works well with a reduced guidance_scale (5.0 for example). So, I would try to reduce the num_inference_steps and guidance_scale and see the results.

@sayakpaul
Copy link
Member

@MC-E I think you accidentally borked the commit history. It shouldn't be like that.

@MC-E
Copy link
Contributor Author

MC-E commented Aug 29, 2023

In order to resolve conflicts, I perform a rebase on this PR:(

@sayakpaul sayakpaul merged commit 12358b9 into huggingface:main Aug 29, 2023
@sayakpaul
Copy link
Member

Thanks for your amazing contribution!

@MC-E
Copy link
Contributor Author

MC-E commented Aug 29, 2023

Thank you for your help:)

@sayakpaul
Copy link
Member

@MC-E
Copy link
Contributor Author

MC-E commented Aug 29, 2023

Okay, thanks:)

yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* T2I-Adapter-XL

* update

* update

* add pipeline

* modify pipeline

* modify pipeline

* modify pipeline

* modify pipeline

* modify pipeline

* modify modeling_text_unet

* fix styling.

* fix: copies.

* adapter settings

* new test case

* new test case

* debugging

* debugging

* debugging

* debugging

* debugging

* debugging

* debugging

* debugging

* revert prints.

* new test case

* remove print

* org test case

* add test_pipeline

* styling.

* fix copies.

* modify test parameter

* style.

* add adapter-xl doc

* double quotes in docs

* Fix potential type mismatch

* style.

---------

Co-authored-by: sayakpaul <[email protected]>
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* T2I-Adapter-XL

* update

* update

* add pipeline

* modify pipeline

* modify pipeline

* modify pipeline

* modify pipeline

* modify pipeline

* modify modeling_text_unet

* fix styling.

* fix: copies.

* adapter settings

* new test case

* new test case

* debugging

* debugging

* debugging

* debugging

* debugging

* debugging

* debugging

* debugging

* revert prints.

* new test case

* remove print

* org test case

* add test_pipeline

* styling.

* fix copies.

* modify test parameter

* style.

* add adapter-xl doc

* double quotes in docs

* Fix potential type mismatch

* style.

---------

Co-authored-by: sayakpaul <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants