ChenyangQiQi / FateZero

[ICCV 2023 Oral] "FateZero: Fusing Attentions for Zero-shot Text-based Video Editing"
http://fate-zero-edit.github.io/
MIT License
1.07k stars 105 forks source link
image-editing stable-diffusion text-driven-editing video-editing video-style-transfer

FateZero: Fusing Attentions for Zero-shot Text-based Video Editing (ICCV23 Oral')

Chenyang Qi, Xiaodong Cun, Yong Zhang, Chenyang Lei, Xintao Wang, Ying Shan, and Qifeng Chen

Open In Colab Hugging Face Spaces GitHub

"silver jeep ➜ posche car" "+ Van Gogh style" "squirrel,Carrot ➜ rabbit,eggplant"
## 🎏 Abstract TL; DR: FateZero is the first zero-shot framework for text-driven video editing via pretrained diffusion models without training.
CLICK for the full abstract > The diffusion-based generative models have achieved remarkable success in text-based image generation. However, since it contains enormous randomness in generation progress, it is still challenging to apply such models for real-world visual content editing, especially in videos. In this paper, we propose FateZero, a zero-shot text-based editing method on real-world videos without per-prompt training or use-specific mask. To edit videos consistently, we propose several techniques based on the pre-trained models. Firstly, in contrast to the straightforward DDIM inversion technique, our approach captures intermediate attention maps during inversion, which effectively retain both structural and motion information. These maps are directly fused in the editing process rather than generated during denoising. To further minimize semantic leakage of the source video, we then fuse self-attentions with a blending mask obtained by cross-attention features from the source prompt. Furthermore, we have implemented a reform of the self-attention mechanism in denoising UNet by introducing spatial-temporal attention to ensure frame consistency. Yet succinct, our method is the first one to show the ability of zero-shot text-driven video style and local attribute editing from the trained text-to-image model. We also have a better zero-shot shape-aware editing ability based on the text-to-video model. Extensive experiments demonstrate our superior temporal consistency and editing capability than previous works.
## πŸ“‹ Changelog - 2023.06.10 Two examples of large motion and multiple object. - 2023.04.18 Code refactoring and support local blending using [blend_latents option](config/teaser/jeep_posche_local_latent_blend.yaml). - 2023.04.04 Release Enhanced Tuning-a-Video [configs](config/tune) and shape editing [ckpts](https://huggingface.co/chenyangqi/), [data](https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/shape.zip) and [config](config/shape) - 2023.03.31 Refine hugging face demo. - 2023.03.27 Excited to Release [`Hugging face demo`](https://huggingface.co/spaces/chenyangqi/FateZero), [`attribute editing config`](config/attribute) and [`data`](https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/attribute.zip) - 2023.03.22 Upload [`style editing config`](config/style) and [`data`](https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/style.zip) and a `colab notebook` [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ChenyangQiQi/FateZero/blob/main/colab_fatezero.ipynb). - 2023.03.21 Provide [Editing guidance](docs/EditingGuidance.md) for in-the-wild video. Update the [`config`](config/low_resource_teaser) for lower resources computers (16G GPU and less than 16G CPU RAM). - 2023.03.17 Release Code and Paper! ## 🚧 Todo
Click for Previous todos - [x] Release the edit config and data for all results, Tune-a-video optimization - [x] Memory and runtime profiling and Editing guidance documents - [x] Colab and hugging-face - [x] code refactoring
- [ ] time & memory optimization - [ ] Release more application ## πŸ›‘ Setup Environment Our method is tested using cuda11, fp16 of accelerator and xformers on a single A100 or 3090. ```bash conda create -n fatezero38 python=3.8 conda activate fatezero38 pip install -r requirements.txt ``` `xformers` is recommended for A100 GPU to save memory and running time.
Click for xformers installation We find its installation not stable. You may try the following wheel: ```bash wget https://github.com/ShivamShrirao/xformers-wheels/releases/download/4c06c79/xformers-0.0.15.dev0+4c06c79.d20221201-cp38-cp38-linux_x86_64.whl pip install xformers-0.0.15.dev0+4c06c79.d20221201-cp38-cp38-linux_x86_64.whl ```
Validate the installation by ``` python test_install.py ``` You may download all data and checkpoints using the following bash command ``` bash download_all.sh ``` The above command take minutes and 100GB. Or you may download the required data and ckpts latter according to your interests. Our environment is similar to Tune-A-video ([official](https://github.com/showlab/Tune-A-Video), [unofficial](https://github.com/bryandlee/Tune-A-Video)) and [prompt-to-prompt](https://github.com/google/prompt-to-prompt/). You may check them for more details. ## βš”οΈ FateZero Editing #### Style and Attribute Editing in Teaser Download the [stable diffusion v1-4](https://huggingface.co/CompVis/stable-diffusion-v1-4) (or other interesting image diffusion model) and put it to `./ckpt/stable-diffusion-v1-4`.
Click for the bash command: ``` mkdir ./ckpt cd ./ckpt # download from huggingface face, takes 20G space git lfs install git clone https://huggingface.co/CompVis/stable-diffusion-v1-4 ```
Then, you could reproduce style and shape editing results in our teaser by running: ```bash accelerate launch test_fatezero.py --config config/teaser/jeep_watercolor.yaml # or CUDA_VISIBLE_DEVICES=0 python test_fatezero.py --config config/teaser/jeep_watercolor.yaml ```
The result is saved at `./result` . (Click for directory structure) ``` result β”œβ”€β”€ teaser β”‚ β”œβ”€β”€ jeep_posche β”‚ β”œβ”€β”€ jeep_watercolor β”‚ β”œβ”€β”€ cross-attention # visualization of cross-attention during inversion β”‚ β”œβ”€β”€ sample # result β”‚ β”œβ”€β”€ train_samples # the input video ```
Editing 8 frames on an Nvidia 3090, use `100G CPU memory, 12G GPU memory` for editing. We also provide some [`low-cost setting`](config/low_resource_teaser) of style editing by different hyper-parameters on a 16GB GPU. You may try these low-cost settings on colab. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ChenyangQiQi/FateZero/blob/main/colab_fatezero.ipynb) More speed and hardware benchmarks are [here](docs/EditingGuidance.md#ddim-hyperparameters). #### Shape and large motion editing with Tune-A-Video Besides style and attribution editing above, we also provide a `Tune-A-Video` checkpoint. You may download from [onedrive](https://hkustconnect-my.sharepoint.com/:f:/g/personal/cqiaa_connect_ust_hk/EviSTWoAOs1EmHtqZruq50kBZu1E8gxDknCPigSvsS96uQ?e=492khj) or from [hugging face model repository](https://huggingface.co/chenyangqi/jeep_tuned_200). Then move it to `./ckpt/jeep_tuned_200/`.
Click for the bash command: ``` mkdir ./ckpt cd ./ckpt # download from huggingface face, takes 10G space git lfs install git clone https://huggingface.co/chenyangqi/jeep_tuned_200 ```
The directory structure should be like this: (Click for directory structure) ``` ckpt β”œβ”€β”€ stable-diffusion-v1-4 β”œβ”€β”€ jeep_tuned_200 ... data β”œβ”€β”€ car-turn β”‚ β”œβ”€β”€ 00000000.png β”‚ β”œβ”€β”€ 00000001.png β”‚ β”œβ”€β”€ ... video_diffusion ```
You could reproduce the shape editing result in our teaser by running: ```bash accelerate launch test_fatezero.py --config config/teaser/jeep_posche.yaml ``` ### Reproduce other results in the paper Download the data from [onedrive](https://hkustconnect-my.sharepoint.com/:f:/g/personal/cqiaa_connect_ust_hk/EkIeHj3CQiBNhm6iEEhJQZwBEBJNCGt3FsANmyqeAYbuXQ?e=SCPJlu) or from Github [Release](https://github.com/ChenyangQiQi/FateZero/releases/tag/v0.0.1).
Click for wget bash command: ``` wget https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/attribute.zip wget https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/style.zip wget https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/shape.zip ```
Unzip and Place it in ['./data'](data). Then use the commands in ['config/style'](config/style) and ['config/attribute'](config/attribute) to get the results. To reproduce other shape editing results, download Tune-A-Video checkpoints from [huggingface](https://huggingface.co/chenyangqi/) :
Click for the bash command: ``` mkdir ./ckpt cd ./ckpt # download from huggingface face, takes 10G space git lfs install git clone https://huggingface.co/chenyangqi/man_skate_250 git clone https://huggingface.co/chenyangqi/swan_150 ```
Then use the commands in ['config/shape'](config/shape). For above Tune-A-Video checkpoints, we fintune stable diffusion with a synthetic negative-prompt [dataset](https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/negative_reg.zip) for regularization and low-rank conovlution for temporal-consistent generation using [tuning config](./config/tune/)
Click for the bash command example: ``` cd ./data wget https://github.com/ChenyangQiQi/FateZero/releases/download/v0.0.1/negative_reg.zip unzip negative_reg cd .. accelerate launch train_tune_a_video.py --config config/tune/jeep.yaml ``` To evaluate our results quantitatively, we provide `CLIP/frame_acc_tem_con.py` to calculate frame accuracy and temporal consistency using pretrained CLIP.
## Editing guidance for YOUR video We provided a editing guidance for in-the-wild video [here](./docs/EditingGuidance.md). The work is still in progress. Welcome to give your feedback in issues. ## Style Editing Results with Stable Diffusion We show the difference between the source prompt and the target prompt in the box below each video. Note mp4 and gif files in this GitHub page are compressed. Please check our [Project Page](https://fate-zero-edit.github.io/) for mp4 files of original video editing results.
"+ Ukiyo-e style" "+ watercolor painting" "+ Monet style"
"+ PokΓ©mon cartoon style" "+ Makoto Shinkai style" "+ watercolor painting"
## Attribute Editing Results with Stable Diffusion
"rabbit, strawberry ➜ white rabbit, flower" "rabbit, strawberry ➜ squirrel, carrot" "rabbit, strawberry ➜ white rabbit, leaves"
"bear ➜ a red tiger" "bear ➜ a yellow leopard" "bear ➜ a brown lion"
"cat ➜ black cat, grass..." "cat ➜ red tiger" "cat ➜ Shiba-Inu"
"orange fish ➜ yellow fish" "squirrel ➜ robot squirrel" "squirrel, Carrot ➜ robot mouse, screwdriver"
"bus ➜ GPU" "gray dog ➜ yellow corgi" "gray dog ➜ robotic dog"
"white duck ➜ yellow rubber duck" "grass ➜ snow" "white fox ➜ grey wolf"
## Shape and large motion editing with Tune-A-Video
"silver jeep ➜ posche car" "Swan ➜ White Duck" "Swan ➜ Pink flamingo"
"A man ➜ A Batman" "A man ➜ A Wonder Woman, With cowboy hat" "A man ➜ A Spider-Man"
## πŸ•Ή Online Demo Thanks to AK and the team from Hugging Face for providing computing resources to support our Hugging-face Demo, which supports up to 30 steps DDIM steps. [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/chenyangqi/FateZero). You may use the UI for testing FateZero built with gradio locally. ``` git clone https://huggingface.co/spaces/chenyangqi/FateZero python app_fatezero.py # we will merge the FateZero on hugging face with that in github repo latter ``` We also provide a Colab demo, which supports 10 DDIM steps. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ChenyangQiQi/FateZero/blob/main/colab_fatezero.ipynb) You may launch the colab as a jupyter notebook on your local machine. We will refine and optimize the above demos in the following days. ## πŸ“€ Demo Video https://user-images.githubusercontent.com/45789244/225698509-79c14793-3153-4bba-9d6e-ede7d811d7f8.mp4 The video here is compressed due to the size limit of GitHub. The original full-resolution video is [here](https://hkustconnect-my.sharepoint.com/:v:/g/personal/cqiaa_connect_ust_hk/EXKDI_nahEhKtiYPvvyU9SkBDTG2W4G1AZ_vkC7ekh3ENw?e=Xhgtmk). ## πŸ“ Citation ``` @article{qi2023fatezero, title={FateZero: Fusing Attentions for Zero-shot Text-based Video Editing}, author={Chenyang Qi and Xiaodong Cun and Yong Zhang and Chenyang Lei and Xintao Wang and Ying Shan and Qifeng Chen}, year={2023}, journal={arXiv:2303.09535}, } ``` ## πŸ’— Acknowledgements This repository borrows heavily from [Tune-A-Video](https://github.com/showlab/Tune-A-Video) and [prompt-to-prompt](https://github.com/google/prompt-to-prompt/). Thanks to the authors for sharing their code and models. ## 🧿 Maintenance This is the codebase for our research work. We are still working hard to update this repo, and more details are coming in days. If you have any questions or ideas to discuss, feel free to contact [Chenyang Qi](cqiaa@connect.ust.hk) or [Xiaodong Cun](vinthony@gmail.com).