https://github.com/cloneofsimo/promptplusplus

https://github.com/cloneofsimo/promptplusplus

Science Score: 10.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: arxiv.org
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (10.3%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: cloneofsimo
  • Language: Jupyter Notebook
  • Default Branch: master
  • Size: 2.35 MB
Statistics
  • Stars: 72
  • Watchers: 7
  • Forks: 7
  • Open Issues: 2
  • Releases: 0
Created over 3 years ago · Last pushed over 3 years ago

https://github.com/cloneofsimo/promptplusplus/blob/master/

# Prompt++


Unofficial Implementation of [Prompt+](https://prompt-plus.github.io/), with bit of my own additions to further explore the P+ space of the stable diffusion. # Introduction We typically use single text conditioning as an input. Naturally, we reuse the same conditioning for all CrossAttention layer. What if we don't? Prompt+ explores this idea, that we can use different text embedding for different cross attention layers, and it works! Prompt+ textual inversion allows you to "extend" the textual inversion process "per-layer", so although we get 16 total tokens, we can expect better results than the original textual inversion. Ok, but the code was not released yet, so I decided to implement it myself. I also added some of my own ideas to further explore the P+ space of the stable diffusion. # what's the difference? One possible suboptimality XTI brings is that the idea of having multiple uncorrelated tokens are implicitly assumed. Indeed, unless some second-order optimzier kicks in, all the extended tokens (in a sense) don't know each other at all. This is certainly weird : we have multiple tokens, that are supposed to be correlated (infact, they have to be idential in the original space), but the optimization doesn't account that. So here I introduce P++ (I just couldn't resist the name), where extended textual inversion occurs with common correlated text embedding. Token $t_i$ lookup embedding $e_i$ has a common embedding $e_c$ that is shared across all tokens. i.e., $$ t_i \rightarrow e_c | e_i $$ Borrowing some ideas from [Encoder4Edit](https://arxiv.org/abs/2102.02766), we make this process "gradually", where $e_i$ are leant only after coarse $e_c$ is learned. Since it is a common tradition(?) to lower the learning rate when "what you have to learn" is small, we also lower the learning rate of $e_i$ as the training progresses. Note that this can be simplified with two-stage process, also even be viewed as [pivotal-tuning](https://arxiv.org/abs/2106.05744). However, I think the general idea is that the tokens should be correlated, and this is one way to achieve that. (just happens to be pivotal-tuning looking) # Results Initial experiments :

> Naive ETI, 1500 steps

> Correlated ETI, 800 steps I literally did 1 experiment, so I don't know if this is a good idea or not. But I think it is worth further exploration. Use this repo if you want to try it out. # Usage ### Installation As of now, this repo requires lora-diffusion as a dependency. You can install it by ```bash pip install git+https://github.com/cloneofsimo/lora.git ``` (I will remove this dependency in the future, maybe...) Install this repo by ```bash pip install git+https://github.com/cloneofsimo/promptplusplus ``` ### Training Use `ppp_train` command to train: following example ```bash export MODEL_NAME="runwayml/stable-diffusion-v1-5" export INSTANCE_DIR="./dataset/data_yc" export OUTPUT_DIR="./exps/yc" ppp_train --pretrained_model_name_or_path=$MODEL_NAME \ --instance_data_dir=$INSTANCE_DIR \ --output_dir=$OUTPUT_DIR \ --placeholder_tokens="" \ --use_template="object" \ --do_coarse_inversion=False \ --preserve_prefix=False \ ``` ### Inference Inference is bit tricky. You need to set attentionprocessor that I made, and overwrite the pipeline of `StableDiffusionPipeline` with custom call function. Luckily, I do that all for you. If you would like to know what is going on, please check out the source code. ```python from ppp import PPPPromptManager from ppp import overwrite_call from ppp import PPPAttenProc from lora_diffusion import patch_pipe from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler import torch model_id = "runwayml/stable-diffusion-v1-5" pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to( "cuda" ) pm = PPPPromptManager(tokenizer= pipe.tokenizer, \ text_encoder=pipe.text_encoder, \ main_token="", preserve_prefix=False, extend_amount=1) pipe.unet.set_attn_processor(PPPAttenProc()) patch_pipe(pipe, "./exps/yc/step_inv_1000.safetensors") with torch.no_grad(): ps = pm.embed_prompt("a colorful photo of a in the jungles") torch.manual_seed(0) overwrite_call(pipe, prompt_embeds=ps).images[0].save("test.png") ``` # References ```bibtex @article{voynov2023p+, title={$ P+ $: Extended Textual Conditioning in Text-to-Image Generation}, author={Voynov, Andrey and Chu, Qinghao and Cohen-Or, Daniel and Aberman, Kfir}, journal={arXiv preprint arXiv:2303.09522}, year={2023} } ``` ```bibtex @article{roich2022pivotal, title={Pivotal tuning for latent-based editing of real images}, author={Roich, Daniel and Mokady, Ron and Bermano, Amit H and Cohen-Or, Daniel}, journal={ACM Transactions on Graphics (TOG)}, volume={42}, number={1}, pages={1--13}, year={2022}, publisher={ACM New York, NY} } ``` ```bibtex @article{tov2021designing, title={Designing an encoder for stylegan image manipulation}, author={Tov, Omer and Alaluf, Yuval and Nitzan, Yotam and Patashnik, Or and Cohen-Or, Daniel}, journal={ACM Transactions on Graphics (TOG)}, volume={40}, number={4}, pages={1--14}, year={2021}, publisher={ACM New York, NY, USA} } ```

Owner

  • Name: Simo Ryu
  • Login: cloneofsimo
  • Kind: user
  • Company: Corca AI

Cats are Turing machines cloneofsimo@gmail.com

GitHub Events

Total
  • Watch event: 2
Last Year
  • Watch event: 2

Issues and Pull Requests

Last synced: over 1 year ago

All Time
  • Total issues: 4
  • Total pull requests: 0
  • Average time to close issues: 1 day
  • Average time to close pull requests: N/A
  • Total issue authors: 3
  • Total pull request authors: 0
  • Average comments per issue: 1.25
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • flow-bee (2)
  • kgonia (1)
  • aisagarw (1)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Dependencies

requirements.txt pypi
  • diffusers >=0.11.0
  • fire *
  • ftfy *
  • scipy *
  • transformers >=4.25.1
setup.py pypi
  • for *
  • open *
  • str *