stjordanis / InstantStyle

InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation πŸ”₯

Home Page:https://instantstyle.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation

Haofan Wang* Β· Matteo Spinelli Β· Qixun Wang Β· Xu Bai Β· Zekui Qin Β· Anthony Chen

InstantX Team

*corresponding authors

Hugging Face ModelScope GitHub

InstantStyle is a general framework that employs two straightforward yet potent techniques for achieving an effective disentanglement of style and content from reference images.

Principle

Separating Content from Image. Benefit from the good characterization of CLIP global features, after subtracting the content text fea- tures from the image features, the style and content can be explicitly decoupled. Although simple, this strategy is quite effective in mitigating content leakage.

Injecting into Style Blocks Only. Empirically, each layer of a deep network captures different semantic information the key observation in our work is that there exists two specific attention layers handling style. Specifically, we find up blocks.0.attentions.1 and down blocks.2.attentions.1 capture style (color, material, atmosphere) and spatial layout (structure, composition) respectively.

Release

  • [2024/04/11] πŸ”₯ We add the experimental distributed inference feature. Check it here.
  • [2024/04/10] πŸ”₯ We support an online demo on ModelScope.
  • [2024/04/09] πŸ”₯ We support an online demo on Huggingface.
  • [2024/04/09] πŸ”₯ We support SDXL-inpainting, more information can be found here.
  • [2024/04/08] πŸ”₯ InstantStyle is supported in AnyV2V for stylized video-to-video editing, demo can be found here.
  • [2024/04/07] πŸ”₯ We support image-based stylization, more information can be found here.
  • [2024/04/07] πŸ”₯ We support an experimental version for SD1.5, more information can be found here.
  • [2024/04/03] πŸ”₯ InstantStyle is supported in ComfyUI_IPAdapter_plus developed by our co-author.
  • [2024/04/03] πŸ”₯ We release the technical report.

Demos

Stylized Synthesis

Image-based Stylized Synthesis

Comparison with Previous Works

Download

Follow IP-Adapter to download pre-trained checkpoints from here.

git clone https://github.com/InstantStyle/InstantStyle.git
cd InstantStyle

# download the models
git lfs install
git clone https://huggingface.co/h94/IP-Adapter
mv IP-Adapter/models models
mv IP-Adapter/sdxl_models sdxl_models

Usage

Our method is fully compatible with IP-Adapter. For feature subtraction, it only works for global feature instead of patch features. For SD1.5, you can find a demo at infer_style_sd15.py, but we find that SD1.5 has weaker perception and understanding of style information, thus this demo is experimental only. All block names can be found in attn_blocks.py and attn_blocks_sd15.py for SDXL and SD1.5 respectively.

import torch
from diffusers import StableDiffusionXLPipeline
from PIL import Image

from ip_adapter import IPAdapterXL

base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
image_encoder_path = "sdxl_models/image_encoder"
ip_ckpt = "sdxl_models/ip-adapter_sdxl.bin"
device = "cuda"

# load SDXL pipeline
pipe = StableDiffusionXLPipeline.from_pretrained(
    base_model_path,
    torch_dtype=torch.float16,
    add_watermarker=False,
)

# reduce memory consumption
pipe.enable_vae_tiling()

# load ip-adapter
# target_blocks=["block"] for original IP-Adapter
# target_blocks=["up_blocks.0.attentions.1"] for style blocks only
# target_blocks = ["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"] # for style+layout blocks
ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device, target_blocks=["up_blocks.0.attentions.1"])

image = "./assets/0.jpg"
image = Image.open(image)
image.resize((512, 512))

# generate image variations with only image prompt
images = ip_model.generate(pil_image=image,
                            prompt="a cat, masterpiece, best quality, high quality",
                            negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
                            scale=1.0,
                            guidance_scale=5,
                            num_samples=1,
                            num_inference_steps=30, 
                            seed=42,
                            #neg_content_prompt="a rabbit",
                            #neg_content_scale=0.5,
                          )

images[0].save("result.png")

Distributed Inference

On distributed setups, you can run inference across multiple GPUs with πŸ€— Accelerate or PyTorch Distributed, which is useful for generating with multiple prompts in parallel, in case you have limited VRAM on each GPU. More information can be found here. Make sure you have installed diffusers from the source and the lastest accelerate.

max_memory = {0:"10GB", 1:"10GB"}
pipe = StableDiffusionXLPipeline.from_pretrained(
    base_model_path,
    torch_dtype=torch.float16,
    add_watermarker=False,
    device_map="balanced",
    max_memory=max_memory
)

Start a local gradio demo

Run the following command:

git clone https://github.com/InstantStyle/InstantStyle.git
cd ./InstantStyle/gradio_demo/
pip install -r requirements.txt
python app.py

Resources

TODO

  • Support in diffusers API, check our PR.
  • Support InstantID for face stylization once stars reach 1K.

Disclaimer

Our released codes and checkpoints are for non-commercial research purposes only. Users are granted the freedom to create images using this tool, but they are obligated to comply with local laws and utilize it responsibly. The developers will not assume any responsibility for potential misuse by users.

Acknowledgements

InstantStyle is developed by the InstantX team and is highly built on IP-Adapter, which has been unfairly compared by many other works. We at InstantStyle make IP-Adapter great again. Additionally, we acknowledge Hu Ye for his valuable discussion.

Star History

Star History Chart

Cite

If you find InstantStyle useful for your research and applications, please cite us using this BibTeX:

@article{wang2024instantstyle,
  title={InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation},
  author={Wang, Haofan and Wang, Qixun and Bai, Xu and Qin, Zekui and Chen, Anthony},
  journal={arXiv preprint arXiv:2404.02733},
  year={2024}
}

For any question, feel free to contact us via haofanwang.ai@gmail.com.

About

InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation πŸ”₯

https://instantstyle.github.io/


Languages

Language:Jupyter Notebook 93.5%Language:Python 6.5%